2 * Copyright (C) 2010, 2013 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #ifndef SyntaxChecker_h
27 #define SyntaxChecker_h
30 #include <yarr/YarrSyntaxChecker.h>
36 struct BinaryExprContext
{
37 BinaryExprContext(SyntaxChecker
& context
)
40 m_context
->m_topBinaryExprs
.append(m_context
->m_topBinaryExpr
);
41 m_context
->m_topBinaryExpr
= 0;
45 m_context
->m_topBinaryExpr
= m_context
->m_topBinaryExprs
.last();
46 m_context
->m_topBinaryExprs
.removeLast();
49 SyntaxChecker
* m_context
;
51 struct UnaryExprContext
{
52 UnaryExprContext(SyntaxChecker
& context
)
55 m_context
->m_topUnaryTokens
.append(m_context
->m_topUnaryToken
);
56 m_context
->m_topUnaryToken
= 0;
60 m_context
->m_topUnaryToken
= m_context
->m_topUnaryTokens
.last();
61 m_context
->m_topUnaryTokens
.removeLast();
64 SyntaxChecker
* m_context
;
67 SyntaxChecker(VM
* , void*)
71 typedef SyntaxChecker FunctionBodyBuilder
;
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
;
81 typedef ExpressionType Expression
;
82 typedef int SourceElements
;
83 typedef int Arguments
;
84 typedef ExpressionType Comma
;
86 ALWAYS_INLINE
Property(void* = 0)
87 : type((PropertyNode::Type
)0)
90 ALWAYS_INLINE
Property(const Identifier
* ident
, PropertyNode::Type ty
)
95 ALWAYS_INLINE
Property(PropertyNode::Type ty
)
100 ALWAYS_INLINE
bool operator!() { return !type
; }
101 const Identifier
* name
;
102 PropertyNode::Type type
;
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
;
112 typedef int ConstDeclList
;
113 typedef int BinaryOperand
;
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
;
121 int createSourceElements() { return 1; }
122 ExpressionType
makeFunctionCallNode(const JSTokenLocation
&, int, int, int, int, int, int, int) { return CallExpr
; }
123 void appendToComma(ExpressionType
& base
, ExpressionType right
) { base
= right
; }
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) { }
155 int createArguments() { return 1; }
156 int createArguments(int) { return 1; }
157 int createArgumentsList(const JSTokenLocation
&, int) { return 1; }
158 int createArgumentsList(const JSTokenLocation
&, int, int) { return 1; }
159 template <bool complete
> Property
createProperty(const Identifier
* name
, int, PropertyNode::Type type
)
162 return Property(type
);
164 return Property(name
, type
);
166 template <bool complete
> Property
createProperty(VM
* vm
, double name
, int, PropertyNode::Type type
)
169 return Property(type
);
170 return Property(&vm
->parserArena
->identifierArena().makeNumericIdentifier(vm
, name
), type
);
172 int createPropertyList(const JSTokenLocation
&, Property
) { return 1; }
173 int createPropertyList(const JSTokenLocation
&, Property
, int) { return 1; }
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) { }
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)
211 return Property(type
);
212 return Property(name
, type
);
214 template <bool strict
> Property
createGetterOrSetterProperty(VM
* vm
, const JSTokenLocation
&, PropertyNode::Type type
, double name
, int, int, int, int, int, int, int)
217 return Property(type
);
218 return Property(&vm
->parserArena
->identifierArena().makeNumericIdentifier(vm
, name
), type
);
221 void appendStatement(int, int) { }
222 void addVar(const Identifier
*, bool) { }
223 int combineCommaNodes(const JSTokenLocation
&, int, int) { return 1; }
224 int evalCount() const { return 0; }
225 void appendBinaryExpressionInfo(int& operandStackDepth
, int expr
, int, int, int, int, int, bool)
227 if (!m_topBinaryExpr
)
228 m_topBinaryExpr
= expr
;
230 m_topBinaryExpr
= BinaryExpr
;
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
; }
239 void appendBinaryOperation(const JSTokenLocation
&, int& operandStackDepth
, int&, BinaryOperand
, BinaryOperand
) { operandStackDepth
++; }
240 void operatorStackAppend(int& operatorStackDepth
, int, int) { operatorStackDepth
++; }
241 int popOperandStack(int&) { int res
= m_topBinaryExpr
; m_topBinaryExpr
= 0; return res
; }
243 void appendUnaryToken(int& stackDepth
, int tok
, int, int, int) { stackDepth
= 1; m_topUnaryToken
= tok
; }
244 int unaryTokenStackLastType(int&) { return m_topUnaryToken
; }
245 unsigned unaryTokenStackLastStart(int&) { return 0; }
246 unsigned unaryTokenStackLastLineStartPosition(int&) { return 0; }
247 void unaryTokenStackRemoveLast(int& stackDepth
) { stackDepth
= 0; }
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; }
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
; }
258 Vector
<int, 8> m_topBinaryExprs
;
259 Vector
<int, 8> m_topUnaryTokens
;