Skip to content

Commit 2d554c2

Browse files
authored
Merge pull request #492 from ratmice/grmtools_directive_2
Change YaccKind parameters to a new `YaccKindResolver` type
2 parents 3395a6c + 2ffcffb commit 2d554c2

File tree

18 files changed

+338
-110
lines changed

18 files changed

+338
-110
lines changed

cfgrammar/src/lib/yacc/ast.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ use indexmap::{IndexMap, IndexSet};
77

88
use super::{
99
parser::YaccParser, Precedence, YaccGrammarError, YaccGrammarErrorKind, YaccGrammarWarning,
10-
YaccGrammarWarningKind, YaccKind,
10+
YaccGrammarWarningKind, YaccKind, YaccKindResolver,
1111
};
1212

1313
use crate::Span;
1414
/// Contains a `GrammarAST` structure produced from a grammar source file.
1515
/// As well as any errors which occurred during the construction of the AST.
1616
pub struct ASTWithValidityInfo {
17+
yacc_kind: Option<YaccKind>,
1718
ast: GrammarAST,
1819
errs: Vec<YaccGrammarError>,
1920
}
@@ -23,18 +24,22 @@ impl ASTWithValidityInfo {
2324
/// encountered during the construction of it. The `ASTWithValidityInfo` can be
2425
/// then unused to construct a `YaccGrammar`, which will either produce an
2526
/// `Ok(YaccGrammar)` or an `Err` which includes these errors.
26-
pub fn new(yacc_kind: YaccKind, s: &str) -> Self {
27+
pub fn new(yacc_kind_resolver: YaccKindResolver, s: &str) -> Self {
2728
let mut errs = Vec::new();
28-
let ast = match yacc_kind {
29-
YaccKind::Original(_) | YaccKind::Grmtools | YaccKind::Eco => {
30-
let mut yp = YaccParser::new(yacc_kind, s.to_string());
31-
yp.parse().map_err(|e| errs.extend(e)).ok();
32-
let mut ast = yp.ast();
29+
let (yacc_kind, ast) = {
30+
let mut yp = YaccParser::new(yacc_kind_resolver, s.to_string());
31+
yp.parse().map_err(|e| errs.extend(e)).ok();
32+
let (yacc_kind, mut ast) = yp.build();
33+
if yacc_kind.is_some() {
3334
ast.complete_and_validate().map_err(|e| errs.push(e)).ok();
34-
ast
3535
}
36+
(yacc_kind, ast)
3637
};
37-
ASTWithValidityInfo { ast, errs }
38+
ASTWithValidityInfo {
39+
ast,
40+
errs,
41+
yacc_kind,
42+
}
3843
}
3944

4045
/// Returns a `GrammarAST` constructed as the result of parsing a source file.
@@ -51,6 +56,11 @@ impl ASTWithValidityInfo {
5156
self.errors().is_empty()
5257
}
5358

59+
/// Returns the `YaccKind` that was used to parse the `GrammarAST`.
60+
pub fn yacc_kind(&self) -> Option<YaccKind> {
61+
self.yacc_kind
62+
}
63+
5464
/// Returns all errors which were encountered during AST construction.
5565
pub fn errors(&self) -> &[YaccGrammarError] {
5666
self.errs.as_slice()

cfgrammar/src/lib/yacc/firsts.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{RIdx, Symbol, TIdx};
99
/// `Firsts` stores all the first sets for a given grammar. For example, given this code and
1010
/// grammar:
1111
/// ```text
12-
/// let grm = YaccGrammar::new(YaccKind::Original(YaccOriginalActionKind::GenericParseTree), "
12+
/// let grm = YaccGrammar::new(YaccKindResolver::Force(YaccKind::Original(YaccOriginalActionKind::GenericParseTree)), "
1313
/// S: A 'b';
1414
/// A: 'a'
1515
/// | ;").unwrap();
@@ -143,7 +143,7 @@ where
143143
#[cfg(test)]
144144
mod test {
145145
use super::{
146-
super::{YaccGrammar, YaccKind, YaccOriginalActionKind},
146+
super::{YaccGrammar, YaccKind, YaccKindResolver, YaccOriginalActionKind},
147147
YaccFirsts,
148148
};
149149
use num_traits::{AsPrimitive, PrimInt, Unsigned};
@@ -180,7 +180,7 @@ mod test {
180180
#[test]
181181
fn test_first() {
182182
let grm = YaccGrammar::new(
183-
YaccKind::Original(YaccOriginalActionKind::GenericParseTree),
183+
YaccKindResolver::Force(YaccKind::Original(YaccOriginalActionKind::GenericParseTree)),
184184
"
185185
%start C
186186
%token c d
@@ -202,7 +202,7 @@ mod test {
202202
#[test]
203203
fn test_first_no_subsequent_rules() {
204204
let grm = YaccGrammar::new(
205-
YaccKind::Original(YaccOriginalActionKind::GenericParseTree),
205+
YaccKindResolver::Force(YaccKind::Original(YaccOriginalActionKind::GenericParseTree)),
206206
"
207207
%start C
208208
%token c d
@@ -220,7 +220,7 @@ mod test {
220220
#[test]
221221
fn test_first_epsilon() {
222222
let grm = YaccGrammar::new(
223-
YaccKind::Original(YaccOriginalActionKind::GenericParseTree),
223+
YaccKindResolver::Force(YaccKind::Original(YaccOriginalActionKind::GenericParseTree)),
224224
"
225225
%start A
226226
%token a b c
@@ -241,7 +241,7 @@ mod test {
241241
#[test]
242242
fn test_last_epsilon() {
243243
let grm = YaccGrammar::new(
244-
YaccKind::Original(YaccOriginalActionKind::GenericParseTree),
244+
YaccKindResolver::Force(YaccKind::Original(YaccOriginalActionKind::GenericParseTree)),
245245
"
246246
%start A
247247
%token b c
@@ -261,7 +261,7 @@ mod test {
261261
#[test]
262262
fn test_first_no_multiples() {
263263
let grm = YaccGrammar::new(
264-
YaccKind::Original(YaccOriginalActionKind::GenericParseTree),
264+
YaccKindResolver::Force(YaccKind::Original(YaccOriginalActionKind::GenericParseTree)),
265265
"
266266
%start A
267267
%token b c
@@ -277,7 +277,7 @@ mod test {
277277

278278
fn eco_grammar() -> YaccGrammar {
279279
YaccGrammar::new(
280-
YaccKind::Original(YaccOriginalActionKind::GenericParseTree),
280+
YaccKindResolver::Force(YaccKind::Original(YaccOriginalActionKind::GenericParseTree)),
281281
"
282282
%start S
283283
%token a b c d f
@@ -308,7 +308,7 @@ mod test {
308308
#[test]
309309
fn test_first_from_eco_bug() {
310310
let grm = YaccGrammar::new(
311-
YaccKind::Original(YaccOriginalActionKind::GenericParseTree),
311+
YaccKindResolver::Force(YaccKind::Original(YaccOriginalActionKind::GenericParseTree)),
312312
"
313313
%start E
314314
%token a b c d e f

cfgrammar/src/lib/yacc/follows.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{RIdx, Symbol, TIdx};
99
/// `Follows` stores all the Follow sets for a given grammar. For example, given this code and
1010
/// grammar:
1111
/// ```text
12-
/// let grm = YaccGrammar::new(YaccKind::Original(YaccOriginalActionKind::GenericParseTree), "
12+
/// let grm = YaccGrammar::new(YaccKindResolver::Force(YaccKind::Original(YaccOriginalActionKind::GenericParseTree)), "
1313
/// S: A 'b';
1414
/// A: 'a' | ;
1515
/// ").unwrap();
@@ -115,7 +115,7 @@ where
115115
#[cfg(test)]
116116
mod test {
117117
use super::{
118-
super::{YaccGrammar, YaccKind, YaccOriginalActionKind},
118+
super::{YaccGrammar, YaccKind, YaccKindResolver, YaccOriginalActionKind},
119119
YaccFollows,
120120
};
121121
use num_traits::{AsPrimitive, PrimInt, Unsigned};
@@ -149,7 +149,7 @@ mod test {
149149
fn test_follow() {
150150
// Adapted from p2 of https://www.cs.uaf.edu/~cs331/notes/FirstFollow.pdf
151151
let grm = YaccGrammar::new(
152-
YaccKind::Original(YaccOriginalActionKind::GenericParseTree),
152+
YaccKindResolver::Force(YaccKind::Original(YaccOriginalActionKind::GenericParseTree)),
153153
"
154154
%start E
155155
%%
@@ -173,7 +173,7 @@ mod test {
173173
fn test_follow2() {
174174
// Adapted from https://www.l2f.inesc-id.pt/~david/w/pt/Top-Down_Parsing/Exercise_5:_Test_2010/07/01
175175
let grm = YaccGrammar::new(
176-
YaccKind::Original(YaccOriginalActionKind::GenericParseTree),
176+
YaccKindResolver::Force(YaccKind::Original(YaccOriginalActionKind::GenericParseTree)),
177177
"
178178
%start A
179179
%%
@@ -196,7 +196,7 @@ mod test {
196196
#[test]
197197
fn test_follow3() {
198198
let grm = YaccGrammar::new(
199-
YaccKind::Original(YaccOriginalActionKind::GenericParseTree),
199+
YaccKindResolver::Force(YaccKind::Original(YaccOriginalActionKind::GenericParseTree)),
200200
"
201201
%start S
202202
%%
@@ -213,7 +213,7 @@ mod test {
213213
#[test]
214214
fn test_follow_corchuelo() {
215215
let grm = YaccGrammar::new(
216-
YaccKind::Original(YaccOriginalActionKind::GenericParseTree),
216+
YaccKindResolver::Force(YaccKind::Original(YaccOriginalActionKind::GenericParseTree)),
217217
"
218218
%start E
219219
%%

0 commit comments

Comments
 (0)