11// Vendored from `smoljson` bef592c5da1c3fe38b2462a8d231b0e0c8a86f80, with explicit permission
22// from the author (`thomcc`). Minimized for cc/simplicity. Modifications and additions made to fit cc's needs.
3- #![ allow( dead_code) ]
43
54use std:: borrow:: Cow ;
6- /// First lifetime is for strings borrowed from the source.
7- /// Second lifetime is for strings borrowed from the parser.
5+
86#[ derive( PartialEq , Debug , Clone ) ]
97pub ( crate ) enum Token < ' s > {
108 Null ,
@@ -55,26 +53,11 @@ impl<'a> Reader<'a> {
5553 }
5654 }
5755
58- #[ inline]
59- pub fn position ( & self ) -> usize {
60- self . pos . min ( self . bytes . len ( ) )
61- }
62-
6356 #[ cold]
6457 pub ( super ) fn err ( & self ) -> Error {
6558 Error ( ( ) )
6659 }
6760
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-
7861 fn bnext_if ( & mut self , b : u8 ) -> bool {
7962 if self . pos < self . bytes . len ( ) && self . bytes [ self . pos ] == b {
8063 self . pos += 1 ;
@@ -109,30 +92,11 @@ impl<'a> Reader<'a> {
10992 }
11093 }
11194
112- fn bpeek_or_nul ( & mut self ) -> u8 {
113- self . bpeek ( ) . unwrap_or ( b'\0' )
114- }
115-
11695 fn bump ( & mut self ) {
11796 self . pos += 1 ;
11897 debug_assert ! ( self . pos <= self . input. len( ) ) ;
11998 }
12099
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-
136100 pub ( super ) fn skipnpeek ( & mut self ) -> Result < Option < u8 > > {
137101 debug_assert ! ( self . stash. is_none( ) ) ;
138102 self . skip_trivial ( ) ?;
@@ -184,10 +148,6 @@ impl<'a> Reader<'a> {
184148 self . pos = p;
185149 }
186150
187- fn cur_ch ( & self ) -> Option < char > {
188- self . input [ self . pos ..] . chars ( ) . next ( )
189- }
190-
191151 fn single_hex_escape ( & mut self ) -> Result < u16 > {
192152 let mut acc = 0 ;
193153 for _ in 0 ..4 {
@@ -204,8 +164,6 @@ impl<'a> Reader<'a> {
204164 }
205165
206166 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.
209167 use core:: char:: REPLACEMENT_CHARACTER as REPLACEMENT ;
210168 const LEAD : core:: ops:: Range < u16 > = 0xd800 ..0xdc00 ;
211169 const TRAIL : core:: ops:: Range < u16 > = 0xdc00 ..0xe000 ;
@@ -281,10 +239,6 @@ impl<'a> Reader<'a> {
281239 Ok ( t)
282240 }
283241
284- pub ( crate ) fn unpeek ( & mut self , t : Token < ' a > ) {
285- assert ! ( self . stash. is_none( ) ) ;
286- self . stash = Some ( t) ;
287- }
288242 pub ( crate ) fn next_token ( & mut self ) -> Result < Option < Token < ' a > > > {
289243 if let Some ( t) = self . stash . take ( ) {
290244 return Ok ( Some ( t) ) ;
@@ -377,17 +331,6 @@ impl<'a> Reader<'a> {
377331 }
378332}
379333
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- }
391334impl < ' a > Reader < ' a > {
392335 pub ( crate ) fn next ( & mut self ) -> Result < Token < ' a > > {
393336 match self . next_token ( ) {
@@ -396,40 +339,6 @@ impl<'a> Reader<'a> {
396339 _ => Err ( self . err ( ) ) ,
397340 }
398341 }
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- }
433342}
434343
435344impl < ' a > Reader < ' a > {
0 commit comments