blob: 0328b1273a224c1f1f09987708ebdd24df4b6758 [file] [log] [blame]
oliver@apple.comde38e3e2010-06-24 04:19:32 +00001/*
mark.lam@apple.comfaa53932013-03-20 09:09:38 +00002 * Copyright (C) 2010, 2013 Apple Inc. All rights reserved.
oliver@apple.comde38e3e2010-06-24 04:19:32 +00003 *
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
oliver@apple.comae5d2ab2011-05-24 18:49:18 +000029#include "Lexer.h"
ap@apple.com94163232013-10-20 00:01:17 +000030#include "YarrSyntaxChecker.h"
barraclough@apple.com7e6bd6d2011-01-10 20:20:15 +000031
oliver@apple.comde38e3e2010-06-24 04:19:32 +000032namespace JSC {
msaboff@apple.com2e5003e2011-10-31 22:13:01 +000033
oliver@apple.comde38e3e2010-06-24 04:19:32 +000034class SyntaxChecker {
35public:
oliver@apple.comfc1b3f82011-01-16 23:54:40 +000036 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
ggaren@apple.com9a9a4b52013-04-18 19:32:17 +000067 SyntaxChecker(VM* , void*)
oliver@apple.comde38e3e2010-06-24 04:19:32 +000068 {
69 }
70
71 typedef SyntaxChecker FunctionBodyBuilder;
oliver@apple.com59301852010-10-11 19:12:29 +000072 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;
oliver@apple.comde38e3e2010-06-24 04:19:32 +000080
oliver@apple.com59301852010-10-11 19:12:29 +000081 typedef ExpressionType Expression;
oliver@apple.comde38e3e2010-06-24 04:19:32 +000082 typedef int SourceElements;
83 typedef int Arguments;
oliver@apple.com59301852010-10-11 19:12:29 +000084 typedef ExpressionType Comma;
oliver@apple.com63f9f132010-07-08 21:54:08 +000085 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)
oliver@apple.com72d38322013-10-21 19:23:24 +000091 : name(ident)
92 , type(ty)
oliver@apple.com63f9f132010-07-08 21:54:08 +000093 {
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 };
oliver@apple.comde38e3e2010-06-24 04:19:32 +0000104 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;
oliver@apple.comd055db62013-10-02 19:11:04 +0000114 typedef int DeconstructionPattern;
115 typedef int ArrayPattern;
116 typedef int ObjectPattern;
117
oliver@apple.comde38e3e2010-06-24 04:19:32 +0000118 static const bool CreatesAST = false;
oliver@apple.com09ed6d02010-09-16 00:05:13 +0000119 static const bool NeedsFreeVariableInfo = false;
antti@apple.com40e8c6f2011-01-20 00:13:03 +0000120 static const bool CanUseFunctionCache = true;
msaboff@apple.com11c2e642011-11-07 17:54:15 +0000121 static const unsigned DontBuildKeywords = LexexFlagsDontBuildKeywords;
122 static const unsigned DontBuildStrings = LexerFlagsDontBuildStrings;
oliver@apple.comde38e3e2010-06-24 04:19:32 +0000123
124 int createSourceElements() { return 1; }
mark.lam@apple.com3b256ca2013-07-30 17:01:40 +0000125 ExpressionType makeFunctionCallNode(const JSTokenLocation&, int, int, int, int, int) { return CallExpr; }
oliver@apple.com59301852010-10-11 19:12:29 +0000126 void appendToComma(ExpressionType& base, ExpressionType right) { base = right; }
commit-queue@webkit.org1b331cb2012-08-06 03:16:46 +0000127 ExpressionType createCommaExpr(const JSTokenLocation&, ExpressionType, ExpressionType right) { return right; }
mark.lam@apple.com3b256ca2013-07-30 17:01:40 +0000128 ExpressionType makeAssignNode(const JSTokenLocation&, ExpressionType, Operator, ExpressionType, bool, bool, int, int, int) { return AssignmentExpr; }
129 ExpressionType makePrefixNode(const JSTokenLocation&, ExpressionType, Operator, int, int, int) { return PreExpr; }
130 ExpressionType makePostfixNode(const JSTokenLocation&, ExpressionType, Operator, int, int, int) { return PostExpr; }
commit-queue@webkit.org1b331cb2012-08-06 03:16:46 +0000131 ExpressionType makeTypeOfNode(const JSTokenLocation&, ExpressionType) { return TypeofExpr; }
mark.lam@apple.com3b256ca2013-07-30 17:01:40 +0000132 ExpressionType makeDeleteNode(const JSTokenLocation&, ExpressionType, int, int, int) { return DeleteExpr; }
commit-queue@webkit.org1b331cb2012-08-06 03:16:46 +0000133 ExpressionType makeNegateNode(const JSTokenLocation&, ExpressionType) { return UnaryExpr; }
134 ExpressionType makeBitwiseNotNode(const JSTokenLocation&, ExpressionType) { return UnaryExpr; }
135 ExpressionType createLogicalNot(const JSTokenLocation&, ExpressionType) { return UnaryExpr; }
136 ExpressionType createUnaryPlus(const JSTokenLocation&, ExpressionType) { return UnaryExpr; }
137 ExpressionType createVoid(const JSTokenLocation&, ExpressionType) { return UnaryExpr; }
138 ExpressionType thisExpr(const JSTokenLocation&) { return ThisExpr; }
mark.lam@apple.com3b256ca2013-07-30 17:01:40 +0000139 ExpressionType createResolve(const JSTokenLocation&, const Identifier*, int) { return ResolveExpr; }
commit-queue@webkit.org1b331cb2012-08-06 03:16:46 +0000140 ExpressionType createObjectLiteral(const JSTokenLocation&) { return ObjectLiteralExpr; }
141 ExpressionType createObjectLiteral(const JSTokenLocation&, int) { return ObjectLiteralExpr; }
142 ExpressionType createArray(const JSTokenLocation&, int) { return ArrayLiteralExpr; }
143 ExpressionType createArray(const JSTokenLocation&, int, int) { return ArrayLiteralExpr; }
144 ExpressionType createNumberExpr(const JSTokenLocation&, double) { return NumberExpr; }
145 ExpressionType createString(const JSTokenLocation&, const Identifier*) { return StringExpr; }
146 ExpressionType createBoolean(const JSTokenLocation&, bool) { return BoolExpr; }
147 ExpressionType createNull(const JSTokenLocation&) { return NullExpr; }
mark.lam@apple.com3b256ca2013-07-30 17:01:40 +0000148 ExpressionType createBracketAccess(const JSTokenLocation&, ExpressionType, ExpressionType, bool, int, int, int) { return BracketExpr; }
149 ExpressionType createDotAccess(const JSTokenLocation&, ExpressionType, const Identifier*, int, int, int) { return DotExpr; }
150 ExpressionType createRegExp(const JSTokenLocation&, const Identifier& pattern, const Identifier&, int) { return Yarr::checkSyntax(pattern.string()) ? 0 : RegExpExpr; }
commit-queue@webkit.org1b331cb2012-08-06 03:16:46 +0000151 ExpressionType createNewExpr(const JSTokenLocation&, ExpressionType, int, int, int, int) { return NewExpr; }
mark.lam@apple.com3b256ca2013-07-30 17:01:40 +0000152 ExpressionType createNewExpr(const JSTokenLocation&, ExpressionType, int, int) { return NewExpr; }
commit-queue@webkit.org1b331cb2012-08-06 03:16:46 +0000153 ExpressionType createConditionalExpr(const JSTokenLocation&, ExpressionType, ExpressionType, ExpressionType) { return ConditionalExpr; }
mark.lam@apple.com3b256ca2013-07-30 17:01:40 +0000154 ExpressionType createAssignResolve(const JSTokenLocation&, const Identifier&, ExpressionType, int, int, int) { return AssignmentExpr; }
mark.lam@apple.com5b45f902013-07-09 16:15:12 +0000155 ExpressionType createFunctionExpr(const JSTokenLocation&, const Identifier*, int, int, int, int, int, int, int) { return FunctionExpr; }
mark.lam@apple.comfa35e782013-11-19 21:55:16 +0000156 int createFunctionBody(const JSTokenLocation&, const JSTokenLocation&, int, int, bool) { return 1; }
157 void setFunctionNameStart(int, int) { }
oliver@apple.comde38e3e2010-06-24 04:19:32 +0000158 int createArguments() { return 1; }
159 int createArguments(int) { return 1; }
oliver@apple.com72f8a822013-10-17 01:02:34 +0000160 ExpressionType createSpreadExpression(const JSTokenLocation&, ExpressionType, int, int, int) { return 1; }
commit-queue@webkit.org1b331cb2012-08-06 03:16:46 +0000161 int createArgumentsList(const JSTokenLocation&, int) { return 1; }
162 int createArgumentsList(const JSTokenLocation&, int, int) { return 1; }
oliver@apple.com7ce312b2013-12-10 21:19:15 +0000163 Property createProperty(const Identifier* name, int, PropertyNode::Type type, bool complete)
oliver@apple.com63f9f132010-07-08 21:54:08 +0000164 {
oliver@apple.com63f9f132010-07-08 21:54:08 +0000165 if (!complete)
166 return Property(type);
oliver@apple.comae5d2ab2011-05-24 18:49:18 +0000167 ASSERT(name);
oliver@apple.com63f9f132010-07-08 21:54:08 +0000168 return Property(name, type);
169 }
oliver@apple.com7ce312b2013-12-10 21:19:15 +0000170 Property createProperty(VM* vm, double name, int, PropertyNode::Type type, bool complete)
oliver@apple.com63f9f132010-07-08 21:54:08 +0000171 {
172 if (!complete)
173 return Property(type);
ggaren@apple.com9a9a4b52013-04-18 19:32:17 +0000174 return Property(&vm->parserArena->identifierArena().makeNumericIdentifier(vm, name), type);
oliver@apple.com63f9f132010-07-08 21:54:08 +0000175 }
oliver@apple.com7ce312b2013-12-10 21:19:15 +0000176 Property createProperty(VM*, ExpressionNode*, int, PropertyNode::Type type, bool)
oliver@apple.com72d38322013-10-21 19:23:24 +0000177 {
178 return Property(type);
179 }
commit-queue@webkit.org1b331cb2012-08-06 03:16:46 +0000180 int createPropertyList(const JSTokenLocation&, Property) { return 1; }
181 int createPropertyList(const JSTokenLocation&, Property, int) { return 1; }
oliver@apple.comde38e3e2010-06-24 04:19:32 +0000182 int createElementList(int, int) { return 1; }
183 int createElementList(int, int, int) { return 1; }
oliver@apple.comd055db62013-10-02 19:11:04 +0000184 int createFormalParameterList(DeconstructionPattern) { return 1; }
185 int createFormalParameterList(int, DeconstructionPattern) { return 1; }
oliver@apple.comde38e3e2010-06-24 04:19:32 +0000186 int createClause(int, int) { return 1; }
187 int createClauseList(int) { return 1; }
188 int createClauseList(int, int) { return 1; }
189 void setUsesArguments(int) { }
mark.lam@apple.com5b45f902013-07-09 16:15:12 +0000190 int createFuncDeclStatement(const JSTokenLocation&, const Identifier*, int, int, int, int, int, int, int) { return 1; }
commit-queue@webkit.org1b331cb2012-08-06 03:16:46 +0000191 int createBlockStatement(const JSTokenLocation&, int, int, int) { return 1; }
192 int createExprStatement(const JSTokenLocation&, int, int, int) { return 1; }
193 int createIfStatement(const JSTokenLocation&, int, int, int, int) { return 1; }
194 int createIfStatement(const JSTokenLocation&, int, int, int, int, int) { return 1; }
195 int createForLoop(const JSTokenLocation&, int, int, int, int, int, int) { return 1; }
mark.lam@apple.com3b256ca2013-07-30 17:01:40 +0000196 int createForInLoop(const JSTokenLocation&, int, int, int, int, int, int, int, int) { return 1; }
oliver@apple.com20a9bf02013-10-04 20:35:24 +0000197 int createForOfLoop(const JSTokenLocation&, int, int, int, int, int, int, int, int) { return 1; }
commit-queue@webkit.org1b331cb2012-08-06 03:16:46 +0000198 int createEmptyStatement(const JSTokenLocation&) { return 1; }
199 int createVarStatement(const JSTokenLocation&, int, int, int) { return 1; }
mark.lam@apple.com3b256ca2013-07-30 17:01:40 +0000200 int createReturnStatement(const JSTokenLocation&, int, int, int) { return 1; }
201 int createBreakStatement(const JSTokenLocation&, int, int) { return 1; }
202 int createBreakStatement(const JSTokenLocation&, const Identifier*, int, int) { return 1; }
203 int createContinueStatement(const JSTokenLocation&, int, int) { return 1; }
204 int createContinueStatement(const JSTokenLocation&, const Identifier*, int, int) { return 1; }
commit-queue@webkit.org1b331cb2012-08-06 03:16:46 +0000205 int createTryStatement(const JSTokenLocation&, int, const Identifier*, int, int, int, int) { return 1; }
206 int createSwitchStatement(const JSTokenLocation&, int, int, int, int, int, int) { return 1; }
207 int createWhileStatement(const JSTokenLocation&, int, int, int, int) { return 1; }
mark.lam@apple.com3b256ca2013-07-30 17:01:40 +0000208 int createWithStatement(const JSTokenLocation&, int, int, int, int, int, int) { return 1; }
commit-queue@webkit.org1b331cb2012-08-06 03:16:46 +0000209 int createDoWhileStatement(const JSTokenLocation&, int, int, int, int) { return 1; }
mark.lam@apple.com3b256ca2013-07-30 17:01:40 +0000210 int createLabelStatement(const JSTokenLocation&, const Identifier*, int, int, int) { return 1; }
211 int createThrowStatement(const JSTokenLocation&, int, int, int) { return 1; }
commit-queue@webkit.org1b331cb2012-08-06 03:16:46 +0000212 int createDebugger(const JSTokenLocation&, int, int) { return 1; }
213 int createConstStatement(const JSTokenLocation&, int, int, int) { return 1; }
214 int appendConstDecl(const JSTokenLocation&, int, const Identifier*, int) { return 1; }
oliver@apple.com7ce312b2013-12-10 21:19:15 +0000215 Property createGetterOrSetterProperty(const JSTokenLocation&, PropertyNode::Type type, bool strict, const Identifier* name, int, int, int, int, int, int, int)
oliver@apple.com63f9f132010-07-08 21:54:08 +0000216 {
217 ASSERT(name);
218 if (!strict)
219 return Property(type);
220 return Property(name, type);
221 }
oliver@apple.com7ce312b2013-12-10 21:19:15 +0000222 Property createGetterOrSetterProperty(VM* vm, const JSTokenLocation&, PropertyNode::Type type, bool strict, double name, int, int, int, int, int, int, int)
commit-queue@webkit.org54da5c22012-02-26 22:51:38 +0000223 {
224 if (!strict)
225 return Property(type);
ggaren@apple.com9a9a4b52013-04-18 19:32:17 +0000226 return Property(&vm->parserArena->identifierArena().makeNumericIdentifier(vm, name), type);
commit-queue@webkit.org54da5c22012-02-26 22:51:38 +0000227 }
oliver@apple.comde38e3e2010-06-24 04:19:32 +0000228
229 void appendStatement(int, int) { }
230 void addVar(const Identifier*, bool) { }
commit-queue@webkit.org1b331cb2012-08-06 03:16:46 +0000231 int combineCommaNodes(const JSTokenLocation&, int, int) { return 1; }
oliver@apple.com6bc11372012-05-07 23:35:52 +0000232 int evalCount() const { return 0; }
mark.lam@apple.com3b256ca2013-07-30 17:01:40 +0000233 void appendBinaryExpressionInfo(int& operandStackDepth, int expr, int, int, int, bool)
oliver@apple.com59301852010-10-11 19:12:29 +0000234 {
235 if (!m_topBinaryExpr)
236 m_topBinaryExpr = expr;
237 else
238 m_topBinaryExpr = BinaryExpr;
239 operandStackDepth++;
240 }
oliver@apple.comde38e3e2010-06-24 04:19:32 +0000241
242 // Logic to handle datastructures used during parsing of binary expressions
243 void operatorStackPop(int& operatorStackDepth) { operatorStackDepth--; }
244 bool operatorStackHasHigherPrecedence(int&, int) { return true; }
oliver@apple.com59301852010-10-11 19:12:29 +0000245 BinaryOperand getFromOperandStack(int) { return m_topBinaryExpr; }
oliver@apple.comde38e3e2010-06-24 04:19:32 +0000246 void shrinkOperandStackBy(int& operandStackDepth, int amount) { operandStackDepth -= amount; }
commit-queue@webkit.org1b331cb2012-08-06 03:16:46 +0000247 void appendBinaryOperation(const JSTokenLocation&, int& operandStackDepth, int&, BinaryOperand, BinaryOperand) { operandStackDepth++; }
oliver@apple.comde38e3e2010-06-24 04:19:32 +0000248 void operatorStackAppend(int& operatorStackDepth, int, int) { operatorStackDepth++; }
oliver@apple.com59301852010-10-11 19:12:29 +0000249 int popOperandStack(int&) { int res = m_topBinaryExpr; m_topBinaryExpr = 0; return res; }
oliver@apple.comde38e3e2010-06-24 04:19:32 +0000250
mark.lam@apple.com3b256ca2013-07-30 17:01:40 +0000251 void appendUnaryToken(int& stackDepth, int tok, int) { stackDepth = 1; m_topUnaryToken = tok; }
oliver@apple.com59301852010-10-11 19:12:29 +0000252 int unaryTokenStackLastType(int&) { return m_topUnaryToken; }
mark.lam@apple.com3b256ca2013-07-30 17:01:40 +0000253 JSTextPosition unaryTokenStackLastStart(int&) { return JSTextPosition(0, 0, 0); }
oliver@apple.com59301852010-10-11 19:12:29 +0000254 void unaryTokenStackRemoveLast(int& stackDepth) { stackDepth = 0; }
oliver@apple.comde38e3e2010-06-24 04:19:32 +0000255
mark.lam@apple.com3b256ca2013-07-30 17:01:40 +0000256 void assignmentStackAppend(int, int, int, int, int, Operator) { }
oliver@apple.com5598c182013-01-23 22:25:07 +0000257 int createAssignment(const JSTokenLocation&, int, int, int, int, int) { RELEASE_ASSERT_NOT_REACHED(); return 1; }
oliver@apple.com72d38322013-10-21 19:23:24 +0000258 const Identifier* getName(const Property& property) const { ASSERT(property.name); return property.name; }
ariya@webkit.orgfa69ec92010-12-16 16:32:14 +0000259 PropertyNode::Type getType(const Property& property) const { return property.type; }
260 bool isResolve(ExpressionType expr) const { return expr == ResolveExpr || expr == ResolveEvalExpr; }
oliver@apple.comd055db62013-10-02 19:11:04 +0000261 ExpressionType createDeconstructingAssignment(const JSTokenLocation&, int, ExpressionType)
262 {
263 return 1;
264 }
oliver@apple.com59301852010-10-11 19:12:29 +0000265
oliver@apple.comd055db62013-10-02 19:11:04 +0000266 ArrayPattern createArrayPattern(const JSTokenLocation&)
267 {
268 return 1;
269 }
270 void appendArrayPatternSkipEntry(ArrayPattern, const JSTokenLocation&)
271 {
272 }
273 void appendArrayPatternEntry(ArrayPattern, const JSTokenLocation&, DeconstructionPattern)
274 {
275 }
276 ObjectPattern createObjectPattern(const JSTokenLocation&)
277 {
278 return 1;
279 }
280 void appendObjectPatternEntry(ArrayPattern, const JSTokenLocation&, bool, const Identifier&, DeconstructionPattern)
281 {
282 }
akling@apple.combdf5d1d2014-01-21 01:10:29 +0000283 DeconstructionPattern createBindingLocation(const JSTokenLocation&, const Identifier&, const JSTextPosition&, const JSTextPosition&)
oliver@apple.comd055db62013-10-02 19:11:04 +0000284 {
285 return 1;
286 }
oliver@apple.com59301852010-10-11 19:12:29 +0000287private:
288 int m_topBinaryExpr;
289 int m_topUnaryToken;
oliver@apple.comfc1b3f82011-01-16 23:54:40 +0000290 Vector<int, 8> m_topBinaryExprs;
291 Vector<int, 8> m_topUnaryTokens;
oliver@apple.comde38e3e2010-06-24 04:19:32 +0000292};
293
294}
295
296#endif