rusty_lr_parser 3.64.1

grammar line parser for rusty_lr
Documentation
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
use proc_macro2::Ident;
use proc_macro2::Literal;
use proc_macro2::Span;
use proc_macro2::TokenStream;

use quote::quote_spanned;

use crate::parser::args::IdentOrLiteral;

/// failed to feed() the token
#[non_exhaustive]
#[derive(Debug)]
pub enum ParseArgError {
    /// feed() failed
    MacroLineParse { span: Span, message: String },
}

#[non_exhaustive]
#[derive(Debug)]
pub enum ArgError {
    MultipleModulePrefixDefinition((Span, TokenStream), (Span, TokenStream)),
    MultipleUserDataDefinition((Span, TokenStream), (Span, TokenStream)),
    MultipleErrorDefinition((Span, TokenStream), (Span, TokenStream)),
    MultipleTokenTypeDefinition((Span, TokenStream), (Span, TokenStream)),
    MultipleEofDefinition((Span, TokenStream), (Span, TokenStream)),
    MultipleStartDefinition(Ident, Ident),

    StartNotDefined,
    EofNotDefined,
    TokenTypeNotDefined,

    /// multiple %prec in the same rule
    MultiplePrecDefinition(Span),
    /// multiple %dprec in the same rule
    MultipleDPrecDefinition(Span),
}

#[non_exhaustive]
#[derive(Debug)]
pub enum ConflictError {
    /// error building given CFG
    ShiftReduceConflict {
        term: String,
        reduce_rule: (usize, rusty_lr_core::rule::ProductionRule<String, String>),
        shift_rules: Vec<(usize, rusty_lr_core::rule::ShiftedRule<String, String>)>,
    },
    /// error building given CFG
    ReduceReduceConflict {
        lookahead: String,
        rule1: (usize, rusty_lr_core::rule::ProductionRule<String, String>),
        rule2: (usize, rusty_lr_core::rule::ProductionRule<String, String>),
    },
}

#[non_exhaustive]
#[derive(Debug)]
pub enum ParseError {
    MultipleRuleDefinition(Ident, Ident),

    /// different reduce type applied to the same terminal symbol
    MultipleReduceDefinition {
        terminal: String,
        old: (Span, rusty_lr_core::rule::ReduceType),
        new: (Span, rusty_lr_core::rule::ReduceType),
    },

    /// multiple %token definition
    MultipleTokenDefinition(Ident, Ident),

    /// same name for terminal and non-terminal exists
    TermNonTermConflict {
        name: Ident,
        terminal: Ident,
        non_terminal: Ident,
    },

    InvalidTerminalRange((Ident, usize, TokenStream), (Ident, usize, TokenStream)),

    /// name given to %start not defined
    StartNonTerminalNotDefined(Ident),

    /// unknown terminal symbol name
    TerminalNotDefined(Ident),

    /// can't use reserved keyword as token name
    ReservedName(Ident),

    /// not supported literal type
    UnsupportedLiteralType(TokenStream),

    /// range in literal terminal set is not valid
    InvalidLiteralRange(Literal, Literal),

    /// TokenType in Literal mode is not supported
    TokenInLiteralMode(Span),

    /// conflicts in precedence definition
    MultiplePrecedenceOrderDefinition {
        cur: IdentOrLiteral,
        old: Span,
    },

    /// Precedence not defined for the given token
    PrecedenceNotDefined(IdentOrLiteral),

    /// All production rules in this non-terminal must have %prec defined
    NonTerminalPrecedenceNotDefined(Span, usize),

    /// ReduceAction must be defined but not defined
    RuleTypeDefinedButActionNotDefined {
        name: Ident,
        span: (Span, Span),
    },

    /// Only terminal or terminal set is allowed
    OnlyTerminalSet(Span, Span),

    /// unknown non-terminal symbol name
    NonTerminalNotDefined(Ident),

    /// only 'usize' literal is allowed for %dprec
    OnlyUsizeLiteral(Span),
}
#[allow(unused)]
impl ArgError {
    pub fn to_compile_error(&self) -> TokenStream {
        let span = self.span();
        let message = self.short_message();
        quote_spanned! {
            span=>
            compile_error!(#message);
        }
    }

    pub fn span(&self) -> Span {
        match self {
            ArgError::MultipleModulePrefixDefinition(
                (span1, tokenstream1),
                (span2, tokenstream2),
            ) => *span2,
            ArgError::MultipleUserDataDefinition((span1, tokenstream1), (span2, tokenstream2)) => {
                *span2
            }
            ArgError::MultipleErrorDefinition((span1, tokenstream1), (span2, tokenstream2)) => {
                *span2
            }
            ArgError::MultipleTokenTypeDefinition((span1, tokenstream1), (span2, tokenstream2)) => {
                *span2
            }
            ArgError::MultipleEofDefinition((span1, tokenstream1), (span2, tokenstream2)) => *span2,
            ArgError::MultipleStartDefinition(old, new) => new.span(),

            ArgError::StartNotDefined => Span::call_site(),
            ArgError::EofNotDefined => Span::call_site(),
            ArgError::TokenTypeNotDefined => Span::call_site(),

            ArgError::MultiplePrecDefinition(span) => *span,
            ArgError::MultipleDPrecDefinition(span) => *span,
        }
    }

    pub fn short_message(&self) -> String {
        match self {
            ArgError::MultipleModulePrefixDefinition(
                (span1, tokenstream1),
                (span2, tokenstream2),
            ) => "Multiple %moduleprefix definition".into(),
            ArgError::MultipleUserDataDefinition((span1, tokenstream1), (span2, tokenstream2)) => {
                "Multiple %userdata definition".into()
            }
            ArgError::MultipleErrorDefinition((span1, tokenstream1), (span2, tokenstream2)) => {
                "Multiple %error definition".into()
            }
            ArgError::MultipleTokenTypeDefinition((span1, tokenstream1), (span2, tokenstream2)) => {
                "Multiple %tokentype definition".into()
            }
            ArgError::MultipleEofDefinition((span1, tokenstream1), (span2, tokenstream2)) => {
                "Multiple %eof definition".into()
            }
            ArgError::MultipleStartDefinition(old, new) => {
                format!("Multiple %start definition: {} and {}", old, new)
            }

            ArgError::StartNotDefined => "Start rule not defined\n>>> %start <rule_name>;".into(),
            ArgError::EofNotDefined => "Eof not defined\n>>> %eof <eof_token_value>;".into(),
            ArgError::TokenTypeNotDefined => {
                "Token type not defined\n>>> %tokentype <token_type_name>;".into()
            }

            ArgError::MultiplePrecDefinition(span) => "Multiple %prec definition".into(),
            ArgError::MultipleDPrecDefinition(span) => "Multiple %dprec definition".into(),
        }
    }
}
#[allow(unused)]
impl ParseArgError {
    pub fn to_compile_error(&self) -> TokenStream {
        let span = self.span();
        let message = self.short_message();
        quote_spanned! {
            span=>
            compile_error!(#message);
        }
    }

    pub fn span(&self) -> Span {
        match self {
            ParseArgError::MacroLineParse { span, message } => *span,
        }
    }

    pub fn short_message(&self) -> String {
        match self {
            ParseArgError::MacroLineParse { span, message } => message.clone(),
        }
    }
}

#[allow(unused)]
impl ParseError {
    pub fn to_compile_error(&self) -> TokenStream {
        let span = self.span();
        let message = self.short_message();
        quote_spanned! {
            span=>
            compile_error!(#message);
        }
    }

    pub fn span(&self) -> Span {
        match self {
            ParseError::MultipleRuleDefinition(old, new) => new.span(),

            ParseError::MultipleReduceDefinition { terminal, old, new } => new.0,

            ParseError::TermNonTermConflict {
                name,
                terminal,
                non_terminal,
            } => name.span(),

            ParseError::InvalidTerminalRange((first, first_index, _), (last, last_index, _)) => {
                first.span()
            }

            ParseError::StartNonTerminalNotDefined(ident) => ident.span(),

            ParseError::TerminalNotDefined(ident) => ident.span(),

            ParseError::MultipleTokenDefinition(old, new) => new.span(),

            ParseError::ReservedName(ident) => ident.span(),

            ParseError::UnsupportedLiteralType(stream) => {
                stream.clone().into_iter().next().unwrap().span()
            }

            ParseError::InvalidLiteralRange(first, last) => first.span(),

            ParseError::TokenInLiteralMode(open_span) => *open_span,

            ParseError::MultiplePrecedenceOrderDefinition { cur, old } => cur.span(),
            ParseError::PrecedenceNotDefined(name) => name.span(),
            ParseError::NonTerminalPrecedenceNotDefined(span, _) => *span,

            ParseError::RuleTypeDefinedButActionNotDefined { name, span } => span.0,
            ParseError::OnlyTerminalSet(span_begin, span_end) => *span_begin,
            ParseError::NonTerminalNotDefined(ident) => ident.span(),
            ParseError::OnlyUsizeLiteral(span) => *span,
        }
    }

    pub fn short_message(&self) -> String {
        match self {
            ParseError::MultipleRuleDefinition(old, new) => {
                format!("Multiple rule definition with same name: {}", old)
            }

            ParseError::MultipleReduceDefinition { terminal, old, new } => {
                format!("Differnt reduce type (%left and %right) applied to the same terminal symbol: {}", terminal)
            }

            ParseError::TermNonTermConflict {
                name,
                terminal,
                non_terminal,
            } => {
                format!("Same name for terminal and non-terminal exists: {}", name)
            }

            ParseError::InvalidTerminalRange((first, first_index, _), (last, last_index, _)) => {
                format!(
                    "Invalid terminal range: [{}({}) - {}({})]",
                    first, first_index, last, last_index
                )
            }

            ParseError::StartNonTerminalNotDefined(ident) => {
                format!("Name given to %start not defined: {}", ident)
            }

            ParseError::TerminalNotDefined(ident) => {
                format!("Unknown terminal symbol name: {}", ident)
            }

            ParseError::MultipleTokenDefinition(old, new) => {
                format!("Multiple %token definition with same name: {}", old)
            }

            ParseError::ReservedName(ident) => {
                format!("'{}' is reserved name", ident)
            }

            ParseError::UnsupportedLiteralType(literal) => {
                format!("Not supported literal type: {}", literal)
            }

            ParseError::InvalidLiteralRange(first, last) => {
                format!(
                    "Range in literal terminal set is not valid: [{} - {}]",
                    first, last
                )
            }

            ParseError::TokenInLiteralMode(_) => {
                "%token with %tokentype `char` or `u8` is not supported. Use 'a' or b'a' instead"
                    .to_string()
            }

            ParseError::MultiplePrecedenceOrderDefinition { cur, old } => {
                format!("Conflicts with precedence definition: {}", cur)
            }
            ParseError::PrecedenceNotDefined(name) => {
                format!("Precedence not defined for the given token: {}", name)
            }
            ParseError::NonTerminalPrecedenceNotDefined(span, nonterm_idx) => {
                "All production rules in this non-terminal must have %prec defined".into()
            }

            ParseError::RuleTypeDefinedButActionNotDefined { name, span } => {
                "ReduceAction must be defined for this rule".into()
            }
            ParseError::OnlyTerminalSet(_, _) => "Only terminal or terminal set is allowed".into(),
            ParseError::NonTerminalNotDefined(ident) => {
                format!("Unknown non-terminal symbol name: {}", ident)
            }
            ParseError::OnlyUsizeLiteral(_) => "Only 'usize' literal is allowed for %dprec".into(),
        }
    }
}

#[allow(unused)]
impl ConflictError {
    pub fn to_compile_error(&self) -> TokenStream {
        let span = self.span();
        let message = self.short_message();
        quote_spanned! {
            span=>
            compile_error!(#message);
        }
    }

    pub fn span(&self) -> Span {
        match self {
            ConflictError::ShiftReduceConflict {
                term,
                reduce_rule: (ruleid, rule),
                shift_rules,
            } => Span::call_site(),
            ConflictError::ReduceReduceConflict {
                lookahead,
                rule1: (ruleid1, rule1),
                rule2: (ruleid2, rule2),
            } => Span::call_site(),
        }
    }

    pub fn short_message(&self) -> String {
        match self {
            ConflictError::ShiftReduceConflict {
                term,
                reduce_rule: (ruleid, rule),
                shift_rules,
            } => {
                format!(
                    "Shift-Reduce conflict with terminal symbol: {}\n>>> Reduce: {}\n>>> Shifts: {}",
                    term,
                    rule,
                    shift_rules
                        .iter()
                        .map(|(ruleid, rule)| format!("{}", rule))
                        .collect::<Vec<_>>()
                        .join("\n>>>")
                )
            }
            ConflictError::ReduceReduceConflict {
                lookahead,
                rule1: (ruleid1, rule1),
                rule2: (ruleid2, rule2),
            } => {
                format!(
                    "Reduce-Reduce conflict with lookahead symbol: {}\n>>> Rule1: {}\n>>> Rule2: {}",
                    lookahead, rule1, rule2
                )
            }
        }
    }
}