svg-filters & basic parser #15

Merged
schrottkatze merged 67 commits from schrottkatze/iowo:svg-filters into main 2024-07-08 18:29:05 +00:00
5 changed files with 74 additions and 24 deletions
Showing only changes of commit bfd4b3765f - Show all commits

View file

@ -38,6 +38,10 @@ pub(crate) fn parser<
let word = select! { Token::Word(word) => word }; let word = select! { Token::Word(word) => word };
let expr = recursive(|expr| { 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! { let var = select! {
Token::VarIdent(name) => (Expr::Var as fn(_) -> _, name), Token::VarIdent(name) => (Expr::Var as fn(_) -> _, name),
Token::InputIdent(name) => (Expr::InputVar as fn(_) -> _, name) Token::InputIdent(name) => (Expr::InputVar as fn(_) -> _, name)

View file

@ -4,12 +4,12 @@ use indexmap::IndexMap;
use super::{Span, Spanned}; use super::{Span, Spanned};
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq)]
pub struct File<'src> { pub struct File<'src> {
pub decls: IndexMap<Spanned<&'src str>, Expression<'src>>, pub decls: IndexMap<Spanned<&'src str>, Expression<'src>>,
} }
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq)]
pub struct Expression<'src> { pub struct Expression<'src> {
pub expr: Expr<'src>, pub expr: Expr<'src>,
pub span: Span, pub span: Span,
@ -21,7 +21,7 @@ impl<'src> Expression<'src> {
} }
} }
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq)]
pub enum Expr<'src> { pub enum Expr<'src> {
Node( Node(
Spanned<&'src str>, Spanned<&'src str>,
@ -45,4 +45,13 @@ pub enum Expr<'src> {
// @ // @
InputVar(&'src str), InputVar(&'src str),
AttrSet(Spanned<IndexMap<Spanned<&'src str>, Expression<'src>>>), 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, Let,
#[token("in")] #[token("in")]
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), Word(&'a str),
#[regex("\\$[a-zA-Z0-9_\\-]+", |lex| &lex.slice()[1..])] #[regex("\\$[a-zA-Z0-9_\\-]+", |lex| &lex.slice()[1..])]
VarIdent(&'a str), VarIdent(&'a str),

View file

@ -34,7 +34,7 @@ lexer_test! {
lexer_test! { lexer_test! {
test_lex_subgroup, 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::Word("subgroup"),
Token::ParenOpen, Token::ParenOpen,
@ -47,14 +47,14 @@ lexer_test! {
Token::Pipe, Token::Pipe,
Token::Word("b"), Token::Word("b"),
Token::BraceOpen, Token::BraceOpen,
Token::Word("1"), Token::Word("in1"),
Token::Colon, Token::Colon,
Token::VarIdent("first"), Token::VarIdent("first"),
Token::BraceClose, Token::BraceClose,
Token::Pipe, Token::Pipe,
Token::Word("c"), Token::Word("c"),
Token::BraceOpen, Token::BraceOpen,
Token::Word("1"), Token::Word("in1"),
Token::Colon, Token::Colon,
Token::VarIdent("second"), Token::VarIdent("second"),
Token::BraceClose Token::BraceClose
@ -105,3 +105,35 @@ lexer_test! {
Token::Word("c") 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 def blend1 = [
| uwu open "test.png",
{ foo: @bar open "test2.png"
, hello: world @| test { more: params } | yay ]
} | blend multiply 0.6
!| awa
@| nya
| rawr;
def test = meow def blend2 = open "test.png" | blend multiply 0.6 [ open test2.png ]
{ hello: $foo
, world: @bar
};
def blah = {
pipe1: meow | uwu,
pipe2: lorem | ipsum
} | flkjdsafkjl;