@@ -11,6 +11,8 @@ pub mod errors;
11
11
mod parser;
12
12
13
13
use errors:: Result ;
14
+ use lalrpop_util:: ParseError ;
15
+ use std:: fmt:: Write ;
14
16
15
17
pub fn parse_program ( text : & str ) -> Result < ast:: Program > {
16
18
match parser:: parse_Program ( text) {
@@ -29,7 +31,28 @@ pub fn parse_ty(text: &str) -> Result<ast::Ty> {
29
31
pub fn parse_goal ( text : & str ) -> Result < Box < ast:: Goal > > {
30
32
match parser:: parse_Goal ( text) {
31
33
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
+ }
33
56
}
34
57
}
35
58
0 commit comments