eww/src/parser.lalrpop
2021-07-01 20:38:23 +02:00

37 lines
885 B
Text

use std::str::FromStr;
//use crate::lexer;
use crate::Expr;
use crate::Span;
grammar;
pub Expr: Expr = {
<l:@L> "(" <elems:(<Expr>)+> ")" <r:@R> => Expr::List(Span(l, r), elems),
<l:@L> "{" <elems:(<(<Expr>)> <(<Expr>)>)*> "}" <r:@R> => Expr::Table(Span(l, r), elems),
<x:Keyword> => x,
<x:Symbol> => x,
<l:@L> <x:StrLit> <r:@R> => Expr::Str(Span(l, r), x),
<l:@L> <x:Num> <r:@R> => Expr::Number(Span(l, r), x),
Comment => Expr::Comment,
};
Keyword: Expr = <l:@L> <x:r":[^\s]+"> <r:@R> => Expr::Keyword(Span(l, r), x.to_string());
Symbol: Expr = <l:@L> <x:r"[a-zA-Z_!\?<>/.*-+][^\s{}\(\)]*"> <r:@R> => Expr::Symbol(Span(l, r), x.to_string());
StrLit: String = {
<x:r#""(?:[^"\\]|\\.)*""#> => {
x[1..x.len() - 1].to_owned()
},
}
Comment: () = r";[^\n\r]*";
Num: i32 = <r"[0-9]+"> => i32::from_str(<>).unwrap();
// vim:shiftwidth=4