This commit is contained in:
LunarAkai 2025-08-07 13:19:26 +02:00
commit 95fedac802
3 changed files with 39 additions and 5 deletions

View file

@ -1,6 +1,6 @@
/// Abstract Syntax Tree /// Abstract Syntax Tree
#[derive(Debug)] #[derive(Debug, Clone)]
pub enum Expression<'src> { pub enum Expression<'src> {
// Identifier // Identifier
Ident(&'src str), Ident(&'src str),

View file

@ -1,5 +1,5 @@
use chumsky::{ use chumsky::{
combinator::Or, error::Rich, extra, input::ValueInput, prelude::{choice, just, recursive}, primitive::select, recursive, select, select_ref, span::SimpleSpan, text::{self, ascii::{ident, keyword}, whitespace}, Boxed, IterParser, Parser combinator::Or, error::Rich, extra, input::ValueInput, prelude::{choice, just, nested_delimiters, recursive, via_parser}, primitive::select, recursive, select, select_ref, span::{self, SimpleSpan}, text::{self, ascii::{ident, keyword}, whitespace}, Boxed, ConfigIterParser, IterParser, Parser
}; };
use crate::{language_frontend::abstract_syntax_tree::ast::Expression, language_frontend::lexer::tokens::Token}; use crate::{language_frontend::abstract_syntax_tree::ast::Expression, language_frontend::lexer::tokens::Token};
@ -22,16 +22,38 @@ where
Token::Keyword(k) if k == kw => (), Token::Keyword(k) if k == kw => (),
} }
}; };
let eq = just(Token::Equals).labelled("="); let eq = just(Token::Equals).labelled("=");
/*
let block = recursive(|block| {
let indent = just(Token::NewLine)
.ignore_then(just(Token::Indent))
.ignore_then(block.clone().separated_by(just(Token::NewLine)).at_least(1))
.then_ignore(just(Token::Dedent));
block.with_ctx(0)
});
*/
let expr = recursive(|expr| { let expr = recursive(|expr| {
let block = expr
.clone()
.delimited_by(just(Token::BraceBegin), just(Token::BraceEnd));
// 'Atoms' are expressions that contain no ambiguity
let atom = select! { let atom = select! {
Token::Float(x) => Expression::Float(x), Token::Float(x) => Expression::Float(x),
Token::Integer(x) => Expression::Integer(x), Token::Integer(x) => Expression::Integer(x),
}.or(expr.clone().delimited_by(just(Token::ParenBegin), just(Token::ParenEnd))); }.or(
expr.clone().delimited_by(just(Token::ParenBegin),
just(Token::ParenEnd))
).or(block);
let unary = just(Token::Substract) let unary = just(Token::Substract)
@ -59,6 +81,8 @@ where
add_sub add_sub
}); });
let decl = recursive(|decl| { let decl = recursive(|decl| {
let var = keyword("var") let var = keyword("var")
.ignore_then(ident) .ignore_then(ident)

View file

@ -3,9 +3,14 @@ use std::fmt;
use logos::{Lexer, Logos}; use logos::{Lexer, Logos};
#[derive(Logos, Debug, Clone, PartialEq)] #[derive(Logos, Debug, Clone, PartialEq)]
#[logos(skip r"[ \t\r\n\f]+")] // Skip whitespace #[logos(skip r"[ \r\f]+")] // Skip whitespace
pub enum Token<'src> { pub enum Token<'src> {
Error, Error,
Null,
Indent,
NewLine,
Dedent,
#[token("false", |_| false)] #[token("false", |_| false)]
#[token("true", |_| true)] #[token("true", |_| true)]
@ -66,6 +71,10 @@ impl fmt::Display for Token<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match self {
Token::Float(s) => write!(f, "{s}"), Token::Float(s) => write!(f, "{s}"),
Token::Null => write!(f, "<null>"),
Token::Indent => write!(f, "<indent>"),
Token::NewLine => write!(f, "<new_line>"),
Token::Dedent => write!(f, "<dedent>"),
Token::Add => write!(f, "+"), Token::Add => write!(f, "+"),
Token::Bool(_) => write!(f, "+"), Token::Bool(_) => write!(f, "+"),
Token::Substract => write!(f, "-"), Token::Substract => write!(f, "-"),
@ -81,7 +90,8 @@ impl fmt::Display for Token<'_> {
Token::Ident(s) => write!(f, "{s}"), Token::Ident(s) => write!(f, "{s}"),
Token::String(s) => write!(f, "{s}"), Token::String(s) => write!(f, "{s}"),
Token::Keyword(s) => write!(f, "{s}"), Token::Keyword(s) => write!(f, "{s}"),
Token::Error => write!(f, "<error>") Token::Error => write!(f, "<error>"),
} }
} }
} }