From bfd4b3765f35a98112b691d2f29c2c3446fe4d5c Mon Sep 17 00:00:00 2001 From: Schrottkatze Date: Mon, 8 Apr 2024 15:43:42 +0200 Subject: [PATCH] lang: state with confusing error --- crates/lang/src/parser.rs | 4 ++++ crates/lang/src/parser/ast.rs | 15 ++++++++++--- crates/lang/src/tokens.rs | 18 +++++++++++++++- crates/lang/src/tokens/tests.rs | 38 ++++++++++++++++++++++++++++++--- testfiles/test.owo | 23 ++++++-------------- 5 files changed, 74 insertions(+), 24 deletions(-) diff --git a/crates/lang/src/parser.rs b/crates/lang/src/parser.rs index 8090f7b..7c0e406 100644 --- a/crates/lang/src/parser.rs +++ b/crates/lang/src/parser.rs @@ -38,6 +38,10 @@ pub(crate) fn parser< let word = select! { Token::Word(word) => word }; let expr = recursive(|expr| { + let lit = select! { + Token::Int(i) = e => Expression::new(Expr::Lit(ast::Lit::Int(i.parse().unwrap())), e.span()), + Token::Float(f) = e => Expression::new(Expr::Lit(ast::Lit::Float(f.parse().unwrap())), e.span()), + }; let var = select! { Token::VarIdent(name) => (Expr::Var as fn(_) -> _, name), Token::InputIdent(name) => (Expr::InputVar as fn(_) -> _, name) diff --git a/crates/lang/src/parser/ast.rs b/crates/lang/src/parser/ast.rs index 02ce0ae..d21ad49 100644 --- a/crates/lang/src/parser/ast.rs +++ b/crates/lang/src/parser/ast.rs @@ -4,12 +4,12 @@ use indexmap::IndexMap; use super::{Span, Spanned}; -#[derive(Debug, PartialEq, Eq)] +#[derive(Debug, PartialEq)] pub struct File<'src> { pub decls: IndexMap, Expression<'src>>, } -#[derive(Debug, PartialEq, Eq)] +#[derive(Debug, PartialEq)] pub struct Expression<'src> { pub expr: Expr<'src>, pub span: Span, @@ -21,7 +21,7 @@ impl<'src> Expression<'src> { } } -#[derive(Debug, PartialEq, Eq)] +#[derive(Debug, PartialEq)] pub enum Expr<'src> { Node( Spanned<&'src str>, @@ -45,4 +45,13 @@ pub enum Expr<'src> { // @ InputVar(&'src str), AttrSet(Spanned, Expression<'src>>>), + Lit(Lit<'src>), +} + +#[derive(Debug, PartialEq)] +pub enum Lit<'src> { + // TODO: more bigger better number types + Int(i64), + Float(f64), + String(&'src str), } diff --git a/crates/lang/src/tokens.rs b/crates/lang/src/tokens.rs index e881663..6caa57c 100644 --- a/crates/lang/src/tokens.rs +++ b/crates/lang/src/tokens.rs @@ -14,7 +14,23 @@ pub enum Token<'a> { Let, #[token("in")] In, - #[regex("[a-zA-Z0-9_\\-]+", |lex| lex.slice())] + #[regex("[\\d]+", |lex| lex.slice())] + Int(&'a str), + #[regex("[+-]?([\\d]+\\.[\\d]*|[\\d]*\\.[\\d]+)", |lex| lex.slice())] + Float(&'a str), + // TODO: more bigger better more complex string lexing + // TODO: templating? + #[regex(r#""([^"\\]|\\["\\bnfrt]|u[a-fA-F0-9]{4})*""#, |lex| lex.slice())] + String(&'a str), + #[token("+")] + Plus, + #[token("-")] + Minus, + #[token("*")] + Mult, + #[token("/")] + Div, + #[regex("[a-zA-Z_]+[a-zA-Z0-9_\\-]*", |lex| lex.slice())] Word(&'a str), #[regex("\\$[a-zA-Z0-9_\\-]+", |lex| &lex.slice()[1..])] VarIdent(&'a str), diff --git a/crates/lang/src/tokens/tests.rs b/crates/lang/src/tokens/tests.rs index fe4270e..2b0454f 100644 --- a/crates/lang/src/tokens/tests.rs +++ b/crates/lang/src/tokens/tests.rs @@ -34,7 +34,7 @@ lexer_test! { lexer_test! { test_lex_subgroup, - "subgroup(first, second) = a | b { 1: $first } | c { 1: $second }", + "subgroup(first, second) = a | b { in1: $first } | c { in1: $second }", [ Token::Word("subgroup"), Token::ParenOpen, @@ -47,14 +47,14 @@ lexer_test! { Token::Pipe, Token::Word("b"), Token::BraceOpen, - Token::Word("1"), + Token::Word("in1"), Token::Colon, Token::VarIdent("first"), Token::BraceClose, Token::Pipe, Token::Word("c"), Token::BraceOpen, - Token::Word("1"), + Token::Word("in1"), Token::Colon, Token::VarIdent("second"), Token::BraceClose @@ -105,3 +105,35 @@ lexer_test! { Token::Word("c") ] } + +lexer_test! { + test_lex_int_literal, + "42", + [ + Token::Int("42") + ] +} + +lexer_test! { + test_lex_float_literal_0, + "1.5", + [ + Token::Float("1.5") + ] +} + +lexer_test! { + test_lex_float_literal_1, + "42.", + [ + Token::Float("42.") + ] +} + +lexer_test! { + test_lex_float_literal_2, + ".42", + [ + Token::Float(".42") + ] +} diff --git a/testfiles/test.owo b/testfiles/test.owo index 11930d6..809a6e1 100644 --- a/testfiles/test.owo +++ b/testfiles/test.owo @@ -1,18 +1,7 @@ -def main = meow - | uwu - { foo: @bar - , hello: world @| test { more: params } | yay - } - !| awa - @| nya - | rawr; +def blend1 = [ + open "test.png", + open "test2.png" + ] + | blend multiply 0.6 -def test = meow - { hello: $foo - , world: @bar - }; - -def blah = { - pipe1: meow | uwu, - pipe2: lorem | ipsum -} | flkjdsafkjl; +def blend2 = open "test.png" | blend multiply 0.6 [ open test2.png ]