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
//! Here we use conflict resolution to parse mathematical expressions.
//!
//! The grammar will be :
//! ```text
//! Start → Statements
//! Statements → Statement Statements
//! Statements →
//! Statement → Function
//! Statement → RETURN_KW Expression ;
//! Statement → LET_KW IDENT = Expression ;
//! Statement → Expression ;
//! Function → FN_KW IDENT ( FunctionArgs ) { Statements }
//! FunctionArgs → FunctionArg , FunctionArgs
//! FunctionArgs → FunctionArg
//! FunctionArgs →
//! FunctionArg → IDENT
//! Expression → INT
//! Expression → IDENT
//! Expression → Expression ( Args )
//! Expression → Expression + Expression
//! Expression → Expression - Expression
//! Expression → Expression * Expression
//! Expression → Expression / Expression
//! Expression → - Expression
//! Args → Expression , Args
//! Args → Expression
//! Args →
//! ```
//! With the usual precedence for expressions.

/*
 * Copyright 2022 Arnaud Golfouse
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

use ielr::{
    input::{ConflictSolution, ConflictingAction, Grammar, Node, Symbol, Token},
    output::Lookahead,
};
use std::collections::HashMap;

const START: Node = Node(0);
const STATEMENTS: Node = Node(1);
const STATEMENT: Node = Node(2);
const FUNCTION: Node = Node(3);
const FUNCTION_ARGS: Node = Node(4);
const FUNCTION_ARG: Node = Node(5);
const EXPRESSION: Node = Node(6);
const ARGS: Node = Node(7);

/// workaround for the lack of const unwrap
const fn new_token(t: u16) -> Token {
    match Token::new(t) {
        Some(t) => t,
        None => unreachable!(),
    }
}

const RETURN_KW: Token = new_token(1);
const LET_KW: Token = new_token(2);
const IDENT: Token = new_token(3);
const SEMICOLON: Token = new_token(4);
const EQUAL: Token = new_token(5);
const FN_KW: Token = new_token(6);
const PARENTHESIS_LEFT: Token = new_token(7);
const PARENTHESIS_RIGHT: Token = new_token(8);
const BRACE_LEFT: Token = new_token(9);
const BRACE_RIGHT: Token = new_token(10);
const COMMA: Token = new_token(11);
const INT: Token = new_token(12);
const PLUS: Token = new_token(13);
const MINUS: Token = new_token(14);
const STAR: Token = new_token(15);
const SLASH: Token = new_token(16);

fn main() {
    use Symbol::{Node as N, Token as T};

    let mut grammar = Grammar::new();
    // productions that do not need precedence annotations
    for (lhs, rhs) in [
        (START, vec![N(STATEMENTS)]),
        (STATEMENTS, vec![N(STATEMENT), N(STATEMENTS)]),
        (STATEMENTS, vec![]),
        (STATEMENT, vec![N(FUNCTION)]),
        (STATEMENT, vec![T(RETURN_KW), N(EXPRESSION), T(SEMICOLON)]),
        (
            STATEMENT,
            vec![T(LET_KW), T(IDENT), T(EQUAL), N(EXPRESSION), T(SEMICOLON)],
        ),
        (STATEMENT, vec![N(EXPRESSION), T(SEMICOLON)]),
        (
            FUNCTION,
            vec![
                T(FN_KW),
                T(IDENT),
                T(PARENTHESIS_LEFT),
                N(FUNCTION_ARGS),
                T(PARENTHESIS_RIGHT),
                T(BRACE_LEFT),
                N(STATEMENTS),
                T(BRACE_RIGHT),
            ],
        ),
        (
            FUNCTION_ARGS,
            vec![N(FUNCTION_ARG), T(COMMA), N(FUNCTION_ARGS)],
        ),
        (FUNCTION_ARGS, vec![N(FUNCTION_ARG)]),
        (FUNCTION_ARGS, vec![]),
        (FUNCTION_ARG, vec![T(IDENT)]),
        (EXPRESSION, vec![T(INT)]),
        (EXPRESSION, vec![T(IDENT)]),
        (ARGS, vec![N(EXPRESSION), T(COMMA), N(ARGS)]),
        (ARGS, vec![N(EXPRESSION)]),
        (ARGS, vec![]),
    ] {
        grammar.add_production(lhs, rhs).unwrap();
    }

    let add_prod = grammar
        .add_production(EXPRESSION, vec![N(EXPRESSION), T(PLUS), N(EXPRESSION)])
        .unwrap();
    let sub_prod = grammar
        .add_production(EXPRESSION, vec![N(EXPRESSION), T(MINUS), N(EXPRESSION)])
        .unwrap();
    let mul_prod = grammar
        .add_production(EXPRESSION, vec![N(EXPRESSION), T(STAR), N(EXPRESSION)])
        .unwrap();
    let div_prod = grammar
        .add_production(EXPRESSION, vec![N(EXPRESSION), T(SLASH), N(EXPRESSION)])
        .unwrap();
    let neg_prod = grammar
        .add_production(EXPRESSION, vec![T(MINUS), N(EXPRESSION)])
        .unwrap();

    // Add conflicts solutions.

    let operator_to_production = HashMap::from([
        (PLUS, add_prod),
        (MINUS, sub_prod),
        (STAR, mul_prod),
        (SLASH, div_prod),
    ]);

    // - all binary operators are left-associative here
    // - '-' has precedence over all binary operators
    for operator in [PLUS, MINUS, STAR, SLASH] {
        grammar.add_conflict_solution(ConflictSolution {
            prefer: ConflictingAction::Reduce(operator_to_production[&operator]),
            over: ConflictingAction::Shift(Lookahead::Token(operator)),
        });
        grammar.add_conflict_solution(ConflictSolution {
            prefer: ConflictingAction::Reduce(neg_prod),
            over: ConflictingAction::Shift(Lookahead::Token(operator)),
        });
    }

    // operator with the same precedence
    for (operator1, operator2) in [(PLUS, MINUS), (STAR, SLASH)] {
        grammar.add_conflict_solution(ConflictSolution {
            prefer: ConflictingAction::Reduce(operator_to_production[&operator1]),
            over: ConflictingAction::Shift(Lookahead::Token(operator2)),
        });
        grammar.add_conflict_solution(ConflictSolution {
            prefer: ConflictingAction::Reduce(operator_to_production[&operator2]),
            over: ConflictingAction::Shift(Lookahead::Token(operator1)),
        });
    }

    // operator with the different precedence
    for (prefer, over) in [(STAR, PLUS), (STAR, MINUS), (SLASH, PLUS), (SLASH, MINUS)] {
        grammar.add_conflict_solution(ConflictSolution {
            prefer: ConflictingAction::Shift(Lookahead::Token(prefer)),
            over: ConflictingAction::Reduce(operator_to_production[&over]),
        });
        grammar.add_conflict_solution(ConflictSolution {
            prefer: ConflictingAction::Reduce(operator_to_production[&prefer]),
            over: ConflictingAction::Shift(Lookahead::Token(over)),
        });
    }

    // Now we build the parsing table !
    let (_tables, _statistics) = ielr::compute_table(
        // We are required to specify LR(1) when using conflict resolution.
        ielr::Algorithm::Lr(std::num::NonZeroU8::new(1).unwrap()),
        // We do not care about a maximum number of states
        &grammar,
        [START],
    )
    .unwrap();
}