1 | /*
|
---|
2 | * Copyright (C) 1999-2000 Harri Porten ([email protected])
|
---|
3 | * Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
|
---|
4 | *
|
---|
5 | * This library is free software; you can redistribute it and/or
|
---|
6 | * modify it under the terms of the GNU Library General Public
|
---|
7 | * License as published by the Free Software Foundation; either
|
---|
8 | * version 2 of the License, or (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This library is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
13 | * Library General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU Library General Public License
|
---|
16 | * along with this library; see the file COPYING.LIB. If not, write to
|
---|
17 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
---|
18 | * Boston, MA 02110-1301, USA.
|
---|
19 | *
|
---|
20 | */
|
---|
21 |
|
---|
22 | #ifndef Lexer_h
|
---|
23 | #define Lexer_h
|
---|
24 |
|
---|
25 | #include "Identifier.h"
|
---|
26 | #include "Lookup.h"
|
---|
27 | #include "SegmentedVector.h"
|
---|
28 | #include "SourceCode.h"
|
---|
29 | #include <wtf/Vector.h>
|
---|
30 | #include <wtf/unicode/Unicode.h>
|
---|
31 |
|
---|
32 | namespace JSC {
|
---|
33 |
|
---|
34 | class RegExp;
|
---|
35 |
|
---|
36 | class Lexer : Noncopyable {
|
---|
37 | public:
|
---|
38 | void setCode(const SourceCode&);
|
---|
39 | void setIsReparsing() { m_isReparsing = true; }
|
---|
40 | int lex(void* lvalp, void* llocp);
|
---|
41 |
|
---|
42 | int lineNo() const { return yylineno; }
|
---|
43 |
|
---|
44 | bool prevTerminator() const { return m_terminator; }
|
---|
45 |
|
---|
46 | enum State {
|
---|
47 | Start,
|
---|
48 | IdentifierOrKeyword,
|
---|
49 | Identifier,
|
---|
50 | InIdentifierOrKeyword,
|
---|
51 | InIdentifier,
|
---|
52 | InIdentifierStartUnicodeEscapeStart,
|
---|
53 | InIdentifierStartUnicodeEscape,
|
---|
54 | InIdentifierPartUnicodeEscapeStart,
|
---|
55 | InIdentifierPartUnicodeEscape,
|
---|
56 | InSingleLineComment,
|
---|
57 | InMultiLineComment,
|
---|
58 | InNum,
|
---|
59 | InNum0,
|
---|
60 | InHex,
|
---|
61 | InOctal,
|
---|
62 | InDecimal,
|
---|
63 | InExponentIndicator,
|
---|
64 | InExponent,
|
---|
65 | Hex,
|
---|
66 | Octal,
|
---|
67 | Number,
|
---|
68 | String,
|
---|
69 | Eof,
|
---|
70 | InString,
|
---|
71 | InEscapeSequence,
|
---|
72 | InHexEscape,
|
---|
73 | InUnicodeEscape,
|
---|
74 | Other,
|
---|
75 | Bad
|
---|
76 | };
|
---|
77 |
|
---|
78 | bool scanRegExp();
|
---|
79 | const UString& pattern() const { return m_pattern; }
|
---|
80 | const UString& flags() const { return m_flags; }
|
---|
81 |
|
---|
82 | static unsigned char convertHex(int);
|
---|
83 | static unsigned char convertHex(int c1, int c2);
|
---|
84 | static UChar convertUnicode(int c1, int c2, int c3, int c4);
|
---|
85 | static bool isIdentStart(int);
|
---|
86 | static bool isIdentPart(int);
|
---|
87 | static bool isHexDigit(int);
|
---|
88 |
|
---|
89 | bool sawError() const { return m_error; }
|
---|
90 |
|
---|
91 | void clear();
|
---|
92 | SourceCode sourceCode(int openBrace, int closeBrace, int firstLine) { return SourceCode(m_source->provider(), openBrace, closeBrace + 1, firstLine); }
|
---|
93 |
|
---|
94 | static inline bool isWhiteSpace(int ch)
|
---|
95 | {
|
---|
96 | return ch == '\t' || ch == 0x0b || ch == 0x0c || WTF::Unicode::isSeparatorSpace(ch);
|
---|
97 | }
|
---|
98 |
|
---|
99 | static inline bool isLineTerminator(int ch)
|
---|
100 | {
|
---|
101 | return ch == '\r' || ch == '\n' || ch == 0x2028 || ch == 0x2029;
|
---|
102 | }
|
---|
103 |
|
---|
104 | private:
|
---|
105 | friend class JSGlobalData;
|
---|
106 | Lexer(JSGlobalData*);
|
---|
107 | ~Lexer();
|
---|
108 |
|
---|
109 | void setDone(State);
|
---|
110 | void shift(unsigned int p);
|
---|
111 | void nextLine();
|
---|
112 | int lookupKeyword(const char *);
|
---|
113 |
|
---|
114 | bool isWhiteSpace() const;
|
---|
115 | bool isLineTerminator();
|
---|
116 | static bool isOctalDigit(int);
|
---|
117 |
|
---|
118 | ALWAYS_INLINE int matchPunctuator(int& charPos, int c1, int c2, int c3, int c4);
|
---|
119 | static unsigned short singleEscape(unsigned short);
|
---|
120 | static unsigned short convertOctal(int c1, int c2, int c3);
|
---|
121 |
|
---|
122 | void record8(int);
|
---|
123 | void record16(int);
|
---|
124 | void record16(UChar);
|
---|
125 |
|
---|
126 | JSC::Identifier* makeIdentifier(const Vector<UChar>& buffer)
|
---|
127 | {
|
---|
128 | m_identifiers.append(JSC::Identifier(m_globalData, buffer.data(), buffer.size()));
|
---|
129 | return &m_identifiers.last();
|
---|
130 | }
|
---|
131 |
|
---|
132 | static const size_t initialReadBufferCapacity = 32;
|
---|
133 | static const size_t initialIdentifierTableCapacity = 64;
|
---|
134 |
|
---|
135 | int yylineno;
|
---|
136 | int yycolumn;
|
---|
137 |
|
---|
138 | bool m_done;
|
---|
139 | Vector<char> m_buffer8;
|
---|
140 | Vector<UChar> m_buffer16;
|
---|
141 | bool m_terminator;
|
---|
142 | bool m_restrKeyword;
|
---|
143 | bool m_delimited; // encountered delimiter like "'" and "}" on last run
|
---|
144 | bool m_skipLF;
|
---|
145 | bool m_skipCR;
|
---|
146 | bool m_eatNextIdentifier;
|
---|
147 | int m_stackToken;
|
---|
148 | int m_lastToken;
|
---|
149 |
|
---|
150 | State m_state;
|
---|
151 | unsigned int m_position;
|
---|
152 | const SourceCode* m_source;
|
---|
153 | const UChar* m_code;
|
---|
154 | unsigned int m_length;
|
---|
155 | bool m_isReparsing;
|
---|
156 | int m_atLineStart;
|
---|
157 | bool m_error;
|
---|
158 |
|
---|
159 | // current and following unicode characters (int to allow for -1 for end-of-file marker)
|
---|
160 | int m_current;
|
---|
161 | int m_next1;
|
---|
162 | int m_next2;
|
---|
163 | int m_next3;
|
---|
164 |
|
---|
165 | int m_currentOffset;
|
---|
166 | int m_nextOffset1;
|
---|
167 | int m_nextOffset2;
|
---|
168 | int m_nextOffset3;
|
---|
169 |
|
---|
170 | SegmentedVector<JSC::Identifier, initialIdentifierTableCapacity> m_identifiers;
|
---|
171 |
|
---|
172 | JSGlobalData* m_globalData;
|
---|
173 |
|
---|
174 | UString m_pattern;
|
---|
175 | UString m_flags;
|
---|
176 |
|
---|
177 | const HashTable m_mainTable;
|
---|
178 | };
|
---|
179 |
|
---|
180 | } // namespace JSC
|
---|
181 |
|
---|
182 | #endif // Lexer_h
|
---|