]> git.saurik.com Git - apple/javascriptcore.git/blob - parser/SyntaxChecker.h
JavaScriptCore-1097.13.tar.gz
[apple/javascriptcore.git] / parser / SyntaxChecker.h
1 /*
2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #ifndef SyntaxChecker_h
27 #define SyntaxChecker_h
28
29 #include "Lexer.h"
30 #include <yarr/YarrSyntaxChecker.h>
31
32 namespace JSC {
33
34 class SyntaxChecker {
35 public:
36 struct BinaryExprContext {
37 BinaryExprContext(SyntaxChecker& context)
38 : m_context(&context)
39 {
40 m_context->m_topBinaryExprs.append(m_context->m_topBinaryExpr);
41 m_context->m_topBinaryExpr = 0;
42 }
43 ~BinaryExprContext()
44 {
45 m_context->m_topBinaryExpr = m_context->m_topBinaryExprs.last();
46 m_context->m_topBinaryExprs.removeLast();
47 }
48 private:
49 SyntaxChecker* m_context;
50 };
51 struct UnaryExprContext {
52 UnaryExprContext(SyntaxChecker& context)
53 : m_context(&context)
54 {
55 m_context->m_topUnaryTokens.append(m_context->m_topUnaryToken);
56 m_context->m_topUnaryToken = 0;
57 }
58 ~UnaryExprContext()
59 {
60 m_context->m_topUnaryToken = m_context->m_topUnaryTokens.last();
61 m_context->m_topUnaryTokens.removeLast();
62 }
63 private:
64 SyntaxChecker* m_context;
65 };
66
67 SyntaxChecker(JSGlobalData* , void*)
68 {
69 }
70
71 typedef SyntaxChecker FunctionBodyBuilder;
72 enum { NoneExpr = 0,
73 ResolveEvalExpr, ResolveExpr, NumberExpr, StringExpr,
74 ThisExpr, NullExpr, BoolExpr, RegExpExpr, ObjectLiteralExpr,
75 FunctionExpr, BracketExpr, DotExpr, CallExpr,
76 NewExpr, PreExpr, PostExpr, UnaryExpr, BinaryExpr,
77 ConditionalExpr, AssignmentExpr, TypeofExpr,
78 DeleteExpr, ArrayLiteralExpr };
79 typedef int ExpressionType;
80
81 typedef ExpressionType Expression;
82 typedef int SourceElements;
83 typedef int Arguments;
84 typedef ExpressionType Comma;
85 struct Property {
86 ALWAYS_INLINE Property(void* = 0)
87 : type((PropertyNode::Type)0)
88 {
89 }
90 ALWAYS_INLINE Property(const Identifier* ident, PropertyNode::Type ty)
91 : name(ident)
92 , type(ty)
93 {
94 }
95 ALWAYS_INLINE Property(PropertyNode::Type ty)
96 : name(0)
97 , type(ty)
98 {
99 }
100 ALWAYS_INLINE bool operator!() { return !type; }
101 const Identifier* name;
102 PropertyNode::Type type;
103 };
104 typedef int PropertyList;
105 typedef int ElementList;
106 typedef int ArgumentsList;
107 typedef int FormalParameterList;
108 typedef int FunctionBody;
109 typedef int Statement;
110 typedef int ClauseList;
111 typedef int Clause;
112 typedef int ConstDeclList;
113 typedef int BinaryOperand;
114
115 static const bool CreatesAST = false;
116 static const bool NeedsFreeVariableInfo = false;
117 static const bool CanUseFunctionCache = true;
118 static const unsigned DontBuildKeywords = LexexFlagsDontBuildKeywords;
119 static const unsigned DontBuildStrings = LexerFlagsDontBuildStrings;
120
121 int createSourceElements() { return 1; }
122 ExpressionType makeFunctionCallNode(int, int, int, int, int, int) { return CallExpr; }
123 void appendToComma(ExpressionType& base, ExpressionType right) { base = right; }
124 ExpressionType createCommaExpr(int, ExpressionType, ExpressionType right) { return right; }
125 ExpressionType makeAssignNode(int, ExpressionType, Operator, ExpressionType, bool, bool, int, int, int) { return AssignmentExpr; }
126 ExpressionType makePrefixNode(int, ExpressionType, Operator, int, int, int) { return PreExpr; }
127 ExpressionType makePostfixNode(int, ExpressionType, Operator, int, int, int) { return PostExpr; }
128 ExpressionType makeTypeOfNode(int, ExpressionType) { return TypeofExpr; }
129 ExpressionType makeDeleteNode(int, ExpressionType, int, int, int) { return DeleteExpr; }
130 ExpressionType makeNegateNode(int, ExpressionType) { return UnaryExpr; }
131 ExpressionType makeBitwiseNotNode(int, ExpressionType) { return UnaryExpr; }
132 ExpressionType createLogicalNot(int, ExpressionType) { return UnaryExpr; }
133 ExpressionType createUnaryPlus(int, ExpressionType) { return UnaryExpr; }
134 ExpressionType createVoid(int, ExpressionType) { return UnaryExpr; }
135 ExpressionType thisExpr(int) { return ThisExpr; }
136 ExpressionType createResolve(int, const Identifier*, int) { return ResolveExpr; }
137 ExpressionType createObjectLiteral(int) { return ObjectLiteralExpr; }
138 ExpressionType createObjectLiteral(int, int) { return ObjectLiteralExpr; }
139 ExpressionType createArray(int, int) { return ArrayLiteralExpr; }
140 ExpressionType createArray(int, int, int) { return ArrayLiteralExpr; }
141 ExpressionType createNumberExpr(int, double) { return NumberExpr; }
142 ExpressionType createString(int, const Identifier*) { return StringExpr; }
143 ExpressionType createBoolean(int, bool) { return BoolExpr; }
144 ExpressionType createNull(int) { return NullExpr; }
145 ExpressionType createBracketAccess(int, ExpressionType, ExpressionType, bool, int, int, int) { return BracketExpr; }
146 ExpressionType createDotAccess(int, ExpressionType, const Identifier*, int, int, int) { return DotExpr; }
147 ExpressionType createRegExp(int, const Identifier& pattern, const Identifier&, int) { return Yarr::checkSyntax(pattern.ustring()) ? 0 : RegExpExpr; }
148 ExpressionType createNewExpr(int, ExpressionType, int, int, int, int) { return NewExpr; }
149 ExpressionType createNewExpr(int, ExpressionType, int, int) { return NewExpr; }
150 ExpressionType createConditionalExpr(int, ExpressionType, ExpressionType, ExpressionType) { return ConditionalExpr; }
151 ExpressionType createAssignResolve(int, const Identifier&, ExpressionType, bool, int, int, int) { return AssignmentExpr; }
152 ExpressionType createFunctionExpr(int, const Identifier*, int, int, int, int, int, int) { return FunctionExpr; }
153 int createFunctionBody(int, bool) { return 1; }
154 int createArguments() { return 1; }
155 int createArguments(int) { return 1; }
156 int createArgumentsList(int, int) { return 1; }
157 int createArgumentsList(int, int, int) { return 1; }
158 template <bool complete> Property createProperty(const Identifier* name, int, PropertyNode::Type type)
159 {
160 if (!complete)
161 return Property(type);
162 ASSERT(name);
163 return Property(name, type);
164 }
165 template <bool complete> Property createProperty(JSGlobalData* globalData, double name, int, PropertyNode::Type type)
166 {
167 if (!complete)
168 return Property(type);
169 return Property(&globalData->parserArena->identifierArena().makeNumericIdentifier(globalData, name), type);
170 }
171 int createPropertyList(int, Property) { return 1; }
172 int createPropertyList(int, Property, int) { return 1; }
173 int createElementList(int, int) { return 1; }
174 int createElementList(int, int, int) { return 1; }
175 int createFormalParameterList(const Identifier&) { return 1; }
176 int createFormalParameterList(int, const Identifier&) { return 1; }
177 int createClause(int, int) { return 1; }
178 int createClauseList(int) { return 1; }
179 int createClauseList(int, int) { return 1; }
180 void setUsesArguments(int) { }
181 int createFuncDeclStatement(int, const Identifier*, int, int, int, int, int, int) { return 1; }
182 int createBlockStatement(int, int, int, int) { return 1; }
183 int createExprStatement(int, int, int, int) { return 1; }
184 int createIfStatement(int, int, int, int, int) { return 1; }
185 int createIfStatement(int, int, int, int, int, int) { return 1; }
186 int createForLoop(int, int, int, int, int, bool, int, int) { return 1; }
187 int createForInLoop(int, const Identifier*, int, int, int, int, int, int, int, int, int, int) { return 1; }
188 int createForInLoop(int, int, int, int, int, int, int, int, int) { return 1; }
189 int createEmptyStatement(int) { return 1; }
190 int createVarStatement(int, int, int, int) { return 1; }
191 int createReturnStatement(int, int, int, int, int, int) { return 1; }
192 int createBreakStatement(int, int, int, int, int) { return 1; }
193 int createBreakStatement(int, const Identifier*, int, int, int, int) { return 1; }
194 int createContinueStatement(int, int, int, int, int) { return 1; }
195 int createContinueStatement(int, const Identifier*, int, int, int, int) { return 1; }
196 int createTryStatement(int, int, const Identifier*, int, int, int, int) { return 1; }
197 int createSwitchStatement(int, int, int, int, int, int, int) { return 1; }
198 int createWhileStatement(int, int, int, int, int) { return 1; }
199 int createWithStatement(int, int, int, int, int, int, int) { return 1; }
200 int createDoWhileStatement(int, int, int, int, int) { return 1; }
201 int createLabelStatement(int, const Identifier*, int, int, int) { return 1; }
202 int createThrowStatement(int, int, int, int, int, int) { return 1; }
203 int createDebugger(int, int, int) { return 1; }
204 int createConstStatement(int, int, int, int) { return 1; }
205 int appendConstDecl(int, int, const Identifier*, int) { return 1; }
206 template <bool strict> Property createGetterOrSetterProperty(int, PropertyNode::Type type, const Identifier* name, int, int, int, int, int, int)
207 {
208 ASSERT(name);
209 if (!strict)
210 return Property(type);
211 return Property(name, type);
212 }
213 template <bool strict> Property createGetterOrSetterProperty(JSGlobalData* globalData, int, PropertyNode::Type type, double name, int, int, int, int, int, int)
214 {
215 if (!strict)
216 return Property(type);
217 return Property(&globalData->parserArena->identifierArena().makeNumericIdentifier(globalData, name), type);
218 }
219
220 void appendStatement(int, int) { }
221 void addVar(const Identifier*, bool) { }
222 int combineCommaNodes(int, int, int) { return 1; }
223 int evalCount() const { return 0; }
224 void appendBinaryExpressionInfo(int& operandStackDepth, int expr, int, int, int, bool)
225 {
226 if (!m_topBinaryExpr)
227 m_topBinaryExpr = expr;
228 else
229 m_topBinaryExpr = BinaryExpr;
230 operandStackDepth++;
231 }
232
233 // Logic to handle datastructures used during parsing of binary expressions
234 void operatorStackPop(int& operatorStackDepth) { operatorStackDepth--; }
235 bool operatorStackHasHigherPrecedence(int&, int) { return true; }
236 BinaryOperand getFromOperandStack(int) { return m_topBinaryExpr; }
237 void shrinkOperandStackBy(int& operandStackDepth, int amount) { operandStackDepth -= amount; }
238 void appendBinaryOperation(int, int& operandStackDepth, int&, BinaryOperand, BinaryOperand) { operandStackDepth++; }
239 void operatorStackAppend(int& operatorStackDepth, int, int) { operatorStackDepth++; }
240 int popOperandStack(int&) { int res = m_topBinaryExpr; m_topBinaryExpr = 0; return res; }
241
242 void appendUnaryToken(int& stackDepth, int tok, int) { stackDepth = 1; m_topUnaryToken = tok; }
243 int unaryTokenStackLastType(int&) { return m_topUnaryToken; }
244 int unaryTokenStackLastStart(int&) { return 0; }
245 void unaryTokenStackRemoveLast(int& stackDepth) { stackDepth = 0; }
246
247 void assignmentStackAppend(int, int, int, int, int, Operator) { }
248 int createAssignment(int, int, int, int, int, int) { ASSERT_NOT_REACHED(); return 1; }
249 const Identifier& getName(const Property& property) const { ASSERT(property.name); return *property.name; }
250 PropertyNode::Type getType(const Property& property) const { return property.type; }
251 bool isResolve(ExpressionType expr) const { return expr == ResolveExpr || expr == ResolveEvalExpr; }
252
253 private:
254 int m_topBinaryExpr;
255 int m_topUnaryToken;
256 Vector<int, 8> m_topBinaryExprs;
257 Vector<int, 8> m_topUnaryTokens;
258 };
259
260 }
261
262 #endif