-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathmain.rs
299 lines (281 loc) · 10 KB
/
main.rs
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
mod diagnostics;
use crate::diagnostics::*;
use cfgrammar::{
yacc::{ast::ASTWithValidityInfo, YaccGrammar, YaccKind, YaccOriginalActionKind},
Span,
};
use getopts::Options;
use lrlex::{DefaultLexerTypes, LRNonStreamingLexerDef, LexerDef};
use lrpar::{
parser::{RTParserBuilder, RecoveryKind},
LexerTypes,
};
use lrtable::{from_yacc, Minimiser};
use num_traits::ToPrimitive as _;
use std::{
env,
fs::File,
io::Read,
path::{Path, PathBuf},
process,
};
const WARNING: &str = "[Warning]";
const ERROR: &str = "[Error]";
fn usage(prog: &str, msg: &str) -> ! {
let path = Path::new(prog);
let leaf = match path.file_name() {
Some(m) => m.to_str().unwrap(),
None => "lrpar",
};
if !msg.is_empty() {
eprintln!("{}", msg);
}
eprintln!("Usage: {} [-r <cpctplus|none>] [-y <eco|grmtools|original>] [-q] <lexer.l> <parser.y> <input file>", leaf);
process::exit(1);
}
fn read_file<P: AsRef<Path>>(path: P) -> String {
let mut f = match File::open(&path) {
Ok(r) => r,
Err(e) => {
eprintln!("Can't open file {}: {}", path.as_ref().display(), e);
process::exit(1);
}
};
let mut s = String::new();
f.read_to_string(&mut s).unwrap();
s
}
fn indent(s: &str, indent: &str) -> String {
format!("{indent}{}\n", s.trim_end_matches('\n')).replace('\n', &format!("\n{}", indent))
}
pub fn set_rule_ids<LexerTypesT: LexerTypes<StorageT = u32>, LT: LexerDef<LexerTypesT>>(
lexerdef: &mut LT,
grm: &YaccGrammar,
) -> (Option<Vec<Span>>, Option<Vec<Span>>)
where
usize: num_traits::AsPrimitive<LexerTypesT::StorageT>,
{
let rule_ids = grm
.tokens_map()
.iter()
.map(|(&n, &i)| (n, usize::from(i).to_u32().unwrap()))
.collect::<std::collections::HashMap<&str, u32>>();
let (missing_from_lexer, missing_from_parser) = lexerdef.set_rule_ids_spanned(&rule_ids);
let missing_from_lexer = missing_from_lexer.map(|tokens| {
tokens
.iter()
.map(|name| {
grm.token_span(*grm.tokens_map().get(name).unwrap())
.expect("Given token should have a span")
})
.collect::<Vec<_>>()
});
let missing_from_parser =
missing_from_parser.map(|tokens| tokens.iter().map(|(_, span)| *span).collect::<Vec<_>>());
(missing_from_lexer, missing_from_parser)
}
fn main() {
let args: Vec<String> = env::args().collect();
let prog = &args[0];
let matches = match Options::new()
.optflag("h", "help", "")
.optflag("q", "quiet", "Don't print warnings such as conflicts")
.optopt(
"r",
"recoverer",
"Recoverer to be used (default: cpctplus)",
"cpctplus|none",
)
.optopt(
"y",
"yaccvariant",
"Yacc variant to be parsed (default: original)",
"eco|original|grmtools",
)
.parse(&args[1..])
{
Ok(m) => m,
Err(f) => usage(prog, f.to_string().as_str()),
};
if matches.opt_present("h") {
usage(prog, "");
}
let quiet = matches.opt_present("q");
let recoverykind = match matches.opt_str("r") {
None => RecoveryKind::CPCTPlus,
Some(s) => match &*s.to_lowercase() {
"cpctplus" => RecoveryKind::CPCTPlus,
"none" => RecoveryKind::None,
_ => usage(prog, &format!("Unknown recoverer '{}'.", s)),
},
};
let yacckind = match matches.opt_str("y") {
None => YaccKind::Original(YaccOriginalActionKind::GenericParseTree),
Some(s) => match &*s.to_lowercase() {
"eco" => YaccKind::Eco,
"grmtools" => YaccKind::Grmtools,
"original" => YaccKind::Original(YaccOriginalActionKind::GenericParseTree),
_ => usage(prog, &format!("Unknown Yacc variant '{}'.", s)),
},
};
if matches.free.len() != 3 {
usage(prog, "Too few arguments given.");
}
let lex_l_path = PathBuf::from(&matches.free[0]);
let lex_src = read_file(&lex_l_path);
let mut lexerdef = match LRNonStreamingLexerDef::<DefaultLexerTypes<u32>>::from_str(&lex_src) {
Ok(ast) => ast,
Err(errs) => {
let formatter = SpannedDiagnosticFormatter::new(&lex_src, &lex_l_path).unwrap();
eprintln!("{ERROR}{}", formatter.file_location_msg("", None));
for e in errs {
eprintln!("{}", indent(&formatter.format_error(e).to_string(), " "));
}
process::exit(1);
}
};
let yacc_y_path = PathBuf::from(&matches.free[1]);
let yacc_src = read_file(&yacc_y_path);
let ast_validation = ASTWithValidityInfo::new(yacckind, &yacc_src);
let warnings = ast_validation.ast().warnings();
let res = YaccGrammar::new_from_ast_with_validity_info(yacckind, &ast_validation);
let mut yacc_diagnostic_formatter: Option<SpannedDiagnosticFormatter> = None;
let grm = match res {
Ok(x) => {
if !warnings.is_empty() {
let formatter = SpannedDiagnosticFormatter::new(&yacc_src, &yacc_y_path).unwrap();
eprintln!("{WARNING}{}", formatter.file_location_msg("", None));
for w in warnings {
eprintln!("{}", indent(&formatter.format_warning(w), " "));
}
yacc_diagnostic_formatter = Some(formatter);
}
x
}
Err(errs) => {
let formatter = SpannedDiagnosticFormatter::new(&yacc_src, &yacc_y_path).unwrap();
eprintln!("{ERROR}{}", formatter.file_location_msg("", None));
for e in errs {
eprintln!("{}", indent(&formatter.format_error(e).to_string(), " "));
}
eprintln!("{WARNING}{}", formatter.file_location_msg("", None));
for w in warnings {
eprintln!("{}", indent(&formatter.format_warning(w), " "));
}
process::exit(1);
}
};
let (sgraph, stable) = match from_yacc(&grm, Minimiser::Pager) {
Ok(x) => x,
Err(s) => {
eprintln!("{}: {}", &yacc_y_path.display(), &s);
process::exit(1);
}
};
if !quiet {
if let Some(c) = stable.conflicts() {
let formatter = if let Some(yacc_diagnostic_formatter) = &yacc_diagnostic_formatter {
yacc_diagnostic_formatter
} else {
let formatter = SpannedDiagnosticFormatter::new(&yacc_src, &yacc_y_path).unwrap();
yacc_diagnostic_formatter = Some(formatter);
yacc_diagnostic_formatter.as_ref().unwrap()
};
let pp_rr = if let Some(i) = grm.expectrr() {
i != c.rr_len()
} else {
0 != c.rr_len()
};
let pp_sr = if let Some(i) = grm.expect() {
i != c.sr_len()
} else {
0 != c.sr_len()
};
if pp_rr {
println!("{}", c.pp_rr(&grm));
}
if pp_sr {
println!("{}", c.pp_sr(&grm));
}
if pp_rr || pp_sr {
println!("Stategraph:\n{}\n", sgraph.pp_core_states(&grm));
}
formatter.handle_conflicts::<DefaultLexerTypes<u32>>(
&grm,
ast_validation.ast(),
c,
&sgraph,
&stable,
);
}
}
let (missing_from_lexer, missing_from_parser) = set_rule_ids(&mut lexerdef, &grm);
{
if !quiet {
if let Some(token_spans) = missing_from_lexer {
let formatter = SpannedDiagnosticFormatter::new(&yacc_src, &yacc_y_path).unwrap();
let warn_indent = " ".repeat(WARNING.len());
eprintln!(
"{WARNING} these tokens are not referenced in the lexer but defined as follows"
);
eprintln!(
"{warn_indent} {}",
formatter.file_location_msg("in the grammar", None)
);
for span in token_spans {
eprintln!(
"{}",
formatter.underline_span_with_text(
span,
"Missing from lexer".to_string(),
'^'
)
);
}
}
eprintln!();
if let Some(token_spans) = missing_from_parser {
let formatter = SpannedDiagnosticFormatter::new(&lex_src, &lex_l_path).unwrap();
let err_indent = " ".repeat(ERROR.len());
eprintln!(
"{ERROR} these tokens are not referenced in the grammar but defined as follows"
);
eprintln!(
"{err_indent} {}",
formatter.file_location_msg("in the lexer", None)
);
for span in token_spans {
eprintln!(
"{}",
formatter.underline_span_with_text(
span,
"Missing from parser".to_string(),
'^'
)
);
}
process::exit(1);
}
}
}
let input = if &matches.free[2] == "-" {
let mut s = String::new();
std::io::stdin().read_to_string(&mut s).unwrap();
s
} else {
read_file(&matches.free[2])
};
let lexer = lexerdef.lexer(&input);
let pb = RTParserBuilder::new(&grm, &stable).recoverer(recoverykind);
let (pt, errs) = pb.parse_generictree(&lexer);
match pt {
Some(pt) => println!("{}", pt.pp(&grm, &input)),
None => println!("Unable to repair input sufficiently to produce parse tree.\n"),
}
for e in &errs {
println!("{}", e.pp(&lexer, &|t| grm.token_epp(t)));
}
if !errs.is_empty() {
process::exit(1);
}
}