2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3 * Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
4 * Copyright (C) 2010 Zoltan Herczeg (zherczeg@inf.u-szeged.hu)
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.
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.
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.
28 #include "ParserArena.h"
29 #include "SourceCode.h"
30 #include <wtf/ASCIICType.h>
31 #include <wtf/AlwaysInline.h>
32 #include <wtf/SegmentedVector.h>
33 #include <wtf/Vector.h>
34 #include <wtf/unicode/Unicode.h>
41 WTF_MAKE_NONCOPYABLE(Lexer); WTF_MAKE_FAST_ALLOCATED;
43 // Character manipulation functions.
44 static bool isWhiteSpace(int character);
45 static bool isLineTerminator(int character);
46 static unsigned char convertHex(int c1, int c2);
47 static UChar convertUnicode(int c1, int c2, int c3, int c4);
49 // Functions to set up parsing.
50 void setCode(const SourceCode&, ParserArena&);
51 void setIsReparsing() { m_isReparsing = true; }
52 bool isReparsing() const { return m_isReparsing; }
54 // Functions for the parser itself.
56 IgnoreReservedWords = 1,
60 JSTokenType lex(JSTokenData*, JSTokenInfo*, unsigned, bool strictMode);
61 bool nextTokenIsColon();
62 int lineNumber() const { return m_lineNumber; }
63 void setLastLineNumber(int lastLineNumber) { m_lastLineNumber = lastLineNumber; }
64 int lastLineNumber() const { return m_lastLineNumber; }
65 bool prevTerminator() const { return m_terminator; }
66 SourceCode sourceCode(int openBrace, int closeBrace, int firstLine);
67 bool scanRegExp(const Identifier*& pattern, const Identifier*& flags, UChar patternPrefix = 0);
70 // Functions for use after parsing.
71 bool sawError() const { return m_error; }
72 UString getErrorMessage() const { return m_lexErrorMessage; }
74 int currentOffset() { return m_code - m_codeStart; }
75 void setOffset(int offset)
78 m_lexErrorMessage = UString();
79 m_code = m_codeStart + offset;
82 // Faster than an if-else sequence
84 if (LIKELY(m_code < m_codeEnd))
87 void setLineNumber(int line)
92 SourceProvider* sourceProvider() const { return m_source->provider(); }
94 JSTokenType lexExpectIdentifier(JSTokenData*, JSTokenInfo*, unsigned, bool strictMode);
96 bool isKeyword(const Identifier&);
99 friend class JSGlobalData;
101 Lexer(JSGlobalData*);
106 void record16(UChar);
108 void copyCodeWithoutBOMs();
110 ALWAYS_INLINE void shift();
111 ALWAYS_INLINE int peek(int offset);
112 int getUnicodeCharacter();
113 void shiftLineTerminator();
115 UString getInvalidCharMessage();
116 ALWAYS_INLINE const UChar* currentCharacter() const;
117 ALWAYS_INLINE int currentOffset() const;
119 ALWAYS_INLINE const Identifier* makeIdentifier(const UChar* characters, size_t length);
121 ALWAYS_INLINE bool lastTokenWasRestrKeyword() const;
123 enum ShiftType { DoBoundsCheck, DoNotBoundsCheck };
124 template <int shiftAmount, ShiftType shouldBoundsCheck> void internalShift();
125 template <bool shouldCreateIdentifier> ALWAYS_INLINE JSTokenType parseKeyword(JSTokenData*);
126 template <bool shouldBuildIdentifiers> ALWAYS_INLINE JSTokenType parseIdentifier(JSTokenData*, unsigned, bool strictMode);
127 template <bool shouldBuildStrings> ALWAYS_INLINE bool parseString(JSTokenData*, bool strictMode);
128 ALWAYS_INLINE void parseHex(double& returnValue);
129 ALWAYS_INLINE bool parseOctal(double& returnValue);
130 ALWAYS_INLINE bool parseDecimal(double& returnValue);
131 ALWAYS_INLINE void parseNumberAfterDecimalPoint();
132 ALWAYS_INLINE bool parseNumberAfterExponentIndicator();
133 ALWAYS_INLINE bool parseMultilineComment();
135 static const size_t initialReadBufferCapacity = 32;
138 int m_lastLineNumber;
140 Vector<char> m_buffer8;
141 Vector<UChar> m_buffer16;
143 bool m_delimited; // encountered delimiter like "'" and "}" on last run
146 const SourceCode* m_source;
148 const UChar* m_codeStart;
149 const UChar* m_codeEnd;
153 UString m_lexErrorMessage;
155 // current and following unicode characters (int to allow for -1 for end-of-file marker)
158 IdentifierArena* m_arena;
160 JSGlobalData* m_globalData;
162 const HashTable m_keywordTable;
165 ALWAYS_INLINE bool Lexer::isWhiteSpace(int ch)
167 return isASCII(ch) ? (ch == ' ' || ch == '\t' || ch == 0xB || ch == 0xC) : (WTF::Unicode::isSeparatorSpace(ch) || ch == 0xFEFF);
170 ALWAYS_INLINE bool Lexer::isLineTerminator(int ch)
172 return ch == '\r' || ch == '\n' || (ch & ~1) == 0x2028;
175 inline unsigned char Lexer::convertHex(int c1, int c2)
177 return (toASCIIHexValue(c1) << 4) | toASCIIHexValue(c2);
180 inline UChar Lexer::convertUnicode(int c1, int c2, int c3, int c4)
182 return (convertHex(c1, c2) << 8) | convertHex(c3, c4);
185 ALWAYS_INLINE const Identifier* Lexer::makeIdentifier(const UChar* characters, size_t length)
187 return &m_arena->makeIdentifier(m_globalData, characters, length);
190 ALWAYS_INLINE JSTokenType Lexer::lexExpectIdentifier(JSTokenData* tokenData, JSTokenInfo* tokenInfo, unsigned lexType, bool strictMode)
192 ASSERT((lexType & IgnoreReservedWords));
193 const UChar* start = m_code;
194 const UChar* ptr = start;
195 const UChar* end = m_codeEnd;
200 if (!WTF::isASCIIAlpha(*ptr))
204 if (!WTF::isASCIIAlphanumeric(*ptr))
211 if ((!WTF::isASCII(*ptr)) || (*ptr == '\\') || (*ptr == '_') || (*ptr == '$'))
219 // Create the identifier if needed
220 if (lexType & DontBuildKeywords)
221 tokenData->ident = 0;
223 tokenData->ident = makeIdentifier(start, ptr - start);
224 tokenInfo->line = m_lineNumber;
225 tokenInfo->startOffset = start - m_codeStart;
226 tokenInfo->endOffset = currentOffset();
231 return lex(tokenData, tokenInfo, lexType, strictMode);