1 | /*
|
---|
2 | * Copyright (C) 1999-2000 Harri Porten ([email protected])
|
---|
3 | * Copyright (C) 2002-2019 Apple Inc. All rights reserved.
|
---|
4 | * Copyright (C) 2010 Zoltan Herczeg ([email protected])
|
---|
5 | *
|
---|
6 | * This library is free software; you can redistribute it and/or
|
---|
7 | * modify it under the terms of the GNU Library General Public
|
---|
8 | * License as published by the Free Software Foundation; either
|
---|
9 | * version 2 of the License, or (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This library is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
14 | * Library General Public License for more details.
|
---|
15 | *
|
---|
16 | * You should have received a copy of the GNU Library General Public License
|
---|
17 | * along with this library; see the file COPYING.LIB. If not, write to
|
---|
18 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
---|
19 | * Boston, MA 02110-1301, USA.
|
---|
20 | *
|
---|
21 | */
|
---|
22 |
|
---|
23 | #pragma once
|
---|
24 |
|
---|
25 | #include "Lookup.h"
|
---|
26 | #include "ParserArena.h"
|
---|
27 | #include "ParserModes.h"
|
---|
28 | #include "ParserTokens.h"
|
---|
29 | #include "SourceCode.h"
|
---|
30 | #include <wtf/ASCIICType.h>
|
---|
31 | #include <wtf/Vector.h>
|
---|
32 | #include <wtf/unicode/CharacterNames.h>
|
---|
33 |
|
---|
34 | namespace JSC {
|
---|
35 |
|
---|
36 | struct ParsedUnicodeEscapeValue;
|
---|
37 |
|
---|
38 | enum class LexerFlags : uint8_t {
|
---|
39 | IgnoreReservedWords = 1 << 0,
|
---|
40 | DontBuildStrings = 1 << 1,
|
---|
41 | DontBuildKeywords = 1 << 2
|
---|
42 | };
|
---|
43 |
|
---|
44 | bool isLexerKeyword(const Identifier&);
|
---|
45 |
|
---|
46 | template <typename T>
|
---|
47 | class Lexer {
|
---|
48 | WTF_MAKE_NONCOPYABLE(Lexer);
|
---|
49 | WTF_MAKE_FAST_ALLOCATED;
|
---|
50 |
|
---|
51 | public:
|
---|
52 | Lexer(VM&, JSParserBuiltinMode, JSParserScriptMode);
|
---|
53 | ~Lexer();
|
---|
54 |
|
---|
55 | // Character manipulation functions.
|
---|
56 | static bool isWhiteSpace(T character);
|
---|
57 | static bool isLineTerminator(T character);
|
---|
58 | static unsigned char convertHex(int c1, int c2);
|
---|
59 | static UChar convertUnicode(int c1, int c2, int c3, int c4);
|
---|
60 |
|
---|
61 | // Functions to set up parsing.
|
---|
62 | void setCode(const SourceCode&, ParserArena*);
|
---|
63 | void setIsReparsingFunction() { m_isReparsingFunction = true; }
|
---|
64 | bool isReparsingFunction() const { return m_isReparsingFunction; }
|
---|
65 |
|
---|
66 | JSTokenType lex(JSToken*, OptionSet<LexerFlags>, bool strictMode);
|
---|
67 | JSTokenType lexWithoutClearingLineTerminator(JSToken*, OptionSet<LexerFlags>, bool strictMode);
|
---|
68 | bool nextTokenIsColon();
|
---|
69 | int lineNumber() const { return m_lineNumber; }
|
---|
70 | ALWAYS_INLINE int currentOffset() const { return offsetFromSourcePtr(m_code); }
|
---|
71 | ALWAYS_INLINE int currentLineStartOffset() const { return offsetFromSourcePtr(m_lineStart); }
|
---|
72 | ALWAYS_INLINE JSTextPosition currentPosition() const
|
---|
73 | {
|
---|
74 | return JSTextPosition(m_lineNumber, currentOffset(), currentLineStartOffset());
|
---|
75 | }
|
---|
76 | JSTextPosition positionBeforeLastNewline() const { return m_positionBeforeLastNewline; }
|
---|
77 | JSTokenLocation lastTokenLocation() const { return m_lastTokenLocation; }
|
---|
78 | void setLastLineNumber(int lastLineNumber) { m_lastLineNumber = lastLineNumber; }
|
---|
79 | int lastLineNumber() const { return m_lastLineNumber; }
|
---|
80 | bool hasLineTerminatorBeforeToken() const { return m_hasLineTerminatorBeforeToken; }
|
---|
81 | JSTokenType scanRegExp(JSToken*, UChar patternPrefix = 0);
|
---|
82 | enum class RawStringsBuildMode { BuildRawStrings, DontBuildRawStrings };
|
---|
83 | JSTokenType scanTemplateString(JSToken*, RawStringsBuildMode);
|
---|
84 |
|
---|
85 | // Functions for use after parsing.
|
---|
86 | bool sawError() const { return m_error; }
|
---|
87 | void setSawError(bool sawError) { m_error = sawError; }
|
---|
88 | String getErrorMessage() const { return m_lexErrorMessage; }
|
---|
89 | void setErrorMessage(const String& errorMessage) { m_lexErrorMessage = errorMessage; }
|
---|
90 | String sourceURLDirective() const { return m_sourceURLDirective; }
|
---|
91 | String sourceMappingURLDirective() const { return m_sourceMappingURLDirective; }
|
---|
92 | void clear();
|
---|
93 | void setOffset(int offset, int lineStartOffset)
|
---|
94 | {
|
---|
95 | m_error = 0;
|
---|
96 | m_lexErrorMessage = String();
|
---|
97 |
|
---|
98 | m_code = sourcePtrFromOffset(offset);
|
---|
99 | m_lineStart = sourcePtrFromOffset(lineStartOffset);
|
---|
100 | ASSERT(currentOffset() >= currentLineStartOffset());
|
---|
101 |
|
---|
102 | m_buffer8.shrink(0);
|
---|
103 | m_buffer16.shrink(0);
|
---|
104 | if (LIKELY(m_code < m_codeEnd))
|
---|
105 | m_current = *m_code;
|
---|
106 | else
|
---|
107 | m_current = 0;
|
---|
108 | }
|
---|
109 | void setLineNumber(int line)
|
---|
110 | {
|
---|
111 | ASSERT(line >= 0);
|
---|
112 | m_lineNumber = line;
|
---|
113 | }
|
---|
114 | void setHasLineTerminatorBeforeToken(bool terminator)
|
---|
115 | {
|
---|
116 | m_hasLineTerminatorBeforeToken = terminator;
|
---|
117 | }
|
---|
118 |
|
---|
119 | JSTokenType lexExpectIdentifier(JSToken*, OptionSet<LexerFlags>, bool strictMode);
|
---|
120 |
|
---|
121 | ALWAYS_INLINE StringView getToken(const JSToken& token)
|
---|
122 | {
|
---|
123 | SourceProvider* sourceProvider = m_source->provider();
|
---|
124 | ASSERT_WITH_MESSAGE(token.m_location.startOffset <= token.m_location.endOffset, "Calling this function with the baked token.");
|
---|
125 | return sourceProvider->getRange(token.m_location.startOffset, token.m_location.endOffset);
|
---|
126 | }
|
---|
127 |
|
---|
128 | size_t codeLength() { return m_codeEnd - m_codeStart; }
|
---|
129 |
|
---|
130 | private:
|
---|
131 | void record8(int);
|
---|
132 | void append8(const T*, size_t);
|
---|
133 | void record16(int);
|
---|
134 | void record16(T);
|
---|
135 | void recordUnicodeCodePoint(UChar32);
|
---|
136 | void append16(const LChar*, size_t);
|
---|
137 | void append16(const UChar* characters, size_t length) { m_buffer16.append(characters, length); }
|
---|
138 |
|
---|
139 | UChar32 currentCodePoint() const;
|
---|
140 | ALWAYS_INLINE void shift();
|
---|
141 | ALWAYS_INLINE bool atEnd() const;
|
---|
142 | ALWAYS_INLINE T peek(int offset) const;
|
---|
143 |
|
---|
144 | ParsedUnicodeEscapeValue parseUnicodeEscape();
|
---|
145 | void shiftLineTerminator();
|
---|
146 |
|
---|
147 | ALWAYS_INLINE int offsetFromSourcePtr(const T* ptr) const { return ptr - m_codeStart; }
|
---|
148 | ALWAYS_INLINE const T* sourcePtrFromOffset(int offset) const { return m_codeStart + offset; }
|
---|
149 |
|
---|
150 | String invalidCharacterMessage() const;
|
---|
151 | ALWAYS_INLINE const T* currentSourcePtr() const;
|
---|
152 |
|
---|
153 | ALWAYS_INLINE void setCodeStart(StringView);
|
---|
154 |
|
---|
155 | ALWAYS_INLINE const Identifier* makeIdentifier(const LChar* characters, size_t length);
|
---|
156 | ALWAYS_INLINE const Identifier* makeIdentifier(const UChar* characters, size_t length);
|
---|
157 | ALWAYS_INLINE const Identifier* makeLCharIdentifier(const LChar* characters, size_t length);
|
---|
158 | ALWAYS_INLINE const Identifier* makeLCharIdentifier(const UChar* characters, size_t length);
|
---|
159 | ALWAYS_INLINE const Identifier* makeRightSizedIdentifier(const UChar* characters, size_t length, UChar orAllChars);
|
---|
160 | ALWAYS_INLINE const Identifier* makeIdentifierLCharFromUChar(const UChar* characters, size_t length);
|
---|
161 | ALWAYS_INLINE const Identifier* makeEmptyIdentifier();
|
---|
162 |
|
---|
163 | ALWAYS_INLINE bool lastTokenWasRestrKeyword() const;
|
---|
164 |
|
---|
165 | ALWAYS_INLINE void skipWhitespace();
|
---|
166 |
|
---|
167 | template <int shiftAmount> void internalShift();
|
---|
168 | template <bool shouldCreateIdentifier> ALWAYS_INLINE JSTokenType parseKeyword(JSTokenData*);
|
---|
169 | template <bool shouldBuildIdentifiers> ALWAYS_INLINE JSTokenType parseIdentifier(JSTokenData*, OptionSet<LexerFlags>, bool strictMode);
|
---|
170 | template <bool shouldBuildIdentifiers> NEVER_INLINE JSTokenType parseIdentifierSlowCase(JSTokenData*, OptionSet<LexerFlags>, bool strictMode, const T* identifierStart);
|
---|
171 | enum StringParseResult {
|
---|
172 | StringParsedSuccessfully,
|
---|
173 | StringUnterminated,
|
---|
174 | StringCannotBeParsed
|
---|
175 | };
|
---|
176 | template <bool shouldBuildStrings> ALWAYS_INLINE StringParseResult parseString(JSTokenData*, bool strictMode);
|
---|
177 | template <bool shouldBuildStrings> NEVER_INLINE StringParseResult parseStringSlowCase(JSTokenData*, bool strictMode);
|
---|
178 |
|
---|
179 |
|
---|
180 | template <bool shouldBuildStrings> ALWAYS_INLINE StringParseResult parseComplexEscape(bool strictMode);
|
---|
181 | ALWAYS_INLINE StringParseResult parseTemplateLiteral(JSTokenData*, RawStringsBuildMode);
|
---|
182 |
|
---|
183 | using NumberParseResult = std::variant<double, const Identifier*>;
|
---|
184 | ALWAYS_INLINE std::optional<NumberParseResult> parseHex();
|
---|
185 | ALWAYS_INLINE std::optional<NumberParseResult> parseBinary();
|
---|
186 | ALWAYS_INLINE std::optional<NumberParseResult> parseOctal();
|
---|
187 | ALWAYS_INLINE std::optional<NumberParseResult> parseDecimal();
|
---|
188 | ALWAYS_INLINE bool parseNumberAfterDecimalPoint();
|
---|
189 | ALWAYS_INLINE bool parseNumberAfterExponentIndicator();
|
---|
190 | ALWAYS_INLINE bool parseMultilineComment();
|
---|
191 |
|
---|
192 | ALWAYS_INLINE void parseCommentDirective();
|
---|
193 | ALWAYS_INLINE String parseCommentDirectiveValue();
|
---|
194 |
|
---|
195 | template <unsigned length>
|
---|
196 | ALWAYS_INLINE bool consume(const char (&input)[length]);
|
---|
197 |
|
---|
198 | void fillTokenInfo(JSToken*, JSTokenType, int lineNumber, int endOffset, int lineStartOffset, JSTextPosition endPosition);
|
---|
199 |
|
---|
200 | static constexpr size_t initialReadBufferCapacity = 32;
|
---|
201 |
|
---|
202 | int m_lineNumber;
|
---|
203 | int m_lastLineNumber;
|
---|
204 |
|
---|
205 | Vector<LChar> m_buffer8;
|
---|
206 | Vector<UChar> m_buffer16;
|
---|
207 | Vector<UChar> m_bufferForRawTemplateString16;
|
---|
208 | bool m_hasLineTerminatorBeforeToken;
|
---|
209 | int m_lastToken;
|
---|
210 |
|
---|
211 | const SourceCode* m_source;
|
---|
212 | unsigned m_sourceOffset;
|
---|
213 | const T* m_code;
|
---|
214 | const T* m_codeStart;
|
---|
215 | const T* m_codeEnd;
|
---|
216 | const T* m_codeStartPlusOffset;
|
---|
217 | const T* m_lineStart;
|
---|
218 | JSTextPosition m_positionBeforeLastNewline;
|
---|
219 | JSTokenLocation m_lastTokenLocation;
|
---|
220 | bool m_isReparsingFunction;
|
---|
221 | bool m_atLineStart;
|
---|
222 | bool m_error;
|
---|
223 | String m_lexErrorMessage;
|
---|
224 |
|
---|
225 | String m_sourceURLDirective;
|
---|
226 | String m_sourceMappingURLDirective;
|
---|
227 |
|
---|
228 | T m_current;
|
---|
229 |
|
---|
230 | IdentifierArena* m_arena;
|
---|
231 |
|
---|
232 | VM& m_vm;
|
---|
233 | bool m_parsingBuiltinFunction;
|
---|
234 | JSParserScriptMode m_scriptMode;
|
---|
235 | };
|
---|
236 |
|
---|
237 | template <>
|
---|
238 | ALWAYS_INLINE bool Lexer<LChar>::isWhiteSpace(LChar ch)
|
---|
239 | {
|
---|
240 | return ch == ' ' || ch == '\t' || ch == 0xB || ch == 0xC || ch == 0xA0;
|
---|
241 | }
|
---|
242 |
|
---|
243 | template <>
|
---|
244 | ALWAYS_INLINE bool Lexer<UChar>::isWhiteSpace(UChar ch)
|
---|
245 | {
|
---|
246 | return isLatin1(ch) ? Lexer<LChar>::isWhiteSpace(static_cast<LChar>(ch)) : (u_charType(ch) == U_SPACE_SEPARATOR || ch == byteOrderMark);
|
---|
247 | }
|
---|
248 |
|
---|
249 | template <>
|
---|
250 | ALWAYS_INLINE bool Lexer<LChar>::isLineTerminator(LChar ch)
|
---|
251 | {
|
---|
252 | return ch == '\r' || ch == '\n';
|
---|
253 | }
|
---|
254 |
|
---|
255 | template <>
|
---|
256 | ALWAYS_INLINE bool Lexer<UChar>::isLineTerminator(UChar ch)
|
---|
257 | {
|
---|
258 | return ch == '\r' || ch == '\n' || (ch & ~1) == 0x2028;
|
---|
259 | }
|
---|
260 |
|
---|
261 | template <typename T>
|
---|
262 | inline unsigned char Lexer<T>::convertHex(int c1, int c2)
|
---|
263 | {
|
---|
264 | return (toASCIIHexValue(c1) << 4) | toASCIIHexValue(c2);
|
---|
265 | }
|
---|
266 |
|
---|
267 | template <typename T>
|
---|
268 | inline UChar Lexer<T>::convertUnicode(int c1, int c2, int c3, int c4)
|
---|
269 | {
|
---|
270 | return (convertHex(c1, c2) << 8) | convertHex(c3, c4);
|
---|
271 | }
|
---|
272 |
|
---|
273 | template <typename T>
|
---|
274 | ALWAYS_INLINE const Identifier* Lexer<T>::makeIdentifier(const LChar* characters, size_t length)
|
---|
275 | {
|
---|
276 | return &m_arena->makeIdentifier(m_vm, characters, length);
|
---|
277 | }
|
---|
278 |
|
---|
279 | template <typename T>
|
---|
280 | ALWAYS_INLINE const Identifier* Lexer<T>::makeIdentifier(const UChar* characters, size_t length)
|
---|
281 | {
|
---|
282 | return &m_arena->makeIdentifier(m_vm, characters, length);
|
---|
283 | }
|
---|
284 |
|
---|
285 | template <>
|
---|
286 | ALWAYS_INLINE const Identifier* Lexer<LChar>::makeRightSizedIdentifier(const UChar* characters, size_t length, UChar)
|
---|
287 | {
|
---|
288 | return &m_arena->makeIdentifierLCharFromUChar(m_vm, characters, length);
|
---|
289 | }
|
---|
290 |
|
---|
291 | template <>
|
---|
292 | ALWAYS_INLINE const Identifier* Lexer<UChar>::makeRightSizedIdentifier(const UChar* characters, size_t length, UChar orAllChars)
|
---|
293 | {
|
---|
294 | if (!(orAllChars & ~0xff))
|
---|
295 | return &m_arena->makeIdentifierLCharFromUChar(m_vm, characters, length);
|
---|
296 |
|
---|
297 | return &m_arena->makeIdentifier(m_vm, characters, length);
|
---|
298 | }
|
---|
299 |
|
---|
300 | template <typename T>
|
---|
301 | ALWAYS_INLINE const Identifier* Lexer<T>::makeEmptyIdentifier()
|
---|
302 | {
|
---|
303 | return &m_arena->makeEmptyIdentifier(m_vm);
|
---|
304 | }
|
---|
305 |
|
---|
306 | template <>
|
---|
307 | ALWAYS_INLINE void Lexer<LChar>::setCodeStart(StringView sourceString)
|
---|
308 | {
|
---|
309 | ASSERT(sourceString.is8Bit());
|
---|
310 | m_codeStart = sourceString.characters8();
|
---|
311 | }
|
---|
312 |
|
---|
313 | template <>
|
---|
314 | ALWAYS_INLINE void Lexer<UChar>::setCodeStart(StringView sourceString)
|
---|
315 | {
|
---|
316 | ASSERT(!sourceString.is8Bit());
|
---|
317 | m_codeStart = sourceString.characters16();
|
---|
318 | }
|
---|
319 |
|
---|
320 | template <typename T>
|
---|
321 | ALWAYS_INLINE const Identifier* Lexer<T>::makeIdentifierLCharFromUChar(const UChar* characters, size_t length)
|
---|
322 | {
|
---|
323 | return &m_arena->makeIdentifierLCharFromUChar(m_vm, characters, length);
|
---|
324 | }
|
---|
325 |
|
---|
326 | template <typename T>
|
---|
327 | ALWAYS_INLINE const Identifier* Lexer<T>::makeLCharIdentifier(const LChar* characters, size_t length)
|
---|
328 | {
|
---|
329 | return &m_arena->makeIdentifier(m_vm, characters, length);
|
---|
330 | }
|
---|
331 |
|
---|
332 | template <typename T>
|
---|
333 | ALWAYS_INLINE const Identifier* Lexer<T>::makeLCharIdentifier(const UChar* characters, size_t length)
|
---|
334 | {
|
---|
335 | return &m_arena->makeIdentifierLCharFromUChar(m_vm, characters, length);
|
---|
336 | }
|
---|
337 |
|
---|
338 | #if ASSERT_ENABLED
|
---|
339 | bool isSafeBuiltinIdentifier(VM&, const Identifier*);
|
---|
340 | #else
|
---|
341 | ALWAYS_INLINE bool isSafeBuiltinIdentifier(VM&, const Identifier*) { return true; }
|
---|
342 | #endif // ASSERT_ENABLED
|
---|
343 |
|
---|
344 | template <typename T>
|
---|
345 | ALWAYS_INLINE JSTokenType Lexer<T>::lexExpectIdentifier(JSToken* tokenRecord, OptionSet<LexerFlags> lexerFlags, bool strictMode)
|
---|
346 | {
|
---|
347 | JSTokenData* tokenData = &tokenRecord->m_data;
|
---|
348 | JSTokenLocation* tokenLocation = &tokenRecord->m_location;
|
---|
349 | ASSERT(lexerFlags.contains(LexerFlags::IgnoreReservedWords));
|
---|
350 | const T* start = m_code;
|
---|
351 | const T* ptr = start;
|
---|
352 | const T* end = m_codeEnd;
|
---|
353 | JSTextPosition startPosition = currentPosition();
|
---|
354 | if (ptr >= end) {
|
---|
355 | ASSERT(ptr == end);
|
---|
356 | goto slowCase;
|
---|
357 | }
|
---|
358 | if (!WTF::isASCIIAlpha(*ptr))
|
---|
359 | goto slowCase;
|
---|
360 | ++ptr;
|
---|
361 | while (ptr < end) {
|
---|
362 | if (!WTF::isASCIIAlphanumeric(*ptr))
|
---|
363 | break;
|
---|
364 | ++ptr;
|
---|
365 | }
|
---|
366 |
|
---|
367 | // Here's the shift
|
---|
368 | if (ptr < end) {
|
---|
369 | if ((!WTF::isASCII(*ptr)) || (*ptr == '\\') || (*ptr == '_') || (*ptr == '$'))
|
---|
370 | goto slowCase;
|
---|
371 | m_current = *ptr;
|
---|
372 | } else
|
---|
373 | m_current = 0;
|
---|
374 |
|
---|
375 | m_code = ptr;
|
---|
376 | ASSERT(currentOffset() >= currentLineStartOffset());
|
---|
377 |
|
---|
378 | // Create the identifier if needed
|
---|
379 | if (lexerFlags.contains(LexerFlags::DontBuildKeywords)
|
---|
380 | #if ASSERT_ENABLED
|
---|
381 | && !m_parsingBuiltinFunction
|
---|
382 | #endif
|
---|
383 | )
|
---|
384 | tokenData->ident = nullptr;
|
---|
385 | else
|
---|
386 | tokenData->ident = makeLCharIdentifier(start, ptr - start);
|
---|
387 |
|
---|
388 | tokenLocation->line = m_lineNumber;
|
---|
389 | tokenLocation->lineStartOffset = currentLineStartOffset();
|
---|
390 | tokenLocation->startOffset = offsetFromSourcePtr(start);
|
---|
391 | tokenLocation->endOffset = currentOffset();
|
---|
392 | ASSERT(tokenLocation->startOffset >= tokenLocation->lineStartOffset);
|
---|
393 | tokenRecord->m_startPosition = startPosition;
|
---|
394 | tokenRecord->m_endPosition = currentPosition();
|
---|
395 | #if ASSERT_ENABLED
|
---|
396 | if (m_parsingBuiltinFunction) {
|
---|
397 | if (!isSafeBuiltinIdentifier(m_vm, tokenData->ident))
|
---|
398 | return ERRORTOK;
|
---|
399 | }
|
---|
400 | #endif
|
---|
401 |
|
---|
402 | m_lastToken = IDENT;
|
---|
403 | return IDENT;
|
---|
404 |
|
---|
405 | slowCase:
|
---|
406 | return lex(tokenRecord, lexerFlags, strictMode);
|
---|
407 | }
|
---|
408 |
|
---|
409 | template <typename T>
|
---|
410 | ALWAYS_INLINE JSTokenType Lexer<T>::lex(JSToken* tokenRecord, OptionSet<LexerFlags> lexerFlags, bool strictMode)
|
---|
411 | {
|
---|
412 | m_hasLineTerminatorBeforeToken = false;
|
---|
413 | return lexWithoutClearingLineTerminator(tokenRecord, lexerFlags, strictMode);
|
---|
414 | }
|
---|
415 |
|
---|
416 | } // namespace JSC
|
---|