1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
|
/*
* Copyright (C) 2010, 2013 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef SyntaxChecker_h
#define SyntaxChecker_h
#include "Lexer.h"
#include "ParserFunctionInfo.h"
#include "YarrSyntaxChecker.h"
namespace JSC {
class SyntaxChecker {
public:
struct BinaryExprContext {
BinaryExprContext(SyntaxChecker& context)
: m_context(&context)
{
m_context->m_topBinaryExprs.append(m_context->m_topBinaryExpr);
m_context->m_topBinaryExpr = 0;
}
~BinaryExprContext()
{
m_context->m_topBinaryExpr = m_context->m_topBinaryExprs.last();
m_context->m_topBinaryExprs.removeLast();
}
private:
SyntaxChecker* m_context;
};
struct UnaryExprContext {
UnaryExprContext(SyntaxChecker& context)
: m_context(&context)
{
m_context->m_topUnaryTokens.append(m_context->m_topUnaryToken);
m_context->m_topUnaryToken = 0;
}
~UnaryExprContext()
{
m_context->m_topUnaryToken = m_context->m_topUnaryTokens.last();
m_context->m_topUnaryTokens.removeLast();
}
private:
SyntaxChecker* m_context;
};
SyntaxChecker(VM* , void*)
{
}
enum { NoneExpr = 0,
ResolveEvalExpr, ResolveExpr, IntegerExpr, DoubleExpr, StringExpr,
ThisExpr, NullExpr, BoolExpr, RegExpExpr, ObjectLiteralExpr,
FunctionExpr, ClassExpr, SuperExpr, BracketExpr, DotExpr, CallExpr,
NewExpr, PreExpr, PostExpr, UnaryExpr, BinaryExpr,
ConditionalExpr, AssignmentExpr, TypeofExpr, NewTargetExpr,
DeleteExpr, ArrayLiteralExpr, BindingDestructuring, RestParameter,
ArrayDestructuring, ObjectDestructuring, SourceElementsResult,
FunctionBodyResult, SpreadExpr, ArgumentsResult,
PropertyListResult, ArgumentsListResult, ElementsListResult,
StatementResult, FormalParameterListResult, ClauseResult,
ClauseListResult, CommaExpr, DestructuringAssignment,
TemplateStringResult, TemplateStringListResult,
TemplateExpressionListResult, TemplateExpr,
TaggedTemplateExpr, YieldExpr,
ModuleNameResult,
ImportSpecifierResult, ImportSpecifierListResult,
ExportSpecifierResult, ExportSpecifierListResult
};
typedef int ExpressionType;
typedef ExpressionType Expression;
typedef int SourceElements;
typedef int Arguments;
typedef ExpressionType Comma;
struct Property {
ALWAYS_INLINE Property(void* = 0)
: type((PropertyNode::Type)0)
{
}
ALWAYS_INLINE Property(const Identifier* ident, PropertyNode::Type ty)
: name(ident)
, type(ty)
{
}
ALWAYS_INLINE Property(PropertyNode::Type ty)
: name(0)
, type(ty)
{
}
ALWAYS_INLINE bool operator!() { return !type; }
const Identifier* name;
PropertyNode::Type type;
};
typedef int PropertyList;
typedef int ElementList;
typedef int ArgumentsList;
typedef int TemplateExpressionList;
typedef int TemplateString;
typedef int TemplateStringList;
typedef int TemplateLiteral;
typedef int FormalParameterList;
typedef int FunctionBody;
typedef int ClassExpression;
typedef int ModuleName;
typedef int ImportSpecifier;
typedef int ImportSpecifierList;
typedef int ExportSpecifier;
typedef int ExportSpecifierList;
typedef int Statement;
typedef int ClauseList;
typedef int Clause;
typedef int BinaryOperand;
typedef int DestructuringPattern;
typedef DestructuringPattern ArrayPattern;
typedef DestructuringPattern ObjectPattern;
typedef DestructuringPattern RestPattern;
static const bool CreatesAST = false;
static const bool NeedsFreeVariableInfo = false;
static const bool CanUseFunctionCache = true;
static const unsigned DontBuildKeywords = LexexFlagsDontBuildKeywords;
static const unsigned DontBuildStrings = LexerFlagsDontBuildStrings;
int createSourceElements() { return SourceElementsResult; }
ExpressionType makeFunctionCallNode(const JSTokenLocation&, int, int, int, int, int) { return CallExpr; }
ExpressionType createCommaExpr(const JSTokenLocation&, ExpressionType expr) { return expr; }
ExpressionType appendToCommaExpr(const JSTokenLocation&, ExpressionType& head, ExpressionType, ExpressionType next) { head = next; return next; }
ExpressionType makeAssignNode(const JSTokenLocation&, ExpressionType, Operator, ExpressionType, bool, bool, int, int, int) { return AssignmentExpr; }
ExpressionType makePrefixNode(const JSTokenLocation&, ExpressionType, Operator, int, int, int) { return PreExpr; }
ExpressionType makePostfixNode(const JSTokenLocation&, ExpressionType, Operator, int, int, int) { return PostExpr; }
ExpressionType makeTypeOfNode(const JSTokenLocation&, ExpressionType) { return TypeofExpr; }
ExpressionType makeDeleteNode(const JSTokenLocation&, ExpressionType, int, int, int) { return DeleteExpr; }
ExpressionType makeNegateNode(const JSTokenLocation&, ExpressionType) { return UnaryExpr; }
ExpressionType makeBitwiseNotNode(const JSTokenLocation&, ExpressionType) { return UnaryExpr; }
ExpressionType createLogicalNot(const JSTokenLocation&, ExpressionType) { return UnaryExpr; }
ExpressionType createUnaryPlus(const JSTokenLocation&, ExpressionType) { return UnaryExpr; }
ExpressionType createVoid(const JSTokenLocation&, ExpressionType) { return UnaryExpr; }
ExpressionType createThisExpr(const JSTokenLocation&, ThisTDZMode) { return ThisExpr; }
ExpressionType createSuperExpr(const JSTokenLocation&) { return SuperExpr; }
ExpressionType createNewTargetExpr(const JSTokenLocation&) { return NewTargetExpr; }
ExpressionType createResolve(const JSTokenLocation&, const Identifier&, int, int) { return ResolveExpr; }
ExpressionType createObjectLiteral(const JSTokenLocation&) { return ObjectLiteralExpr; }
ExpressionType createObjectLiteral(const JSTokenLocation&, int) { return ObjectLiteralExpr; }
ExpressionType createArray(const JSTokenLocation&, int) { return ArrayLiteralExpr; }
ExpressionType createArray(const JSTokenLocation&, int, int) { return ArrayLiteralExpr; }
ExpressionType createDoubleExpr(const JSTokenLocation&, double) { return DoubleExpr; }
ExpressionType createIntegerExpr(const JSTokenLocation&, double) { return IntegerExpr; }
ExpressionType createString(const JSTokenLocation&, const Identifier*) { return StringExpr; }
ExpressionType createBoolean(const JSTokenLocation&, bool) { return BoolExpr; }
ExpressionType createNull(const JSTokenLocation&) { return NullExpr; }
ExpressionType createBracketAccess(const JSTokenLocation&, ExpressionType, ExpressionType, bool, int, int, int) { return BracketExpr; }
ExpressionType createDotAccess(const JSTokenLocation&, ExpressionType, const Identifier*, int, int, int) { return DotExpr; }
ExpressionType createRegExp(const JSTokenLocation&, const Identifier& pattern, const Identifier&, int) { return Yarr::checkSyntax(pattern.string()) ? 0 : RegExpExpr; }
ExpressionType createNewExpr(const JSTokenLocation&, ExpressionType, int, int, int, int) { return NewExpr; }
ExpressionType createNewExpr(const JSTokenLocation&, ExpressionType, int, int) { return NewExpr; }
ExpressionType createConditionalExpr(const JSTokenLocation&, ExpressionType, ExpressionType, ExpressionType) { return ConditionalExpr; }
ExpressionType createAssignResolve(const JSTokenLocation&, const Identifier&, ExpressionType, int, int, int, AssignmentContext) { return AssignmentExpr; }
ExpressionType createEmptyVarExpression(const JSTokenLocation&, const Identifier&) { return AssignmentExpr; }
ExpressionType createEmptyLetExpression(const JSTokenLocation&, const Identifier&) { return AssignmentExpr; }
ExpressionType createYield(const JSTokenLocation&) { return YieldExpr; }
ExpressionType createYield(const JSTokenLocation&, ExpressionType, bool, int, int, int) { return YieldExpr; }
ClassExpression createClassExpr(const JSTokenLocation&, const Identifier&, VariableEnvironment&, ExpressionType, ExpressionType, PropertyList, PropertyList) { return ClassExpr; }
ExpressionType createFunctionExpr(const JSTokenLocation&, const ParserFunctionInfo<SyntaxChecker>&) { return FunctionExpr; }
int createFunctionMetadata(const JSTokenLocation&, const JSTokenLocation&, int, int, bool, int, int, int, ConstructorKind, SuperBinding, unsigned, SourceParseMode, bool) { return FunctionBodyResult; }
ExpressionType createArrowFunctionExpr(const JSTokenLocation&, const ParserFunctionInfo<SyntaxChecker>&) { return FunctionExpr; }
void setFunctionNameStart(int, int) { }
int createArguments() { return ArgumentsResult; }
int createArguments(int) { return ArgumentsResult; }
ExpressionType createSpreadExpression(const JSTokenLocation&, ExpressionType, int, int, int) { return SpreadExpr; }
TemplateString createTemplateString(const JSTokenLocation&, const Identifier&, const Identifier&) { return TemplateStringResult; }
TemplateStringList createTemplateStringList(TemplateString) { return TemplateStringListResult; }
TemplateStringList createTemplateStringList(TemplateStringList, TemplateString) { return TemplateStringListResult; }
TemplateExpressionList createTemplateExpressionList(Expression) { return TemplateExpressionListResult; }
TemplateExpressionList createTemplateExpressionList(TemplateExpressionList, Expression) { return TemplateExpressionListResult; }
TemplateLiteral createTemplateLiteral(const JSTokenLocation&, TemplateStringList) { return TemplateExpr; }
TemplateLiteral createTemplateLiteral(const JSTokenLocation&, TemplateStringList, TemplateExpressionList) { return TemplateExpr; }
ExpressionType createTaggedTemplate(const JSTokenLocation&, ExpressionType, TemplateLiteral, int, int, int) { return TaggedTemplateExpr; }
int createArgumentsList(const JSTokenLocation&, int) { return ArgumentsListResult; }
int createArgumentsList(const JSTokenLocation&, int, int) { return ArgumentsListResult; }
Property createProperty(const Identifier* name, int, PropertyNode::Type type, PropertyNode::PutType, bool complete, SuperBinding = SuperBinding::NotNeeded)
{
if (!complete)
return Property(type);
ASSERT(name);
return Property(name, type);
}
Property createProperty(VM* vm, ParserArena& parserArena, double name, int, PropertyNode::Type type, PropertyNode::PutType, bool complete)
{
if (!complete)
return Property(type);
return Property(&parserArena.identifierArena().makeNumericIdentifier(vm, name), type);
}
Property createProperty(int, int, PropertyNode::Type type, PropertyNode::PutType, bool, SuperBinding = SuperBinding::NotNeeded)
{
return Property(type);
}
int createPropertyList(const JSTokenLocation&, Property) { return PropertyListResult; }
int createPropertyList(const JSTokenLocation&, Property, int) { return PropertyListResult; }
int createElementList(int, int) { return ElementsListResult; }
int createElementList(int, int, int) { return ElementsListResult; }
int createElementList(int) { return ElementsListResult; }
int createFormalParameterList() { return FormalParameterListResult; }
void appendParameter(int, DestructuringPattern, int) { }
int createClause(int, int) { return ClauseResult; }
int createClauseList(int) { return ClauseListResult; }
int createClauseList(int, int) { return ClauseListResult; }
int createFuncDeclStatement(const JSTokenLocation&, const ParserFunctionInfo<SyntaxChecker>&) { return StatementResult; }
int createClassDeclStatement(const JSTokenLocation&, ClassExpression,
const JSTextPosition&, const JSTextPosition&, int, int) { return StatementResult; }
int createBlockStatement(const JSTokenLocation&, int, int, int, VariableEnvironment&) { return StatementResult; }
int createExprStatement(const JSTokenLocation&, int, int, int) { return StatementResult; }
int createIfStatement(const JSTokenLocation&, int, int, int, int) { return StatementResult; }
int createIfStatement(const JSTokenLocation&, int, int, int, int, int) { return StatementResult; }
int createForLoop(const JSTokenLocation&, int, int, int, int, int, int, VariableEnvironment&) { return StatementResult; }
int createForInLoop(const JSTokenLocation&, int, int, int, int, int, int, int, int, VariableEnvironment&) { return StatementResult; }
int createForOfLoop(const JSTokenLocation&, int, int, int, int, int, int, int, int, VariableEnvironment&) { return StatementResult; }
int createEmptyStatement(const JSTokenLocation&) { return StatementResult; }
int createDeclarationStatement(const JSTokenLocation&, int, int, int) { return StatementResult; }
int createReturnStatement(const JSTokenLocation&, int, int, int) { return StatementResult; }
int createBreakStatement(const JSTokenLocation&, int, int) { return StatementResult; }
int createBreakStatement(const JSTokenLocation&, const Identifier*, int, int) { return StatementResult; }
int createContinueStatement(const JSTokenLocation&, int, int) { return StatementResult; }
int createContinueStatement(const JSTokenLocation&, const Identifier*, int, int) { return StatementResult; }
int createTryStatement(const JSTokenLocation&, int, int, int, int, int, int, VariableEnvironment&) { return StatementResult; }
int createSwitchStatement(const JSTokenLocation&, int, int, int, int, int, int, VariableEnvironment&) { return StatementResult; }
int createWhileStatement(const JSTokenLocation&, int, int, int, int) { return StatementResult; }
int createWithStatement(const JSTokenLocation&, int, int, int, int, int, int) { return StatementResult; }
int createDoWhileStatement(const JSTokenLocation&, int, int, int, int) { return StatementResult; }
int createLabelStatement(const JSTokenLocation&, const Identifier*, int, int, int) { return StatementResult; }
int createThrowStatement(const JSTokenLocation&, int, int, int) { return StatementResult; }
int createDebugger(const JSTokenLocation&, int, int) { return StatementResult; }
int createConstStatement(const JSTokenLocation&, int, int, int) { return StatementResult; }
int createModuleName(const JSTokenLocation&, const Identifier&) { return ModuleNameResult; }
ImportSpecifier createImportSpecifier(const JSTokenLocation&, const Identifier&, const Identifier&) { return ImportSpecifierResult; }
ImportSpecifierList createImportSpecifierList() { return ImportSpecifierListResult; }
void appendImportSpecifier(ImportSpecifierList, ImportSpecifier) { }
int createImportDeclaration(const JSTokenLocation&, ImportSpecifierList, ModuleName) { return StatementResult; }
int createExportAllDeclaration(const JSTokenLocation&, ModuleName) { return StatementResult; }
int createExportDefaultDeclaration(const JSTokenLocation&, int, const Identifier&) { return StatementResult; }
int createExportLocalDeclaration(const JSTokenLocation&, int) { return StatementResult; }
int createExportNamedDeclaration(const JSTokenLocation&, ExportSpecifierList, ModuleName) { return StatementResult; }
ExportSpecifier createExportSpecifier(const JSTokenLocation&, const Identifier&, const Identifier&) { return ExportSpecifierResult; }
ExportSpecifierList createExportSpecifierList() { return ExportSpecifierListResult; }
void appendExportSpecifier(ExportSpecifierList, ExportSpecifier) { }
int appendConstDecl(const JSTokenLocation&, int, const Identifier*, int) { return StatementResult; }
Property createGetterOrSetterProperty(const JSTokenLocation&, PropertyNode::Type type, bool strict, const Identifier* name, const ParserFunctionInfo<SyntaxChecker>&, SuperBinding)
{
ASSERT(name);
if (!strict)
return Property(type);
return Property(name, type);
}
Property createGetterOrSetterProperty(const JSTokenLocation&, PropertyNode::Type type, bool, int, const ParserFunctionInfo<SyntaxChecker>&, SuperBinding)
{
return Property(type);
}
Property createGetterOrSetterProperty(VM* vm, ParserArena& parserArena, const JSTokenLocation&, PropertyNode::Type type, bool strict, double name, const ParserFunctionInfo<SyntaxChecker>&, SuperBinding)
{
if (!strict)
return Property(type);
return Property(&parserArena.identifierArena().makeNumericIdentifier(vm, name), type);
}
void appendStatement(int, int) { }
int combineCommaNodes(const JSTokenLocation&, int, int) { return CommaExpr; }
int evalCount() const { return 0; }
void appendBinaryExpressionInfo(int& operandStackDepth, int expr, int, int, int, bool)
{
if (!m_topBinaryExpr)
m_topBinaryExpr = expr;
else
m_topBinaryExpr = BinaryExpr;
operandStackDepth++;
}
// Logic to handle datastructures used during parsing of binary expressions
void operatorStackPop(int& operatorStackDepth) { operatorStackDepth--; }
bool operatorStackHasHigherPrecedence(int&, int) { return true; }
BinaryOperand getFromOperandStack(int) { return m_topBinaryExpr; }
void shrinkOperandStackBy(int& operandStackDepth, int amount) { operandStackDepth -= amount; }
void appendBinaryOperation(const JSTokenLocation&, int& operandStackDepth, int&, BinaryOperand, BinaryOperand) { operandStackDepth++; }
void operatorStackAppend(int& operatorStackDepth, int, int) { operatorStackDepth++; }
int popOperandStack(int&) { int res = m_topBinaryExpr; m_topBinaryExpr = 0; return res; }
void appendUnaryToken(int& stackDepth, int tok, int) { stackDepth = 1; m_topUnaryToken = tok; }
int unaryTokenStackLastType(int&) { return m_topUnaryToken; }
JSTextPosition unaryTokenStackLastStart(int&) { return JSTextPosition(0, 0, 0); }
void unaryTokenStackRemoveLast(int& stackDepth) { stackDepth = 0; }
void assignmentStackAppend(int, int, int, int, int, Operator) { }
int createAssignment(const JSTokenLocation&, int, int, int, int, int) { RELEASE_ASSERT_NOT_REACHED(); return AssignmentExpr; }
const Identifier* getName(const Property& property) const { return property.name; }
PropertyNode::Type getType(const Property& property) const { return property.type; }
bool isResolve(ExpressionType expr) const { return expr == ResolveExpr || expr == ResolveEvalExpr; }
ExpressionType createDestructuringAssignment(const JSTokenLocation&, int, ExpressionType)
{
return DestructuringAssignment;
}
ArrayPattern createArrayPattern(const JSTokenLocation&)
{
return ArrayDestructuring;
}
void appendArrayPatternSkipEntry(ArrayPattern, const JSTokenLocation&)
{
}
void appendArrayPatternEntry(ArrayPattern, const JSTokenLocation&, DestructuringPattern, int)
{
}
void appendArrayPatternRestEntry(ArrayPattern, const JSTokenLocation&, DestructuringPattern)
{
}
void finishArrayPattern(ArrayPattern, const JSTextPosition&, const JSTextPosition&, const JSTextPosition&)
{
}
ObjectPattern createObjectPattern(const JSTokenLocation&)
{
return ObjectDestructuring;
}
void appendObjectPatternEntry(ArrayPattern, const JSTokenLocation&, bool, const Identifier&, DestructuringPattern, int)
{
}
void appendObjectPatternEntry(ArrayPattern, const JSTokenLocation&, Expression, DestructuringPattern, Expression)
{
}
DestructuringPattern createBindingLocation(const JSTokenLocation&, const Identifier&, const JSTextPosition&, const JSTextPosition&, AssignmentContext)
{
return BindingDestructuring;
}
RestPattern createRestParameter(const Identifier&, size_t, const JSTextPosition&, const JSTextPosition&)
{
return RestParameter;
}
DestructuringPattern createAssignmentElement(const Expression&, const JSTextPosition&, const JSTextPosition&)
{
return BindingDestructuring;
}
bool isBindingNode(DestructuringPattern pattern)
{
return pattern == BindingDestructuring;
}
bool isAssignmentLocation(ExpressionType type)
{
return type == ResolveExpr || type == DotExpr || type == BracketExpr;
}
bool isObjectLiteral(ExpressionType type)
{
return type == ObjectLiteralExpr;
}
bool isArrayLiteral(ExpressionType type)
{
return type == ArrayLiteralExpr;
}
bool isObjectOrArrayLiteral(ExpressionType type)
{
return isObjectLiteral(type) || isArrayLiteral(type);
}
void setEndOffset(int, int) { }
int endOffset(int) { return 0; }
void setStartOffset(int, int) { }
void propagateArgumentsUse() { }
private:
int m_topBinaryExpr;
int m_topUnaryToken;
Vector<int, 8> m_topBinaryExprs;
Vector<int, 8> m_topUnaryTokens;
};
}
#endif
|