1 | // -*- c-basic-offset: 2 -*-
|
---|
2 | /*
|
---|
3 | * This file is part of the KDE libraries
|
---|
4 | * Copyright (C) 1999-2000 Harri Porten ([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 Steet, Fifth Floor,
|
---|
19 | * Boston, MA 02110-1301, USA.
|
---|
20 | *
|
---|
21 | */
|
---|
22 |
|
---|
23 | #ifndef _KJSLEXER_H_
|
---|
24 | #define _KJSLEXER_H_
|
---|
25 |
|
---|
26 | #include "ustring.h"
|
---|
27 |
|
---|
28 |
|
---|
29 | namespace KJS {
|
---|
30 |
|
---|
31 | class Identifier;
|
---|
32 |
|
---|
33 | class RegExp;
|
---|
34 |
|
---|
35 | class Lexer {
|
---|
36 | public:
|
---|
37 | Lexer();
|
---|
38 | ~Lexer();
|
---|
39 | static Lexer *curr();
|
---|
40 |
|
---|
41 | void setCode(const UString &sourceURL, int startingLineNumber, const UChar *c, unsigned int len);
|
---|
42 | int lex();
|
---|
43 |
|
---|
44 | int lineNo() const { return yylineno; }
|
---|
45 | UString sourceURL() const { return m_sourceURL; }
|
---|
46 |
|
---|
47 | bool prevTerminator() const { return terminator; }
|
---|
48 |
|
---|
49 | enum State { Start,
|
---|
50 | Identifier,
|
---|
51 | InIdentifier,
|
---|
52 | InSingleLineComment,
|
---|
53 | InMultiLineComment,
|
---|
54 | InNum,
|
---|
55 | InNum0,
|
---|
56 | InHex,
|
---|
57 | InOctal,
|
---|
58 | InDecimal,
|
---|
59 | InExponentIndicator,
|
---|
60 | InExponent,
|
---|
61 | Hex,
|
---|
62 | Octal,
|
---|
63 | Number,
|
---|
64 | String,
|
---|
65 | Eof,
|
---|
66 | InString,
|
---|
67 | InEscapeSequence,
|
---|
68 | InHexEscape,
|
---|
69 | InUnicodeEscape,
|
---|
70 | Other,
|
---|
71 | Bad };
|
---|
72 |
|
---|
73 | bool scanRegExp();
|
---|
74 | UString pattern, flags;
|
---|
75 |
|
---|
76 | private:
|
---|
77 | int yylineno;
|
---|
78 | UString m_sourceURL;
|
---|
79 | bool done;
|
---|
80 | char *buffer8;
|
---|
81 | UChar *buffer16;
|
---|
82 | unsigned int size8, size16;
|
---|
83 | unsigned int pos8, pos16;
|
---|
84 | bool terminator;
|
---|
85 | bool restrKeyword;
|
---|
86 | // encountered delimiter like "'" and "}" on last run
|
---|
87 | bool delimited;
|
---|
88 | bool skipLF;
|
---|
89 | bool skipCR;
|
---|
90 | bool eatNextIdentifier;
|
---|
91 | int stackToken;
|
---|
92 | int lastToken;
|
---|
93 |
|
---|
94 | State state;
|
---|
95 | void setDone(State s);
|
---|
96 | unsigned int pos;
|
---|
97 | void shift(unsigned int p);
|
---|
98 | void nextLine();
|
---|
99 | int lookupKeyword(const char *);
|
---|
100 |
|
---|
101 | bool isWhiteSpace() const;
|
---|
102 | bool isLineTerminator();
|
---|
103 | bool isOctalDigit(unsigned short c) const;
|
---|
104 |
|
---|
105 | int matchPunctuator(unsigned short c1, unsigned short c2,
|
---|
106 | unsigned short c3, unsigned short c4);
|
---|
107 | unsigned short singleEscape(unsigned short c) const;
|
---|
108 | unsigned short convertOctal(unsigned short c1, unsigned short c2,
|
---|
109 | unsigned short c3) const;
|
---|
110 | public:
|
---|
111 | static unsigned char convertHex(unsigned short c1);
|
---|
112 | static unsigned char convertHex(unsigned short c1, unsigned short c2);
|
---|
113 | static UChar convertUnicode(unsigned short c1, unsigned short c2,
|
---|
114 | unsigned short c3, unsigned short c4);
|
---|
115 | static bool isIdentLetter(unsigned short c);
|
---|
116 | static bool isDecimalDigit(unsigned short c);
|
---|
117 | static bool isHexDigit(unsigned short c);
|
---|
118 |
|
---|
119 | #ifdef KJS_DEBUG_MEM
|
---|
120 | /**
|
---|
121 | * Clear statically allocated resources
|
---|
122 | */
|
---|
123 | static void globalClear();
|
---|
124 | #endif
|
---|
125 |
|
---|
126 | bool sawError() const { return error; }
|
---|
127 | void doneParsing();
|
---|
128 |
|
---|
129 | private:
|
---|
130 |
|
---|
131 | void record8(unsigned short c);
|
---|
132 | void record16(UChar c);
|
---|
133 |
|
---|
134 | KJS::Identifier *makeIdentifier(UChar *buffer, unsigned int pos);
|
---|
135 | UString *makeUString(UChar *buffer, unsigned int pos);
|
---|
136 |
|
---|
137 | const UChar *code;
|
---|
138 | unsigned int length;
|
---|
139 | int yycolumn;
|
---|
140 | #ifndef KJS_PURE_ECMA
|
---|
141 | int bol; // begin of line
|
---|
142 | #endif
|
---|
143 | bool error;
|
---|
144 |
|
---|
145 | // current and following unicode characters
|
---|
146 | unsigned short current, next1, next2, next3;
|
---|
147 |
|
---|
148 | UString **strings;
|
---|
149 | unsigned int numStrings;
|
---|
150 | unsigned int stringsCapacity;
|
---|
151 |
|
---|
152 | KJS::Identifier **identifiers;
|
---|
153 | unsigned int numIdentifiers;
|
---|
154 | unsigned int identifiersCapacity;
|
---|
155 |
|
---|
156 | // for future extensions
|
---|
157 | class LexerPrivate;
|
---|
158 | LexerPrivate *priv;
|
---|
159 | };
|
---|
160 |
|
---|
161 | } // namespace
|
---|
162 |
|
---|
163 | #endif
|
---|