langlang_syntax 0.1.2

langlang is a parser generator based on Parsing Expression Grammars (library)
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
use std::collections::HashMap;
use std::fmt::Debug;
use std::string::{String as StdString, ToString};

use langlang_value::source_map::Span;

/// Grammar is the top-level AST node for the input grammar language.
#[derive(Debug)]
pub struct Grammar {
    pub span: Span,
    pub imports: Vec<Import>,
    pub definition_names: Vec<StdString>,
    pub definitions: HashMap<StdString, Definition>,
}

impl Grammar {
    pub fn new(
        span: Span,
        imports: Vec<Import>,
        definition_names: Vec<StdString>,
        definitions: HashMap<StdString, Definition>,
    ) -> Self {
        Self {
            span,
            imports,
            definition_names,
            definitions,
        }
    }

    pub fn add_definition(&mut self, d: &Definition) {
        if self.definitions.get(&d.name).is_none() {
            self.definition_names.push(d.name.clone());
            self.definitions.insert(d.name.clone(), d.clone());
        }
    }
}

impl ToString for Grammar {
    fn to_string(&self) -> StdString {
        let mut output = StdString::new();
        for i in &self.imports {
            output.push_str(&i.to_string());
            output.push('\n');
        }
        output.push('\n');
        for name in &self.definition_names {
            let d = &self.definitions[name];
            output.push_str(&d.to_string());
            output.push('\n');
        }
        output
    }
}

/// Import represents an import node and contains both names to be
/// imported and the path to import the names from.
#[derive(Clone, Debug)]
pub struct Import {
    pub span: Span,
    pub path: StdString,
    pub names: Vec<StdString>,
}

impl ToString for Import {
    fn to_string(&self) -> StdString {
        format!(
            "@import {} from \"{}\"",
            fmtlistsep(", ", &self.names),
            self.path
        )
    }
}

impl Import {
    pub fn new(span: Span, path: StdString, names: Vec<StdString>) -> Self {
        Self { span, path, names }
    }
}

/// Definition represents a single production definition.  It stores
/// both the name and the expression associated with the production.
#[derive(Clone, Debug)]
pub struct Definition {
    pub span: Span,
    pub name: StdString,
    pub expr: Expression,
}

impl Definition {
    pub fn new(span: Span, name: StdString, expr: Expression) -> Self {
        Self { span, name, expr }
    }
}

impl ToString for Definition {
    fn to_string(&self) -> StdString {
        format!("{} <- {}", self.name, self.expr.to_string())
    }
}

pub trait IsSyntactic {
    fn is_syntactic(&self) -> bool {
        false
    }
}

fn is_syntactic_list<T: IsSyntactic>(items: &[T]) -> bool {
    items
        .iter()
        .map(|i| i.is_syntactic())
        .reduce(|acc, i| acc && i)
        .unwrap_or(false)
}

#[derive(Clone, Debug, PartialEq)]
pub enum Expression {
    Sequence(Sequence),
    Choice(Choice),
    Lex(Lex),
    And(And),
    Not(Not),
    Optional(Optional),
    ZeroOrMore(ZeroOrMore),
    OneOrMore(OneOrMore),
    Precedence(Precedence),
    Label(Label),
    List(List),
    Node(Node),
    Identifier(Identifier),
    Literal(Literal),
    Empty(Empty),
}

impl IsSyntactic for Expression {
    fn is_syntactic(&self) -> bool {
        match self {
            Expression::Choice(v) => is_syntactic_list(&v.items),
            Expression::Sequence(v) => v.is_syntactic(),
            Expression::Lex(_) => true,
            Expression::And(v) => v.expr.is_syntactic(),
            Expression::Not(v) => v.expr.is_syntactic(),
            Expression::Optional(v) => v.expr.is_syntactic(),
            Expression::ZeroOrMore(v) => v.expr.is_syntactic(),
            Expression::OneOrMore(v) => v.expr.is_syntactic(),
            Expression::Precedence(v) => v.expr.is_syntactic(),
            Expression::Label(v) => v.expr.is_syntactic(),
            Expression::List(v) => is_syntactic_list(&v.items),
            Expression::Node(v) => v.expr.is_syntactic(),
            Expression::Identifier(_) => false,
            Expression::Literal(_) => true,
            Expression::Empty(_) => true,
        }
    }
}

impl ToString for Expression {
    fn to_string(&self) -> StdString {
        match self {
            Expression::Choice(v) => format!("({})", fmtlistsep(" / ", &v.items)),
            Expression::Sequence(v) => fmtlistsep(" ", &v.items),
            Expression::Lex(v) => fmtprefix("#", &v.expr),
            Expression::And(v) => fmtprefix("&", &v.expr),
            Expression::Not(v) => fmtprefix("!", &v.expr),
            Expression::Optional(v) => fmtsuffix("?", &v.expr),
            Expression::ZeroOrMore(v) => fmtsuffix("*", &v.expr),
            Expression::OneOrMore(v) => fmtsuffix("+", &v.expr),
            Expression::Precedence(v) => format!("{}{}", v.expr.to_string(), v.precedence),
            Expression::Label(v) => format!("{}^{}", v.expr.to_string(), v.label),
            Expression::List(v) => format!("[{}]", fmtlistsep(", ", &v.items)),
            Expression::Node(v) => format!("{} {{{}}}", v.name, v.expr.to_string()),
            Expression::Identifier(v) => v.name.to_string(),
            Expression::Literal(v) => v.to_string(),
            Expression::Empty(_) => "".to_string(),
        }
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct Sequence {
    pub span: Span,
    pub items: Vec<Expression>,
}

impl Sequence {
    pub fn new_expr(span: Span, items: Vec<Expression>) -> Expression {
        Expression::Sequence(Self { span, items })
    }
}

impl IsSyntactic for Sequence {
    fn is_syntactic(&self) -> bool {
        is_syntactic_list(&self.items)
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct Choice {
    pub span: Span,
    pub items: Vec<Expression>,
}

impl Choice {
    pub fn new_expr(span: Span, items: Vec<Expression>) -> Expression {
        Expression::Choice(Choice::new(span, items))
    }

    pub fn new(span: Span, items: Vec<Expression>) -> Self {
        Self { span, items }
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct Lex {
    pub span: Span,
    pub expr: Box<Expression>,
}

impl Lex {
    pub fn new_expr(span: Span, expr: Box<Expression>) -> Expression {
        Expression::Lex(Lex::new(span, expr))
    }

    pub fn new(span: Span, expr: Box<Expression>) -> Self {
        Self { span, expr }
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct And {
    pub span: Span,
    pub expr: Box<Expression>,
}

impl And {
    pub fn new_expr(span: Span, expr: Box<Expression>) -> Expression {
        Expression::And(Self::new(span, expr))
    }

    pub fn new(span: Span, expr: Box<Expression>) -> Self {
        Self { span, expr }
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct Not {
    pub span: Span,
    pub expr: Box<Expression>,
}

impl Not {
    pub fn new_expr(span: Span, expr: Box<Expression>) -> Expression {
        Expression::Not(Self { span, expr })
    }

    pub fn new(span: Span, expr: Box<Expression>) -> Self {
        Self { span, expr }
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct Optional {
    pub span: Span,
    pub expr: Box<Expression>,
}

impl Optional {
    pub fn new_expr(span: Span, expr: Box<Expression>) -> Expression {
        Expression::Optional(Self { span, expr })
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct ZeroOrMore {
    pub span: Span,
    pub expr: Box<Expression>,
}

impl ZeroOrMore {
    pub fn new_expr(span: Span, expr: Box<Expression>) -> Expression {
        Expression::ZeroOrMore(Self { span, expr })
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct OneOrMore {
    pub span: Span,
    pub expr: Box<Expression>,
}

impl OneOrMore {
    pub fn new_expr(span: Span, expr: Box<Expression>) -> Expression {
        Expression::OneOrMore(Self { span, expr })
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct Precedence {
    pub span: Span,
    pub expr: Box<Expression>,
    pub precedence: usize,
}

impl Precedence {
    pub fn new_expr(span: Span, expr: Box<Expression>, precedence: usize) -> Expression {
        Expression::Precedence(Self {
            span,
            expr,
            precedence,
        })
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct Label {
    pub span: Span,
    pub label: StdString,
    pub expr: Box<Expression>,
}

impl Label {
    pub fn new_expr(span: Span, label: StdString, expr: Box<Expression>) -> Expression {
        Expression::Label(Self { span, label, expr })
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct List {
    pub span: Span,
    pub items: Vec<Expression>,
}

impl List {
    pub fn new_expr(span: Span, items: Vec<Expression>) -> Expression {
        Expression::List(Self { span, items })
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct Node {
    pub span: Span,
    pub name: StdString,
    pub expr: Box<Expression>,
}

impl Node {
    pub fn new_expr(span: Span, name: StdString, expr: Box<Expression>) -> Expression {
        Expression::Node(Self { span, name, expr })
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct Identifier {
    pub span: Span,
    pub name: StdString,
}

impl Identifier {
    pub fn new_expr(span: Span, name: StdString) -> Expression {
        Expression::Identifier(Self::new(span, name))
    }

    pub fn new(span: Span, name: StdString) -> Self {
        Self { span, name }
    }
}

#[derive(Clone, Debug, PartialEq)]
pub enum Literal {
    String(String),
    Class(Class),
    Range(Range),
    Char(Char),
    Any(Any),
}

impl ToString for Literal {
    fn to_string(&self) -> StdString {
        match self {
            Literal::String(v) => format!("\"{}\"", v.to_string()),
            Literal::Class(v) => v.to_string(),
            Literal::Range(v) => format!("{}-{}", v.start, v.end),
            Literal::Char(v) => v.to_string(),
            Literal::Any(_) => ".".to_string(),
        }
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct String {
    pub span: Span,
    pub value: StdString,
}

impl String {
    pub fn new_expr(span: Span, value: StdString) -> Expression {
        Expression::Literal(Literal::String(Self { span, value }))
    }
}

impl ToString for String {
    fn to_string(&self) -> StdString {
        self.value
            .chars()
            .flat_map(|c| c.escape_default())
            .collect()
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct Class {
    pub span: Span,
    pub literals: Vec<Literal>,
}

impl Class {
    pub fn new_expr(span: Span, literals: Vec<Literal>) -> Expression {
        Expression::Literal(Literal::Class(Self { span, literals }))
    }
}

impl ToString for Class {
    fn to_string(&self) -> StdString {
        let mut output = StdString::new();
        output.push('[');
        for l in &self.literals {
            output.push_str(&l.to_string());
        }
        output.push(']');
        output
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct Range {
    pub span: Span,
    pub start: char,
    pub end: char,
}

impl Range {
    pub fn new(span: Span, start: char, end: char) -> Self {
        Self { span, start, end }
    }
}

/// Char stores the position and value of a single character matcher
#[derive(Clone, Debug, PartialEq)]
pub struct Char {
    pub span: Span,
    pub value: char,
}

impl Char {
    pub fn new(span: Span, value: char) -> Self {
        Self { span, value }
    }
}

impl ToString for Char {
    fn to_string(&self) -> StdString {
        self.value.escape_default().collect()
    }
}

/// Any is the operator that matches anything but EOF
#[derive(Clone, Debug, PartialEq)]
pub struct Any {
    pub span: Span,
}

impl Any {
    pub fn new_expr(span: Span) -> Expression {
        Expression::Literal(Literal::Any(Self { span }))
    }
}

/// Empty represents the empty alternative of an ordered choice
/// operator.  Both start and end of such span are the same as no
/// input is consumed.
#[derive(Clone, Debug, PartialEq)]
pub struct Empty {
    pub span: Span,
}

impl Empty {
    pub fn new_expr(span: Span) -> Expression {
        Expression::Empty(Self { span })
    }
}

// formatting functions

fn fmtlistsep<T: ToString>(sep: &str, items: &Vec<T>) -> StdString {
    let mut output = StdString::new();
    let len = items.len();

    for (index, item) in items.iter().enumerate() {
        output.push_str(&item.to_string());
        if index < len - 1 {
            output.push_str(sep);
        }
    }

    output
}

fn fmtprefix(prefix: &str, node: &Expression) -> StdString {
    if tree_height(node) > 1 {
        return format!("{}({})", prefix, node.to_string());
    }
    if let Expression::Sequence(seq) = node {
        if seq.items.len() > 1 {
            return format!("{}({})", prefix, node.to_string());
        }
    }
    format!("{}{}", prefix, node.to_string())
}

fn fmtsuffix(suffix: &str, node: &Expression) -> StdString {
    if tree_height(node) > 1 {
        return format!("({}){}", node.to_string(), suffix);
    }
    if let Expression::Sequence(seq) = node {
        if seq.items.len() > 1 {
            return format!("({}){}", node.to_string(), suffix);
        }
    }
    format!("{}{}", node.to_string(), suffix)
}

fn tree_height(n: &Expression) -> usize {
    match n {
        Expression::Sequence(v) => items_height(&v.items),
        Expression::Choice(v) => items_height(&v.items) + 1,
        Expression::Lex(v) => tree_height(&v.expr) + 1,
        Expression::And(v) => tree_height(&v.expr) + 1,
        Expression::Not(v) => tree_height(&v.expr) + 1,
        Expression::Optional(v) => tree_height(&v.expr) + 1,
        Expression::ZeroOrMore(v) => tree_height(&v.expr) + 1,
        Expression::OneOrMore(v) => tree_height(&v.expr) + 1,
        Expression::Precedence(v) => tree_height(&v.expr) + 1,
        Expression::Label(v) => tree_height(&v.expr) + 1,
        Expression::List(v) => items_height(&v.items) + 1,
        Expression::Node(v) => tree_height(&v.expr) + 1,
        Expression::Identifier(_) => 1,
        Expression::Literal(_) => 1,
        Expression::Empty(_) => 1,
    }
}

fn items_height(items: &[Expression]) -> usize {
    items
        .iter()
        .map(tree_height)
        .fold(usize::MIN, |a, b| a.max(b))
}