1
1
// Vendored from `smoljson` bef592c5da1c3fe38b2462a8d231b0e0c8a86f80, with explicit permission
2
2
// from the author (`thomcc`). Minimized for cc/simplicity. Modifications and additions made to fit cc's needs.
3
- #![ allow( dead_code) ]
4
3
5
4
use std:: borrow:: Cow ;
6
- /// First lifetime is for strings borrowed from the source.
7
- /// Second lifetime is for strings borrowed from the parser.
5
+
8
6
#[ derive( PartialEq , Debug , Clone ) ]
9
7
pub ( crate ) enum Token < ' s > {
10
8
Null ,
@@ -55,26 +53,11 @@ impl<'a> Reader<'a> {
55
53
}
56
54
}
57
55
58
- #[ inline]
59
- pub fn position ( & self ) -> usize {
60
- self . pos . min ( self . bytes . len ( ) )
61
- }
62
-
63
56
#[ cold]
64
57
pub ( super ) fn err ( & self ) -> Error {
65
58
Error ( ( ) )
66
59
}
67
60
68
- /// Returns `Err` if there are any more non-whitespace/non-comment (if this
69
- /// reader's dialect allows comments) characters in the input.
70
- pub fn finish ( mut self ) -> Result < ( ) > {
71
- match self . next_token ( ) {
72
- Ok ( Some ( _) ) => Err ( self . err ( ) ) ,
73
- Ok ( None ) => Ok ( ( ) ) ,
74
- Err ( e) => Err ( e) ,
75
- }
76
- }
77
-
78
61
fn bnext_if ( & mut self , b : u8 ) -> bool {
79
62
if self . pos < self . bytes . len ( ) && self . bytes [ self . pos ] == b {
80
63
self . pos += 1 ;
@@ -109,30 +92,11 @@ impl<'a> Reader<'a> {
109
92
}
110
93
}
111
94
112
- fn bpeek_or_nul ( & mut self ) -> u8 {
113
- self . bpeek ( ) . unwrap_or ( b'\0' )
114
- }
115
-
116
95
fn bump ( & mut self ) {
117
96
self . pos += 1 ;
118
97
debug_assert ! ( self . pos <= self . input. len( ) ) ;
119
98
}
120
99
121
- fn finished ( & self ) -> bool {
122
- self . pos >= self . bytes . len ( )
123
- }
124
-
125
- pub ( super ) fn ref_stash ( & self ) -> Option < & Token < ' a > > {
126
- self . stash . as_ref ( )
127
- }
128
-
129
- pub ( super ) fn mut_stash ( & mut self ) -> & mut Option < Token < ' a > > {
130
- & mut self . stash
131
- }
132
- pub ( super ) fn take_stash ( & mut self ) -> Option < Token < ' a > > {
133
- self . stash . take ( )
134
- }
135
-
136
100
pub ( super ) fn skipnpeek ( & mut self ) -> Result < Option < u8 > > {
137
101
debug_assert ! ( self . stash. is_none( ) ) ;
138
102
self . skip_trivial ( ) ?;
@@ -184,10 +148,6 @@ impl<'a> Reader<'a> {
184
148
self . pos = p;
185
149
}
186
150
187
- fn cur_ch ( & self ) -> Option < char > {
188
- self . input [ self . pos ..] . chars ( ) . next ( )
189
- }
190
-
191
151
fn single_hex_escape ( & mut self ) -> Result < u16 > {
192
152
let mut acc = 0 ;
193
153
for _ in 0 ..4 {
@@ -204,8 +164,6 @@ impl<'a> Reader<'a> {
204
164
}
205
165
206
166
fn read_hex_escape ( & mut self ) -> Result < ( ) > {
207
- // todo: option where we reutrn an error (instead using replacement
208
- // char) if unescaping produces unpaired surrogates.
209
167
use core:: char:: REPLACEMENT_CHARACTER as REPLACEMENT ;
210
168
const LEAD : core:: ops:: Range < u16 > = 0xd800 ..0xdc00 ;
211
169
const TRAIL : core:: ops:: Range < u16 > = 0xdc00 ..0xe000 ;
@@ -281,10 +239,6 @@ impl<'a> Reader<'a> {
281
239
Ok ( t)
282
240
}
283
241
284
- pub ( crate ) fn unpeek ( & mut self , t : Token < ' a > ) {
285
- assert ! ( self . stash. is_none( ) ) ;
286
- self . stash = Some ( t) ;
287
- }
288
242
pub ( crate ) fn next_token ( & mut self ) -> Result < Option < Token < ' a > > > {
289
243
if let Some ( t) = self . stash . take ( ) {
290
244
return Ok ( Some ( t) ) ;
@@ -377,17 +331,6 @@ impl<'a> Reader<'a> {
377
331
}
378
332
}
379
333
380
- macro_rules! tok_tester {
381
- ( $( $func: ident matches $tok: ident) ;* ) => { $(
382
- pub ( crate ) fn $func( & mut self ) -> Result <( ) > {
383
- match self . next_token( ) {
384
- Ok ( Some ( Token :: $tok) ) => Ok ( ( ) ) ,
385
- Err ( e) => Err ( e) ,
386
- _ => Err ( self . err( ) ) ,
387
- }
388
- }
389
- ) * } ;
390
- }
391
334
impl < ' a > Reader < ' a > {
392
335
pub ( crate ) fn next ( & mut self ) -> Result < Token < ' a > > {
393
336
match self . next_token ( ) {
@@ -396,40 +339,6 @@ impl<'a> Reader<'a> {
396
339
_ => Err ( self . err ( ) ) ,
397
340
}
398
341
}
399
- tok_tester ! {
400
- array_begin matches ArrayBegin ;
401
- // array_end matches ArrayEnd;
402
- obj_begin matches ObjectBegin ;
403
- // obj_end matches ObjectEnd;
404
- comma matches Comma ;
405
- colon matches Colon ;
406
- null matches Null
407
- }
408
- pub ( crate ) fn comma_or_obj_end ( & mut self ) -> Result < bool > {
409
- match self . next_token ( ) {
410
- Ok ( Some ( Token :: Comma ) ) => Ok ( true ) ,
411
- Ok ( Some ( Token :: ObjectEnd ) ) => Ok ( false ) ,
412
- Err ( e) => Err ( e) ,
413
- _ => Err ( self . err ( ) ) ,
414
- }
415
- }
416
- pub ( crate ) fn comma_or_array_end ( & mut self ) -> Result < bool > {
417
- match self . next_token ( ) {
418
- Ok ( Some ( Token :: Comma ) ) => Ok ( true ) ,
419
- Ok ( Some ( Token :: ArrayEnd ) ) => Ok ( false ) ,
420
- Err ( e) => Err ( e) ,
421
- _ => Err ( self . err ( ) ) ,
422
- }
423
- }
424
- pub ( crate ) fn key ( & mut self ) -> Result < Cow < ' a , str > > {
425
- match self . next_token ( ) {
426
- Ok ( Some ( Token :: StrBorrow ( b) ) ) => Ok ( Cow :: Borrowed ( b) ) ,
427
- Ok ( Some ( Token :: StrOwn ( b) ) ) => Ok ( Cow :: Owned ( b. into ( ) ) ) ,
428
- Err ( e) => Err ( e) ,
429
- Ok ( Some ( _t) ) => Err ( self . err ( ) ) ,
430
- _o => Err ( self . err ( ) ) ,
431
- }
432
- }
433
342
}
434
343
435
344
impl < ' a > Reader < ' a > {
0 commit comments