1- use core :: cfg:: { TokenSet , Syntax } ;
2- use core :: lex:: LexIterator ;
1+ use pgen_core :: cfg:: { TokenSet , Syntax } ;
2+ use pgen_core :: lex:: Token ;
33
4- use super :: builder:: { LRAction , LR1Configure } ;
4+ use crate :: error:: ParseError ;
5+ use crate :: builder:: { LRAction , LR1Configure } ;
56
67pub ( super ) struct LR1Driver < ' a , ' b , T , S > ( & ' b LR1Configure < ' a , T , S > )
78where
1920
2021 pub fn run < ' c > (
2122 & self ,
22- lexer : & mut impl LexIterator < ' a , ' c , T > ,
23+ lexer : & mut impl Iterator < Item = Token < ' a , ' c , T > > ,
2324 ) -> anyhow:: Result < ( ) > {
2425 let mut stack = vec ! [ 0 ] ;
2526 loop {
@@ -29,31 +30,31 @@ where
2930 let action = match input {
3031 Some ( token) => (
3132 self . 0 . action_table [ top] . get ( & token. kind ) . unwrap ( ) ,
32- Some ( token. as_str ( ) ) ,
33+ Some ( token) ,
3334 ) ,
3435 None => (
3536 & self . 0 . eof_action_table [ top] ,
3637 None
3738 ) ,
3839 } ;
39- match action. 0 {
40- LRAction :: Shift ( new_state) => {
40+ match action {
41+ ( LRAction :: Shift ( new_state) , _ ) => {
4142 stack. push ( * new_state) ;
4243 break ;
4344 }
44- LRAction :: Reduce ( _, goto, elems_cnt) => {
45+ ( LRAction :: Reduce ( _, goto, elems_cnt) , _ ) => {
4546 stack. truncate ( stack. len ( ) - elems_cnt) ;
4647 stack. push ( self . 0 . goto_table [ stack[ stack. len ( ) - 1 ] ] [ * goto] ) ;
4748 }
48- LRAction :: None => {
49- let pos = lexer. pos ( ) ;
50- let pos = match action. 1 {
51- Some ( raw) => ( pos. 0 , pos. 1 - ( raw. len ( ) as u32 ) ) ,
52- None => pos,
53- } ;
54- return Err ( anyhow:: anyhow!( "Error at {:?}" , pos) . into ( ) ) ;
49+ ( LRAction :: Accept , _) => {
50+ return Ok ( ( ) ) ;
51+ }
52+ ( LRAction :: None , Some ( token) ) => {
53+ return Err ( ParseError :: new_unexpected_token ( token) . into ( ) ) ;
54+ }
55+ ( LRAction :: None , None ) => {
56+ return Err ( ParseError :: UnexpectedEOF . into ( ) ) ;
5557 }
56- LRAction :: Accept => return Ok ( ( ) ) ,
5758 }
5859 }
5960 }
0 commit comments