source: webkit/trunk/JavaScriptCore/kjs/lexer.h@ 28479

Last change on this file since 28479 was 27859, checked in by [email protected], 18 years ago

2007-11-16 Mark Rowe <[email protected]>

Reviewed by Eric.

Replace strings, identifier, buffer8 and buffer16 members of Lexer with vectors.
SunSpider claims this is a 0.7% speedup.

  • kjs/lexer.cpp: (KJS::Lexer::Lexer): (KJS::Lexer::lex): (KJS::Lexer::record8): (KJS::Lexer::record16): (KJS::Lexer::scanRegExp): (KJS::Lexer::clear): (KJS::Lexer::makeIdentifier): (KJS::Lexer::makeUString):
  • kjs/lexer.h:
  • kjs/ustring.cpp: (KJS::UString::UString): Add a convenience constructor that takes a const Vector<UChar>&.
  • kjs/ustring.h:
  • Property svn:eol-style set to native
File size: 4.1 KB
Line 
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 * Copyright (C) 2007 Apple Inc.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 *
22 */
23
24#ifndef Lexer_h
25#define Lexer_h
26
27#include "ustring.h"
28#include <wtf/Vector.h>
29
30namespace KJS {
31
32 class Identifier;
33 class RegExp;
34
35 class Lexer : Noncopyable {
36 public:
37 void setCode(const UString &sourceURL, int startingLineNumber, const UChar *c, unsigned int len);
38 int lex();
39
40 int lineNo() const { return yylineno; }
41 UString sourceURL() const { return m_sourceURL; }
42
43 bool prevTerminator() const { return terminator; }
44
45 enum State { Start,
46 IdentifierOrKeyword,
47 Identifier,
48 InIdentifierOrKeyword,
49 InIdentifier,
50 InIdentifierUnicodeEscapeStart,
51 InIdentifierUnicodeEscape,
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 const UString& pattern() const { return m_pattern; }
75 const UString& flags() const { return m_flags; }
76
77 static unsigned char convertHex(int);
78 static unsigned char convertHex(int c1, int c2);
79 static UChar convertUnicode(int c1, int c2, int c3, int c4);
80 static bool isIdentStart(int);
81 static bool isIdentPart(int);
82 static bool isHexDigit(int);
83
84 bool sawError() const { return error; }
85
86 void clear();
87
88 private:
89 friend Lexer& lexer();
90 Lexer();
91
92 int yylineno;
93 UString m_sourceURL;
94 bool done;
95 Vector<char> m_buffer8;
96 Vector<UChar> m_buffer16;
97 bool terminator;
98 bool restrKeyword;
99 // encountered delimiter like "'" and "}" on last run
100 bool delimited;
101 bool skipLF;
102 bool skipCR;
103 bool eatNextIdentifier;
104 int stackToken;
105 int lastToken;
106
107 State state;
108 void setDone(State);
109 unsigned int pos;
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 int matchPunctuator(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 KJS::Identifier* makeIdentifier(const Vector<UChar>& buffer);
127 UString* makeUString(const Vector<UChar>& buffer);
128
129 const UChar *code;
130 unsigned int length;
131 int yycolumn;
132#ifndef KJS_PURE_ECMA
133 int bol; // begin of line
134#endif
135 bool error;
136
137 // current and following unicode characters (int to allow for -1 for end-of-file marker)
138 int current, next1, next2, next3;
139
140 Vector<UString*> m_strings;
141 Vector<KJS::Identifier*> m_identifiers;
142
143 UString m_pattern;
144 UString m_flags;
145 };
146
147 Lexer& lexer(); // Returns the singletone JavaScript lexer.
148
149} // namespace KJS
150
151#endif // Lexer_h
Note: See TracBrowser for help on using the repository browser.