@@ -46,6 +46,57 @@ pub struct StringReader<'a> {
4646}
4747
4848impl < ' a > StringReader < ' a > {
49+ pub fn new ( sess : & ' a ParseSess ,
50+ source_file : Lrc < syntax_pos:: SourceFile > ,
51+ override_span : Option < Span > ) -> Self {
52+ let mut sr = StringReader :: new_internal ( sess, source_file, override_span) ;
53+ sr. bump ( ) ;
54+ sr
55+ }
56+
57+ pub fn retokenize ( sess : & ' a ParseSess , mut span : Span ) -> Self {
58+ let begin = sess. source_map ( ) . lookup_byte_offset ( span. lo ( ) ) ;
59+ let end = sess. source_map ( ) . lookup_byte_offset ( span. hi ( ) ) ;
60+
61+ // Make the range zero-length if the span is invalid.
62+ if span. lo ( ) > span. hi ( ) || begin. sf . start_pos != end. sf . start_pos {
63+ span = span. shrink_to_lo ( ) ;
64+ }
65+
66+ let mut sr = StringReader :: new_internal ( sess, begin. sf , None ) ;
67+
68+ // Seek the lexer to the right byte range.
69+ sr. next_pos = span. lo ( ) ;
70+ sr. end_src_index = sr. src_index ( span. hi ( ) ) ;
71+
72+ sr. bump ( ) ;
73+
74+ sr
75+ }
76+
77+ fn new_internal ( sess : & ' a ParseSess , source_file : Lrc < syntax_pos:: SourceFile > ,
78+ override_span : Option < Span > ) -> Self
79+ {
80+ if source_file. src . is_none ( ) {
81+ sess. span_diagnostic . bug ( & format ! ( "Cannot lex source_file without source: {}" ,
82+ source_file. name) ) ;
83+ }
84+
85+ let src = ( * source_file. src . as_ref ( ) . unwrap ( ) ) . clone ( ) ;
86+
87+ StringReader {
88+ sess,
89+ next_pos : source_file. start_pos ,
90+ pos : source_file. start_pos ,
91+ ch : Some ( '\n' ) ,
92+ source_file,
93+ end_src_index : src. len ( ) ,
94+ src,
95+ fatal_errs : Vec :: new ( ) ,
96+ override_span,
97+ }
98+ }
99+
49100 fn mk_sp ( & self , lo : BytePos , hi : BytePos ) -> Span {
50101 self . mk_sp_and_raw ( lo, hi) . 0
51102 }
@@ -149,57 +200,6 @@ impl<'a> StringReader<'a> {
149200 buffer
150201 }
151202
152- pub fn new ( sess : & ' a ParseSess ,
153- source_file : Lrc < syntax_pos:: SourceFile > ,
154- override_span : Option < Span > ) -> Self {
155- let mut sr = StringReader :: new_internal ( sess, source_file, override_span) ;
156- sr. bump ( ) ;
157- sr
158- }
159-
160- fn new_internal ( sess : & ' a ParseSess , source_file : Lrc < syntax_pos:: SourceFile > ,
161- override_span : Option < Span > ) -> Self
162- {
163- if source_file. src . is_none ( ) {
164- sess. span_diagnostic . bug ( & format ! ( "Cannot lex source_file without source: {}" ,
165- source_file. name) ) ;
166- }
167-
168- let src = ( * source_file. src . as_ref ( ) . unwrap ( ) ) . clone ( ) ;
169-
170- StringReader {
171- sess,
172- next_pos : source_file. start_pos ,
173- pos : source_file. start_pos ,
174- ch : Some ( '\n' ) ,
175- source_file,
176- end_src_index : src. len ( ) ,
177- src,
178- fatal_errs : Vec :: new ( ) ,
179- override_span,
180- }
181- }
182-
183- pub fn retokenize ( sess : & ' a ParseSess , mut span : Span ) -> Self {
184- let begin = sess. source_map ( ) . lookup_byte_offset ( span. lo ( ) ) ;
185- let end = sess. source_map ( ) . lookup_byte_offset ( span. hi ( ) ) ;
186-
187- // Make the range zero-length if the span is invalid.
188- if span. lo ( ) > span. hi ( ) || begin. sf . start_pos != end. sf . start_pos {
189- span = span. shrink_to_lo ( ) ;
190- }
191-
192- let mut sr = StringReader :: new_internal ( sess, begin. sf , None ) ;
193-
194- // Seek the lexer to the right byte range.
195- sr. next_pos = span. lo ( ) ;
196- sr. end_src_index = sr. src_index ( span. hi ( ) ) ;
197-
198- sr. bump ( ) ;
199-
200- sr
201- }
202-
203203 #[ inline]
204204 fn ch_is ( & self , c : char ) -> bool {
205205 self . ch == Some ( c)
0 commit comments