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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
/*******************************************************************************
 * Copyright (c) 2019 Association Cénotélie (cenotelie.fr)
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this program.
 * If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

//! Library for grammars

use std::cmp::Ordering;
use std::collections::HashMap;
use std::fmt::{Display, Formatter};
use std::sync::atomic::{AtomicUsize, Ordering as AtomicOrdering};

use hime_redist::parsers::{TreeAction, TREE_ACTION_DROP, TREE_ACTION_NONE, TREE_ACTION_PROMOTE};

use crate::errors::{Error, UnmatchableTokenError};
use crate::finite::{FinalItem, DFA, EPSILON, NFA};
use crate::lr::Graph;
use crate::sdk::InMemoryParser;
use crate::{InputReference, ParsingMethod};

/// Represents a symbol in a grammar
pub trait Symbol {
    /// Gets the unique indentifier (within a grammar) of this symbol
    fn get_id(&self) -> usize;

    /// Gets the name of this symbol
    fn get_name(&self) -> &str;

    /// Gets a description for this symbol
    fn get_description(&self) -> String;
}

/// A reference to a terminal in a terminal rule
#[derive(Debug, Clone)]
pub struct TerminalReference {
    /// The identifier of the referring terminal
    pub referring_id: usize,
    /// The input reference
    pub input_ref: InputReference
}

/// Represents a terminal symbol in a grammar
#[derive(Debug, Clone)]
pub struct Terminal {
    /// The unique indentifier (within a grammar) of this symbol
    pub id: usize,
    /// The name of this symbol
    pub name: String,
    /// The inline value of this terminal
    pub value: String,
    /// The input reference for the definition
    pub input_ref: InputReference,
    /// The NFA that is used to match this terminal
    pub nfa: NFA,
    /// The context of this terminal
    pub context: usize,
    /// Whether the terminal is anonymous
    pub is_anonymous: bool,
    /// Whether the terminal is a fragment
    pub is_fragment: bool,
    /// The references to this terminal by others
    pub terminal_references: Vec<TerminalReference>
}

impl Terminal {
    /// Gets the priority of this terminal
    #[must_use]
    pub fn priority(&self) -> usize {
        self.id
    }
}

impl Symbol for Terminal {
    /// Gets the unique indentifier (within a grammar) of this symbol
    fn get_id(&self) -> usize {
        self.id
    }

    /// Gets the name of this symbol
    fn get_name(&self) -> &str {
        &self.name
    }

    /// Gets a description for this symbol
    fn get_description(&self) -> String {
        if self.is_anonymous {
            format!("Inline terminal `{}`", &self.value)
        } else {
            format!(
                "Terminal {}`{}`",
                if self.is_fragment { "(fragment) " } else { "" },
                self.name
            )
        }
    }
}

impl PartialEq for Terminal {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}

impl Eq for Terminal {}

impl Ord for Terminal {
    fn cmp(&self, other: &Terminal) -> Ordering {
        self.id.cmp(&other.id)
    }
}

impl PartialOrd for Terminal {
    fn partial_cmp(&self, other: &Terminal) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

/// Represents a reference to a terminal-like
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum TerminalRef {
    /// Represents a fake terminal, used as a marker by LR-related algorithms
    Dummy,
    /// Represents the epsilon symbol in a grammar, i.e. a terminal with an empty value
    Epsilon,
    /// Represents the dollar symbol in a grammar, i.e. the marker of end of input
    Dollar,
    /// Represents the absence of terminal, used as a marker by LR-related algorithms
    NullTerminal,
    /// A terminal in a grammar
    Terminal(usize)
}

impl TerminalRef {
    /// Gets the symbol id for the terminal
    #[must_use]
    pub fn sid(self) -> usize {
        match self {
            TerminalRef::Dummy | TerminalRef::NullTerminal => 0,
            TerminalRef::Epsilon => 1,
            TerminalRef::Dollar => 2,
            TerminalRef::Terminal(id) => id
        }
    }

    /// Gets the terminal priority
    #[must_use]
    pub fn priority(self) -> usize {
        self.sid()
    }
}

impl Ord for TerminalRef {
    fn cmp(&self, other: &TerminalRef) -> Ordering {
        self.priority().cmp(&other.priority())
    }
}

impl PartialOrd for TerminalRef {
    fn partial_cmp(&self, other: &TerminalRef) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

/// Represents a set of unique terminals (sorted by ID)
#[derive(Debug, Clone, Default, Eq)]
pub struct TerminalSet {
    /// The backing content
    pub content: Vec<TerminalRef>
}

impl PartialEq for TerminalSet {
    fn eq(&self, other: &TerminalSet) -> bool {
        self.content.len() == other.content.len()
            && self
                .content
                .iter()
                .all(|t_ref| other.content.contains(t_ref))
    }
}

impl TerminalSet {
    /// Creates a set with a single element
    #[must_use]
    pub fn single(terminal: TerminalRef) -> TerminalSet {
        TerminalSet {
            content: vec![terminal]
        }
    }

    /// Gets the number of states in this automaton
    #[must_use]
    pub fn len(&self) -> usize {
        self.content.len()
    }

    /// Gets whether the NFA has no state
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.content.is_empty()
    }

    /// Adds a new terminal
    fn do_add(&mut self, item: TerminalRef) -> bool {
        if self.content.contains(&item) {
            false
        } else {
            self.content.push(item);
            true
        }
    }

    /// Adds a new terminal
    pub fn add(&mut self, item: TerminalRef) -> bool {
        let modified = self.do_add(item);
        self.content.sort();
        modified
    }

    /// Adds new terminals
    pub fn add_others(&mut self, others: &TerminalSet) -> bool {
        let mut modified = false;
        for item in &others.content {
            modified |= self.do_add(*item);
        }
        self.content.sort();
        modified
    }

    /// Removes all items from this collection
    pub fn clear(&mut self) {
        self.content.clear();
    }

    /// Sorts this set by priority
    pub fn sort(&mut self) {
        self.content.sort();
    }
}

/// Represents a virtual symbol in a grammar
#[derive(Debug, Clone)]
pub struct Virtual {
    /// The unique indentifier (within a grammar) of this symbol
    pub id: usize,
    /// The name of this symbol
    pub name: String
}

impl Virtual {
    /// Creates a new variable
    #[must_use]
    pub fn new(id: usize, name: String) -> Virtual {
        Virtual { id, name }
    }
}

impl Symbol for Virtual {
    /// Gets the unique indentifier (within a grammar) of this symbol
    fn get_id(&self) -> usize {
        self.id
    }

    /// Gets the name of this symbol
    fn get_name(&self) -> &str {
        &self.name
    }

    /// Gets a description for this symbol
    fn get_description(&self) -> String {
        format!("Virtual symbol `{}`", &self.name)
    }
}

impl PartialEq for Virtual {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}

impl Eq for Virtual {}

/// Represents a symbol for a semantic action in a grammar
#[derive(Debug, Clone)]
pub struct Action {
    /// The unique indentifier (within a grammar) of this symbol
    pub id: usize,
    /// The name of this symbol
    pub name: String
}

impl Action {
    /// Creates a new variable
    #[must_use]
    pub fn new(id: usize, name: String) -> Action {
        Action { id, name }
    }
}

impl Symbol for Action {
    /// Gets the unique indentifier (within a grammar) of this symbol
    fn get_id(&self) -> usize {
        self.id
    }

    /// Gets the name of this symbol
    fn get_name(&self) -> &str {
        &self.name
    }

    /// Gets a description for this symbol
    fn get_description(&self) -> String {
        format!("Action `{}`", &self.name)
    }
}

impl PartialEq for Action {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}

impl Eq for Action {}

/// Represents a variable in a grammar
#[derive(Debug, Clone)]
pub struct Variable {
    /// The unique indentifier (within a grammar) of this symbol
    pub id: usize,
    /// The name of this symbol
    pub name: String,
    /// If the variable was generated, the identifier of the variable in the context of which it was generated
    pub generated_for: Option<usize>,
    /// The rules for this variable
    pub rules: Vec<Rule>,
    /// The FIRSTS set for this variable
    pub firsts: TerminalSet,
    /// The FOLLOWERS set for this variable
    pub followers: TerminalSet
}

impl Variable {
    /// Creates a new variable
    #[must_use]
    pub fn new(id: usize, name: String, generated_for: Option<usize>) -> Variable {
        Variable {
            id,
            name,
            generated_for,
            rules: Vec::new(),
            firsts: TerminalSet::default(),
            followers: TerminalSet::default()
        }
    }

    /// Adds the given rule for this variable as a unique element
    pub fn add_rule(&mut self, rule: Rule) {
        if !self.rules.contains(&rule) {
            self.rules.push(rule);
        }
    }

    /// Compute rule choices for all rules for this variable
    pub fn compute_choices(&mut self) {
        for rule in &mut self.rules {
            rule.body.compute_choices();
        }
    }

    /// Computes the FIRSTS set for this variable
    pub fn compute_firsts(&mut self, firsts_for_var: &mut HashMap<usize, TerminalSet>) -> bool {
        let mut modified = false;
        for rule in &mut self.rules {
            modified |= self.firsts.add_others(&rule.body.firsts);
            modified |= firsts_for_var
                .entry(self.id)
                .or_insert_with(TerminalSet::default)
                .add_others(&rule.body.firsts);
            modified |= rule.body.compute_firsts(firsts_for_var);
        }
        modified
    }

    /// Computes the initial FOLLOWERS sets
    pub fn compute_initial_follower(&self, followers: &mut HashMap<usize, TerminalSet>) {
        for rule in &self.rules {
            rule.body.compute_initial_follower(followers);
        }
    }

    /// Propagate the followers
    pub fn propagate_followers(&self, followers: &mut HashMap<usize, TerminalSet>) -> bool {
        let mut modified = false;
        for rule in &self.rules {
            modified |= rule.body.propagate_followers(rule.head, followers);
        }
        modified
    }
}

impl Symbol for Variable {
    /// Gets the unique indentifier (within a grammar) of this symbol
    fn get_id(&self) -> usize {
        self.id
    }

    /// Gets the name of this symbol
    fn get_name(&self) -> &str {
        &self.name
    }

    /// Gets a description for this symbol
    fn get_description(&self) -> String {
        format!("Variable `{}`", &self.name)
    }
}

impl PartialEq for Variable {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}

impl Eq for Variable {}

/// Represents a reference to a grammar symbol
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum SymbolRef {
    /// Represents a fake terminal, used as a marker by LR-related algorithms
    Dummy,
    /// Represents the epsilon symbol in a grammar, i.e. a terminal with an empty value
    Epsilon,
    /// Represents the dollar symbol in a grammar, i.e. the marker of end of input
    Dollar,
    /// Represents the absence of terminal, used as a marker by LR-related algorithms
    NullTerminal,
    /// A terminal in a grammar
    Terminal(usize),
    /// A variable in a grammar
    Variable(usize),
    /// A virtual symbol in a grammar
    Virtual(usize),
    /// An action symbol in a grammar
    Action(usize)
}

impl SymbolRef {
    /// Gets the terminal priority
    #[must_use]
    pub fn priority(self) -> usize {
        match self {
            SymbolRef::Dummy | SymbolRef::NullTerminal => 0,
            SymbolRef::Epsilon => 1,
            SymbolRef::Dollar => 2,
            SymbolRef::Terminal(id)
            | SymbolRef::Variable(id)
            | SymbolRef::Virtual(id)
            | SymbolRef::Action(id) => id
        }
    }
}

impl Ord for SymbolRef {
    fn cmp(&self, other: &SymbolRef) -> Ordering {
        self.priority().cmp(&other.priority())
    }
}

impl PartialOrd for SymbolRef {
    fn partial_cmp(&self, other: &SymbolRef) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl From<TerminalRef> for SymbolRef {
    fn from(terminal: TerminalRef) -> Self {
        match terminal {
            TerminalRef::Dummy => SymbolRef::Dummy,
            TerminalRef::Epsilon => SymbolRef::Epsilon,
            TerminalRef::Dollar => SymbolRef::Dollar,
            TerminalRef::NullTerminal => SymbolRef::NullTerminal,
            TerminalRef::Terminal(id) => SymbolRef::Terminal(id)
        }
    }
}

/// Represents an element in the body of a grammar rule
#[derive(Debug, Copy, Clone)]
pub struct RuleBodyElement {
    /// The symbol of this element
    pub symbol: SymbolRef,
    /// The action applied on this element
    pub action: TreeAction,
    /// The reference to this body element in the input
    pub input_ref: Option<InputReference>
}

impl PartialEq for RuleBodyElement {
    fn eq(&self, other: &Self) -> bool {
        self.symbol == other.symbol && self.action == other.action
    }
}

impl RuleBodyElement {
    /// Creates a new body element
    #[must_use]
    pub fn new(
        symbol: SymbolRef,
        action: TreeAction,
        input_ref: Option<InputReference>
    ) -> RuleBodyElement {
        RuleBodyElement {
            symbol,
            action,
            input_ref
        }
    }

    /// Gets a version of this element without the action
    #[must_use]
    pub fn no_action(&self) -> RuleBodyElement {
        RuleBodyElement {
            symbol: self.symbol,
            action: TREE_ACTION_NONE,
            input_ref: self.input_ref
        }
    }
}

/// Represents a choice in a rule, i.e. the remainder of a rule's body
#[derive(Debug, Clone, Default)]
pub struct RuleChoice {
    /// The elements in this body
    pub elements: Vec<RuleBodyElement>,
    /// The FIRSTS set of terminals
    pub firsts: TerminalSet
}

impl RuleChoice {
    /// Creates a new choice from a single symbol
    #[must_use]
    pub fn from_single_part(element: &RuleBodyElement) -> RuleChoice {
        RuleChoice {
            elements: vec![element.no_action()],
            firsts: TerminalSet::default()
        }
    }

    /// Initializes this rule body from elements
    #[must_use]
    pub fn from_parts(elements: Vec<RuleBodyElement>) -> RuleBody {
        RuleBody {
            elements,
            firsts: TerminalSet::default(),
            choices: Vec::new()
        }
    }

    /// Gets the length of the rule choice
    #[must_use]
    pub fn len(&self) -> usize {
        self.elements.len()
    }

    /// Gets wether the rule choice is empty
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.elements.is_empty()
    }

    /// Appends a single symbol to the choice
    pub fn append_part(&mut self, element: &RuleBodyElement) {
        self.elements.push(element.no_action());
    }

    /// Appends the content of another choice to this one
    pub fn append_choice(&mut self, other: &RuleChoice) {
        for element in &other.elements {
            self.elements.push(*element);
        }
    }

    /// Computes the FIRSTS set for this rule choice
    pub fn compute_firsts(
        &mut self,
        next: &TerminalSet,
        firsts_for_var: &mut HashMap<usize, TerminalSet>
    ) -> bool {
        // If the choice is empty : Add the ε to the Firsts and return
        if self.elements.is_empty() {
            return self.firsts.add(TerminalRef::Epsilon);
        }
        match self.elements[0].symbol {
            SymbolRef::Variable(id) => {
                let mut modified = false;
                if let Some(var_firsts) = firsts_for_var.get(&id) {
                    for first in &var_firsts.content {
                        match *first {
                            TerminalRef::Epsilon => {
                                modified |= self.firsts.add_others(next);
                            }
                            _ => {
                                modified |= self.firsts.add(*first);
                            }
                        }
                    }
                }
                modified
            }
            SymbolRef::Terminal(id) => self.firsts.add(TerminalRef::Terminal(id)),
            SymbolRef::Dummy => self.firsts.add(TerminalRef::Dummy),
            SymbolRef::Epsilon => self.firsts.add(TerminalRef::Epsilon),
            SymbolRef::Dollar => self.firsts.add(TerminalRef::Dollar),
            SymbolRef::NullTerminal => self.firsts.add(TerminalRef::NullTerminal),
            _ => false
        }
    }
}

impl PartialEq for RuleChoice {
    fn eq(&self, other: &Self) -> bool {
        self.elements.len() == other.elements.len()
            && self
                .elements
                .iter()
                .zip(other.elements.iter())
                .all(|(e1, e2)| e1 == e2)
    }
}

impl Eq for RuleChoice {}

/// Common trait for different kind of rule body
pub trait RuleBodyTrait {
    /// Produces the concatenation of two elements
    fn concatenate(left: &Self, right: &Self) -> Self;

    /// Apply a tree action to all elements in the body
    fn apply_action(&mut self, action: TreeAction);
}

/// A set of rule bodies
pub struct BodySet<T: RuleBodyTrait> {
    /// The bodies in the set
    pub bodies: Vec<T>
}

impl<T: RuleBodyTrait> BodySet<T> {
    /// Builds the union of the left and right set
    #[must_use]
    pub fn union(mut left: BodySet<T>, mut right: BodySet<T>) -> BodySet<T> {
        let mut bodies = Vec::with_capacity(left.bodies.len() + right.bodies.len());
        bodies.append(&mut left.bodies);
        bodies.append(&mut right.bodies);
        BodySet { bodies }
    }

    /// Builds the product of the left and right set
    #[must_use]
    pub fn product(left: BodySet<T>, right: &BodySet<T>) -> BodySet<T> {
        let mut bodies = Vec::with_capacity(left.bodies.len() * right.bodies.len());
        for body_left in left.bodies {
            for body_right in &right.bodies {
                bodies.push(T::concatenate(&body_left, body_right));
            }
        }
        BodySet { bodies }
    }

    /// Apply a tree action to all elements in the bodies
    pub fn apply_action(&mut self, action: TreeAction) {
        for body in &mut self.bodies {
            body.apply_action(action);
        }
    }
}

/// Represents the body of a grammar rule
#[derive(Debug, Clone, Default)]
pub struct RuleBody {
    /// The elements in this body
    pub elements: Vec<RuleBodyElement>,
    /// The FIRSTS set of terminals
    pub firsts: TerminalSet,
    /// The choices in this body
    pub choices: Vec<RuleChoice>
}

impl RuleBodyTrait for RuleBody {
    fn concatenate(left: &RuleBody, right: &RuleBody) -> RuleBody {
        let mut elements = Vec::with_capacity(left.elements.len() + right.elements.len());
        for element in &left.elements {
            elements.push(*element);
        }
        for element in &right.elements {
            elements.push(*element);
        }
        RuleBody {
            elements,
            firsts: TerminalSet::default(),
            choices: Vec::new()
        }
    }

    fn apply_action(&mut self, action: TreeAction) {
        for element in &mut self.elements {
            element.action = action;
        }
    }
}

impl RuleBody {
    /// Initializes this rule body
    #[must_use]
    pub fn empty() -> RuleBody {
        RuleBody {
            elements: Vec::new(),
            firsts: TerminalSet::default(),
            choices: Vec::new()
        }
    }

    /// Initializes this rule body
    #[must_use]
    pub fn single(symbol: SymbolRef, input_ref: InputReference) -> RuleBody {
        RuleBody {
            elements: vec![RuleBodyElement::new(
                symbol,
                TREE_ACTION_NONE,
                Some(input_ref)
            )],
            firsts: TerminalSet::default(),
            choices: Vec::new()
        }
    }

    /// Initializes this rule body from elements
    #[must_use]
    pub fn from_parts(elements: Vec<RuleBodyElement>) -> RuleBody {
        RuleBody {
            elements,
            firsts: TerminalSet::default(),
            choices: Vec::new()
        }
    }

    /// Gets the length of the rule choice
    #[must_use]
    pub fn len(&self) -> usize {
        self.elements.len()
    }

    /// Gets wether the rule choice is empty
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.elements.is_empty()
    }

    /// Appends the content of another choice to this one
    pub fn append_choice(&mut self, other: &RuleChoice) {
        for element in &other.elements {
            self.elements.push(*element);
        }
    }

    /// Applies the given action to all elements in this body
    pub fn apply_action(&mut self, action: TreeAction) {
        for element in &mut self.elements {
            element.action = action;
        }
    }

    /// Computes the FIRSTS set for this rule
    pub fn compute_firsts(&mut self, firsts_for_var: &mut HashMap<usize, TerminalSet>) -> bool {
        let mut modified = false;
        for i in (0..self.choices.len()).rev() {
            if i == self.choices.len() - 1 {
                modified |= self.choices[i].compute_firsts(&TerminalSet::default(), firsts_for_var);
            } else {
                let next_firsts = self.choices[i + 1].firsts.clone();
                modified |= self.choices[i].compute_firsts(&next_firsts, firsts_for_var);
            }
        }
        modified |= self.firsts.add_others(&self.choices[0].firsts);
        modified
    }

    /// Computes the choices for this rule body
    fn compute_choices(&mut self) {
        if self.choices.is_empty() {
            // For each element of the definition which is not a virtual symbol nor an action symbol
            for element in &self.elements {
                match element.symbol {
                    SymbolRef::Virtual(_) | SymbolRef::Action(_) => {}
                    _ => {
                        // Append the symbol to all the choices definition
                        for choice in &mut self.choices {
                            choice.append_part(element);
                        }
                        // Create a new choice with only the symbol
                        self.choices.push(RuleChoice::from_single_part(element));
                    }
                }
            }
            // Create a new empty choice
            self.choices.push(RuleChoice::default());
            self.firsts.add_others(&self.choices[0].firsts);
        }
    }

    /// Computes the initial FOLLOWERS sets
    pub fn compute_initial_follower(&self, followers: &mut HashMap<usize, TerminalSet>) {
        // For all choices but the last (empty)
        for (i, choice) in self.choices.iter().enumerate().take(self.choices.len() - 1) {
            if let SymbolRef::Variable(id) = choice.elements[0].symbol {
                // add the FIRSTS set of the next choice to the variable followers except ε
                for first in &self.choices[i + 1].firsts.content {
                    if *first != TerminalRef::Epsilon {
                        followers.entry(id).or_default().add(*first);
                    }
                }
            }
        }
    }

    /// Propagate the followers
    pub fn propagate_followers(
        &self,
        head: usize,
        followers: &mut HashMap<usize, TerminalSet>
    ) -> bool {
        let mut modified = false;
        // For all choices but the last (empty)
        for (i, choice) in self.choices.iter().enumerate().take(self.choices.len() - 1) {
            if let SymbolRef::Variable(id) = choice.elements[0].symbol {
                // if the next choice FIRSTS set contains ε
                // add the FOLLOWERS of the head variable to the FOLLOWERS of the found variable
                if self.choices[i + 1]
                    .firsts
                    .content
                    .contains(&TerminalRef::Epsilon)
                {
                    let head_followers = followers.get(&head).cloned().unwrap_or_default();
                    modified |= followers.entry(id).or_default().add_others(&head_followers);
                }
            }
        }
        modified
    }
}

impl PartialEq for RuleBody {
    fn eq(&self, other: &Self) -> bool {
        self.elements.len() == other.elements.len()
            && self
                .elements
                .iter()
                .zip(other.elements.iter())
                .all(|(e1, e2)| e1 == e2)
    }
}

impl Eq for RuleBody {}

/// Represents a grammar rule
#[derive(Debug, Clone)]
pub struct Rule {
    /// The rule's head variable
    pub head: usize,
    /// The action on the rule's head
    pub head_action: TreeAction,
    /// The input reference for the rule's head
    pub head_input_ref: InputReference,
    /// The rule's body
    pub body: RuleBody,
    /// The lexical context pushed by this rule
    pub context: usize
}

impl Rule {
    /// Initializes this rule
    #[must_use]
    pub fn new(
        head: usize,
        head_action: TreeAction,
        input_ref: InputReference,
        body: RuleBody,
        context: usize
    ) -> Rule {
        Rule {
            head,
            head_action,
            head_input_ref: input_ref,
            body,
            context
        }
    }
}

impl PartialEq for Rule {
    fn eq(&self, other: &Self) -> bool {
        self.head == other.head && self.body == other.body
    }
}

impl Eq for Rule {}

/// A reference to a grammar rule
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct RuleRef {
    /// The identifier of the variable
    pub variable: usize,
    /// The index of the rule for the variable
    pub index: usize
}

impl RuleRef {
    /// Creates a new rule reference
    #[must_use]
    pub fn new(variable: usize, index: usize) -> RuleRef {
        RuleRef { variable, index }
    }

    /// Gets the referenced rule in the grammar
    ///
    /// # Panics
    ///
    /// Panic when the rule's head cannot be found in the grammar
    #[must_use]
    pub fn get_rule_in<'g>(&self, grammar: &'g Grammar) -> &'g Rule {
        &grammar
            .variables
            .iter()
            .find(|v| v.id == self.variable)
            .unwrap()
            .rules[self.index]
    }
}

/// A reference to a choice in a grammar rule
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct RuleChoiceRef {
    /// The associated rule
    pub rule: RuleRef,
    /// The position within the rule
    pub position: usize
}

impl RuleChoiceRef {
    /// Creates a new choice reference
    #[must_use]
    pub fn new(variable: usize, index: usize, position: usize) -> RuleChoiceRef {
        RuleChoiceRef {
            rule: RuleRef::new(variable, index),
            position
        }
    }
}

/// Reference to a template rule
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct TemplateRuleRef {
    /// Index of the template rule
    pub template: usize,
    /// The input reference for this call
    pub input_ref: InputReference,
    /// The arguments to the template
    pub arguments: Vec<TemplateRuleSymbol>
}

/// A symbol in a template rule
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum TemplateRuleSymbol {
    /// A reference to the n-th parameter of the template
    Parameter(usize),
    /// A usual reference to a grammar symbol
    Symbol(SymbolRef),
    /// A call to template rule
    Template(TemplateRuleRef)
}

/// An element in a template rule
#[derive(Debug, Clone)]
pub struct TemplateRuleElement {
    /// The symbol of this element
    pub symbol: TemplateRuleSymbol,
    /// The action applied on this element
    pub action: TreeAction,
    /// The reference to this body element in the input
    pub input_ref: InputReference
}

impl PartialEq for TemplateRuleElement {
    fn eq(&self, other: &Self) -> bool {
        self.symbol == other.symbol && self.action == other.action
    }
}

impl TemplateRuleElement {
    /// Creates a new body element
    #[must_use]
    pub fn new(
        symbol: TemplateRuleSymbol,
        action: TreeAction,
        input_ref: InputReference
    ) -> TemplateRuleElement {
        TemplateRuleElement {
            symbol,
            action,
            input_ref
        }
    }
}

/// An instance of a template rule
#[derive(Debug, Clone)]
pub struct TemplateRuleInstance {
    /// The arguments used for this instance
    pub arguments: Vec<SymbolRef>,
    /// The identifier of the produced head variable
    pub head: usize
}

impl PartialEq for TemplateRuleInstance {
    fn eq(&self, other: &Self) -> bool {
        self.arguments.len() == other.arguments.len()
            && self
                .arguments
                .iter()
                .zip(other.arguments.iter())
                .all(|(arg1, arg2)| arg1 == arg2)
    }
}

impl Eq for TemplateRuleInstance {}

/// A body for a template rule
#[derive(Debug, Clone)]
pub struct TemplateRuleBody {
    /// The elements in the rule's body
    pub elements: Vec<TemplateRuleElement>
}

impl RuleBodyTrait for TemplateRuleBody {
    fn concatenate(left: &TemplateRuleBody, right: &TemplateRuleBody) -> TemplateRuleBody {
        let mut elements = Vec::with_capacity(left.elements.len() + right.elements.len());
        for element in &left.elements {
            elements.push(element.clone());
        }
        for element in &right.elements {
            elements.push(element.clone());
        }
        TemplateRuleBody { elements }
    }

    fn apply_action(&mut self, action: TreeAction) {
        for element in &mut self.elements {
            element.action = action;
        }
    }
}

impl TemplateRuleBody {
    /// Initializes this rule body
    #[must_use]
    pub fn empty() -> TemplateRuleBody {
        TemplateRuleBody {
            elements: Vec::new()
        }
    }

    /// Initializes this rule body
    #[must_use]
    pub fn single(symbol: TemplateRuleSymbol, input_ref: InputReference) -> TemplateRuleBody {
        TemplateRuleBody {
            elements: vec![TemplateRuleElement::new(
                symbol,
                TREE_ACTION_NONE,
                input_ref
            )]
        }
    }
}

/// A parameter for the template rule
#[derive(Debug, Clone)]
pub struct TemplateRuleParam {
    /// The name of the parameter
    pub name: String,
    /// The input reference
    pub input_ref: InputReference
}

/// A template rule in a grammar
#[derive(Debug, Clone)]
pub struct TemplateRule {
    /// The base head name for the rule
    pub name: String,
    /// The input reference for the definition
    pub input_ref: InputReference,
    /// The name of the parameters for the rule
    pub parameters: Vec<TemplateRuleParam>,
    /// The action on the rule's head
    pub head_action: TreeAction,
    /// The lexical context pushed by this rule
    pub context: usize,
    /// The possible bodies for the template rule
    pub bodies: Vec<TemplateRuleBody>,
    /// The known instanes of this rule
    pub instances: Vec<TemplateRuleInstance>
}

impl TemplateRule {
    /// Initializes a new template rule
    #[must_use]
    pub fn new(
        name: String,
        input_ref: InputReference,
        parameters: Vec<TemplateRuleParam>
    ) -> TemplateRule {
        TemplateRule {
            name,
            input_ref,
            parameters,
            head_action: TREE_ACTION_NONE,
            context: 0,
            bodies: Vec::new(),
            instances: Vec::new()
        }
    }

    /// Determines whether an instance for the specified arguments already exist
    #[must_use]
    pub fn has_instance(&self, arguments: &[SymbolRef]) -> bool {
        self.instances.iter().any(|instance| {
            instance.arguments.len() == arguments.len()
                && instance
                    .arguments
                    .iter()
                    .zip(arguments.iter())
                    .all(|(a1, a2)| a1 == a2)
        })
    }
}

/// The prefix for the generated terminal names
pub const PREFIX_GENERATED_TERMINAL: &str = "__T";
/// The prefix for the generated variable names
pub const PREFIX_GENERATED_VARIABLE: &str = "__V";
/// The name of the generated axiom variable
pub const GENERATED_AXIOM: &str = "__VAxiom";
/// Name of the grammar option specifying the grammar's axiom variable
pub const OPTION_AXIOM: &str = "Axiom";
/// Name of the grammar option specifying the grammar's separator terminal
pub const OPTION_SEPARATOR: &str = "Separator";
/// The output path for compilation artifacts
pub const OPTION_OUTPUT_PATH: &str = "OutputPath";
/// The parser type to generate, defaults to LALR1
pub const OPTION_METHOD: &str = "Method";
/// The runtime to target, defaults to Net
pub const OPTION_RUNTIME: &str = "Runtime";
/// The compilation mode, defaults to Sources
pub const OPTION_MODE: &str = "Mode";
/// The namespace to use for the generated code
pub const OPTION_NAMESPACE: &str = "Namespace";
/// The access mode for the generated code, defaults to Internal
pub const OPTION_ACCESS_MODIFIER: &str = "Modifier";
/// The name of the default lexical context
pub const DEFAULT_CONTEXT_NAME: &str = "__default";

/// The counter for the generation of unique names across multiple grammars
static NEXT_UNIQUE_SID: AtomicUsize = AtomicUsize::new(0);

/// Generates a unique identifier
fn generate_unique_id() -> String {
    let value = NEXT_UNIQUE_SID.fetch_add(1, AtomicOrdering::SeqCst);
    format!("{value:0X}")
}

/// An option for the grammar
#[derive(Debug, Clone)]
pub struct GrammarOption {
    /// The reference in the input for this option's name
    pub name_input_ref: InputReference,
    /// The reference in the input for this option's value
    pub value_input_ref: InputReference,
    /// The option's value
    pub value: String
}

/// Represents a grammar
#[derive(Debug, Clone)]
pub struct Grammar {
    /// The reference in the input for this grammar
    pub input_ref: InputReference,
    /// The grammar's name
    pub name: String,
    /// The next unique symbol identifier for this grammar
    pub next_sid: usize,
    /// The grammar's options
    pub options: HashMap<String, GrammarOption>,
    /// The lexical contexts defined in this grammar
    pub contexts: Vec<String>,
    /// The grammar's terminals
    pub terminals: Vec<Terminal>,
    /// The grammar's variables
    pub variables: Vec<Variable>,
    /// The grammar's virtual symbols
    pub virtuals: Vec<Virtual>,
    /// The grammar's action symbols
    pub actions: Vec<Action>,
    /// The template rules
    pub template_rules: Vec<TemplateRule>
}

/// Represents the build data for a grammar
#[derive(Debug, Clone)]
pub struct BuildData {
    /// The DFA
    pub dfa: DFA,
    /// The expected terminals
    pub expected: TerminalSet,
    /// The separator terminal
    pub separator: Option<TerminalRef>,
    /// The parsing method
    pub method: ParsingMethod,
    /// The LR graph
    pub graph: Graph
}

impl Grammar {
    /// Initializes this grammar
    #[must_use]
    pub fn new(input_ref: InputReference, name: String) -> Grammar {
        Grammar {
            input_ref,
            name,
            next_sid: 3,
            options: HashMap::new(),
            contexts: vec![DEFAULT_CONTEXT_NAME.to_string()],
            terminals: Vec::new(),
            variables: Vec::new(),
            virtuals: Vec::new(),
            actions: Vec::new(),
            template_rules: Vec::new()
        }
    }

    /// Gets the next available symbol id
    fn get_next_sid(&mut self) -> usize {
        let result = self.next_sid;
        self.next_sid += 1;
        result
    }

    /// Adds an option to this grammar
    pub fn add_option(
        &mut self,
        name_input_ref: InputReference,
        value_input_ref: InputReference,
        name: String,
        value: String
    ) {
        self.options.insert(
            name,
            GrammarOption {
                name_input_ref,
                value_input_ref,
                value
            }
        );
    }

    /// Gets an option
    #[must_use]
    pub fn get_option(&self, name: &str) -> Option<&GrammarOption> {
        self.options.get(name)
    }

    /// Gets the symbol with the given name in this grammar
    #[must_use]
    pub fn get_symbol(&self, name: &str) -> Option<SymbolRef> {
        if let Some(symbol) = self.terminals.iter().find(|t| t.name == name) {
            return Some(SymbolRef::Terminal(symbol.id));
        }
        if let Some(symbol) = self.variables.iter().find(|t| t.name == name) {
            return Some(SymbolRef::Variable(symbol.id));
        }
        if let Some(symbol) = self.virtuals.iter().find(|t| t.name == name) {
            return Some(SymbolRef::Virtual(symbol.id));
        }
        if let Some(symbol) = self.actions.iter().find(|t| t.name == name) {
            return Some(SymbolRef::Action(symbol.id));
        }
        None
    }

    /// Gets the name of a symbol
    ///
    /// # Panics
    ///
    /// Panic when the symbol could not be found in this grammar
    #[must_use]
    pub fn get_symbol_name(&self, symbol: SymbolRef) -> &str {
        match symbol {
            SymbolRef::Dummy | SymbolRef::NullTerminal => "",
            SymbolRef::Epsilon => "ε",
            SymbolRef::Dollar => "$",
            SymbolRef::Terminal(id) => self
                .terminals
                .iter()
                .find(|s| s.id == id)
                .map(|s| &s.name)
                .unwrap(),
            SymbolRef::Variable(id) => self
                .variables
                .iter()
                .find(|s| s.id == id)
                .map(|s| &s.name)
                .unwrap(),
            SymbolRef::Virtual(id) => self
                .virtuals
                .iter()
                .find(|s| s.id == id)
                .map(|s| &s.name)
                .unwrap(),
            SymbolRef::Action(id) => self
                .actions
                .iter()
                .find(|s| s.id == id)
                .map(|s| &s.name)
                .unwrap()
        }
    }

    /// Gets the value of a symbol
    ///
    /// # Panics
    ///
    /// Panic when the symbol could not be found in this grammar
    #[must_use]
    pub fn get_symbol_value(&self, symbol: SymbolRef) -> &str {
        match symbol {
            SymbolRef::Terminal(id) => self
                .terminals
                .iter()
                .find(|s| s.id == id)
                .map(|s| &s.value)
                .unwrap(),
            _ => self.get_symbol_name(symbol)
        }
    }

    /// Resolves the specified lexical context name for this grammar
    pub fn resolve_context(&mut self, name: &str) -> usize {
        if let Some(index) = self.contexts.iter().position(|c| name == c) {
            index
        } else {
            let index = self.contexts.len();
            self.contexts.push(name.to_string());
            index
        }
    }

    /// Adds the given anonymous terminal to this grammar
    pub fn add_terminal_anonymous(
        &mut self,
        value: String,
        input_ref: InputReference,
        nfa: NFA
    ) -> &mut Terminal {
        let name = format!("{}{}", PREFIX_GENERATED_TERMINAL, generate_unique_id());
        self.add_terminal(name, value, input_ref, nfa, 0, true, false)
    }

    /// Adds the given named terminal to this grammar
    ///
    /// # Panics
    ///
    /// Panic when the specified context does not exist in the grammar
    pub fn add_terminal_named(
        &mut self,
        name: String,
        input_ref: InputReference,
        nfa: NFA,
        context: &str,
        is_fragment: bool
    ) -> &mut Terminal {
        let context = self.contexts.iter().position(|c| c == context).unwrap();
        let value = name.clone();
        self.add_terminal(name, value, input_ref, nfa, context, false, is_fragment)
    }

    /// Adds a terminal to the grammar
    #[allow(clippy::too_many_arguments)]
    fn add_terminal(
        &mut self,
        name: String,
        value: String,
        input_ref: InputReference,
        nfa: NFA,
        context: usize,
        is_anonymous: bool,
        is_fragment: bool
    ) -> &mut Terminal {
        let index = self.terminals.len();
        let terminal = Terminal {
            id: self.get_next_sid(),
            name,
            value,
            input_ref,
            nfa,
            context,
            is_anonymous,
            is_fragment,
            terminal_references: Vec::new()
        };
        self.terminals.push(terminal);
        &mut self.terminals[index]
    }

    /// Gets the terminal with the specified identifier
    #[must_use]
    pub fn get_terminal(&self, sid: usize) -> Option<&Terminal> {
        self.terminals.iter().find(|t| t.id == sid)
    }

    /// Gets the terminal with the specified identifier
    pub fn get_terminal_mut(&mut self, sid: usize) -> Option<&mut Terminal> {
        self.terminals.iter_mut().find(|t| t.id == sid)
    }

    /// Gets the terminal with the given name
    #[must_use]
    pub fn get_terminal_for_name(&self, name: &str) -> Option<&Terminal> {
        self.terminals.iter().find(|t| t.name == name)
    }

    /// Gets the terminal with the given name
    #[must_use]
    pub fn get_terminal_for_value(&self, value: &str) -> Option<&Terminal> {
        self.terminals.iter().find(|t| t.value == value)
    }

    /// Gets the context for a terminal
    ///
    /// # Panics
    ///
    /// Panic when the terminal cannot be found in the grammar
    #[must_use]
    pub fn get_terminal_context(&self, terminal: TerminalRef) -> usize {
        match terminal {
            TerminalRef::Dummy
            | TerminalRef::Epsilon
            | TerminalRef::Dollar
            | TerminalRef::NullTerminal => 0,
            TerminalRef::Terminal(id) => self.terminals.iter().find(|t| t.id == id).unwrap().context
        }
    }

    /// Generates a new variable
    pub fn generate_variable(&mut self, context_variable: usize) -> &mut Variable {
        let index = self.variables.len();
        let sid = self.get_next_sid();
        let name = format!("{PREFIX_GENERATED_VARIABLE}{sid}");
        self.variables
            .push(Variable::new(sid, name, Some(context_variable)));
        &mut self.variables[index]
    }

    /// Gets the variable with the specified identifier
    #[must_use]
    pub fn get_variable(&self, sid: usize) -> Option<&Variable> {
        self.variables.iter().find(|v| v.id == sid)
    }

    /// Gets the variable with the specified name
    #[must_use]
    pub fn get_variable_for_name(&self, name: &str) -> Option<&Variable> {
        self.variables.iter().find(|v| v.name == name)
    }

    /// Adds a variable with the given name to this grammar
    pub fn add_variable(&mut self, name: &str) -> &mut Variable {
        if let Some(index) = self.variables.iter().position(|v| v.name == name) {
            return &mut self.variables[index];
        }
        let index = self.variables.len();
        let sid = self.get_next_sid();
        self.variables
            .push(Variable::new(sid, name.to_string(), None));
        &mut self.variables[index]
    }

    /// Inherit the specified variable
    fn inherit_variable(&mut self, other: &Variable) {
        if self.variables.iter().all(|v| v.name != other.name) {
            // no variable with the same name
            let sid = self.next_sid + other.id - 3;
            self.variables
                .push(Variable::new(sid, other.name.clone(), None));
        }
    }

    /// Gets the virtual with the specified identifier
    #[must_use]
    pub fn get_virtual(&self, sid: usize) -> Option<&Virtual> {
        self.virtuals.iter().find(|v| v.id == sid)
    }

    /// Adds a virtual symbol with the given name to this grammar
    pub fn add_virtual(&mut self, name: &str) -> &mut Virtual {
        if let Some(index) = self.virtuals.iter().position(|v| v.name == name) {
            return &mut self.virtuals[index];
        }
        let index = self.virtuals.len();
        let sid = self.get_next_sid();
        self.virtuals.push(Virtual::new(sid, name.to_string()));
        &mut self.virtuals[index]
    }

    /// Inherit the specified virtual
    fn inherit_virtal(&mut self, other: &Virtual) {
        if self.virtuals.iter().all(|v| v.name != other.name) {
            // no variable with the same name
            let sid = self.next_sid + other.id - 3;
            self.virtuals.push(Virtual::new(sid, other.name.clone()));
        }
    }

    /// Gets the action with the specified identifier
    #[must_use]
    pub fn get_action(&self, sid: usize) -> Option<&Action> {
        self.actions.iter().find(|v| v.id == sid)
    }

    /// Adds an action symbol with the given name to this grammar
    pub fn add_action(&mut self, name: &str) -> &mut Action {
        if let Some(index) = self.actions.iter().position(|v| v.name == name) {
            return &mut self.actions[index];
        }
        let index = self.actions.len();
        let sid = self.get_next_sid();
        self.actions.push(Action::new(sid, name.to_string()));
        &mut self.actions[index]
    }

    /// Inherit the specified action
    fn inherit_action(&mut self, other: &Action) {
        if self.actions.iter().all(|v| v.name != other.name) {
            // no variable with the same name
            let sid = self.next_sid + other.id - 3;
            self.actions.push(Action::new(sid, other.name.clone()));
        }
    }

    /// Adds a template rule with the given name to this grammar
    pub fn add_template_rule(
        &mut self,
        name: &str,
        input_ref: InputReference,
        parameters: Vec<TemplateRuleParam>
    ) -> &mut TemplateRule {
        if let Some(index) = self.template_rules.iter().position(|v| v.name == name) {
            return &mut self.template_rules[index];
        }
        let index = self.template_rules.len();
        self.template_rules
            .push(TemplateRule::new(name.to_string(), input_ref, parameters));
        &mut self.template_rules[index]
    }

    /// Generates a new template rule
    pub fn generate_template_rule(
        &mut self,
        input_ref: InputReference,
        parameters: Vec<TemplateRuleParam>
    ) -> &mut TemplateRule {
        let sid = self.get_next_sid();
        let name = format!("{PREFIX_GENERATED_VARIABLE}{sid}");
        self.add_template_rule(&name, input_ref, parameters)
    }

    /// Instantiate a template rule
    ///
    /// # Errors
    ///
    /// Return an error when the wrong number of arguments are passed to the template rule
    pub fn instantiate_template_rule(
        &mut self,
        name: &str,
        call_ref: InputReference,
        arguments: Vec<SymbolRef>
    ) -> Result<SymbolRef, Error> {
        match self.template_rules.iter().position(|r| r.name == name) {
            None => Err(Error::TemplateRuleNotFound(call_ref, name.to_string())),
            Some(template_index) => {
                let rule = &self.template_rules[template_index];
                if rule.parameters.len() == arguments.len() {
                    Ok(self.instantiate_template_rule_at(template_index, call_ref, arguments))
                } else {
                    Err(Error::TemplateRuleWrongNumberOfArgs(
                        call_ref,
                        rule.parameters.len(),
                        arguments.len()
                    ))
                }
            }
        }
    }

    /// Instantiate a symbol in a template rule
    fn instantiate_template_symbol(
        &mut self,
        template_index: usize,
        instance_index: usize,
        symbol: &TemplateRuleSymbol
    ) -> SymbolRef {
        match symbol {
            TemplateRuleSymbol::Parameter(index) => {
                self.template_rules[template_index].instances[instance_index].arguments[*index]
            }
            TemplateRuleSymbol::Symbol(symbol) => *symbol,
            TemplateRuleSymbol::Template(template_ref) => {
                let mut new_arguments = Vec::new();
                for arg in &template_ref.arguments {
                    new_arguments.push(self.instantiate_template_symbol(
                        template_index,
                        instance_index,
                        arg
                    ));
                }
                self.instantiate_template_rule_at(
                    template_ref.template,
                    template_ref.input_ref,
                    new_arguments
                )
            }
        }
    }

    /// Instantiate a template rule
    fn instantiate_template_rule_at(
        &mut self,
        template_index: usize,
        call_ref: InputReference,
        arguments: Vec<SymbolRef>
    ) -> SymbolRef {
        let mut new_instance = TemplateRuleInstance { arguments, head: 0 };
        if let Some(instance) = self.template_rules[template_index]
            .instances
            .iter()
            .find(|instance| *instance == &new_instance)
        {
            SymbolRef::Variable(instance.head)
        } else {
            // push new instance
            let args_names: Vec<&str> = new_instance
                .arguments
                .iter()
                .map(|arg| self.get_symbol_name(*arg))
                .collect();
            let args_names = args_names.join(", ");
            let name = format!(
                "{}<{}>",
                &self.template_rules[template_index].name, args_names
            );
            new_instance.head = self.add_variable(&name).id;
            let instance_index = self.template_rules[template_index].instances.len();
            self.template_rules[template_index]
                .instances
                .push(new_instance);

            // fill-in the body
            let head_action = self.template_rules[template_index].head_action;
            let context = self.template_rules[template_index].context;
            let mut bodies = Vec::new();
            for body in self.template_rules[template_index].bodies.clone() {
                let mut elements = Vec::new();
                for element in body.elements {
                    elements.push(RuleBodyElement {
                        symbol: self.instantiate_template_symbol(
                            template_index,
                            instance_index,
                            &element.symbol
                        ),
                        action: element.action,
                        input_ref: Some(element.input_ref)
                    });
                }
                bodies.push(RuleBody::from_parts(elements));
            }
            let head = {
                let variable = self.add_variable(&name);
                for body in bodies {
                    variable.rules.push(Rule {
                        head: variable.id,
                        head_action,
                        head_input_ref: call_ref,
                        body,
                        context
                    });
                }
                variable.id
            };

            SymbolRef::Variable(head)
        }
    }

    /// Inherit from the given parent
    pub fn inherit(&mut self, other: &Grammar) {
        self.inherit_options(other);
        self.inherit_terminals(other);
        self.inherit_variables(other);
        self.inherit_virtuals(other);
        self.inherit_actions(other);
        self.inherit_rules(other);
        self.inherit_template_rules(other);
        self.next_sid += other.next_sid - 3;
    }

    /// Inherits the options from the parent grammar
    fn inherit_options(&mut self, other: &Grammar) {
        for (name, option) in &other.options {
            self.options.insert(name.clone(), option.clone());
        }
    }

    /// Inherits the terminals from the parent grammar
    fn inherit_terminals(&mut self, other: &Grammar) {
        for terminal in &other.terminals {
            if self
                .terminals
                .iter()
                .all(|t| t.name != terminal.name && t.value != terminal.value)
            {
                // not already defined in this grammar
                let sid = self.next_sid + terminal.id - 3;
                let context = self.resolve_context(&other.contexts[terminal.context]);
                let mut nfa = terminal.nfa.clone_no_finals();
                nfa.states[nfa.exit]
                    .items
                    .push(FinalItem::Terminal(sid, context));
                self.terminals.push(Terminal {
                    id: sid,
                    name: terminal.name.clone(),
                    value: terminal.value.clone(),
                    input_ref: terminal.input_ref,
                    nfa,
                    context,
                    is_fragment: terminal.is_fragment,
                    is_anonymous: terminal.is_anonymous,
                    terminal_references: Vec::new()
                });
            }
        }
    }

    /// Inherits the virtuals from the parent grammar
    fn inherit_virtuals(&mut self, other: &Grammar) {
        for symbol in &other.virtuals {
            self.inherit_virtal(symbol);
        }
    }

    /// Inherits the actions from the parent grammar
    fn inherit_actions(&mut self, other: &Grammar) {
        for symbol in &other.actions {
            self.inherit_action(symbol);
        }
    }

    /// Inherits the variables from the parent grammar
    fn inherit_variables(&mut self, other: &Grammar) {
        for symbol in &other.variables {
            self.inherit_variable(symbol);
        }
    }

    /// Inherits the grammar rules from the parent grammar
    fn inherit_rules(&mut self, other: &Grammar) {
        for variable in &other.variables {
            let head = self
                .variables
                .iter()
                .find(|v| v.name == variable.name)
                .unwrap()
                .id;
            let mut rules = variable
                .rules
                .iter()
                .map(|rule| {
                    let context_name = &other.contexts[rule.context];
                    let context = self
                        .contexts
                        .iter()
                        .position(|c| c == context_name)
                        .unwrap();
                    let elements = rule
                        .body
                        .elements
                        .iter()
                        .map(|element| {
                            RuleBodyElement::new(
                                self.map_symbol_ref(other, element.symbol),
                                element.action,
                                element.input_ref
                            )
                        })
                        .collect();
                    Rule::new(
                        head,
                        rule.head_action,
                        rule.head_input_ref,
                        RuleBody::from_parts(elements),
                        context
                    )
                })
                .collect();
            let head = self
                .variables
                .iter_mut()
                .find(|v| v.name == variable.name)
                .unwrap();
            head.rules.append(&mut rules);
        }
    }

    /// Creates the equivalent template rule symbol for this grammar
    fn inherit_template_rule_symbol(
        &self,
        other: &Grammar,
        symbol: &TemplateRuleSymbol
    ) -> TemplateRuleSymbol {
        match symbol {
            TemplateRuleSymbol::Parameter(index) => TemplateRuleSymbol::Parameter(*index),
            TemplateRuleSymbol::Symbol(symbol) => {
                TemplateRuleSymbol::Symbol(self.map_symbol_ref(other, *symbol))
            }
            TemplateRuleSymbol::Template(template_ref) => {
                let name = &other.template_rules[template_ref.template].name;
                let index = self
                    .template_rules
                    .iter()
                    .enumerate()
                    .find(|(_i, r)| &r.name == name)
                    .unwrap()
                    .0;
                TemplateRuleSymbol::Template(TemplateRuleRef {
                    template: index,
                    input_ref: template_ref.input_ref,
                    arguments: template_ref
                        .arguments
                        .iter()
                        .map(|symbol| self.inherit_template_rule_symbol(other, symbol))
                        .collect()
                })
            }
        }
    }

    /// Inherit the template rules from the parent grammar
    fn inherit_template_rules(&mut self, other: &Grammar) {
        let mut couples: Vec<(usize, &TemplateRule)> = Vec::new();
        for rule in &other.template_rules {
            if self.template_rules.iter().all(|tr| tr.name != rule.name) {
                // does not exist yet
                let index = self.template_rules.len();
                self.template_rules.push(TemplateRule {
                    name: rule.name.clone(),
                    input_ref: rule.input_ref,
                    parameters: rule.parameters.clone(),
                    head_action: rule.head_action,
                    context: rule.context,
                    bodies: Vec::new(),
                    instances: rule
                        .instances
                        .iter()
                        .map(|instance| {
                            let old_variable = other
                                .variables
                                .iter()
                                .find(|v| v.id == instance.head)
                                .unwrap();
                            let new_variable = self
                                .variables
                                .iter()
                                .find(|v| v.name == old_variable.name)
                                .unwrap();
                            TemplateRuleInstance {
                                arguments: instance
                                    .arguments
                                    .iter()
                                    .map(|arg| self.map_symbol_ref(other, *arg))
                                    .collect(),
                                head: new_variable.id
                            }
                        })
                        .collect()
                });
                couples.push((index, rule));
            }
        }

        for (index, other_rule) in couples {
            for body in &other_rule.bodies {
                let mut elements = Vec::new();
                for element in &body.elements {
                    let symbol = self.inherit_template_rule_symbol(other, &element.symbol);
                    elements.push(TemplateRuleElement {
                        symbol,
                        action: element.action,
                        input_ref: element.input_ref
                    });
                }
                self.template_rules[index]
                    .bodies
                    .push(TemplateRuleBody { elements });
            }
        }
    }

    /// Maps a symbol from a grammar to this one
    fn map_symbol_ref(&self, other: &Grammar, symbol: SymbolRef) -> SymbolRef {
        match symbol {
            SymbolRef::Dummy => SymbolRef::Dummy,
            SymbolRef::Epsilon => SymbolRef::Epsilon,
            SymbolRef::Dollar => SymbolRef::Dollar,
            SymbolRef::NullTerminal => SymbolRef::NullTerminal,
            SymbolRef::Terminal(id) => {
                let other_symbol = other.terminals.iter().find(|s| s.id == id).unwrap();
                let symbol = self
                    .terminals
                    .iter()
                    .find(|s| s.name == other_symbol.name)
                    .unwrap();
                SymbolRef::Terminal(symbol.id)
            }
            SymbolRef::Variable(id) => {
                let other_symbol = other.variables.iter().find(|s| s.id == id).unwrap();
                let symbol = self
                    .variables
                    .iter()
                    .find(|s| s.name == other_symbol.name)
                    .unwrap();
                SymbolRef::Variable(symbol.id)
            }
            SymbolRef::Virtual(id) => {
                let other_symbol = other.virtuals.iter().find(|s| s.id == id).unwrap();
                let symbol = self
                    .virtuals
                    .iter()
                    .find(|s| s.name == other_symbol.name)
                    .unwrap();
                SymbolRef::Virtual(symbol.id)
            }
            SymbolRef::Action(id) => {
                let other_symbol = other.actions.iter().find(|s| s.id == id).unwrap();
                let symbol = self
                    .actions
                    .iter()
                    .find(|s| s.name == other_symbol.name)
                    .unwrap();
                SymbolRef::Action(symbol.id)
            }
        }
    }

    /// Builds the complete DFA that matches the terminals in this grammar
    #[allow(clippy::similar_names)]
    #[must_use]
    pub fn build_dfa(&self) -> DFA {
        let mut nfa = NFA::new_minimal();
        for terminal in self.terminals.iter().filter(|t| !t.is_fragment) {
            let (entry, _) = nfa.insert_sub_nfa(&terminal.nfa);
            nfa.add_transition(nfa.entry, EPSILON, entry);
        }
        let mut dfa = DFA::from_nfa(nfa).minimize();
        dfa.repack_transitions();
        dfa.prune();
        dfa
    }

    /// Prepares this grammar for code and data generation
    /// This methods inserts a new grammar rule as its axiom and computes the FIRSTS and FOLLOWERS sets
    ///
    /// # Errors
    ///
    /// Return an error when the axiom is not properly defined
    pub fn prepare(&mut self, grammar_index: usize) -> Result<(), Error> {
        self.add_real_axiom(grammar_index)?;
        for variable in &mut self.variables {
            variable.compute_choices();
        }
        self.compute_firsts();
        self.compute_followers();
        Ok(())
    }

    /// Adds the real axiom to this grammar
    fn add_real_axiom(&mut self, grammar_index: usize) -> Result<(), Error> {
        let axiom_option = self
            .options
            .get(OPTION_AXIOM)
            .ok_or(Error::AxiomNotSpecified(grammar_index))?;
        let axiom_id = self
            .variables
            .iter()
            .find(|v| v.name == axiom_option.value)
            .ok_or(Error::AxiomNotDefined(grammar_index))?
            .id;
        let input_ref = axiom_option.value_input_ref;
        // Create the real axiom rule variable and rule
        let real_axiom = self.add_variable(GENERATED_AXIOM);
        real_axiom.rules.push(Rule::new(
            real_axiom.id,
            TREE_ACTION_NONE,
            input_ref,
            RuleBody::from_parts(vec![
                RuleBodyElement::new(SymbolRef::Variable(axiom_id), TREE_ACTION_PROMOTE, None),
                RuleBodyElement::new(SymbolRef::Dollar, TREE_ACTION_DROP, None),
            ]),
            0
        ));
        Ok(())
    }

    /// Computes the FIRSTS sets for this grammar
    fn compute_firsts(&mut self) {
        let mut firsts_for_var = HashMap::new();
        let mut modified = true;
        while modified {
            modified = false;
            for variable in &mut self.variables {
                modified |= variable.compute_firsts(&mut firsts_for_var);
            }
        }
    }

    /// Computes the FOLLOWERS sets for this grammar
    fn compute_followers(&mut self) {
        let mut followers = HashMap::new();
        // Apply step 1 to each variable
        for variable in &self.variables {
            variable.compute_initial_follower(&mut followers);
        }
        // Apply step 2 and 3 while some modification has occured
        let mut modified = true;
        while modified {
            modified = false;
            for variable in &self.variables {
                modified |= variable.propagate_followers(&mut followers);
            }
        }
        for variable in &mut self.variables {
            if let Some(followers) = followers.remove(&variable.id) {
                variable.followers = followers;
            }
        }
    }

    /// Build data for this grammar
    ///
    /// # Errors
    ///
    /// Return the errors produced when building the grammar
    pub fn build(
        &mut self,
        parsing_method: Option<ParsingMethod>,
        grammar_index: usize
    ) -> Result<BuildData, Vec<Error>> {
        if let Err(error) = self.prepare(grammar_index) {
            return Err(vec![error]);
        };
        // Build DFA
        let dfa = self.build_dfa();
        // Check that no terminal match the empty string
        if !dfa.states.is_empty() && dfa.states[0].is_final() {
            return Err(dfa.states[0]
                .items
                .iter()
                .map(|item| Error::TerminalMatchesEmpty(grammar_index, (*item).into()))
                .collect());
        }
        // Build the data for the lexer
        let expected = dfa.get_expected();
        let separator = match self.get_separator(grammar_index, &expected, &dfa) {
            Ok(separator) => separator,
            Err(error) => return Err(vec![error])
        };
        let method = match self.get_parsing_method(parsing_method, grammar_index) {
            Ok(method) => method,
            Err(error) => return Err(vec![error])
        };
        // Build the data for the parser
        let graph = crate::lr::build_graph(self, grammar_index, &expected, &dfa, method)?;
        Ok(BuildData {
            dfa,
            expected,
            separator,
            method,
            graph
        })
    }

    /// Gets the separator for the grammar
    fn get_separator(
        &self,
        grammar_index: usize,
        expected: &TerminalSet,
        dfa: &DFA
    ) -> Result<Option<TerminalRef>, Error> {
        let Some(option) = self.get_option(OPTION_SEPARATOR) else {
            return Ok(None);
        };
        let Some(terminal) = self.get_terminal_for_name(&option.value) else {
            return Err(Error::SeparatorNotDefined(grammar_index));
        };
        let terminal_ref = TerminalRef::Terminal(terminal.id);
        // warn if the separator is context-sensitive
        if terminal.context != 0 {
            return Err(Error::SeparatorIsContextual(grammar_index, terminal_ref));
        }
        if expected.content.contains(&terminal_ref) {
            // the terminal is produced by the lexer => ok
            return Ok(Some(terminal_ref));
        }
        // the separator will not be produced by the lexer, try to investigate why
        let overriders = dfa.get_overriders(terminal_ref, 0);
        Err(Error::SeparatorCannotBeMatched(
            grammar_index,
            UnmatchableTokenError {
                terminal: terminal_ref,
                overriders
            }
        ))
    }

    /// Gets the parsing method
    fn get_parsing_method(
        &self,
        parsing_method: Option<ParsingMethod>,
        grammar_index: usize
    ) -> Result<ParsingMethod, Error> {
        match parsing_method {
            Some(method) => Ok(method),
            None => match self.get_option(OPTION_METHOD) {
                Some(option) => match option.value.as_ref() {
                    "lr0" => Ok(ParsingMethod::LR0),
                    "lr1" => Ok(ParsingMethod::LR1),
                    "lalr1" => Ok(ParsingMethod::LALR1),
                    "rnglr1" => Ok(ParsingMethod::RNGLR1),
                    "rnglalr1" => Ok(ParsingMethod::RNGLALR1),
                    _ => Err(Error::InvalidOption(
                        grammar_index,
                        OPTION_METHOD.to_string(),
                        vec![
                            String::from("lr0"),
                            String::from("lr1"),
                            String::from("lalr1"),
                            String::from("rnglr1"),
                            String::from("rnglalr1"),
                        ]
                    ))
                },
                None => Ok(ParsingMethod::LALR1)
            }
        }
    }

    /// Builds the in-memory parser for a grammar
    ///
    /// # Errors
    ///
    /// Returns the errors produced by the grammar's compilation
    pub fn get_in_memory<'a>(&'a self, data: &BuildData) -> Result<InMemoryParser<'a>, Vec<Error>> {
        crate::output::build_in_memory_grammar(self, data)
    }
}

impl Display for Grammar {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        for variable in &self.variables {
            for rule in &variable.rules {
                write!(f, "{} ->", variable.name)?;
                for element in &rule.body.choices[0].elements {
                    write!(f, " {}", self.get_symbol_value(element.symbol))?;
                }
                writeln!(f)?;
            }
        }
        Ok(())
    }
}