forked from kevinmehall/rust-peg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.rs
132 lines (117 loc) · 2.98 KB
/
ast.rs
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use proc_macro2::{Group, Ident, Literal, Span, TokenStream};
#[derive(Debug)]
pub struct Grammar {
pub doc: Option<TokenStream>,
pub visibility: Option<TokenStream>,
pub name: Ident,
pub args: Vec<(Ident, TokenStream)>,
pub items: Vec<Item>,
pub input_type: TokenStream,
}
impl Grammar {
pub fn iter_rules(&self) -> impl Iterator<Item = &Rule> {
self.items.iter().filter_map(|item| match item {
Item::Rule(r) => Some(r),
_ => None,
})
}
}
#[derive(Debug)]
pub enum Item {
Use(TokenStream),
Rule(Rule),
}
#[derive(Debug)]
pub struct Rule {
pub span: Span,
pub name: Ident,
pub ty_params: Option<Vec<TokenStream>>,
pub params: Vec<RuleParam>,
pub expr: SpannedExpr,
pub ret_type: Option<TokenStream>,
pub doc: Option<TokenStream>,
pub visibility: Option<TokenStream>,
pub cached: bool,
}
#[derive(Debug)]
pub struct RuleParam {
pub name: Ident,
pub ty: RuleParamTy,
}
#[derive(Debug)]
pub enum RuleParamTy {
Rust(TokenStream),
Rule(TokenStream),
}
#[derive(Debug, Clone)]
pub struct TaggedExpr {
pub name: Option<Ident>,
pub expr: SpannedExpr,
}
#[derive(Debug, Clone)]
pub struct SpannedExpr {
pub span: Span,
pub expr: Expr,
}
#[derive(Debug, Clone)]
pub enum Expr {
LiteralExpr(Literal),
PatternExpr(Group),
RuleExpr(Ident, Vec<RuleArg>),
MethodExpr(Ident, TokenStream),
ChoiceExpr(Vec<SpannedExpr>),
OptionalExpr(Box<SpannedExpr>),
Repeat { inner: Box<SpannedExpr>, bound: BoundedRepeat, sep: Option<Box<SpannedExpr>> },
PosAssertExpr(Box<SpannedExpr>),
NegAssertExpr(Box<SpannedExpr>),
ActionExpr(Vec<TaggedExpr>, Option<Group>),
MatchStrExpr(Box<SpannedExpr>),
PositionExpr,
QuietExpr(Box<SpannedExpr>),
FailExpr(Literal),
PrecedenceExpr {
levels: Vec<PrecedenceLevel>,
},
MarkerExpr(bool),
}
impl Expr {
pub fn at(self, sp: Span) -> SpannedExpr {
SpannedExpr { expr: self, span:sp }
}
}
#[derive(Debug, Clone)]
pub enum RuleArg {
Rust(TokenStream),
Peg(SpannedExpr),
}
#[derive(Debug, Clone)]
pub struct PrecedenceLevel {
pub operators: Vec<PrecedenceOperator>,
}
#[derive(Debug, Clone)]
pub struct PrecedenceOperator {
pub span: Span,
pub elements: Vec<TaggedExpr>,
pub action: Group,
}
#[derive(Debug, Clone)]
pub enum BoundedRepeat {
None,
Plus,
Exact(TokenStream),
Both(Option<TokenStream>, Option<TokenStream>),
}
impl BoundedRepeat {
pub fn has_lower_bound(&self) -> bool {
match self {
BoundedRepeat::None | BoundedRepeat::Both(None, _) => false,
BoundedRepeat::Plus | BoundedRepeat::Exact(_) | BoundedRepeat::Both(Some(_), _) => true
}
}
pub fn has_upper_bound(&self) -> bool {
match self {
BoundedRepeat::None | BoundedRepeat::Plus | BoundedRepeat::Both(_, None) => false,
BoundedRepeat::Exact(_) | BoundedRepeat::Both(_, Some(_)) => true
}
}
}