File tree Expand file tree Collapse file tree 2 files changed +25
-10
lines changed Expand file tree Collapse file tree 2 files changed +25
-10
lines changed Original file line number Diff line number Diff line change @@ -50,6 +50,7 @@ impl<'a> LexedStr<'a> {
50
50
res
51
51
}
52
52
53
+ /// NB: only valid to call with Output from Reparser/TopLevelEntry.
53
54
pub fn intersperse_trivia (
54
55
& self ,
55
56
output : & crate :: Output ,
@@ -76,7 +77,7 @@ impl<'a> LexedStr<'a> {
76
77
builder. eat_trivias ( ) ;
77
78
( builder. sink ) ( StrStep :: Exit ) ;
78
79
}
79
- State :: PendingEnter | State :: Normal => ( ) ,
80
+ State :: PendingEnter | State :: Normal => unreachable ! ( ) ,
80
81
}
81
82
82
83
let is_eof = builder. pos == builder. lexed . len ( ) ;
@@ -100,8 +101,9 @@ enum State {
100
101
impl Builder < ' _ , ' _ > {
101
102
fn token ( & mut self , kind : SyntaxKind , n_tokens : u8 ) {
102
103
match mem:: replace ( & mut self . state , State :: Normal ) {
104
+ State :: PendingEnter => unreachable ! ( ) ,
103
105
State :: PendingExit => ( self . sink ) ( StrStep :: Exit ) ,
104
- State :: PendingEnter | State :: Normal => ( ) ,
106
+ State :: Normal => ( ) ,
105
107
}
106
108
self . eat_trivias ( ) ;
107
109
self . do_token ( kind, n_tokens as usize ) ;
Original file line number Diff line number Diff line change 1
- use crate :: { LexedStr , PrefixEntryPoint , StrStep } ;
1
+ use crate :: { LexedStr , PrefixEntryPoint , Step } ;
2
2
3
3
#[ test]
4
4
fn vis ( ) {
@@ -30,12 +30,25 @@ fn stmt() {
30
30
fn check_prefix ( entry : PrefixEntryPoint , input : & str , prefix : & str ) {
31
31
let lexed = LexedStr :: new ( input) ;
32
32
let input = lexed. to_input ( ) ;
33
- let output = entry. parse ( & input) ;
34
33
35
- let mut buf = String :: new ( ) ;
36
- lexed. intersperse_trivia ( & output, & mut |step| match step {
37
- StrStep :: Token { kind : _, text } => buf. push_str ( text) ,
38
- _ => ( ) ,
39
- } ) ;
40
- assert_eq ! ( buf. trim( ) , prefix)
34
+ let mut n_tokens = 0 ;
35
+ for step in entry. parse ( & input) . iter ( ) {
36
+ match step {
37
+ Step :: Token { n_input_tokens, .. } => n_tokens += n_input_tokens as usize ,
38
+ Step :: Enter { .. } | Step :: Exit | Step :: Error { .. } => ( ) ,
39
+ }
40
+ }
41
+
42
+ let mut i = 0 ;
43
+ loop {
44
+ if n_tokens == 0 {
45
+ break ;
46
+ }
47
+ if !lexed. kind ( i) . is_trivia ( ) {
48
+ n_tokens -= 1 ;
49
+ }
50
+ i += 1 ;
51
+ }
52
+ let buf = & lexed. as_str ( ) [ ..lexed. text_start ( i) ] ;
53
+ assert_eq ! ( buf, prefix) ;
41
54
}
You can’t perform that action at this time.
0 commit comments