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
339
340
341
342
343
344
345
346
347
348
//! Rustlr is an LR-style parser generator for Rust. Advanced features
//! include:
//! 1. Option to automatically generate the AST datatypes and semantic actions, with manual overrides possible. Rustlr's grammar format contains a sublanguage
//! that controls how ASTS are created, so that the generated types do
//! not necessarily reflect the format of the grammar.
//! 2. Option to use [bumpalo](https://docs.rs/bumpalo/latest/bumpalo/index.html) to create
//! ASTS types that enable *nested* pattern matching against recursive types.
//!
//! 3. Recognizes regex-style operators `*`, `+` and `?`, which simplify
//! the writing of grammars and allow better ASTs to be created.
//! 4. An experimental feature that recognizes *Selective Marcus-Leermakers*
//! grammars. This is a class of unambiguous grammars that's
//! larger than traditional LR grammars. They are especially helpful
//! in avoiding conflicts when new production rules are added to a grammar.
//! 5. The ability to train the parser interactively for better error reporting
//! 6. Also generates parsers for F# and other .Net languages
//!
//! A **[TUTORIAL](<https://chuckcscccl.github.io/rustlr_project/>)**
//! is separately available that will explain the
//! format of grammars and how to generate and deploy parsers for several
//! examples. The documentation found here should be used as a technical
//! reference.
//!
//! **INSTALLING RUSTLR**
//!
//! Rustlr consists of two main components: the parser generation routines and
//! the runtime parser routines that interpret the generated parsing tables.
//! The default installation will install both. However, the runtime parser
//! can be installed independently.
//!
//!
//! Rustlr should first be installed as a command-line application:
//! **`cargo install rustlr`**. This will install both the generator and
//! runtime parser.
//!
//! Parser generation can also be invoked from within a rust
//! program with the [generate] function of the rustlr crate.
//!
//! Once a parser has been generated and included in another crate, rustlr
//! should be installed with only the runtime parsing routines with
//! **`cargo add rustlr --no-default-features`**. Alternatively, add the
//! the following to your Cargo.toml:
//! ```
//! [dependencies]
//! rustlr = { version = "0.5", default-features = false }
//! ```
//!
//! **Compatibility Notice:**
//!
//! There is another optional feature, `legacy-parser`, that can be enabled
//! with or without the parser generation routines, that is required for
//! grammars and parsers for very old versions of rustlr (prior to version 0.2).
//! This feature is *not* included by default and must be installed with
//! the `cargo install/add --features legacy-parser` option.
//!
//! Many of the items exported are only required by the parsers
//! that are generated, and are not intended to be used in other programs.
//! However, rustlr uses traits and trait objects to loosely couple the
//! various components of the runtime parser so that custom interfaces, such as
//! those for graphical IDEs, can be built around a basic [ZCParser::parse_core]
//! function.
//!
pub use *;
use *;
use *;
pub use *;
pub use *;
use *;
pub use *;
// experimental
//mod logos_lexer;
use LALRMachine;
use ;
pub use ;
pub use ;
pub const VERSION:&'static str = "0.5.1";
/// This function can be called from within Rust to generate a parser/lexer.
/// It takes the same arguments as the rustlr command-line application.
/// Furthermore, if given the `-trace 0` option, no output will be
/// sent to stdout or stderr. Instead, a log of events is recorded and
/// is returned. An `Ok(_)` result indicates that some parser was created
/// and an `Err(_)` result indicates failure.
/// Example:
/// ```ignore
/// let report = rustlr::generate("simplecalc.grammar -o src/main.rs -trace 0");
/// ```
/// This function is retained for backwards compatiblity. It is recommended
/// to call [generate] instead.
let mut filepath = "";
let mut parserfile = Stringfrom; // -o target
let mut lalr = false; // changed from false in version 0.2.0
let mut newlalr = true;
let mut tracelev:usize = 1; // trace-level
let mut verbose = false;
let mut zc = true;
let mut genlex = false;
let mut genabsyn = false;
let mut lrsd = false;
let mut lrsdmaxk:usize = MAXK;
let mut regenerate = false;
let mut mode = 0;
let mut conv_yacc = false;
let mut argi = 1; // next argument position
while
match args ,
"verbose" | "-verbose" => ,
"-zc" | "zero_copy" => ,
"genlex" | "-genlex" => ,
"-genabsyn" | "-ast" | "-auto" => ,
"-nozc" => ,
"binary" | "-binary" => ,
"-o" =>
},
_ => ,
}//match directive
argi+=1;
}//while there are command-line args
if filepath.len==0
if conv_yacc
if zc && verbose
if tracelev>0 && verbose
if tracelev>1
let mut grammar1 = new;
grammar1.genlex = genlex;
grammar1.genabsyn = genabsyn;
grammar1.tracelev = tracelev;
grammar1.mode = mode; // 0 for rust, 1 for fsharp
let parsedok = grammar1.parse_grammar; // ***
if !parsedok
// Check grammar integrity: now done inside parse
if grammar1.name.len<2 // derive grammar name
let gramname = grammar1.name.clone;
let pfsuffix = if mode==1 else ;
if grammar1.genabsyn
grammar1.delay_transform; // static delayed reduction markers
if tracelev>2
grammar1.compute_NullableRf;
if tracelev>2
//grammar1.compute_FirstIM();
grammar1.compute_First;
let mut fsm0;
if lrsd else // not lrsd
if newlalr
else // old code
if tracelev>2 && !newlalr && !lrsd
else if tracelev>1 && !newlalr && !lrsd //print states
if parserfile.len<1 || parserfile.ends_with || parserfile.ends_with
if fsm0.States.len>65536
let write_result =
if mode==1
else
if zc
else ; // write_result =
//if tracelev>0 && !lrsd {eprintln!("{} total states",fsm0.FSM.len());}
fsm0.Gmr.logprint;
if let Ok = write_result
else if let Err = write_result
let mut savedlog = Stringnew;
if tracelev==0
Ok
}//rustle1