Skip to content

Commit 5637a6f

Browse files
authored
Merge pull request #33 from nikomatsakis/slightly-better-repl
Slightly better repl
2 parents a7555f8 + 1f75189 commit 5637a6f

File tree

4 files changed

+3496
-3114
lines changed

4 files changed

+3496
-3114
lines changed

chalk-parse/src/lib.rs

+24-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ pub mod errors;
1111
mod parser;
1212

1313
use errors::Result;
14+
use lalrpop_util::ParseError;
15+
use std::fmt::Write;
1416

1517
pub fn parse_program(text: &str) -> Result<ast::Program> {
1618
match parser::parse_Program(text) {
@@ -29,7 +31,28 @@ pub fn parse_ty(text: &str) -> Result<ast::Ty> {
2931
pub fn parse_goal(text: &str) -> Result<Box<ast::Goal>> {
3032
match parser::parse_Goal(text) {
3133
Ok(v) => Ok(v),
32-
Err(e) => bail!("parse error: {:?}", e),
34+
Err(e) => {
35+
let position_string = |start: usize, end: usize| {
36+
let mut output = String::new();
37+
let text = text.replace("\n", " ").replace("\r", " ");
38+
write!(output, "position: `{}`\n", text).expect("str-write cannot fail");
39+
write!(output, " ").expect("str-write cannot fail");
40+
for _ in 0..start { output.push_str(" "); }
41+
for _ in start..end { output.push_str("^"); }
42+
output.push_str("\n");
43+
output
44+
};
45+
match e {
46+
ParseError::InvalidToken { location } =>
47+
bail!("parse error: {:?}\n{}", e, position_string(location, location+1)),
48+
ParseError::UnrecognizedToken { token: Some((start, _, end)), .. } =>
49+
bail!("parse error: {:?}\n{}", e, position_string(start, end)),
50+
ParseError::ExtraToken { token: (start, _, end), .. } =>
51+
bail!("parse error: {:?}\n{}", e, position_string(start, end)),
52+
_ =>
53+
bail!("parse error: {:?}", e),
54+
}
55+
}
3356
}
3457
}
3558

chalk-parse/src/parser.lalrpop

+12-5
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,22 @@ use lalrpop_intern::intern;
44
grammar;
55

66
pub Program: Program = {
7-
Item* => Program { items: <> }
7+
Items => Program { items: <> }
88
};
99

10-
Item: Item = {
11-
StructDefn => Item::StructDefn(<>),
12-
TraitDefn => Item::TraitDefn(<>),
13-
Impl => Item::Impl(<>),
10+
Items: Vec<Item> = {
11+
Item* => <>.into_iter().filter_map(|v| v).collect()
1412
};
1513

14+
Item: Option<Item> = {
15+
Comment => None,
16+
StructDefn => Some(Item::StructDefn(<>)),
17+
TraitDefn => Some(Item::TraitDefn(<>)),
18+
Impl => Some(Item::Impl(<>)),
19+
};
20+
21+
Comment: () = r"//.*";
22+
1623
pub Goal: Box<Goal> = {
1724
Goal1,
1825
<g1:Goal1> "," <g2:Goal> => Box::new(Goal::And(g1, g2)),

0 commit comments

Comments
 (0)