-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.rs
338 lines (300 loc) · 8.95 KB
/
parser.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
use crate::parser::args::PatternArgs;
use crate::parser::args::GrammarArgs;
use crate::parser::args::RuleDefArgs;
use crate::parser::args::RuleLineArgs;
use crate::parser::lexer::Lexed;
use crate::parser::args::TerminalSetOrIdent;
use crate::terminalset::TerminalSet;
use crate::terminalset::TerminalSetItem;
use proc_macro2::Group;
use proc_macro2::Ident;
use proc_macro2::Span;
use proc_macro2::TokenStream;
use proc_macro2::Punct;
use proc_macro2::Spacing;
use quote::ToTokens;
use std::boxed::Box;
use rusty_lr_core::ReduceType;
// bootstrap the parser for the grammar
// this define the actual parser for proc-macro line parsing
// This should be changed to GLR parser in the future
macro_rules! punct(
($l:literal) => {
Punct::new($l, Spacing::Alone)
};
);
%%
%moduleprefix ::rusty_lr_core;
%userdata GrammarArgs;
%tokentype Lexed;
%token ident Lexed::Ident(Ident::new("id", Span::call_site()));
%token colon Lexed::Colon(punct!(':'));
%token semicolon Lexed::Semicolon(punct!(';'));
%token pipe Lexed::Pipe(punct!('|'));
%token percent Lexed::Percent(punct!('%'));
%token equal Lexed::Equal(punct!('='));
%token plus Lexed::Plus(punct!('+'));
%token star Lexed::Star(punct!('*'));
%token question Lexed::Question(punct!('?'));
%token caret Lexed::Caret(punct!('^'));
%token minus Lexed::Minus(punct!('-'));
%token exclamation Lexed::Exclamation(punct!('!'));
%token slash Lexed::Slash(punct!('/'));
%token otherpunct Lexed::OtherPunct(punct!('.'));
%token literal Lexed::Literal(None);
%token parengroup Lexed::ParenGroup(None);
%token bracegroup Lexed::BraceGroup(None);
%token bracketgroup Lexed::BracketGroup(None);
%token nonegroup Lexed::NoneGroup(None);
%token lparen Lexed::LParen(Span::call_site());
%token rparen Lexed::RParen(Span::call_site());
%token lbrace Lexed::LBrace(Span::call_site());
%token rbrace Lexed::RBrace(Span::call_site());
%token lbracket Lexed::LBracket(Span::call_site());
%token rbracket Lexed::RBracket(Span::call_site());
%token left Lexed::Left(punct!('%'),Ident::new("id", Span::call_site()));
%token right Lexed::Right(punct!('%'),Ident::new("id", Span::call_site()));
%token token Lexed::Token(punct!('%'),Ident::new("id", Span::call_site()));
%token start Lexed::Start(punct!('%'),Ident::new("id", Span::call_site()));
%token eofdef Lexed::EofDef(punct!('%'),Ident::new("id", Span::call_site()));
%token tokentype Lexed::TokenType(punct!('%'),Ident::new("id", Span::call_site()));
%token userdata Lexed::UserData(punct!('%'),Ident::new("id", Span::call_site()));
%token errortype Lexed::ErrorType(punct!('%'),Ident::new("id", Span::call_site()));
%token moduleprefix Lexed::ModulePrefix(punct!('%'),Ident::new("id", Span::call_site()));
%token lalr Lexed::Lalr(punct!('%'),Ident::new("id", Span::call_site()));
%token glr Lexed::Glr(punct!('%'),Ident::new("id", Span::call_site()));
%eof Lexed::Eof;
%start Grammar;
Rule(RuleDefArgs) : ident RuleType colon RuleLines semicolon {
let ident = if let Lexed::Ident(ident) = ident {
ident
} else {
unreachable!( "Rule-Ident" );
};
if let Lexed::Colon(colon) = colon {
let span = colon.span();
if let Some(fisrt) = RuleLines.first_mut() {
fisrt.separator_span = span;
}
}else {
unreachable!( "Rule-Colon2" );
}
RuleDefArgs {
name: ident,
typename: RuleType.map(|t| t.stream()),
rule_lines: RuleLines
}
}
;
RuleType(Option<Group>): parengroup {
if let Lexed::ParenGroup(group) = parengroup {
group
}else{
unreachable!( "RuleType - Group" );
}
}
| {
None
}
;
RuleLines(Vec<RuleLineArgs>): RuleLines pipe RuleLine {
if let Lexed::Pipe(punct) = pipe {
RuleLine.separator_span = punct.span();
RuleLines.push( RuleLine );
}
RuleLines
}
| RuleLine {
vec![ RuleLine ]
}
;
RuleLine(RuleLineArgs): TokenMapped* Action
{
RuleLineArgs {
tokens: TokenMapped,
reduce_action: Action.map(|action| action.to_token_stream()),
separator_span: Span::call_site(),
id: 0,
}
}
;
TokenMapped((Option<Ident>, PatternArgs)): Pattern {
( None, Pattern )
}
| ident equal Pattern {
if let Lexed::Ident(ident) = ident {
( Some(ident), Pattern )
}else {
unreachable!( "Token-Ident" );
}
}
;
TerminalSetItem(TerminalSetItem): ident {
let ident = if let Lexed::Ident(ident) = ident {
ident
}else {
unreachable!( "TerminalSetItem-Range1" );
};
TerminalSetItem::Terminal( ident )
}
| first=ident minus last=ident {
let first = if let Lexed::Ident(first) = first {
first
}else {
unreachable!( "TerminalSetItem-Range1" );
};
let last = if let Lexed::Ident(last) = last {
last
}else {
unreachable!( "TerminalSetItem-Range3" );
};
TerminalSetItem::Range( first, last )
}
;
TerminalSet(TerminalSet): lbracket caret? TerminalSetItem* rbracket {
let open_span = if let Lexed::LBracket(lbracket) = lbracket {
lbracket
} else {
unreachable!( "TerminalSet-Open" );
};
let close_span = if let Lexed::RBracket(rbracket) = rbracket {
rbracket
} else {
unreachable!( "TerminalSet-Close" );
};
TerminalSet {
negate: caret.is_some(),
items: TerminalSetItem,
open_span,
close_span,
}
}
;
Pattern(PatternArgs): ident {
if let Lexed::Ident(ident) = ident {
PatternArgs::Ident( ident )
}else {
unreachable!( "Pattern-Ident" );
}
}
| Pattern plus {
if let Lexed::Plus(plus) = plus {
PatternArgs::Plus( Box::new(Pattern), plus.span() )
}else {
unreachable!( "Pattern-Plus" );
}
}
| Pattern star {
if let Lexed::Star(star) = star {
PatternArgs::Star( Box::new(Pattern), star.span() )
}else {
unreachable!( "Pattern-Star" );
}
}
| Pattern question {
if let Lexed::Question(question) = question {
PatternArgs::Question( Box::new(Pattern), question.span() )
}else {
unreachable!( "Pattern-Question" );
}
}
| Pattern exclamation {
if let Lexed::Exclamation(exclamation) = exclamation {
PatternArgs::Exclamation( Box::new(Pattern), exclamation.span() )
}else {
unreachable!( "Pattern-Exclamation" );
}
}
| TerminalSet {
PatternArgs::TerminalSet( TerminalSet )
}
| Pattern slash TerminalSetOrIdent {
PatternArgs::Lookaheads( Box::new(Pattern), TerminalSetOrIdent )
}
| lparen Pattern+ rparen {
let open = if let Lexed::LParen(lparen) = lparen {
lparen
} else {
unreachable!( "Pattern-Group-Open" );
};
let close = if let Lexed::RParen(rparen) = rparen {
rparen
} else {
unreachable!( "Pattern-Group-Close" );
};
PatternArgs::Group(Pattern, open, close)
}
;
Action(Option<Group>): bracegroup {
if let Lexed::BraceGroup(group) = bracegroup {
group
}else {
unreachable!( "Action0" );
}
}
| { None }
;
TokenDef((Ident, TokenStream)): token ident RustCode semicolon
{
if let Lexed::Ident(ident) = ident {
( ident, RustCode )
}else {
unreachable!( "TokenDef-Ident" );
}
}
;
RustCode(TokenStream): t=[^semicolon lparen-glr]+ {
let mut tokens = TokenStream::new();
for token in t.into_iter() {
token.append_to_stream(&mut tokens);
}
tokens
};
StartDef(Ident): start ident semicolon {
if let Lexed::Ident(ident) = ident {
ident
}else {
unreachable!( "StartDef-Ident" );
}
}
;
EofDef((Span,TokenStream)): eofdef RustCode semicolon { (eofdef.span(), RustCode) }
;
TokenTypeDef((Span,TokenStream)): tokentype RustCode semicolon { (tokentype.span(), RustCode) }
;
UserDataDef((Span,TokenStream)): userdata RustCode semicolon { (userdata.span(),RustCode) }
;
TerminalSetOrIdent(TerminalSetOrIdent): TerminalSet { TerminalSetOrIdent::TerminalSet( TerminalSet ) }
| ident {
if let Lexed::Ident(ident) = ident {
TerminalSetOrIdent::Ident( ident )
}else {
unreachable!( "TerminalSetOrIdent-Ident" );
}
}
;
ReduceType(ReduceType): left { ReduceType::Left }
| right { ReduceType::Right }
;
ReduceDef((TerminalSetOrIdent, ReduceType)): reducetype=ReduceType TerminalSetOrIdent semicolon {
( TerminalSetOrIdent, reducetype )
}
;
ErrorDef((Span,TokenStream)): errortype RustCode semicolon { (errortype.span(), RustCode) }
;
ModulePrefixDef((Span,TokenStream)): moduleprefix RustCode semicolon { (moduleprefix.span(), RustCode) };
Glr: glr semicolon;
Lalr: lalr semicolon;
GrammarLine : Rule { data.rules.push(Rule); }
| TokenDef { data.terminals.push(TokenDef); }
| StartDef { data.start_rule_name.push(StartDef); }
| EofDef { data.eof.push(EofDef); }
| TokenTypeDef { data.token_typename.push(TokenTypeDef); }
| UserDataDef { data.userdata_typename.push(UserDataDef); }
| ReduceDef { data.reduce_types.push(ReduceDef); }
| ErrorDef { data.error_typename.push(ErrorDef); }
| ModulePrefixDef { data.module_prefix.push(ModulePrefixDef); }
| Lalr { data.lalr = true; }
| Glr { data.glr = true; }
;
Grammar: GrammarLine+;