1 | /*
|
---|
2 | * This file is part of the KDE libraries
|
---|
3 | * Copyright (C) 1999-2000 Harri Porten ([email protected])
|
---|
4 | * Copyright (C) 2007 Apple Inc.
|
---|
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 | #ifndef Lexer_h
|
---|
24 | #define Lexer_h
|
---|
25 |
|
---|
26 | #include "Lookup.h"
|
---|
27 | #include "UString.h"
|
---|
28 | #include <wtf/Vector.h>
|
---|
29 | #include "SourceCode.h"
|
---|
30 |
|
---|
31 | namespace JSC {
|
---|
32 |
|
---|
33 | class Identifier;
|
---|
34 | class RegExp;
|
---|
35 |
|
---|
36 | class Lexer : Noncopyable {
|
---|
37 | public:
|
---|
38 | void setCode(const SourceCode&);
|
---|
39 | int lex(void* lvalp, void* llocp);
|
---|
40 |
|
---|
41 | int lineNo() const { return yylineno; }
|
---|
42 |
|
---|
43 | bool prevTerminator() const { return m_terminator; }
|
---|
44 |
|
---|
45 | enum State {
|
---|
46 | Start,
|
---|
47 | IdentifierOrKeyword,
|
---|
48 | Identifier,
|
---|
49 | InIdentifierOrKeyword,
|
---|
50 | InIdentifier,
|
---|
51 | InIdentifierStartUnicodeEscapeStart,
|
---|
52 | InIdentifierStartUnicodeEscape,
|
---|
53 | InIdentifierPartUnicodeEscapeStart,
|
---|
54 | InIdentifierPartUnicodeEscape,
|
---|
55 | InSingleLineComment,
|
---|
56 | InMultiLineComment,
|
---|
57 | InNum,
|
---|
58 | InNum0,
|
---|
59 | InHex,
|
---|
60 | InOctal,
|
---|
61 | InDecimal,
|
---|
62 | InExponentIndicator,
|
---|
63 | InExponent,
|
---|
64 | Hex,
|
---|
65 | Octal,
|
---|
66 | Number,
|
---|
67 | String,
|
---|
68 | Eof,
|
---|
69 | InString,
|
---|
70 | InEscapeSequence,
|
---|
71 | InHexEscape,
|
---|
72 | InUnicodeEscape,
|
---|
73 | Other,
|
---|
74 | Bad
|
---|
75 | };
|
---|
76 |
|
---|
77 | bool scanRegExp();
|
---|
78 | const UString& pattern() const { return m_pattern; }
|
---|
79 | const UString& flags() const { return m_flags; }
|
---|
80 |
|
---|
81 | static unsigned char convertHex(int);
|
---|
82 | static unsigned char convertHex(int c1, int c2);
|
---|
83 | static UChar convertUnicode(int c1, int c2, int c3, int c4);
|
---|
84 | static bool isIdentStart(int);
|
---|
85 | static bool isIdentPart(int);
|
---|
86 | static bool isHexDigit(int);
|
---|
87 |
|
---|
88 | bool sawError() const { return m_error; }
|
---|
89 |
|
---|
90 | void clear();
|
---|
91 | SourceCode sourceCode(int openBrace, int closeBrace, int firstLine) { return SourceCode(m_source->provider(), m_source->startOffset() + openBrace + 1, m_source->startOffset() + closeBrace, firstLine); }
|
---|
92 |
|
---|
93 | private:
|
---|
94 | friend class JSGlobalData;
|
---|
95 | Lexer(JSGlobalData*);
|
---|
96 | ~Lexer();
|
---|
97 |
|
---|
98 | void setDone(State);
|
---|
99 | void shift(unsigned int p);
|
---|
100 | void nextLine();
|
---|
101 | int lookupKeyword(const char *);
|
---|
102 |
|
---|
103 | bool isWhiteSpace() const;
|
---|
104 | bool isLineTerminator();
|
---|
105 | static bool isOctalDigit(int);
|
---|
106 |
|
---|
107 | int matchPunctuator(int& charPos, int c1, int c2, int c3, int c4);
|
---|
108 | static unsigned short singleEscape(unsigned short);
|
---|
109 | static unsigned short convertOctal(int c1, int c2, int c3);
|
---|
110 |
|
---|
111 | void record8(int);
|
---|
112 | void record16(int);
|
---|
113 | void record16(UChar);
|
---|
114 |
|
---|
115 | JSC::Identifier* makeIdentifier(const Vector<UChar>& buffer);
|
---|
116 |
|
---|
117 | int yylineno;
|
---|
118 | int yycolumn;
|
---|
119 |
|
---|
120 | bool m_done;
|
---|
121 | Vector<char> m_buffer8;
|
---|
122 | Vector<UChar> m_buffer16;
|
---|
123 | bool m_terminator;
|
---|
124 | bool m_restrKeyword;
|
---|
125 | bool m_delimited; // encountered delimiter like "'" and "}" on last run
|
---|
126 | bool m_skipLF;
|
---|
127 | bool m_skipCR;
|
---|
128 | bool m_eatNextIdentifier;
|
---|
129 | int m_stackToken;
|
---|
130 | int m_lastToken;
|
---|
131 |
|
---|
132 | State m_state;
|
---|
133 | unsigned int m_position;
|
---|
134 | const SourceCode* m_source;
|
---|
135 | const UChar* m_code;
|
---|
136 | unsigned int m_length;
|
---|
137 | int m_atLineStart;
|
---|
138 | bool m_error;
|
---|
139 |
|
---|
140 | // current and following unicode characters (int to allow for -1 for end-of-file marker)
|
---|
141 | int m_current;
|
---|
142 | int m_next1;
|
---|
143 | int m_next2;
|
---|
144 | int m_next3;
|
---|
145 |
|
---|
146 | int m_currentOffset;
|
---|
147 | int m_nextOffset1;
|
---|
148 | int m_nextOffset2;
|
---|
149 | int m_nextOffset3;
|
---|
150 |
|
---|
151 | Vector<UString*> m_strings;
|
---|
152 | Vector<JSC::Identifier*> m_identifiers;
|
---|
153 |
|
---|
154 | JSGlobalData* m_globalData;
|
---|
155 |
|
---|
156 | UString m_pattern;
|
---|
157 | UString m_flags;
|
---|
158 |
|
---|
159 | const HashTable m_mainTable;
|
---|
160 | };
|
---|
161 |
|
---|
162 | } // namespace JSC
|
---|
163 |
|
---|
164 | #endif // Lexer_h
|
---|