@@ -168,64 +168,83 @@ uint32_t swift::validateUTF8CharacterAndAdvance(const char *&Ptr,
168
168
// Setup and Helper Methods
169
169
// ===----------------------------------------------------------------------===//
170
170
171
- Lexer::Lexer (const LangOptions &Options ,
172
- const SourceManager &SM, DiagnosticEngine *Diags ,
173
- unsigned BufferID , bool InSILMode,
171
+ Lexer::Lexer (const PrincipalTag &, const LangOptions &LangOpts ,
172
+ const SourceManager &SourceMgr, unsigned BufferID ,
173
+ DiagnosticEngine *Diags , bool InSILMode,
174
174
CommentRetentionMode RetainComments,
175
175
TriviaRetentionMode TriviaRetention)
176
- : LangOpts(Options), SourceMgr(SM), Diags(Diags), BufferID(BufferID),
177
- InSILMode(InSILMode), RetainComments(RetainComments),
178
- TriviaRetention(TriviaRetention) {
176
+ : LangOpts(LangOpts), SourceMgr(SourceMgr), BufferID(BufferID),
177
+ Diags(Diags), InSILMode(InSILMode), RetainComments(RetainComments),
178
+ TriviaRetention(TriviaRetention) {}
179
+
180
+ void Lexer::initialize (unsigned Offset, unsigned EndOffset) {
181
+ assert (Offset <= EndOffset);
182
+
179
183
// Initialize buffer pointers.
180
- StringRef contents = SM.extractText (SM.getRangeForBuffer (BufferID));
184
+ StringRef contents =
185
+ SourceMgr.extractText (SourceMgr.getRangeForBuffer (BufferID));
181
186
BufferStart = contents.data ();
182
187
BufferEnd = contents.data () + contents.size ();
188
+ assert (*BufferEnd == 0 );
189
+ assert (BufferStart + Offset <= BufferEnd);
190
+ assert (BufferStart + EndOffset <= BufferEnd);
183
191
184
192
// Check for Unicode BOM at start of file (Only UTF-8 BOM supported now).
185
- size_t BOMLength = llvm::StringSwitch<size_t >(contents)
186
- .StartsWith (" \xEF\xBB\xBF " , 3 )
187
- .Default (0 );
193
+ size_t BOMLength = contents.startswith (" \xEF\xBB\xBF " ) ? 3 : 0 ;
188
194
189
195
// Keep information about existance of UTF-8 BOM for transparency source code
190
196
// editing with libSyntax.
191
- CurPtr = BufferStart;
192
197
ContentStart = BufferStart + BOMLength;
193
198
194
199
// Initialize code completion.
195
- if (BufferID == SM .getCodeCompletionBufferID ()) {
196
- const char *Ptr = BufferStart + SM .getCodeCompletionOffset ();
200
+ if (BufferID == SourceMgr .getCodeCompletionBufferID ()) {
201
+ const char *Ptr = BufferStart + SourceMgr .getCodeCompletionOffset ();
197
202
if (Ptr >= BufferStart && Ptr <= BufferEnd)
198
203
CodeCompletionPtr = Ptr;
199
204
}
200
- }
201
205
202
- void Lexer::primeLexer () {
206
+ ArtificialEOF = BufferStart + EndOffset;
207
+ CurPtr = BufferStart + Offset;
208
+
203
209
assert (NextToken.is (tok::NUM_TOKENS));
204
210
lexImpl ();
205
211
assert ((NextToken.isAtStartOfLine () || CurPtr != BufferStart) &&
206
212
" The token should be at the beginning of the line, "
207
213
" or we should be lexing from the middle of the buffer" );
208
214
}
209
215
210
- void Lexer::initSubLexer (Lexer &Parent, State BeginState, State EndState) {
216
+ Lexer::Lexer (const LangOptions &Options, const SourceManager &SourceMgr,
217
+ unsigned BufferID, DiagnosticEngine *Diags, bool InSILMode,
218
+ CommentRetentionMode RetainComments,
219
+ TriviaRetentionMode TriviaRetention)
220
+ : Lexer(PrincipalTag(), Options, SourceMgr, BufferID, Diags, InSILMode,
221
+ RetainComments, TriviaRetention) {
222
+ unsigned EndOffset = SourceMgr.getRangeForBuffer (BufferID).getByteLength ();
223
+ initialize (/* Offset=*/ 0 , EndOffset);
224
+ }
225
+
226
+ Lexer::Lexer (const LangOptions &Options, const SourceManager &SourceMgr,
227
+ unsigned BufferID, DiagnosticEngine *Diags, bool InSILMode,
228
+ CommentRetentionMode RetainComments,
229
+ TriviaRetentionMode TriviaRetention, unsigned Offset,
230
+ unsigned EndOffset)
231
+ : Lexer(PrincipalTag(), Options, SourceMgr, BufferID, Diags, InSILMode,
232
+ RetainComments, TriviaRetention) {
233
+ initialize (Offset, EndOffset);
234
+ }
235
+
236
+ Lexer::Lexer (Lexer &Parent, State BeginState, State EndState)
237
+ : Lexer(PrincipalTag(), Parent.LangOpts, Parent.SourceMgr, Parent.BufferID,
238
+ Parent.Diags, Parent.InSILMode, Parent.RetainComments,
239
+ Parent.TriviaRetention) {
211
240
assert (BufferID == SourceMgr.findBufferContainingLoc (BeginState.Loc ) &&
212
241
" state for the wrong buffer" );
213
242
assert (BufferID == SourceMgr.findBufferContainingLoc (EndState.Loc ) &&
214
243
" state for the wrong buffer" );
215
244
216
- // If the parent lexer should stop prematurely, and the ArtificialEOF
217
- // position is in this subrange, then we should stop at that point, too.
218
- const char *BeginStatePtr = getBufferPtrForSourceLoc (BeginState.Loc );
219
- const char *EndStatePtr = getBufferPtrForSourceLoc (EndState.Loc );
220
- if (Parent.ArtificialEOF &&
221
- Parent.ArtificialEOF >= BeginStatePtr &&
222
- Parent.ArtificialEOF <= EndStatePtr) {
223
- ArtificialEOF = Parent.ArtificialEOF ;
224
- } else
225
- ArtificialEOF = EndStatePtr;
226
-
227
- primeLexer ();
228
- restoreState (BeginState);
245
+ unsigned Offset = SourceMgr.getLocOffsetInBuffer (BeginState.Loc , BufferID);
246
+ unsigned EndOffset = SourceMgr.getLocOffsetInBuffer (EndState.Loc , BufferID);
247
+ initialize (Offset, EndOffset);
229
248
}
230
249
231
250
InFlightDiagnostic Lexer::diagnose (const char *Loc, Diagnostic Diag) {
@@ -255,7 +274,7 @@ void Lexer::formToken(tok Kind, const char *TokStart, bool MultilineString) {
255
274
// When we are lexing a subrange from the middle of a file buffer, we will
256
275
// run past the end of the range, but will stay within the file. Check if
257
276
// we are past the imaginary EOF, and synthesize a tok::eof in this case.
258
- if (Kind != tok::eof && ArtificialEOF && TokStart >= ArtificialEOF) {
277
+ if (Kind != tok::eof && TokStart >= ArtificialEOF) {
259
278
Kind = tok::eof;
260
279
}
261
280
unsigned CommentLength = 0 ;
0 commit comments