lang: state with confusing error

This commit is contained in:
Schrottkatze 2024-04-08 15:43:42 +02:00
parent 198c74c7ae
commit bfd4b3765f
Signed by: schrottkatze
SSH key fingerprint: SHA256:hXb3t1vINBFCiDCmhRABHX5ocdbLiKyCdKI4HK2Rbbc
5 changed files with 74 additions and 24 deletions

View file

@ -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)

View file

@ -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<Spanned<&'src str>, 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<IndexMap<Spanned<&'src str>, 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),
}

View file

@ -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),

View file

@ -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")
]
}

View file

@ -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 ]