|
| 1 | +use cfgrammar::yacc::ast::ASTWithValidityInfo; |
1 | 2 | use glob::glob; |
2 | 3 | #[path = "src/cgen_helper.rs"] |
3 | 4 | mod cgen_helper; |
@@ -46,6 +47,68 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { |
46 | 47 | .build() |
47 | 48 | .unwrap(); |
48 | 49 | } |
| 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 | + } |
49 | 112 | println!("cargo::rerun-if-changed=src/storaget.l"); |
50 | 113 | println!( |
51 | 114 | "cargo::rerun-if-changed={}/storaget.l.rs", |
|
0 commit comments