-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.lalrpop
43 lines (35 loc) · 1 KB
/
grammar.lalrpop
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use crate::parser::{lexer, ast, Error};
use lexer::Tok;
use ast::Ast;
grammar();
pub Ast: ast::Ast = {
#[precedence(level="0")]
Term,
#[precedence(level="1")] #[assoc(side="right")]
<t: Ast> "?" => Ast::optional(t),
#[precedence(level="2")] #[assoc(side="left")]
<lhs: Ast> <rhs: Ast> => Ast::then(lhs, rhs),
#[precedence(level="3")] #[assoc(side="left")]
<lhs: Ast> "|" <rhs: Ast> => Ast::or(lhs, rhs),
}
Term: ast::Ast = {
<c: "char"> => Ast::char(c),
<"digit"> => Ast::digit(),
<" "> => Ast::space(),
"(" <p: Ast> ")" => p,
"(" "!" <p: Ast> ")" => Ast::case_sensitive(p)
}
extern {
type Location = usize;
type Error = Error;
enum Tok {
"|" => lexer::Tok::Or,
"?" => lexer::Tok::QMark,
"(" => lexer::Tok::LParen,
")" => lexer::Tok::RParen,
"!" => lexer::Tok::Exclam,
"char" => lexer::Tok::Char { c: <char>, .. },
"digit" => lexer::Tok::Digit,
" " => lexer::Tok::Space,
}
}