]> git.saurik.com Git - apple/javascriptcore.git/blame - parser/SyntaxChecker.h
JavaScriptCore-1218.34.tar.gz
[apple/javascriptcore.git] / parser / SyntaxChecker.h
CommitLineData
14957cd0 1/*
93a37866 2 * Copyright (C) 2010, 2013 Apple Inc. All rights reserved.
14957cd0
A
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
32namespace JSC {
6fe7ccc8 33
14957cd0
A
34class SyntaxChecker {
35public:
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
93a37866 67 SyntaxChecker(VM* , void*)
14957cd0
A
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;
6fe7ccc8
A
118 static const unsigned DontBuildKeywords = LexexFlagsDontBuildKeywords;
119 static const unsigned DontBuildStrings = LexerFlagsDontBuildStrings;
14957cd0
A
120
121 int createSourceElements() { return 1; }
93a37866 122 ExpressionType makeFunctionCallNode(const JSTokenLocation&, int, int, int, int, int, int, int) { return CallExpr; }
14957cd0 123 void appendToComma(ExpressionType& base, ExpressionType right) { base = right; }
93a37866
A
124 ExpressionType createCommaExpr(const JSTokenLocation&, ExpressionType, ExpressionType right) { return right; }
125 ExpressionType makeAssignNode(const JSTokenLocation&, ExpressionType, Operator, ExpressionType, bool, bool, int, int, int, int, int) { return AssignmentExpr; }
126 ExpressionType makePrefixNode(const JSTokenLocation&, ExpressionType, Operator, int, int, int, int, int) { return PreExpr; }
127 ExpressionType makePostfixNode(const JSTokenLocation&, ExpressionType, Operator, int, int, int, int, int) { return PostExpr; }
128 ExpressionType makeTypeOfNode(const JSTokenLocation&, ExpressionType) { return TypeofExpr; }
129 ExpressionType makeDeleteNode(const JSTokenLocation&, ExpressionType, int, int, int, int, int) { return DeleteExpr; }
130 ExpressionType makeNegateNode(const JSTokenLocation&, ExpressionType) { return UnaryExpr; }
131 ExpressionType makeBitwiseNotNode(const JSTokenLocation&, ExpressionType) { return UnaryExpr; }
132 ExpressionType createLogicalNot(const JSTokenLocation&, ExpressionType) { return UnaryExpr; }
133 ExpressionType createUnaryPlus(const JSTokenLocation&, ExpressionType) { return UnaryExpr; }
134 ExpressionType createVoid(const JSTokenLocation&, ExpressionType) { return UnaryExpr; }
135 ExpressionType thisExpr(const JSTokenLocation&) { return ThisExpr; }
136 ExpressionType createResolve(const JSTokenLocation&, const Identifier*, int, int, int) { return ResolveExpr; }
137 ExpressionType createObjectLiteral(const JSTokenLocation&) { return ObjectLiteralExpr; }
138 ExpressionType createObjectLiteral(const JSTokenLocation&, int) { return ObjectLiteralExpr; }
139 ExpressionType createArray(const JSTokenLocation&, int) { return ArrayLiteralExpr; }
140 ExpressionType createArray(const JSTokenLocation&, int, int) { return ArrayLiteralExpr; }
141 ExpressionType createNumberExpr(const JSTokenLocation&, double) { return NumberExpr; }
142 ExpressionType createString(const JSTokenLocation&, const Identifier*) { return StringExpr; }
143 ExpressionType createBoolean(const JSTokenLocation&, bool) { return BoolExpr; }
144 ExpressionType createNull(const JSTokenLocation&) { return NullExpr; }
145 ExpressionType createBracketAccess(const JSTokenLocation&, ExpressionType, ExpressionType, bool, int, int, int, int, int) { return BracketExpr; }
146 ExpressionType createDotAccess(const JSTokenLocation&, ExpressionType, const Identifier*, int, int, int, int, int) { return DotExpr; }
147 ExpressionType createRegExp(const JSTokenLocation&, const Identifier& pattern, const Identifier&, int, int, int) { return Yarr::checkSyntax(pattern.string()) ? 0 : RegExpExpr; }
148 ExpressionType createNewExpr(const JSTokenLocation&, ExpressionType, int, int, int, int, int, int) { return NewExpr; }
149 ExpressionType createNewExpr(const JSTokenLocation&, ExpressionType, int, int, int, int) { return NewExpr; }
150 ExpressionType createConditionalExpr(const JSTokenLocation&, ExpressionType, ExpressionType, ExpressionType) { return ConditionalExpr; }
151 ExpressionType createAssignResolve(const JSTokenLocation&, const Identifier&, ExpressionType, int, int, int, int, int) { return AssignmentExpr; }
152 ExpressionType createFunctionExpr(const JSTokenLocation&, const Identifier*, int, int, int, int, int, int, int) { return FunctionExpr; }
153 int createFunctionBody(const JSTokenLocation&, const JSTokenLocation&, int, bool) { return 1; }
154 void setFunctionStart(int, int) { }
14957cd0
A
155 int createArguments() { return 1; }
156 int createArguments(int) { return 1; }
93a37866
A
157 int createArgumentsList(const JSTokenLocation&, int) { return 1; }
158 int createArgumentsList(const JSTokenLocation&, int, int) { return 1; }
14957cd0
A
159 template <bool complete> Property createProperty(const Identifier* name, int, PropertyNode::Type type)
160 {
161 if (!complete)
162 return Property(type);
163 ASSERT(name);
164 return Property(name, type);
165 }
93a37866 166 template <bool complete> Property createProperty(VM* vm, double name, int, PropertyNode::Type type)
14957cd0
A
167 {
168 if (!complete)
169 return Property(type);
93a37866 170 return Property(&vm->parserArena->identifierArena().makeNumericIdentifier(vm, name), type);
14957cd0 171 }
93a37866
A
172 int createPropertyList(const JSTokenLocation&, Property) { return 1; }
173 int createPropertyList(const JSTokenLocation&, Property, int) { return 1; }
14957cd0
A
174 int createElementList(int, int) { return 1; }
175 int createElementList(int, int, int) { return 1; }
176 int createFormalParameterList(const Identifier&) { return 1; }
177 int createFormalParameterList(int, const Identifier&) { return 1; }
178 int createClause(int, int) { return 1; }
179 int createClauseList(int) { return 1; }
180 int createClauseList(int, int) { return 1; }
181 void setUsesArguments(int) { }
93a37866
A
182 int createFuncDeclStatement(const JSTokenLocation&, const Identifier*, int, int, int, int, int, int, int) { return 1; }
183 int createBlockStatement(const JSTokenLocation&, int, int, int) { return 1; }
184 int createExprStatement(const JSTokenLocation&, int, int, int) { return 1; }
185 int createIfStatement(const JSTokenLocation&, int, int, int, int) { return 1; }
186 int createIfStatement(const JSTokenLocation&, int, int, int, int, int) { return 1; }
187 int createForLoop(const JSTokenLocation&, int, int, int, int, int, int) { return 1; }
188 int createForInLoop(const JSTokenLocation&, const Identifier*, int, int, int, int, int, int, int, int, int, int, int, int) { return 1; }
189 int createForInLoop(const JSTokenLocation&, int, int, int, int, int, int, int, int, int, int) { return 1; }
190 int createEmptyStatement(const JSTokenLocation&) { return 1; }
191 int createVarStatement(const JSTokenLocation&, int, int, int) { return 1; }
192 int createReturnStatement(const JSTokenLocation&, int, int, int, int, int, int, int) { return 1; }
193 int createBreakStatement(const JSTokenLocation&, int, int, int, int, int) { return 1; }
194 int createBreakStatement(const JSTokenLocation&, const Identifier*, int, int, int, int, int) { return 1; }
195 int createContinueStatement(const JSTokenLocation&, int, int, int, int, int) { return 1; }
196 int createContinueStatement(const JSTokenLocation&, const Identifier*, int, int, int, int, int) { return 1; }
197 int createTryStatement(const JSTokenLocation&, int, const Identifier*, int, int, int, int) { return 1; }
198 int createSwitchStatement(const JSTokenLocation&, int, int, int, int, int, int) { return 1; }
199 int createWhileStatement(const JSTokenLocation&, int, int, int, int) { return 1; }
200 int createWithStatement(const JSTokenLocation&, int, int, int, int, int, int, int, int) { return 1; }
201 int createDoWhileStatement(const JSTokenLocation&, int, int, int, int) { return 1; }
202 int createLabelStatement(const JSTokenLocation&, const Identifier*, int, int, int, int, int) { return 1; }
203 int createThrowStatement(const JSTokenLocation&, int, int, int, int, int, int, int) { return 1; }
204 int createDebugger(const JSTokenLocation&, int, int) { return 1; }
205 int createConstStatement(const JSTokenLocation&, int, int, int) { return 1; }
206 int appendConstDecl(const JSTokenLocation&, int, const Identifier*, int) { return 1; }
207 template <bool strict> Property createGetterOrSetterProperty(const JSTokenLocation&, PropertyNode::Type type, const Identifier* name, int, int, int, int, int, int, int)
14957cd0
A
208 {
209 ASSERT(name);
210 if (!strict)
211 return Property(type);
212 return Property(name, type);
213 }
93a37866 214 template <bool strict> Property createGetterOrSetterProperty(VM* vm, const JSTokenLocation&, PropertyNode::Type type, double name, int, int, int, int, int, int, int)
6fe7ccc8
A
215 {
216 if (!strict)
217 return Property(type);
93a37866 218 return Property(&vm->parserArena->identifierArena().makeNumericIdentifier(vm, name), type);
6fe7ccc8 219 }
14957cd0
A
220
221 void appendStatement(int, int) { }
222 void addVar(const Identifier*, bool) { }
93a37866 223 int combineCommaNodes(const JSTokenLocation&, int, int) { return 1; }
14957cd0 224 int evalCount() const { return 0; }
93a37866 225 void appendBinaryExpressionInfo(int& operandStackDepth, int expr, int, int, int, int, int, bool)
14957cd0
A
226 {
227 if (!m_topBinaryExpr)
228 m_topBinaryExpr = expr;
229 else
230 m_topBinaryExpr = BinaryExpr;
231 operandStackDepth++;
232 }
233
234 // Logic to handle datastructures used during parsing of binary expressions
235 void operatorStackPop(int& operatorStackDepth) { operatorStackDepth--; }
236 bool operatorStackHasHigherPrecedence(int&, int) { return true; }
237 BinaryOperand getFromOperandStack(int) { return m_topBinaryExpr; }
238 void shrinkOperandStackBy(int& operandStackDepth, int amount) { operandStackDepth -= amount; }
93a37866 239 void appendBinaryOperation(const JSTokenLocation&, int& operandStackDepth, int&, BinaryOperand, BinaryOperand) { operandStackDepth++; }
14957cd0
A
240 void operatorStackAppend(int& operatorStackDepth, int, int) { operatorStackDepth++; }
241 int popOperandStack(int&) { int res = m_topBinaryExpr; m_topBinaryExpr = 0; return res; }
242
93a37866 243 void appendUnaryToken(int& stackDepth, int tok, int, int, int) { stackDepth = 1; m_topUnaryToken = tok; }
14957cd0 244 int unaryTokenStackLastType(int&) { return m_topUnaryToken; }
93a37866
A
245 unsigned unaryTokenStackLastStart(int&) { return 0; }
246 unsigned unaryTokenStackLastLineStartPosition(int&) { return 0; }
14957cd0
A
247 void unaryTokenStackRemoveLast(int& stackDepth) { stackDepth = 0; }
248
93a37866
A
249 void assignmentStackAppend(int, int, int, int, int, int, int, Operator) { }
250 int createAssignment(const JSTokenLocation&, int, int, int, int, int) { RELEASE_ASSERT_NOT_REACHED(); return 1; }
14957cd0
A
251 const Identifier& getName(const Property& property) const { ASSERT(property.name); return *property.name; }
252 PropertyNode::Type getType(const Property& property) const { return property.type; }
253 bool isResolve(ExpressionType expr) const { return expr == ResolveExpr || expr == ResolveEvalExpr; }
254
255private:
256 int m_topBinaryExpr;
257 int m_topUnaryToken;
258 Vector<int, 8> m_topBinaryExprs;
259 Vector<int, 8> m_topUnaryTokens;
260};
261
262}
263
264#endif