Skip to content

Commit 5edbfc5

Browse files
committed
Test building parsers with modified start rules.
1 parent 456156a commit 5edbfc5

File tree

5 files changed

+111
-1
lines changed

5 files changed

+111
-1
lines changed

lrpar/cttests/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ crate-type = ["cdylib"]
1212
[build-dependencies]
1313
cfgrammar = { path = "../../cfgrammar" }
1414
lrlex = { path = "../../lrlex" }
15-
lrpar = { path = "../" }
15+
lrpar = { path = "../", features = ["_unstable_api"] }
1616
glob.workspace = true
1717
yaml-rust2.workspace = true
1818
cfg_aliases = "0.2.1"

lrpar/cttests/build.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use cfgrammar::yacc::ast::ASTWithValidityInfo;
12
use glob::glob;
23
#[path = "src/cgen_helper.rs"]
34
mod cgen_helper;
@@ -46,6 +47,68 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
4647
.build()
4748
.unwrap();
4849
}
50+
51+
{
52+
use lrpar::unstable_api::UnstableApi;
53+
// In this case we'll be building multiple grammars
54+
//
55+
// 1. Parse multi_start_rule.y into an AST
56+
// 2. Clone the original and change the start rule.
57+
// 3. Build a grammar for `multi_start_rule.y` unchanged.
58+
// 4. Build the modified grammar.
59+
let grammar_path = &std::env::current_dir().unwrap().join("src/multi_start.y");
60+
let grammar_src = std::fs::read_to_string(grammar_path).unwrap();
61+
let grammar_src_clone = grammar_src.clone();
62+
let valid_ast = ASTWithValidityInfo::new(cfgrammar::yacc::YaccKind::Grmtools, &grammar_src);
63+
eprintln!("rules {:?}", valid_ast.ast().rules);
64+
let bstart_rule = valid_ast.ast().get_rule("BStart").unwrap().clone();
65+
let modified_ast = valid_ast.clone_and_change_start_rule(bstart_rule).unwrap();
66+
CTLexerBuilder::new()
67+
.lrpar_config(move |ctp| {
68+
ctp.grammar_ast(valid_ast.clone(), UnstableApi)
69+
.with_grammar_src(grammar_src.clone(), UnstableApi)
70+
.grammar_in_src_dir("multi_start.y")
71+
.unwrap()
72+
.mod_name("ast_unmodified_y")
73+
.output_path(format!(
74+
"{}/ast_unmodified.y.rs",
75+
std::env::var("OUT_DIR").unwrap()
76+
))
77+
})
78+
.lexer_in_src_dir("multi_start.l")
79+
.unwrap()
80+
.output_path(format!(
81+
"{}/ast_unmodified.l.rs",
82+
std::env::var("OUT_DIR").unwrap()
83+
))
84+
.mod_name("ast_unmodified_l")
85+
.build()
86+
.unwrap();
87+
CTLexerBuilder::new()
88+
.lrpar_config(move |ctp| {
89+
ctp.grammar_ast(modified_ast.clone(), UnstableApi)
90+
.with_grammar_src(grammar_src_clone.clone(), UnstableApi)
91+
.grammar_in_src_dir("multi_start.y")
92+
.unwrap()
93+
.mod_name("ast_modified_y")
94+
.output_path(format!(
95+
"{}/ast_modified.y.rs",
96+
std::env::var("OUT_DIR").unwrap()
97+
))
98+
// We still need to disable these because they are checked after ast validation.
99+
.warnings_are_errors(false)
100+
.show_warnings(false)
101+
})
102+
.lexer_in_src_dir("multi_start.l")
103+
.unwrap()
104+
.mod_name("ast_modified_l")
105+
.output_path(format!(
106+
"{}/ast_modified.l.rs",
107+
std::env::var("OUT_DIR").unwrap()
108+
))
109+
.build()
110+
.unwrap();
111+
}
49112
println!("cargo::rerun-if-changed=src/storaget.l");
50113
println!(
51114
"cargo::rerun-if-changed={}/storaget.l.rs",

lrpar/cttests/src/lib.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ lrpar_mod!("storaget.y");
6262
lrlex_mod!("grmtools_section.l");
6363
lrpar_mod!("grmtools_section.y");
6464

65+
lrlex_mod!("ast_unmodified.l");
66+
lrpar_mod!("ast_unmodified.y");
67+
68+
lrlex_mod!("ast_modified.l");
69+
lrpar_mod!("ast_modified.y");
70+
6571
#[test]
6672
fn multitypes() {
6773
let lexerdef = multitypes_l::lexerdef();
@@ -423,6 +429,26 @@ fn test_lex_flags() {
423429
}
424430
}
425431

432+
#[test]
433+
fn ast_unmodified() {
434+
let lexerdef = ast_unmodified_l::lexerdef();
435+
let lexer = lexerdef.lexer("A: BBBB, CCCCC;");
436+
match &ast_unmodified_y::parse(&lexer) {
437+
(_, errs) if errs.is_empty() => (),
438+
(_, e) => panic!("{:?}", e),
439+
}
440+
}
441+
442+
#[test]
443+
fn ast_modified() {
444+
let lexerdef = ast_modified_l::lexerdef();
445+
let lexer = lexerdef.lexer("CCCCC, BBBB");
446+
match &ast_modified_y::parse(&lexer) {
447+
(_, errs) if errs.is_empty() => (),
448+
(_, e) => panic!("{:?}", e),
449+
}
450+
}
451+
426452
// Codegen failure tests
427453
#[cfg(test)]
428454
generate_codegen_fail_tests!("src/ctfails/*.test");

lrpar/cttests/src/multi_start.l

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
%%
2+
A+ 'A'
3+
B+ 'B'
4+
C+ 'C'
5+
; ';'
6+
: ':'
7+
, ','
8+
[ \n\t] ;

lrpar/cttests/src/multi_start.y

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
%grmtools{yacckind: Grmtools}
2+
%start AStart
3+
%token A B C
4+
%%
5+
6+
AStart -> ()
7+
: A ':' BStart ';' {()}
8+
;
9+
10+
BStart -> ()
11+
: B ',' C {()}
12+
| C ',' B {()}
13+
;

0 commit comments

Comments
 (0)