1
- use std:: cmp:: min;
2
-
3
1
use pg_lexer:: { SyntaxKind , Token , TokenType , WHITESPACE_TOKENS } ;
4
- use text_size:: { TextRange , TextSize } ;
5
-
6
- use crate :: syntax_error:: SyntaxError ;
7
2
8
- /// Main parser that exposes the `cstree` api, and collects errors and statements
9
3
pub struct Parser {
10
- /// The ranges of the statements
11
- ranges : Vec < ( usize , usize ) > ,
12
- /// The syntax errors accumulated during parsing
13
- errors : Vec < SyntaxError > ,
14
- /// The start of the current statement, if any
15
- current_stmt_start : Option < usize > ,
16
4
/// The tokens to parse
17
5
pub tokens : Vec < Token > ,
18
6
/// The current position in the token stream
@@ -23,62 +11,16 @@ pub struct Parser {
23
11
eof_token : Token ,
24
12
}
25
13
26
- /// Result of Building
27
- #[ derive( Debug ) ]
28
- pub struct Parse {
29
- /// The ranges of the errors
30
- pub ranges : Vec < TextRange > ,
31
- /// The syntax errors accumulated during parsing
32
- pub errors : Vec < SyntaxError > ,
33
- }
34
-
35
14
impl Parser {
36
15
pub fn new ( tokens : Vec < Token > ) -> Self {
37
16
Self {
38
17
eof_token : Token :: eof ( usize:: from ( tokens. last ( ) . unwrap ( ) . span . end ( ) ) ) ,
39
- ranges : Vec :: new ( ) ,
40
- errors : Vec :: new ( ) ,
41
- current_stmt_start : None ,
42
18
tokens,
43
19
pos : 0 ,
44
20
whitespace_token_buffer : None ,
45
21
}
46
22
}
47
23
48
- pub fn finish ( self ) -> Parse {
49
- Parse {
50
- ranges : self
51
- . ranges
52
- . iter ( )
53
- . map ( |( start, end) | {
54
- let from = self . tokens . get ( * start) ;
55
- let to = self . tokens . get ( end - 1 ) ;
56
- // get text range from token range
57
- let text_start = from. unwrap ( ) . span . start ( ) ;
58
- let text_end = to. unwrap ( ) . span . end ( ) ;
59
-
60
- TextRange :: new (
61
- TextSize :: try_from ( text_start) . unwrap ( ) ,
62
- TextSize :: try_from ( text_end) . unwrap ( ) ,
63
- )
64
- } )
65
- . collect ( ) ,
66
- errors : self . errors ,
67
- }
68
- }
69
-
70
- /// collects an SyntaxError with an `error` message at `pos`
71
- pub fn error_at_pos ( & mut self , error : String , pos : usize ) {
72
- self . errors . push ( SyntaxError :: new_at_offset (
73
- error,
74
- self . tokens
75
- . get ( min ( self . tokens . len ( ) - 1 , pos) )
76
- . unwrap ( )
77
- . span
78
- . start ( ) ,
79
- ) ) ;
80
- }
81
-
82
24
/// applies token and advances
83
25
pub fn advance ( & mut self ) {
84
26
assert ! ( !self . eof( ) ) ;
@@ -203,25 +145,4 @@ impl Parser {
203
145
pub fn at ( & self , kind : SyntaxKind ) -> bool {
204
146
self . nth ( 0 , false ) . kind == kind
205
147
}
206
-
207
- pub fn expect ( & mut self , kind : SyntaxKind ) {
208
- if self . eat ( kind) {
209
- return ;
210
- }
211
- if self . whitespace_token_buffer . is_some ( ) {
212
- self . error_at_pos (
213
- format ! (
214
- "Expected {:#?}, found {:#?}" ,
215
- kind,
216
- self . tokens[ self . whitespace_token_buffer. unwrap( ) ] . kind
217
- ) ,
218
- self . whitespace_token_buffer . unwrap ( ) ,
219
- ) ;
220
- } else {
221
- self . error_at_pos (
222
- format ! ( "Expected {:#?}, found {:#?}" , kind, self . nth( 0 , false ) ) ,
223
- self . pos + 1 ,
224
- ) ;
225
- }
226
- }
227
148
}
0 commit comments