From e15dd966d523731101f70ccf768bba12435a0208 Mon Sep 17 00:00:00 2001 From: Lorry Tar Creator Date: Thu, 15 Oct 2015 09:45:50 +0000 Subject: webkitgtk-2.10.2 --- Source/JavaScriptCore/parser/ASTBuilder.h | 1253 +++++++ Source/JavaScriptCore/parser/Keywords.table | 57 + Source/JavaScriptCore/parser/Lexer.cpp | 2417 +++++++++++++ Source/JavaScriptCore/parser/Lexer.h | 389 ++ Source/JavaScriptCore/parser/ModuleAnalyzer.cpp | 155 + Source/JavaScriptCore/parser/ModuleAnalyzer.h | 62 + Source/JavaScriptCore/parser/ModuleRecord.cpp | 95 + Source/JavaScriptCore/parser/ModuleRecord.h | 128 + Source/JavaScriptCore/parser/NodeConstructors.h | 1018 ++++++ Source/JavaScriptCore/parser/Nodes.cpp | 205 ++ Source/JavaScriptCore/parser/Nodes.h | 2127 +++++++++++ .../JavaScriptCore/parser/NodesAnalyzeModule.cpp | 96 + Source/JavaScriptCore/parser/Parser.cpp | 3700 ++++++++++++++++++++ Source/JavaScriptCore/parser/Parser.h | 1385 ++++++++ Source/JavaScriptCore/parser/ParserArena.cpp | 76 + Source/JavaScriptCore/parser/ParserArena.h | 193 + Source/JavaScriptCore/parser/ParserError.h | 124 + Source/JavaScriptCore/parser/ParserFunctionInfo.h | 53 + Source/JavaScriptCore/parser/ParserModes.h | 157 + Source/JavaScriptCore/parser/ParserTokens.h | 234 ++ Source/JavaScriptCore/parser/ResultType.h | 189 + Source/JavaScriptCore/parser/SourceCode.cpp | 43 + Source/JavaScriptCore/parser/SourceCode.h | 129 + Source/JavaScriptCore/parser/SourceProvider.cpp | 59 + Source/JavaScriptCore/parser/SourceProvider.h | 130 + .../JavaScriptCore/parser/SourceProviderCache.cpp | 48 + Source/JavaScriptCore/parser/SourceProviderCache.h | 51 + .../parser/SourceProviderCacheItem.h | 153 + Source/JavaScriptCore/parser/SyntaxChecker.h | 372 ++ .../JavaScriptCore/parser/VariableEnvironment.cpp | 98 + Source/JavaScriptCore/parser/VariableEnvironment.h | 98 + 31 files changed, 15294 insertions(+) create mode 100644 Source/JavaScriptCore/parser/ASTBuilder.h create mode 100644 Source/JavaScriptCore/parser/Keywords.table create mode 100644 Source/JavaScriptCore/parser/Lexer.cpp create mode 100644 Source/JavaScriptCore/parser/Lexer.h create mode 100644 Source/JavaScriptCore/parser/ModuleAnalyzer.cpp create mode 100644 Source/JavaScriptCore/parser/ModuleAnalyzer.h create mode 100644 Source/JavaScriptCore/parser/ModuleRecord.cpp create mode 100644 Source/JavaScriptCore/parser/ModuleRecord.h create mode 100644 Source/JavaScriptCore/parser/NodeConstructors.h create mode 100644 Source/JavaScriptCore/parser/Nodes.cpp create mode 100644 Source/JavaScriptCore/parser/Nodes.h create mode 100644 Source/JavaScriptCore/parser/NodesAnalyzeModule.cpp create mode 100644 Source/JavaScriptCore/parser/Parser.cpp create mode 100644 Source/JavaScriptCore/parser/Parser.h create mode 100644 Source/JavaScriptCore/parser/ParserArena.cpp create mode 100644 Source/JavaScriptCore/parser/ParserArena.h create mode 100644 Source/JavaScriptCore/parser/ParserError.h create mode 100644 Source/JavaScriptCore/parser/ParserFunctionInfo.h create mode 100644 Source/JavaScriptCore/parser/ParserModes.h create mode 100644 Source/JavaScriptCore/parser/ParserTokens.h create mode 100644 Source/JavaScriptCore/parser/ResultType.h create mode 100644 Source/JavaScriptCore/parser/SourceCode.cpp create mode 100644 Source/JavaScriptCore/parser/SourceCode.h create mode 100644 Source/JavaScriptCore/parser/SourceProvider.cpp create mode 100644 Source/JavaScriptCore/parser/SourceProvider.h create mode 100644 Source/JavaScriptCore/parser/SourceProviderCache.cpp create mode 100644 Source/JavaScriptCore/parser/SourceProviderCache.h create mode 100644 Source/JavaScriptCore/parser/SourceProviderCacheItem.h create mode 100644 Source/JavaScriptCore/parser/SyntaxChecker.h create mode 100644 Source/JavaScriptCore/parser/VariableEnvironment.cpp create mode 100644 Source/JavaScriptCore/parser/VariableEnvironment.h (limited to 'Source/JavaScriptCore/parser') diff --git a/Source/JavaScriptCore/parser/ASTBuilder.h b/Source/JavaScriptCore/parser/ASTBuilder.h new file mode 100644 index 000000000..7b587730a --- /dev/null +++ b/Source/JavaScriptCore/parser/ASTBuilder.h @@ -0,0 +1,1253 @@ +/* + * 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 ASTBuilder_h +#define ASTBuilder_h + +#include "BuiltinNames.h" +#include "BytecodeIntrinsicRegistry.h" +#include "NodeConstructors.h" +#include "SyntaxChecker.h" +#include "VariableEnvironment.h" +#include + +namespace JSC { + +class ASTBuilder { + struct BinaryOpInfo { + BinaryOpInfo() {} + BinaryOpInfo(const JSTextPosition& otherStart, const JSTextPosition& otherDivot, const JSTextPosition& otherEnd, bool rhsHasAssignment) + : start(otherStart) + , divot(otherDivot) + , end(otherEnd) + , hasAssignment(rhsHasAssignment) + { + } + BinaryOpInfo(const BinaryOpInfo& lhs, const BinaryOpInfo& rhs) + : start(lhs.start) + , divot(rhs.start) + , end(rhs.end) + , hasAssignment(lhs.hasAssignment || rhs.hasAssignment) + { + } + JSTextPosition start; + JSTextPosition divot; + JSTextPosition end; + bool hasAssignment; + }; + + + struct AssignmentInfo { + AssignmentInfo() {} + AssignmentInfo(ExpressionNode* node, const JSTextPosition& start, const JSTextPosition& divot, int initAssignments, Operator op) + : m_node(node) + , m_start(start) + , m_divot(divot) + , m_initAssignments(initAssignments) + , m_op(op) + { + ASSERT(m_divot.offset >= m_divot.lineStartOffset); + ASSERT(m_start.offset >= m_start.lineStartOffset); + } + ExpressionNode* m_node; + JSTextPosition m_start; + JSTextPosition m_divot; + int m_initAssignments; + Operator m_op; + }; +public: + ASTBuilder(VM* vm, ParserArena& parserArena, SourceCode* sourceCode) + : m_vm(vm) + , m_parserArena(parserArena) + , m_sourceCode(sourceCode) + , m_evalCount(0) + { + } + + struct BinaryExprContext { + BinaryExprContext(ASTBuilder&) {} + }; + struct UnaryExprContext { + UnaryExprContext(ASTBuilder&) {} + }; + + typedef ExpressionNode* Expression; + typedef JSC::SourceElements* SourceElements; + typedef ArgumentsNode* Arguments; + typedef CommaNode* Comma; + typedef PropertyNode* Property; + typedef PropertyListNode* PropertyList; + typedef ElementNode* ElementList; + typedef ArgumentListNode* ArgumentsList; +#if ENABLE(ES6_TEMPLATE_LITERAL_SYNTAX) + typedef TemplateExpressionListNode* TemplateExpressionList; + typedef TemplateStringNode* TemplateString; + typedef TemplateStringListNode* TemplateStringList; + typedef TemplateLiteralNode* TemplateLiteral; +#endif + typedef FunctionParameters* FormalParameterList; + typedef FunctionMetadataNode* FunctionBody; +#if ENABLE(ES6_CLASS_SYNTAX) + typedef ClassExprNode* ClassExpression; +#endif + typedef ModuleNameNode* ModuleName; + typedef ImportSpecifierNode* ImportSpecifier; + typedef ImportSpecifierListNode* ImportSpecifierList; + typedef ExportSpecifierNode* ExportSpecifier; + typedef ExportSpecifierListNode* ExportSpecifierList; + typedef StatementNode* Statement; + typedef ClauseListNode* ClauseList; + typedef CaseClauseNode* Clause; + typedef std::pair BinaryOperand; + typedef DestructuringPatternNode* DestructuringPattern; + typedef ArrayPatternNode* ArrayPattern; + typedef ObjectPatternNode* ObjectPattern; + typedef BindingNode* BindingPattern; + static const bool CreatesAST = true; + static const bool NeedsFreeVariableInfo = true; + static const bool CanUseFunctionCache = true; + static const int DontBuildKeywords = 0; + static const int DontBuildStrings = 0; + + ExpressionNode* makeBinaryNode(const JSTokenLocation&, int token, std::pair, std::pair); + ExpressionNode* makeFunctionCallNode(const JSTokenLocation&, ExpressionNode* func, ArgumentsNode* args, const JSTextPosition& divotStart, const JSTextPosition& divot, const JSTextPosition& divotEnd); + + JSC::SourceElements* createSourceElements() { return new (m_parserArena) JSC::SourceElements(); } + + DeclarationStacks::FunctionStack& funcDeclarations() { return m_scope.m_funcDeclarations; } + int features() const { return m_scope.m_features; } + int numConstants() const { return m_scope.m_numConstants; } + + ExpressionNode* makeAssignNode(const JSTokenLocation&, ExpressionNode* left, Operator, ExpressionNode* right, bool leftHasAssignments, bool rightHasAssignments, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end); + ExpressionNode* makePrefixNode(const JSTokenLocation&, ExpressionNode*, Operator, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end); + ExpressionNode* makePostfixNode(const JSTokenLocation&, ExpressionNode*, Operator, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end); + ExpressionNode* makeTypeOfNode(const JSTokenLocation&, ExpressionNode*); + ExpressionNode* makeDeleteNode(const JSTokenLocation&, ExpressionNode*, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end); + ExpressionNode* makeNegateNode(const JSTokenLocation&, ExpressionNode*); + ExpressionNode* makeBitwiseNotNode(const JSTokenLocation&, ExpressionNode*); + ExpressionNode* makeMultNode(const JSTokenLocation&, ExpressionNode* left, ExpressionNode* right, bool rightHasAssignments); + ExpressionNode* makeDivNode(const JSTokenLocation&, ExpressionNode* left, ExpressionNode* right, bool rightHasAssignments); + ExpressionNode* makeModNode(const JSTokenLocation&, ExpressionNode* left, ExpressionNode* right, bool rightHasAssignments); + ExpressionNode* makeAddNode(const JSTokenLocation&, ExpressionNode* left, ExpressionNode* right, bool rightHasAssignments); + ExpressionNode* makeSubNode(const JSTokenLocation&, ExpressionNode* left, ExpressionNode* right, bool rightHasAssignments); + ExpressionNode* makeBitXOrNode(const JSTokenLocation&, ExpressionNode* left, ExpressionNode* right, bool rightHasAssignments); + ExpressionNode* makeBitAndNode(const JSTokenLocation&, ExpressionNode* left, ExpressionNode* right, bool rightHasAssignments); + ExpressionNode* makeBitOrNode(const JSTokenLocation&, ExpressionNode* left, ExpressionNode* right, bool rightHasAssignments); + ExpressionNode* makeLeftShiftNode(const JSTokenLocation&, ExpressionNode* left, ExpressionNode* right, bool rightHasAssignments); + ExpressionNode* makeRightShiftNode(const JSTokenLocation&, ExpressionNode* left, ExpressionNode* right, bool rightHasAssignments); + ExpressionNode* makeURightShiftNode(const JSTokenLocation&, ExpressionNode* left, ExpressionNode* right, bool rightHasAssignments); + + ExpressionNode* createLogicalNot(const JSTokenLocation& location, ExpressionNode* expr) + { + if (expr->isNumber()) + return createBoolean(location, isZeroOrUnordered(static_cast(expr)->value())); + + return new (m_parserArena) LogicalNotNode(location, expr); + } + ExpressionNode* createUnaryPlus(const JSTokenLocation& location, ExpressionNode* expr) { return new (m_parserArena) UnaryPlusNode(location, expr); } + ExpressionNode* createVoid(const JSTokenLocation& location, ExpressionNode* expr) + { + incConstants(); + return new (m_parserArena) VoidNode(location, expr); + } + ExpressionNode* createThisExpr(const JSTokenLocation& location, ThisTDZMode thisTDZMode) + { + usesThis(); + return new (m_parserArena) ThisNode(location, thisTDZMode); + } + ExpressionNode* createSuperExpr(const JSTokenLocation& location) + { + return new (m_parserArena) SuperNode(location); + } + ExpressionNode* createNewTargetExpr(const JSTokenLocation location) + { + return new (m_parserArena) NewTargetNode(location); + } + ExpressionNode* createResolve(const JSTokenLocation& location, const Identifier* ident, const JSTextPosition& start) + { + if (m_vm->propertyNames->arguments == *ident) + usesArguments(); + return new (m_parserArena) ResolveNode(location, *ident, start); + } + ExpressionNode* createObjectLiteral(const JSTokenLocation& location) { return new (m_parserArena) ObjectLiteralNode(location); } + ExpressionNode* createObjectLiteral(const JSTokenLocation& location, PropertyListNode* properties) { return new (m_parserArena) ObjectLiteralNode(location, properties); } + + ExpressionNode* createArray(const JSTokenLocation& location, int elisions) + { + if (elisions) + incConstants(); + return new (m_parserArena) ArrayNode(location, elisions); + } + + ExpressionNode* createArray(const JSTokenLocation& location, ElementNode* elems) { return new (m_parserArena) ArrayNode(location, elems); } + ExpressionNode* createArray(const JSTokenLocation& location, int elisions, ElementNode* elems) + { + if (elisions) + incConstants(); + return new (m_parserArena) ArrayNode(location, elisions, elems); + } + ExpressionNode* createDoubleExpr(const JSTokenLocation& location, double d) + { + incConstants(); + return new (m_parserArena) DoubleNode(location, d); + } + ExpressionNode* createIntegerExpr(const JSTokenLocation& location, double d) + { + incConstants(); + return new (m_parserArena) IntegerNode(location, d); + } + + ExpressionNode* createString(const JSTokenLocation& location, const Identifier* string) + { + incConstants(); + return new (m_parserArena) StringNode(location, *string); + } + + ExpressionNode* createBoolean(const JSTokenLocation& location, bool b) + { + incConstants(); + return new (m_parserArena) BooleanNode(location, b); + } + + ExpressionNode* createNull(const JSTokenLocation& location) + { + incConstants(); + return new (m_parserArena) NullNode(location); + } + + ExpressionNode* createBracketAccess(const JSTokenLocation& location, ExpressionNode* base, ExpressionNode* property, bool propertyHasAssignments, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end) + { + BracketAccessorNode* node = new (m_parserArena) BracketAccessorNode(location, base, property, propertyHasAssignments); + setExceptionLocation(node, start, divot, end); + return node; + } + + ExpressionNode* createDotAccess(const JSTokenLocation& location, ExpressionNode* base, const Identifier* property, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end) + { + DotAccessorNode* node = new (m_parserArena) DotAccessorNode(location, base, *property); + setExceptionLocation(node, start, divot, end); + return node; + } + + ExpressionNode* createSpreadExpression(const JSTokenLocation& location, ExpressionNode* expression, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end) + { + auto node = new (m_parserArena) SpreadExpressionNode(location, expression); + setExceptionLocation(node, start, divot, end); + return node; + } + +#if ENABLE(ES6_TEMPLATE_LITERAL_SYNTAX) + TemplateStringNode* createTemplateString(const JSTokenLocation& location, const Identifier& cooked, const Identifier& raw) + { + return new (m_parserArena) TemplateStringNode(location, cooked, raw); + } + + TemplateStringListNode* createTemplateStringList(TemplateStringNode* templateString) + { + return new (m_parserArena) TemplateStringListNode(templateString); + } + + TemplateStringListNode* createTemplateStringList(TemplateStringListNode* templateStringList, TemplateStringNode* templateString) + { + return new (m_parserArena) TemplateStringListNode(templateStringList, templateString); + } + + TemplateExpressionListNode* createTemplateExpressionList(ExpressionNode* expression) + { + return new (m_parserArena) TemplateExpressionListNode(expression); + } + + TemplateExpressionListNode* createTemplateExpressionList(TemplateExpressionListNode* templateExpressionListNode, ExpressionNode* expression) + { + return new (m_parserArena) TemplateExpressionListNode(templateExpressionListNode, expression); + } + + TemplateLiteralNode* createTemplateLiteral(const JSTokenLocation& location, TemplateStringListNode* templateStringList) + { + return new (m_parserArena) TemplateLiteralNode(location, templateStringList); + } + + TemplateLiteralNode* createTemplateLiteral(const JSTokenLocation& location, TemplateStringListNode* templateStringList, TemplateExpressionListNode* templateExpressionList) + { + return new (m_parserArena) TemplateLiteralNode(location, templateStringList, templateExpressionList); + } + + ExpressionNode* createTaggedTemplate(const JSTokenLocation& location, ExpressionNode* base, TemplateLiteralNode* templateLiteral, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end) + { + auto node = new (m_parserArena) TaggedTemplateNode(location, base, templateLiteral); + setExceptionLocation(node, start, divot, end); + return node; + } +#endif + + ExpressionNode* createRegExp(const JSTokenLocation& location, const Identifier& pattern, const Identifier& flags, const JSTextPosition& start) + { + if (Yarr::checkSyntax(pattern.string())) + return 0; + RegExpNode* node = new (m_parserArena) RegExpNode(location, pattern, flags); + int size = pattern.length() + 2; // + 2 for the two /'s + JSTextPosition end = start + size; + setExceptionLocation(node, start, end, end); + return node; + } + + ExpressionNode* createNewExpr(const JSTokenLocation& location, ExpressionNode* expr, ArgumentsNode* arguments, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end) + { + NewExprNode* node = new (m_parserArena) NewExprNode(location, expr, arguments); + setExceptionLocation(node, start, divot, end); + return node; + } + + ExpressionNode* createNewExpr(const JSTokenLocation& location, ExpressionNode* expr, const JSTextPosition& start, const JSTextPosition& end) + { + NewExprNode* node = new (m_parserArena) NewExprNode(location, expr); + setExceptionLocation(node, start, end, end); + return node; + } + + ExpressionNode* createConditionalExpr(const JSTokenLocation& location, ExpressionNode* condition, ExpressionNode* lhs, ExpressionNode* rhs) + { + return new (m_parserArena) ConditionalNode(location, condition, lhs, rhs); + } + + ExpressionNode* createAssignResolve(const JSTokenLocation& location, const Identifier& ident, ExpressionNode* rhs, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end, AssignmentContext assignmentContext) + { + if (rhs->isFuncExprNode()) + static_cast(rhs)->metadata()->setInferredName(ident); + AssignResolveNode* node = new (m_parserArena) AssignResolveNode(location, ident, rhs, assignmentContext); + setExceptionLocation(node, start, divot, end); + return node; + } + +#if ENABLE(ES6_CLASS_SYNTAX) + ClassExprNode* createClassExpr(const JSTokenLocation& location, const Identifier& name, ExpressionNode* constructor, + ExpressionNode* parentClass, PropertyListNode* instanceMethods, PropertyListNode* staticMethods) + { + return new (m_parserArena) ClassExprNode(location, name, constructor, parentClass, instanceMethods, staticMethods); + } +#endif + + ExpressionNode* createFunctionExpr(const JSTokenLocation& location, const ParserFunctionInfo& functionInfo) + { + FuncExprNode* result = new (m_parserArena) FuncExprNode(location, *functionInfo.name, functionInfo.body, + m_sourceCode->subExpression(functionInfo.startOffset, functionInfo.endOffset, functionInfo.startLine, functionInfo.bodyStartColumn)); + functionInfo.body->setLoc(functionInfo.startLine, functionInfo.endLine, location.startOffset, location.lineStartOffset); + return result; + } + + FunctionMetadataNode* createFunctionMetadata( + const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, + unsigned startColumn, unsigned endColumn, int functionKeywordStart, + int functionNameStart, int parametersStart, bool inStrictContext, + ConstructorKind constructorKind, unsigned parameterCount, SourceParseMode mode) + { + return new (m_parserArena) FunctionMetadataNode( + m_parserArena, startLocation, endLocation, startColumn, endColumn, + functionKeywordStart, functionNameStart, parametersStart, + inStrictContext, constructorKind, parameterCount, mode); + } + + ExpressionNode* createArrowFunctionExpr(const JSTokenLocation& location, const ParserFunctionInfo& functionInfo) + { + SourceCode source = m_sourceCode->subExpression(functionInfo.startOffset, functionInfo.endOffset, functionInfo.startLine, functionInfo.bodyStartColumn); + + FuncExprNode* result = new (m_parserArena) FuncExprNode(location, *functionInfo.name, functionInfo.body, source); + functionInfo.body->setLoc(functionInfo.startLine, functionInfo.endLine, location.startOffset, location.lineStartOffset); + return result; + } + + NEVER_INLINE PropertyNode* createGetterOrSetterProperty(const JSTokenLocation& location, PropertyNode::Type type, bool, + const Identifier* name, const ParserFunctionInfo& functionInfo, SuperBinding superBinding) + { + ASSERT(name); + functionInfo.body->setLoc(functionInfo.startLine, functionInfo.endLine, location.startOffset, location.lineStartOffset); + functionInfo.body->setInferredName(*name); + SourceCode source = m_sourceCode->subExpression(functionInfo.startOffset, functionInfo.endOffset, functionInfo.startLine, functionInfo.bodyStartColumn); + FuncExprNode* funcExpr = new (m_parserArena) FuncExprNode(location, m_vm->propertyNames->nullIdentifier, functionInfo.body, source); + return new (m_parserArena) PropertyNode(*name, funcExpr, type, PropertyNode::Unknown, superBinding); + } + + NEVER_INLINE PropertyNode* createGetterOrSetterProperty(VM* vm, ParserArena& parserArena, const JSTokenLocation& location, PropertyNode::Type type, bool, + double name, const ParserFunctionInfo& functionInfo, SuperBinding superBinding) + { + functionInfo.body->setLoc(functionInfo.startLine, functionInfo.endLine, location.startOffset, location.lineStartOffset); + const Identifier& ident = parserArena.identifierArena().makeNumericIdentifier(vm, name); + SourceCode source = m_sourceCode->subExpression(functionInfo.startOffset, functionInfo.endOffset, functionInfo.startLine, functionInfo.bodyStartColumn); + FuncExprNode* funcExpr = new (m_parserArena) FuncExprNode(location, vm->propertyNames->nullIdentifier, functionInfo.body, source); + return new (m_parserArena) PropertyNode(ident, funcExpr, type, PropertyNode::Unknown, superBinding); + } + + ArgumentsNode* createArguments() { return new (m_parserArena) ArgumentsNode(); } + ArgumentsNode* createArguments(ArgumentListNode* args) { return new (m_parserArena) ArgumentsNode(args); } + ArgumentListNode* createArgumentsList(const JSTokenLocation& location, ExpressionNode* arg) { return new (m_parserArena) ArgumentListNode(location, arg); } + ArgumentListNode* createArgumentsList(const JSTokenLocation& location, ArgumentListNode* args, ExpressionNode* arg) { return new (m_parserArena) ArgumentListNode(location, args, arg); } + + PropertyNode* createProperty(const Identifier* propertyName, ExpressionNode* node, PropertyNode::Type type, PropertyNode::PutType putType, bool, SuperBinding superBinding = SuperBinding::NotNeeded) + { + if (node->isFuncExprNode()) + static_cast(node)->metadata()->setInferredName(*propertyName); + return new (m_parserArena) PropertyNode(*propertyName, node, type, putType, superBinding); + } + PropertyNode* createProperty(VM* vm, ParserArena& parserArena, double propertyName, ExpressionNode* node, PropertyNode::Type type, PropertyNode::PutType putType, bool) + { + return new (m_parserArena) PropertyNode(parserArena.identifierArena().makeNumericIdentifier(vm, propertyName), node, type, putType); + } + PropertyNode* createProperty(ExpressionNode* propertyName, ExpressionNode* node, PropertyNode::Type type, PropertyNode::PutType putType, bool) { return new (m_parserArena) PropertyNode(propertyName, node, type, putType); } + PropertyListNode* createPropertyList(const JSTokenLocation& location, PropertyNode* property) { return new (m_parserArena) PropertyListNode(location, property); } + PropertyListNode* createPropertyList(const JSTokenLocation& location, PropertyNode* property, PropertyListNode* tail) { return new (m_parserArena) PropertyListNode(location, property, tail); } + + ElementNode* createElementList(int elisions, ExpressionNode* expr) { return new (m_parserArena) ElementNode(elisions, expr); } + ElementNode* createElementList(ElementNode* elems, int elisions, ExpressionNode* expr) { return new (m_parserArena) ElementNode(elems, elisions, expr); } + + FormalParameterList createFormalParameterList() { return new (m_parserArena) FunctionParameters(); } + void appendParameter(FormalParameterList list, DestructuringPattern pattern, ExpressionNode* defaultValue) + { + list->append(pattern, defaultValue); + } + + CaseClauseNode* createClause(ExpressionNode* expr, JSC::SourceElements* statements) { return new (m_parserArena) CaseClauseNode(expr, statements); } + ClauseListNode* createClauseList(CaseClauseNode* clause) { return new (m_parserArena) ClauseListNode(clause); } + ClauseListNode* createClauseList(ClauseListNode* tail, CaseClauseNode* clause) { return new (m_parserArena) ClauseListNode(tail, clause); } + + StatementNode* createFuncDeclStatement(const JSTokenLocation& location, const ParserFunctionInfo& functionInfo) + { + FuncDeclNode* decl = new (m_parserArena) FuncDeclNode(location, *functionInfo.name, functionInfo.body, + m_sourceCode->subExpression(functionInfo.startOffset, functionInfo.endOffset, functionInfo.startLine, functionInfo.bodyStartColumn)); + if (*functionInfo.name == m_vm->propertyNames->arguments) + usesArguments(); + m_scope.m_funcDeclarations.append(decl->metadata()); + functionInfo.body->setLoc(functionInfo.startLine, functionInfo.endLine, location.startOffset, location.lineStartOffset); + return decl; + } + +#if ENABLE(ES6_CLASS_SYNTAX) + StatementNode* createClassDeclStatement(const JSTokenLocation& location, ClassExprNode* classExpression, + const JSTextPosition& classStart, const JSTextPosition& classEnd, unsigned startLine, unsigned endLine) + { + ExpressionNode* assign = createAssignResolve(location, classExpression->name(), classExpression, classStart, classStart + 1, classEnd, AssignmentContext::DeclarationStatement); + ClassDeclNode* decl = new (m_parserArena) ClassDeclNode(location, assign); + decl->setLoc(startLine, endLine, location.startOffset, location.lineStartOffset); + return decl; + } +#endif + + StatementNode* createBlockStatement(const JSTokenLocation& location, JSC::SourceElements* elements, int startLine, int endLine, VariableEnvironment& lexicalVariables) + { + BlockNode* block = new (m_parserArena) BlockNode(location, elements, lexicalVariables); + block->setLoc(startLine, endLine, location.startOffset, location.lineStartOffset); + return block; + } + + StatementNode* createExprStatement(const JSTokenLocation& location, ExpressionNode* expr, const JSTextPosition& start, int end) + { + ExprStatementNode* result = new (m_parserArena) ExprStatementNode(location, expr); + result->setLoc(start.line, end, start.offset, start.lineStartOffset); + return result; + } + + StatementNode* createIfStatement(const JSTokenLocation& location, ExpressionNode* condition, StatementNode* trueBlock, StatementNode* falseBlock, int start, int end) + { + IfElseNode* result = new (m_parserArena) IfElseNode(location, condition, trueBlock, falseBlock); + result->setLoc(start, end, location.startOffset, location.lineStartOffset); + return result; + } + + StatementNode* createForLoop(const JSTokenLocation& location, ExpressionNode* initializer, ExpressionNode* condition, ExpressionNode* iter, StatementNode* statements, int start, int end, VariableEnvironment& lexicalVariables) + { + ForNode* result = new (m_parserArena) ForNode(location, initializer, condition, iter, statements, lexicalVariables); + result->setLoc(start, end, location.startOffset, location.lineStartOffset); + return result; + } + + StatementNode* createForInLoop(const JSTokenLocation& location, ExpressionNode* lhs, ExpressionNode* iter, StatementNode* statements, const JSTextPosition& eStart, const JSTextPosition& eDivot, const JSTextPosition& eEnd, int start, int end, VariableEnvironment& lexicalVariables) + { + ForInNode* result = new (m_parserArena) ForInNode(location, lhs, iter, statements, lexicalVariables); + result->setLoc(start, end, location.startOffset, location.lineStartOffset); + setExceptionLocation(result, eStart, eDivot, eEnd); + return result; + } + + StatementNode* createForInLoop(const JSTokenLocation& location, DestructuringPatternNode* pattern, ExpressionNode* iter, StatementNode* statements, const JSTextPosition& eStart, const JSTextPosition& eDivot, const JSTextPosition& eEnd, int start, int end, VariableEnvironment& lexicalVariables) + { + auto lexpr = new (m_parserArena) DestructuringAssignmentNode(location, pattern, 0); + return createForInLoop(location, lexpr, iter, statements, eStart, eDivot, eEnd, start, end, lexicalVariables); + } + + StatementNode* createForOfLoop(const JSTokenLocation& location, ExpressionNode* lhs, ExpressionNode* iter, StatementNode* statements, const JSTextPosition& eStart, const JSTextPosition& eDivot, const JSTextPosition& eEnd, int start, int end, VariableEnvironment& lexicalVariables) + { + ForOfNode* result = new (m_parserArena) ForOfNode(location, lhs, iter, statements, lexicalVariables); + result->setLoc(start, end, location.startOffset, location.lineStartOffset); + setExceptionLocation(result, eStart, eDivot, eEnd); + return result; + } + + StatementNode* createForOfLoop(const JSTokenLocation& location, DestructuringPatternNode* pattern, ExpressionNode* iter, StatementNode* statements, const JSTextPosition& eStart, const JSTextPosition& eDivot, const JSTextPosition& eEnd, int start, int end, VariableEnvironment& lexicalVariables) + { + auto lexpr = new (m_parserArena) DestructuringAssignmentNode(location, pattern, 0); + return createForOfLoop(location, lexpr, iter, statements, eStart, eDivot, eEnd, start, end, lexicalVariables); + } + + bool isBindingNode(const DestructuringPattern& pattern) + { + return pattern->isBindingNode(); + } + + StatementNode* createEmptyStatement(const JSTokenLocation& location) { return new (m_parserArena) EmptyStatementNode(location); } + + StatementNode* createDeclarationStatement(const JSTokenLocation& location, ExpressionNode* expr, int start, int end) + { + StatementNode* result; + result = new (m_parserArena) DeclarationStatement(location, expr); + result->setLoc(start, end, location.startOffset, location.lineStartOffset); + return result; + } + + ExpressionNode* createEmptyVarExpression(const JSTokenLocation& location, const Identifier& identifier) + { + return new (m_parserArena) EmptyVarExpression(location, identifier); + } + + ExpressionNode* createEmptyLetExpression(const JSTokenLocation& location, const Identifier& identifier) + { + return new (m_parserArena) EmptyLetExpression(location, identifier); + } + + StatementNode* createReturnStatement(const JSTokenLocation& location, ExpressionNode* expression, const JSTextPosition& start, const JSTextPosition& end) + { + ReturnNode* result = new (m_parserArena) ReturnNode(location, expression); + setExceptionLocation(result, start, end, end); + result->setLoc(start.line, end.line, start.offset, start.lineStartOffset); + return result; + } + + StatementNode* createBreakStatement(const JSTokenLocation& location, const Identifier* ident, const JSTextPosition& start, const JSTextPosition& end) + { + BreakNode* result = new (m_parserArena) BreakNode(location, *ident); + setExceptionLocation(result, start, end, end); + result->setLoc(start.line, end.line, start.offset, start.lineStartOffset); + return result; + } + + StatementNode* createContinueStatement(const JSTokenLocation& location, const Identifier* ident, const JSTextPosition& start, const JSTextPosition& end) + { + ContinueNode* result = new (m_parserArena) ContinueNode(location, *ident); + setExceptionLocation(result, start, end, end); + result->setLoc(start.line, end.line, start.offset, start.lineStartOffset); + return result; + } + + StatementNode* createTryStatement(const JSTokenLocation& location, StatementNode* tryBlock, const Identifier* ident, StatementNode* catchBlock, StatementNode* finallyBlock, int startLine, int endLine, VariableEnvironment& catchEnvironment) + { + TryNode* result = new (m_parserArena) TryNode(location, tryBlock, *ident, catchBlock, catchEnvironment, finallyBlock); + if (catchBlock) + usesCatch(); + result->setLoc(startLine, endLine, location.startOffset, location.lineStartOffset); + return result; + } + + StatementNode* createSwitchStatement(const JSTokenLocation& location, ExpressionNode* expr, ClauseListNode* firstClauses, CaseClauseNode* defaultClause, ClauseListNode* secondClauses, int startLine, int endLine, VariableEnvironment& lexicalVariables) + { + CaseBlockNode* cases = new (m_parserArena) CaseBlockNode(firstClauses, defaultClause, secondClauses); + SwitchNode* result = new (m_parserArena) SwitchNode(location, expr, cases, lexicalVariables); + result->setLoc(startLine, endLine, location.startOffset, location.lineStartOffset); + return result; + } + + StatementNode* createWhileStatement(const JSTokenLocation& location, ExpressionNode* expr, StatementNode* statement, int startLine, int endLine) + { + WhileNode* result = new (m_parserArena) WhileNode(location, expr, statement); + result->setLoc(startLine, endLine, location.startOffset, location.lineStartOffset); + return result; + } + + StatementNode* createDoWhileStatement(const JSTokenLocation& location, StatementNode* statement, ExpressionNode* expr, int startLine, int endLine) + { + DoWhileNode* result = new (m_parserArena) DoWhileNode(location, statement, expr); + result->setLoc(startLine, endLine, location.startOffset, location.lineStartOffset); + return result; + } + + StatementNode* createLabelStatement(const JSTokenLocation& location, const Identifier* ident, StatementNode* statement, const JSTextPosition& start, const JSTextPosition& end) + { + LabelNode* result = new (m_parserArena) LabelNode(location, *ident, statement); + setExceptionLocation(result, start, end, end); + return result; + } + + StatementNode* createWithStatement(const JSTokenLocation& location, ExpressionNode* expr, StatementNode* statement, unsigned start, const JSTextPosition& end, unsigned startLine, unsigned endLine) + { + usesWith(); + WithNode* result = new (m_parserArena) WithNode(location, expr, statement, end, end - start); + result->setLoc(startLine, endLine, location.startOffset, location.lineStartOffset); + return result; + } + + StatementNode* createThrowStatement(const JSTokenLocation& location, ExpressionNode* expr, const JSTextPosition& start, const JSTextPosition& end) + { + ThrowNode* result = new (m_parserArena) ThrowNode(location, expr); + result->setLoc(start.line, end.line, start.offset, start.lineStartOffset); + setExceptionLocation(result, start, end, end); + return result; + } + + StatementNode* createDebugger(const JSTokenLocation& location, int startLine, int endLine) + { + DebuggerStatementNode* result = new (m_parserArena) DebuggerStatementNode(location); + result->setLoc(startLine, endLine, location.startOffset, location.lineStartOffset); + return result; + } + + ModuleNameNode* createModuleName(const JSTokenLocation& location, const Identifier& moduleName) + { + return new (m_parserArena) ModuleNameNode(location, moduleName); + } + + ImportSpecifierNode* createImportSpecifier(const JSTokenLocation& location, const Identifier& importedName, const Identifier& localName) + { + return new (m_parserArena) ImportSpecifierNode(location, importedName, localName); + } + + ImportSpecifierListNode* createImportSpecifierList() + { + return new (m_parserArena) ImportSpecifierListNode(); + } + + void appendImportSpecifier(ImportSpecifierListNode* specifierList, ImportSpecifierNode* specifier) + { + specifierList->append(specifier); + } + + StatementNode* createImportDeclaration(const JSTokenLocation& location, ImportSpecifierListNode* importSpecifierList, ModuleNameNode* moduleName) + { + return new (m_parserArena) ImportDeclarationNode(location, importSpecifierList, moduleName); + } + + StatementNode* createExportAllDeclaration(const JSTokenLocation& location, ModuleNameNode* moduleName) + { + return new (m_parserArena) ExportAllDeclarationNode(location, moduleName); + } + + StatementNode* createExportDefaultDeclaration(const JSTokenLocation& location, StatementNode* declaration, const Identifier& localName) + { + return new (m_parserArena) ExportDefaultDeclarationNode(location, declaration, localName); + } + + StatementNode* createExportLocalDeclaration(const JSTokenLocation& location, StatementNode* declaration) + { + return new (m_parserArena) ExportLocalDeclarationNode(location, declaration); + } + + StatementNode* createExportNamedDeclaration(const JSTokenLocation& location, ExportSpecifierListNode* exportSpecifierList, ModuleNameNode* moduleName) + { + return new (m_parserArena) ExportNamedDeclarationNode(location, exportSpecifierList, moduleName); + } + + ExportSpecifierNode* createExportSpecifier(const JSTokenLocation& location, const Identifier& localName, const Identifier& exportedName) + { + return new (m_parserArena) ExportSpecifierNode(location, localName, exportedName); + } + + ExportSpecifierListNode* createExportSpecifierList() + { + return new (m_parserArena) ExportSpecifierListNode(); + } + + void appendExportSpecifier(ExportSpecifierListNode* specifierList, ExportSpecifierNode* specifier) + { + specifierList->append(specifier); + } + + void appendStatement(JSC::SourceElements* elements, JSC::StatementNode* statement) + { + elements->append(statement); + } + + CommaNode* createCommaExpr(const JSTokenLocation& location, ExpressionNode* node) + { + return new (m_parserArena) CommaNode(location, node); + } + + CommaNode* appendToCommaExpr(const JSTokenLocation& location, ExpressionNode*, ExpressionNode* tail, ExpressionNode* next) + { + ASSERT(tail->isCommaNode()); + CommaNode* newTail = new (m_parserArena) CommaNode(location, next); + static_cast(tail)->setNext(newTail); + return newTail; + } + + int evalCount() const { return m_evalCount; } + + void appendBinaryExpressionInfo(int& operandStackDepth, ExpressionNode* current, const JSTextPosition& exprStart, const JSTextPosition& lhs, const JSTextPosition& rhs, bool hasAssignments) + { + operandStackDepth++; + m_binaryOperandStack.append(std::make_pair(current, BinaryOpInfo(exprStart, lhs, rhs, hasAssignments))); + } + + // Logic to handle datastructures used during parsing of binary expressions + void operatorStackPop(int& operatorStackDepth) + { + operatorStackDepth--; + m_binaryOperatorStack.removeLast(); + } + bool operatorStackHasHigherPrecedence(int&, int precedence) + { + return precedence <= m_binaryOperatorStack.last().second; + } + const BinaryOperand& getFromOperandStack(int i) { return m_binaryOperandStack[m_binaryOperandStack.size() + i]; } + void shrinkOperandStackBy(int& operandStackDepth, int amount) + { + operandStackDepth -= amount; + ASSERT(operandStackDepth >= 0); + m_binaryOperandStack.resize(m_binaryOperandStack.size() - amount); + } + void appendBinaryOperation(const JSTokenLocation& location, int& operandStackDepth, int&, const BinaryOperand& lhs, const BinaryOperand& rhs) + { + operandStackDepth++; + m_binaryOperandStack.append(std::make_pair(makeBinaryNode(location, m_binaryOperatorStack.last().first, lhs, rhs), BinaryOpInfo(lhs.second, rhs.second))); + } + void operatorStackAppend(int& operatorStackDepth, int op, int precedence) + { + operatorStackDepth++; + m_binaryOperatorStack.append(std::make_pair(op, precedence)); + } + ExpressionNode* popOperandStack(int&) + { + ExpressionNode* result = m_binaryOperandStack.last().first; + m_binaryOperandStack.removeLast(); + return result; + } + + void appendUnaryToken(int& tokenStackDepth, int type, const JSTextPosition& start) + { + tokenStackDepth++; + m_unaryTokenStack.append(std::make_pair(type, start)); + } + + int unaryTokenStackLastType(int&) + { + return m_unaryTokenStack.last().first; + } + + const JSTextPosition& unaryTokenStackLastStart(int&) + { + return m_unaryTokenStack.last().second; + } + + void unaryTokenStackRemoveLast(int& tokenStackDepth) + { + tokenStackDepth--; + m_unaryTokenStack.removeLast(); + } + + void assignmentStackAppend(int& assignmentStackDepth, ExpressionNode* node, const JSTextPosition& start, const JSTextPosition& divot, int assignmentCount, Operator op) + { + assignmentStackDepth++; + ASSERT(start.offset >= start.lineStartOffset); + ASSERT(divot.offset >= divot.lineStartOffset); + m_assignmentInfoStack.append(AssignmentInfo(node, start, divot, assignmentCount, op)); + } + + ExpressionNode* createAssignment(const JSTokenLocation& location, int& assignmentStackDepth, ExpressionNode* rhs, int initialAssignmentCount, int currentAssignmentCount, const JSTextPosition& lastTokenEnd) + { + AssignmentInfo& info = m_assignmentInfoStack.last(); + ExpressionNode* result = makeAssignNode(location, info.m_node, info.m_op, rhs, info.m_initAssignments != initialAssignmentCount, info.m_initAssignments != currentAssignmentCount, info.m_start, info.m_divot + 1, lastTokenEnd); + m_assignmentInfoStack.removeLast(); + assignmentStackDepth--; + return result; + } + + const Identifier* getName(const Property& property) const { return property->name(); } + PropertyNode::Type getType(const Property& property) const { return property->type(); } + + bool isResolve(ExpressionNode* expr) const { return expr->isResolveNode(); } + + ExpressionNode* createDestructuringAssignment(const JSTokenLocation& location, DestructuringPattern pattern, ExpressionNode* initializer) + { + return new (m_parserArena) DestructuringAssignmentNode(location, pattern, initializer); + } + + ArrayPattern createArrayPattern(const JSTokenLocation&) + { + return new (m_parserArena) ArrayPatternNode(); + } + + void appendArrayPatternSkipEntry(ArrayPattern node, const JSTokenLocation& location) + { + node->appendIndex(ArrayPatternNode::BindingType::Elision, location, 0, nullptr); + } + + void appendArrayPatternEntry(ArrayPattern node, const JSTokenLocation& location, DestructuringPattern pattern, ExpressionNode* defaultValue) + { + node->appendIndex(ArrayPatternNode::BindingType::Element, location, pattern, defaultValue); + } + + void appendArrayPatternRestEntry(ArrayPattern node, const JSTokenLocation& location, DestructuringPattern pattern) + { + node->appendIndex(ArrayPatternNode::BindingType::RestElement, location, pattern, nullptr); + } + + void finishArrayPattern(ArrayPattern node, const JSTextPosition& divotStart, const JSTextPosition& divot, const JSTextPosition& divotEnd) + { + setExceptionLocation(node, divotStart, divot, divotEnd); + } + + ObjectPattern createObjectPattern(const JSTokenLocation&) + { + return new (m_parserArena) ObjectPatternNode(); + } + + void appendObjectPatternEntry(ObjectPattern node, const JSTokenLocation& location, bool wasString, const Identifier& identifier, DestructuringPattern pattern, ExpressionNode* defaultValue) + { + node->appendEntry(location, identifier, wasString, pattern, defaultValue); + } + + BindingPattern createBindingLocation(const JSTokenLocation&, const Identifier& boundProperty, const JSTextPosition& start, const JSTextPosition& end, AssignmentContext context) + { + return new (m_parserArena) BindingNode(boundProperty, start, end, context); + } + + void setEndOffset(Node* node, int offset) + { + node->setEndOffset(offset); + } + + int endOffset(Node* node) + { + return node->endOffset(); + } + + void setStartOffset(CaseClauseNode* node, int offset) + { + node->setStartOffset(offset); + } + + void setStartOffset(Node* node, int offset) + { + node->setStartOffset(offset); + } + +private: + struct Scope { + Scope() + : m_features(0) + , m_numConstants(0) + { + } + DeclarationStacks::FunctionStack m_funcDeclarations; + int m_features; + int m_numConstants; + }; + + static void setExceptionLocation(ThrowableExpressionData* node, const JSTextPosition& divotStart, const JSTextPosition& divot, const JSTextPosition& divotEnd) + { + ASSERT(divot.offset >= divot.lineStartOffset); + node->setExceptionSourceCode(divot, divotStart, divotEnd); + } + + void incConstants() { m_scope.m_numConstants++; } + void usesThis() { m_scope.m_features |= ThisFeature; } + void usesCatch() { m_scope.m_features |= CatchFeature; } + void usesArguments() { m_scope.m_features |= ArgumentsFeature; } + void usesWith() { m_scope.m_features |= WithFeature; } + void usesEval() + { + m_evalCount++; + m_scope.m_features |= EvalFeature; + } + ExpressionNode* createIntegerLikeNumber(const JSTokenLocation& location, double d) + { + return new (m_parserArena) IntegerNode(location, d); + } + ExpressionNode* createDoubleLikeNumber(const JSTokenLocation& location, double d) + { + return new (m_parserArena) DoubleNode(location, d); + } + ExpressionNode* createNumberFromBinaryOperation(const JSTokenLocation& location, double value, const NumberNode& originalNodeA, const NumberNode& originalNodeB) + { + if (originalNodeA.isIntegerNode() && originalNodeB.isIntegerNode()) + return createIntegerLikeNumber(location, value); + return createDoubleLikeNumber(location, value); + } + ExpressionNode* createNumberFromUnaryOperation(const JSTokenLocation& location, double value, const NumberNode& originalNode) + { + if (originalNode.isIntegerNode()) + return createIntegerLikeNumber(location, value); + return createDoubleLikeNumber(location, value); + } + + VM* m_vm; + ParserArena& m_parserArena; + SourceCode* m_sourceCode; + Scope m_scope; + Vector m_binaryOperandStack; + Vector m_assignmentInfoStack; + Vector, 10, UnsafeVectorOverflow> m_binaryOperatorStack; + Vector, 10, UnsafeVectorOverflow> m_unaryTokenStack; + int m_evalCount; +}; + +ExpressionNode* ASTBuilder::makeTypeOfNode(const JSTokenLocation& location, ExpressionNode* expr) +{ + if (expr->isResolveNode()) { + ResolveNode* resolve = static_cast(expr); + return new (m_parserArena) TypeOfResolveNode(location, resolve->identifier()); + } + return new (m_parserArena) TypeOfValueNode(location, expr); +} + +ExpressionNode* ASTBuilder::makeDeleteNode(const JSTokenLocation& location, ExpressionNode* expr, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end) +{ + if (!expr->isLocation()) + return new (m_parserArena) DeleteValueNode(location, expr); + if (expr->isResolveNode()) { + ResolveNode* resolve = static_cast(expr); + return new (m_parserArena) DeleteResolveNode(location, resolve->identifier(), divot, start, end); + } + if (expr->isBracketAccessorNode()) { + BracketAccessorNode* bracket = static_cast(expr); + return new (m_parserArena) DeleteBracketNode(location, bracket->base(), bracket->subscript(), divot, start, end); + } + ASSERT(expr->isDotAccessorNode()); + DotAccessorNode* dot = static_cast(expr); + return new (m_parserArena) DeleteDotNode(location, dot->base(), dot->identifier(), divot, start, end); +} + +ExpressionNode* ASTBuilder::makeNegateNode(const JSTokenLocation& location, ExpressionNode* n) +{ + if (n->isNumber()) { + const NumberNode& numberNode = static_cast(*n); + return createNumberFromUnaryOperation(location, -numberNode.value(), numberNode); + } + + return new (m_parserArena) NegateNode(location, n); +} + +ExpressionNode* ASTBuilder::makeBitwiseNotNode(const JSTokenLocation& location, ExpressionNode* expr) +{ + if (expr->isNumber()) + return createIntegerLikeNumber(location, ~toInt32(static_cast(expr)->value())); + return new (m_parserArena) BitwiseNotNode(location, expr); +} + +ExpressionNode* ASTBuilder::makeMultNode(const JSTokenLocation& location, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments) +{ + expr1 = expr1->stripUnaryPlus(); + expr2 = expr2->stripUnaryPlus(); + + if (expr1->isNumber() && expr2->isNumber()) { + const NumberNode& numberExpr1 = static_cast(*expr1); + const NumberNode& numberExpr2 = static_cast(*expr2); + return createNumberFromBinaryOperation(location, numberExpr1.value() * numberExpr2.value(), numberExpr1, numberExpr2); + } + + if (expr1->isNumber() && static_cast(expr1)->value() == 1) + return new (m_parserArena) UnaryPlusNode(location, expr2); + + if (expr2->isNumber() && static_cast(expr2)->value() == 1) + return new (m_parserArena) UnaryPlusNode(location, expr1); + + return new (m_parserArena) MultNode(location, expr1, expr2, rightHasAssignments); +} + +ExpressionNode* ASTBuilder::makeDivNode(const JSTokenLocation& location, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments) +{ + expr1 = expr1->stripUnaryPlus(); + expr2 = expr2->stripUnaryPlus(); + + if (expr1->isNumber() && expr2->isNumber()) { + const NumberNode& numberExpr1 = static_cast(*expr1); + const NumberNode& numberExpr2 = static_cast(*expr2); + double result = numberExpr1.value() / numberExpr2.value(); + if (static_cast(result) == result) + return createNumberFromBinaryOperation(location, result, numberExpr1, numberExpr2); + return createDoubleLikeNumber(location, result); + } + return new (m_parserArena) DivNode(location, expr1, expr2, rightHasAssignments); +} + +ExpressionNode* ASTBuilder::makeModNode(const JSTokenLocation& location, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments) +{ + expr1 = expr1->stripUnaryPlus(); + expr2 = expr2->stripUnaryPlus(); + + if (expr1->isNumber() && expr2->isNumber()) { + const NumberNode& numberExpr1 = static_cast(*expr1); + const NumberNode& numberExpr2 = static_cast(*expr2); + return createIntegerLikeNumber(location, fmod(numberExpr1.value(), numberExpr2.value())); + } + return new (m_parserArena) ModNode(location, expr1, expr2, rightHasAssignments); +} + +ExpressionNode* ASTBuilder::makeAddNode(const JSTokenLocation& location, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments) +{ + + if (expr1->isNumber() && expr2->isNumber()) { + const NumberNode& numberExpr1 = static_cast(*expr1); + const NumberNode& numberExpr2 = static_cast(*expr2); + return createNumberFromBinaryOperation(location, numberExpr1.value() + numberExpr2.value(), numberExpr1, numberExpr2); + } + return new (m_parserArena) AddNode(location, expr1, expr2, rightHasAssignments); +} + +ExpressionNode* ASTBuilder::makeSubNode(const JSTokenLocation& location, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments) +{ + expr1 = expr1->stripUnaryPlus(); + expr2 = expr2->stripUnaryPlus(); + + if (expr1->isNumber() && expr2->isNumber()) { + const NumberNode& numberExpr1 = static_cast(*expr1); + const NumberNode& numberExpr2 = static_cast(*expr2); + return createNumberFromBinaryOperation(location, numberExpr1.value() - numberExpr2.value(), numberExpr1, numberExpr2); + } + return new (m_parserArena) SubNode(location, expr1, expr2, rightHasAssignments); +} + +ExpressionNode* ASTBuilder::makeLeftShiftNode(const JSTokenLocation& location, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments) +{ + if (expr1->isNumber() && expr2->isNumber()) { + const NumberNode& numberExpr1 = static_cast(*expr1); + const NumberNode& numberExpr2 = static_cast(*expr2); + return createIntegerLikeNumber(location, toInt32(numberExpr1.value()) << (toUInt32(numberExpr2.value()) & 0x1f)); + } + return new (m_parserArena) LeftShiftNode(location, expr1, expr2, rightHasAssignments); +} + +ExpressionNode* ASTBuilder::makeRightShiftNode(const JSTokenLocation& location, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments) +{ + if (expr1->isNumber() && expr2->isNumber()) { + const NumberNode& numberExpr1 = static_cast(*expr1); + const NumberNode& numberExpr2 = static_cast(*expr2); + return createIntegerLikeNumber(location, toInt32(numberExpr1.value()) >> (toUInt32(numberExpr2.value()) & 0x1f)); + } + return new (m_parserArena) RightShiftNode(location, expr1, expr2, rightHasAssignments); +} + +ExpressionNode* ASTBuilder::makeURightShiftNode(const JSTokenLocation& location, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments) +{ + if (expr1->isNumber() && expr2->isNumber()) { + const NumberNode& numberExpr1 = static_cast(*expr1); + const NumberNode& numberExpr2 = static_cast(*expr2); + return createIntegerLikeNumber(location, toUInt32(numberExpr1.value()) >> (toUInt32(numberExpr2.value()) & 0x1f)); + } + return new (m_parserArena) UnsignedRightShiftNode(location, expr1, expr2, rightHasAssignments); +} + +ExpressionNode* ASTBuilder::makeBitOrNode(const JSTokenLocation& location, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments) +{ + if (expr1->isNumber() && expr2->isNumber()) { + const NumberNode& numberExpr1 = static_cast(*expr1); + const NumberNode& numberExpr2 = static_cast(*expr2); + return createIntegerLikeNumber(location, toInt32(numberExpr1.value()) | toInt32(numberExpr2.value())); + } + return new (m_parserArena) BitOrNode(location, expr1, expr2, rightHasAssignments); +} + +ExpressionNode* ASTBuilder::makeBitAndNode(const JSTokenLocation& location, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments) +{ + if (expr1->isNumber() && expr2->isNumber()) { + const NumberNode& numberExpr1 = static_cast(*expr1); + const NumberNode& numberExpr2 = static_cast(*expr2); + return createIntegerLikeNumber(location, toInt32(numberExpr1.value()) & toInt32(numberExpr2.value())); + } + return new (m_parserArena) BitAndNode(location, expr1, expr2, rightHasAssignments); +} + +ExpressionNode* ASTBuilder::makeBitXOrNode(const JSTokenLocation& location, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments) +{ + if (expr1->isNumber() && expr2->isNumber()) { + const NumberNode& numberExpr1 = static_cast(*expr1); + const NumberNode& numberExpr2 = static_cast(*expr2); + return createIntegerLikeNumber(location, toInt32(numberExpr1.value()) ^ toInt32(numberExpr2.value())); + } + return new (m_parserArena) BitXOrNode(location, expr1, expr2, rightHasAssignments); +} + +ExpressionNode* ASTBuilder::makeFunctionCallNode(const JSTokenLocation& location, ExpressionNode* func, ArgumentsNode* args, const JSTextPosition& divotStart, const JSTextPosition& divot, const JSTextPosition& divotEnd) +{ + ASSERT(divot.offset >= divot.lineStartOffset); + if (!func->isLocation()) + return new (m_parserArena) FunctionCallValueNode(location, func, args, divot, divotStart, divotEnd); + if (func->isResolveNode()) { + ResolveNode* resolve = static_cast(func); + const Identifier& identifier = resolve->identifier(); + if (identifier == m_vm->propertyNames->eval) { + usesEval(); + return new (m_parserArena) EvalFunctionCallNode(location, args, divot, divotStart, divotEnd); + } + if (BytecodeIntrinsicNode::EmitterType emitter = m_vm->propertyNames->bytecodeIntrinsicRegistry().lookup(identifier)) + return new (m_parserArena) BytecodeIntrinsicNode(location, emitter, identifier, args, divot, divotStart, divotEnd); + return new (m_parserArena) FunctionCallResolveNode(location, identifier, args, divot, divotStart, divotEnd); + } + if (func->isBracketAccessorNode()) { + BracketAccessorNode* bracket = static_cast(func); + FunctionCallBracketNode* node = new (m_parserArena) FunctionCallBracketNode(location, bracket->base(), bracket->subscript(), bracket->subscriptHasAssignments(), args, divot, divotStart, divotEnd); + node->setSubexpressionInfo(bracket->divot(), bracket->divotEnd().offset); + return node; + } + ASSERT(func->isDotAccessorNode()); + DotAccessorNode* dot = static_cast(func); + FunctionCallDotNode* node; + if (dot->identifier() == m_vm->propertyNames->builtinNames().callPublicName() || dot->identifier() == m_vm->propertyNames->builtinNames().callPrivateName()) + node = new (m_parserArena) CallFunctionCallDotNode(location, dot->base(), dot->identifier(), args, divot, divotStart, divotEnd); + else if (dot->identifier() == m_vm->propertyNames->builtinNames().applyPublicName() || dot->identifier() == m_vm->propertyNames->builtinNames().applyPrivateName()) + node = new (m_parserArena) ApplyFunctionCallDotNode(location, dot->base(), dot->identifier(), args, divot, divotStart, divotEnd); + else + node = new (m_parserArena) FunctionCallDotNode(location, dot->base(), dot->identifier(), args, divot, divotStart, divotEnd); + node->setSubexpressionInfo(dot->divot(), dot->divotEnd().offset); + return node; +} + +ExpressionNode* ASTBuilder::makeBinaryNode(const JSTokenLocation& location, int token, std::pair lhs, std::pair rhs) +{ + switch (token) { + case OR: + return new (m_parserArena) LogicalOpNode(location, lhs.first, rhs.first, OpLogicalOr); + + case AND: + return new (m_parserArena) LogicalOpNode(location, lhs.first, rhs.first, OpLogicalAnd); + + case BITOR: + return makeBitOrNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); + + case BITXOR: + return makeBitXOrNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); + + case BITAND: + return makeBitAndNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); + + case EQEQ: + return new (m_parserArena) EqualNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); + + case NE: + return new (m_parserArena) NotEqualNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); + + case STREQ: + return new (m_parserArena) StrictEqualNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); + + case STRNEQ: + return new (m_parserArena) NotStrictEqualNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); + + case LT: + return new (m_parserArena) LessNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); + + case GT: + return new (m_parserArena) GreaterNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); + + case LE: + return new (m_parserArena) LessEqNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); + + case GE: + return new (m_parserArena) GreaterEqNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); + + case INSTANCEOF: { + InstanceOfNode* node = new (m_parserArena) InstanceOfNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); + setExceptionLocation(node, lhs.second.start, rhs.second.start, rhs.second.end); + return node; + } + + case INTOKEN: { + InNode* node = new (m_parserArena) InNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); + setExceptionLocation(node, lhs.second.start, rhs.second.start, rhs.second.end); + return node; + } + + case LSHIFT: + return makeLeftShiftNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); + + case RSHIFT: + return makeRightShiftNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); + + case URSHIFT: + return makeURightShiftNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); + + case PLUS: + return makeAddNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); + + case MINUS: + return makeSubNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); + + case TIMES: + return makeMultNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); + + case DIVIDE: + return makeDivNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); + + case MOD: + return makeModNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); + } + CRASH(); + return 0; +} + +ExpressionNode* ASTBuilder::makeAssignNode(const JSTokenLocation& location, ExpressionNode* loc, Operator op, ExpressionNode* expr, bool locHasAssignments, bool exprHasAssignments, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end) +{ + if (!loc->isLocation()) + return new (m_parserArena) AssignErrorNode(location, divot, start, end); + + if (loc->isResolveNode()) { + ResolveNode* resolve = static_cast(loc); + if (op == OpEqual) { + if (expr->isFuncExprNode()) + static_cast(expr)->metadata()->setInferredName(resolve->identifier()); + AssignResolveNode* node = new (m_parserArena) AssignResolveNode(location, resolve->identifier(), expr, AssignmentContext::AssignmentExpression); + setExceptionLocation(node, start, divot, end); + return node; + } + return new (m_parserArena) ReadModifyResolveNode(location, resolve->identifier(), op, expr, exprHasAssignments, divot, start, end); + } + if (loc->isBracketAccessorNode()) { + BracketAccessorNode* bracket = static_cast(loc); + if (op == OpEqual) + return new (m_parserArena) AssignBracketNode(location, bracket->base(), bracket->subscript(), expr, locHasAssignments, exprHasAssignments, bracket->divot(), start, end); + ReadModifyBracketNode* node = new (m_parserArena) ReadModifyBracketNode(location, bracket->base(), bracket->subscript(), op, expr, locHasAssignments, exprHasAssignments, divot, start, end); + node->setSubexpressionInfo(bracket->divot(), bracket->divotEnd().offset); + return node; + } + ASSERT(loc->isDotAccessorNode()); + DotAccessorNode* dot = static_cast(loc); + if (op == OpEqual) { + if (expr->isFuncExprNode()) + static_cast(expr)->metadata()->setInferredName(dot->identifier()); + return new (m_parserArena) AssignDotNode(location, dot->base(), dot->identifier(), expr, exprHasAssignments, dot->divot(), start, end); + } + + ReadModifyDotNode* node = new (m_parserArena) ReadModifyDotNode(location, dot->base(), dot->identifier(), op, expr, exprHasAssignments, divot, start, end); + node->setSubexpressionInfo(dot->divot(), dot->divotEnd().offset); + return node; +} + +ExpressionNode* ASTBuilder::makePrefixNode(const JSTokenLocation& location, ExpressionNode* expr, Operator op, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end) +{ + return new (m_parserArena) PrefixNode(location, expr, op, divot, start, end); +} + +ExpressionNode* ASTBuilder::makePostfixNode(const JSTokenLocation& location, ExpressionNode* expr, Operator op, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end) +{ + return new (m_parserArena) PostfixNode(location, expr, op, divot, start, end); +} + +} + +#endif diff --git a/Source/JavaScriptCore/parser/Keywords.table b/Source/JavaScriptCore/parser/Keywords.table new file mode 100644 index 000000000..e2ba528a4 --- /dev/null +++ b/Source/JavaScriptCore/parser/Keywords.table @@ -0,0 +1,57 @@ +# Main keywords. +@begin mainTable 47 + +# Types. +null NULLTOKEN +true TRUETOKEN +false FALSETOKEN + +# Keywords. +break BREAK +case CASE +catch CATCH +class CLASSTOKEN +const CONSTTOKEN +default DEFAULT +extends EXTENDS +finally FINALLY +for FOR +instanceof INSTANCEOF +new NEW +var VAR +let LET +continue CONTINUE +function FUNCTION +return RETURN +void VOIDTOKEN +delete DELETETOKEN +if IF +this THISTOKEN +do DO +while WHILE +else ELSE +in INTOKEN +super SUPER +switch SWITCH +throw THROW +try TRY +typeof TYPEOF +with WITH +debugger DEBUGGER + +# Reserved for future use. +enum RESERVED +export EXPORT +import IMPORT + +# Reserved for future use in strict code. +implements RESERVED_IF_STRICT +interface RESERVED_IF_STRICT +package RESERVED_IF_STRICT +private RESERVED_IF_STRICT +protected RESERVED_IF_STRICT +public RESERVED_IF_STRICT +static RESERVED_IF_STRICT +yield RESERVED_IF_STRICT + +@end diff --git a/Source/JavaScriptCore/parser/Lexer.cpp b/Source/JavaScriptCore/parser/Lexer.cpp new file mode 100644 index 000000000..c4db1618a --- /dev/null +++ b/Source/JavaScriptCore/parser/Lexer.cpp @@ -0,0 +1,2417 @@ +/* + * Copyright (C) 1999-2000 Harri Porten (porten@kde.org) + * Copyright (C) 2006, 2007, 2008, 2009, 2011, 2012, 2013 Apple Inc. All Rights Reserved. + * Copyright (C) 2007 Cameron Zwarich (cwzwarich@uwaterloo.ca) + * Copyright (C) 2010 Zoltan Herczeg (zherczeg@inf.u-szeged.hu) + * Copyright (C) 2012 Mathias Bynens (mathias@qiwi.be) + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * + */ + +#include "config.h" +#include "Lexer.h" + +#include "JSFunctionInlines.h" + +#include "BuiltinNames.h" +#include "JSGlobalObjectFunctions.h" +#include "Identifier.h" +#include "Nodes.h" +#include "JSCInlines.h" +#include +#include +#include +#include +#include + +#include "KeywordLookup.h" +#include "Lexer.lut.h" +#include "Parser.h" + +namespace JSC { + +bool isLexerKeyword(const Identifier& identifier) +{ + return JSC::mainTable.entry(identifier); +} + +enum CharacterType { + // Types for the main switch + + // The first three types are fixed, and also used for identifying + // ASCII alpha and alphanumeric characters (see isIdentStart and isIdentPart). + CharacterIdentifierStart, + CharacterZero, + CharacterNumber, + + CharacterInvalid, + CharacterLineTerminator, + CharacterExclamationMark, + CharacterOpenParen, + CharacterCloseParen, + CharacterOpenBracket, + CharacterCloseBracket, + CharacterComma, + CharacterColon, + CharacterQuestion, + CharacterTilde, + CharacterQuote, + CharacterBackQuote, + CharacterDot, + CharacterSlash, + CharacterBackSlash, + CharacterSemicolon, + CharacterOpenBrace, + CharacterCloseBrace, + + CharacterAdd, + CharacterSub, + CharacterMultiply, + CharacterModulo, + CharacterAnd, + CharacterXor, + CharacterOr, + CharacterLess, + CharacterGreater, + CharacterEqual, + + // Other types (only one so far) + CharacterWhiteSpace, + CharacterPrivateIdentifierStart +}; + +// 256 Latin-1 codes +static const unsigned short typesOfLatin1Characters[256] = { +/* 0 - Null */ CharacterInvalid, +/* 1 - Start of Heading */ CharacterInvalid, +/* 2 - Start of Text */ CharacterInvalid, +/* 3 - End of Text */ CharacterInvalid, +/* 4 - End of Transm. */ CharacterInvalid, +/* 5 - Enquiry */ CharacterInvalid, +/* 6 - Acknowledgment */ CharacterInvalid, +/* 7 - Bell */ CharacterInvalid, +/* 8 - Back Space */ CharacterInvalid, +/* 9 - Horizontal Tab */ CharacterWhiteSpace, +/* 10 - Line Feed */ CharacterLineTerminator, +/* 11 - Vertical Tab */ CharacterWhiteSpace, +/* 12 - Form Feed */ CharacterWhiteSpace, +/* 13 - Carriage Return */ CharacterLineTerminator, +/* 14 - Shift Out */ CharacterInvalid, +/* 15 - Shift In */ CharacterInvalid, +/* 16 - Data Line Escape */ CharacterInvalid, +/* 17 - Device Control 1 */ CharacterInvalid, +/* 18 - Device Control 2 */ CharacterInvalid, +/* 19 - Device Control 3 */ CharacterInvalid, +/* 20 - Device Control 4 */ CharacterInvalid, +/* 21 - Negative Ack. */ CharacterInvalid, +/* 22 - Synchronous Idle */ CharacterInvalid, +/* 23 - End of Transmit */ CharacterInvalid, +/* 24 - Cancel */ CharacterInvalid, +/* 25 - End of Medium */ CharacterInvalid, +/* 26 - Substitute */ CharacterInvalid, +/* 27 - Escape */ CharacterInvalid, +/* 28 - File Separator */ CharacterInvalid, +/* 29 - Group Separator */ CharacterInvalid, +/* 30 - Record Separator */ CharacterInvalid, +/* 31 - Unit Separator */ CharacterInvalid, +/* 32 - Space */ CharacterWhiteSpace, +/* 33 - ! */ CharacterExclamationMark, +/* 34 - " */ CharacterQuote, +/* 35 - # */ CharacterInvalid, +/* 36 - $ */ CharacterIdentifierStart, +/* 37 - % */ CharacterModulo, +/* 38 - & */ CharacterAnd, +/* 39 - ' */ CharacterQuote, +/* 40 - ( */ CharacterOpenParen, +/* 41 - ) */ CharacterCloseParen, +/* 42 - * */ CharacterMultiply, +/* 43 - + */ CharacterAdd, +/* 44 - , */ CharacterComma, +/* 45 - - */ CharacterSub, +/* 46 - . */ CharacterDot, +/* 47 - / */ CharacterSlash, +/* 48 - 0 */ CharacterZero, +/* 49 - 1 */ CharacterNumber, +/* 50 - 2 */ CharacterNumber, +/* 51 - 3 */ CharacterNumber, +/* 52 - 4 */ CharacterNumber, +/* 53 - 5 */ CharacterNumber, +/* 54 - 6 */ CharacterNumber, +/* 55 - 7 */ CharacterNumber, +/* 56 - 8 */ CharacterNumber, +/* 57 - 9 */ CharacterNumber, +/* 58 - : */ CharacterColon, +/* 59 - ; */ CharacterSemicolon, +/* 60 - < */ CharacterLess, +/* 61 - = */ CharacterEqual, +/* 62 - > */ CharacterGreater, +/* 63 - ? */ CharacterQuestion, +/* 64 - @ */ CharacterPrivateIdentifierStart, +/* 65 - A */ CharacterIdentifierStart, +/* 66 - B */ CharacterIdentifierStart, +/* 67 - C */ CharacterIdentifierStart, +/* 68 - D */ CharacterIdentifierStart, +/* 69 - E */ CharacterIdentifierStart, +/* 70 - F */ CharacterIdentifierStart, +/* 71 - G */ CharacterIdentifierStart, +/* 72 - H */ CharacterIdentifierStart, +/* 73 - I */ CharacterIdentifierStart, +/* 74 - J */ CharacterIdentifierStart, +/* 75 - K */ CharacterIdentifierStart, +/* 76 - L */ CharacterIdentifierStart, +/* 77 - M */ CharacterIdentifierStart, +/* 78 - N */ CharacterIdentifierStart, +/* 79 - O */ CharacterIdentifierStart, +/* 80 - P */ CharacterIdentifierStart, +/* 81 - Q */ CharacterIdentifierStart, +/* 82 - R */ CharacterIdentifierStart, +/* 83 - S */ CharacterIdentifierStart, +/* 84 - T */ CharacterIdentifierStart, +/* 85 - U */ CharacterIdentifierStart, +/* 86 - V */ CharacterIdentifierStart, +/* 87 - W */ CharacterIdentifierStart, +/* 88 - X */ CharacterIdentifierStart, +/* 89 - Y */ CharacterIdentifierStart, +/* 90 - Z */ CharacterIdentifierStart, +/* 91 - [ */ CharacterOpenBracket, +/* 92 - \ */ CharacterBackSlash, +/* 93 - ] */ CharacterCloseBracket, +/* 94 - ^ */ CharacterXor, +/* 95 - _ */ CharacterIdentifierStart, +#if ENABLE(ES6_TEMPLATE_LITERAL_SYNTAX) +/* 96 - ` */ CharacterBackQuote, +#else +/* 96 - ` */ CharacterInvalid, +#endif +/* 97 - a */ CharacterIdentifierStart, +/* 98 - b */ CharacterIdentifierStart, +/* 99 - c */ CharacterIdentifierStart, +/* 100 - d */ CharacterIdentifierStart, +/* 101 - e */ CharacterIdentifierStart, +/* 102 - f */ CharacterIdentifierStart, +/* 103 - g */ CharacterIdentifierStart, +/* 104 - h */ CharacterIdentifierStart, +/* 105 - i */ CharacterIdentifierStart, +/* 106 - j */ CharacterIdentifierStart, +/* 107 - k */ CharacterIdentifierStart, +/* 108 - l */ CharacterIdentifierStart, +/* 109 - m */ CharacterIdentifierStart, +/* 110 - n */ CharacterIdentifierStart, +/* 111 - o */ CharacterIdentifierStart, +/* 112 - p */ CharacterIdentifierStart, +/* 113 - q */ CharacterIdentifierStart, +/* 114 - r */ CharacterIdentifierStart, +/* 115 - s */ CharacterIdentifierStart, +/* 116 - t */ CharacterIdentifierStart, +/* 117 - u */ CharacterIdentifierStart, +/* 118 - v */ CharacterIdentifierStart, +/* 119 - w */ CharacterIdentifierStart, +/* 120 - x */ CharacterIdentifierStart, +/* 121 - y */ CharacterIdentifierStart, +/* 122 - z */ CharacterIdentifierStart, +/* 123 - { */ CharacterOpenBrace, +/* 124 - | */ CharacterOr, +/* 125 - } */ CharacterCloseBrace, +/* 126 - ~ */ CharacterTilde, +/* 127 - Delete */ CharacterInvalid, +/* 128 - Cc category */ CharacterInvalid, +/* 129 - Cc category */ CharacterInvalid, +/* 130 - Cc category */ CharacterInvalid, +/* 131 - Cc category */ CharacterInvalid, +/* 132 - Cc category */ CharacterInvalid, +/* 133 - Cc category */ CharacterInvalid, +/* 134 - Cc category */ CharacterInvalid, +/* 135 - Cc category */ CharacterInvalid, +/* 136 - Cc category */ CharacterInvalid, +/* 137 - Cc category */ CharacterInvalid, +/* 138 - Cc category */ CharacterInvalid, +/* 139 - Cc category */ CharacterInvalid, +/* 140 - Cc category */ CharacterInvalid, +/* 141 - Cc category */ CharacterInvalid, +/* 142 - Cc category */ CharacterInvalid, +/* 143 - Cc category */ CharacterInvalid, +/* 144 - Cc category */ CharacterInvalid, +/* 145 - Cc category */ CharacterInvalid, +/* 146 - Cc category */ CharacterInvalid, +/* 147 - Cc category */ CharacterInvalid, +/* 148 - Cc category */ CharacterInvalid, +/* 149 - Cc category */ CharacterInvalid, +/* 150 - Cc category */ CharacterInvalid, +/* 151 - Cc category */ CharacterInvalid, +/* 152 - Cc category */ CharacterInvalid, +/* 153 - Cc category */ CharacterInvalid, +/* 154 - Cc category */ CharacterInvalid, +/* 155 - Cc category */ CharacterInvalid, +/* 156 - Cc category */ CharacterInvalid, +/* 157 - Cc category */ CharacterInvalid, +/* 158 - Cc category */ CharacterInvalid, +/* 159 - Cc category */ CharacterInvalid, +/* 160 - Zs category (nbsp) */ CharacterWhiteSpace, +/* 161 - Po category */ CharacterInvalid, +/* 162 - Sc category */ CharacterInvalid, +/* 163 - Sc category */ CharacterInvalid, +/* 164 - Sc category */ CharacterInvalid, +/* 165 - Sc category */ CharacterInvalid, +/* 166 - So category */ CharacterInvalid, +/* 167 - So category */ CharacterInvalid, +/* 168 - Sk category */ CharacterInvalid, +/* 169 - So category */ CharacterInvalid, +/* 170 - Ll category */ CharacterIdentifierStart, +/* 171 - Pi category */ CharacterInvalid, +/* 172 - Sm category */ CharacterInvalid, +/* 173 - Cf category */ CharacterInvalid, +/* 174 - So category */ CharacterInvalid, +/* 175 - Sk category */ CharacterInvalid, +/* 176 - So category */ CharacterInvalid, +/* 177 - Sm category */ CharacterInvalid, +/* 178 - No category */ CharacterInvalid, +/* 179 - No category */ CharacterInvalid, +/* 180 - Sk category */ CharacterInvalid, +/* 181 - Ll category */ CharacterIdentifierStart, +/* 182 - So category */ CharacterInvalid, +/* 183 - Po category */ CharacterInvalid, +/* 184 - Sk category */ CharacterInvalid, +/* 185 - No category */ CharacterInvalid, +/* 186 - Ll category */ CharacterIdentifierStart, +/* 187 - Pf category */ CharacterInvalid, +/* 188 - No category */ CharacterInvalid, +/* 189 - No category */ CharacterInvalid, +/* 190 - No category */ CharacterInvalid, +/* 191 - Po category */ CharacterInvalid, +/* 192 - Lu category */ CharacterIdentifierStart, +/* 193 - Lu category */ CharacterIdentifierStart, +/* 194 - Lu category */ CharacterIdentifierStart, +/* 195 - Lu category */ CharacterIdentifierStart, +/* 196 - Lu category */ CharacterIdentifierStart, +/* 197 - Lu category */ CharacterIdentifierStart, +/* 198 - Lu category */ CharacterIdentifierStart, +/* 199 - Lu category */ CharacterIdentifierStart, +/* 200 - Lu category */ CharacterIdentifierStart, +/* 201 - Lu category */ CharacterIdentifierStart, +/* 202 - Lu category */ CharacterIdentifierStart, +/* 203 - Lu category */ CharacterIdentifierStart, +/* 204 - Lu category */ CharacterIdentifierStart, +/* 205 - Lu category */ CharacterIdentifierStart, +/* 206 - Lu category */ CharacterIdentifierStart, +/* 207 - Lu category */ CharacterIdentifierStart, +/* 208 - Lu category */ CharacterIdentifierStart, +/* 209 - Lu category */ CharacterIdentifierStart, +/* 210 - Lu category */ CharacterIdentifierStart, +/* 211 - Lu category */ CharacterIdentifierStart, +/* 212 - Lu category */ CharacterIdentifierStart, +/* 213 - Lu category */ CharacterIdentifierStart, +/* 214 - Lu category */ CharacterIdentifierStart, +/* 215 - Sm category */ CharacterInvalid, +/* 216 - Lu category */ CharacterIdentifierStart, +/* 217 - Lu category */ CharacterIdentifierStart, +/* 218 - Lu category */ CharacterIdentifierStart, +/* 219 - Lu category */ CharacterIdentifierStart, +/* 220 - Lu category */ CharacterIdentifierStart, +/* 221 - Lu category */ CharacterIdentifierStart, +/* 222 - Lu category */ CharacterIdentifierStart, +/* 223 - Ll category */ CharacterIdentifierStart, +/* 224 - Ll category */ CharacterIdentifierStart, +/* 225 - Ll category */ CharacterIdentifierStart, +/* 226 - Ll category */ CharacterIdentifierStart, +/* 227 - Ll category */ CharacterIdentifierStart, +/* 228 - Ll category */ CharacterIdentifierStart, +/* 229 - Ll category */ CharacterIdentifierStart, +/* 230 - Ll category */ CharacterIdentifierStart, +/* 231 - Ll category */ CharacterIdentifierStart, +/* 232 - Ll category */ CharacterIdentifierStart, +/* 233 - Ll category */ CharacterIdentifierStart, +/* 234 - Ll category */ CharacterIdentifierStart, +/* 235 - Ll category */ CharacterIdentifierStart, +/* 236 - Ll category */ CharacterIdentifierStart, +/* 237 - Ll category */ CharacterIdentifierStart, +/* 238 - Ll category */ CharacterIdentifierStart, +/* 239 - Ll category */ CharacterIdentifierStart, +/* 240 - Ll category */ CharacterIdentifierStart, +/* 241 - Ll category */ CharacterIdentifierStart, +/* 242 - Ll category */ CharacterIdentifierStart, +/* 243 - Ll category */ CharacterIdentifierStart, +/* 244 - Ll category */ CharacterIdentifierStart, +/* 245 - Ll category */ CharacterIdentifierStart, +/* 246 - Ll category */ CharacterIdentifierStart, +/* 247 - Sm category */ CharacterInvalid, +/* 248 - Ll category */ CharacterIdentifierStart, +/* 249 - Ll category */ CharacterIdentifierStart, +/* 250 - Ll category */ CharacterIdentifierStart, +/* 251 - Ll category */ CharacterIdentifierStart, +/* 252 - Ll category */ CharacterIdentifierStart, +/* 253 - Ll category */ CharacterIdentifierStart, +/* 254 - Ll category */ CharacterIdentifierStart, +/* 255 - Ll category */ CharacterIdentifierStart +}; + +// This table provides the character that results from \X where X is the index in the table beginning +// with SPACE. A table value of 0 means that more processing needs to be done. +static const LChar singleCharacterEscapeValuesForASCII[128] = { +/* 0 - Null */ 0, +/* 1 - Start of Heading */ 0, +/* 2 - Start of Text */ 0, +/* 3 - End of Text */ 0, +/* 4 - End of Transm. */ 0, +/* 5 - Enquiry */ 0, +/* 6 - Acknowledgment */ 0, +/* 7 - Bell */ 0, +/* 8 - Back Space */ 0, +/* 9 - Horizontal Tab */ 0, +/* 10 - Line Feed */ 0, +/* 11 - Vertical Tab */ 0, +/* 12 - Form Feed */ 0, +/* 13 - Carriage Return */ 0, +/* 14 - Shift Out */ 0, +/* 15 - Shift In */ 0, +/* 16 - Data Line Escape */ 0, +/* 17 - Device Control 1 */ 0, +/* 18 - Device Control 2 */ 0, +/* 19 - Device Control 3 */ 0, +/* 20 - Device Control 4 */ 0, +/* 21 - Negative Ack. */ 0, +/* 22 - Synchronous Idle */ 0, +/* 23 - End of Transmit */ 0, +/* 24 - Cancel */ 0, +/* 25 - End of Medium */ 0, +/* 26 - Substitute */ 0, +/* 27 - Escape */ 0, +/* 28 - File Separator */ 0, +/* 29 - Group Separator */ 0, +/* 30 - Record Separator */ 0, +/* 31 - Unit Separator */ 0, +/* 32 - Space */ ' ', +/* 33 - ! */ '!', +/* 34 - " */ '"', +/* 35 - # */ '#', +/* 36 - $ */ '$', +/* 37 - % */ '%', +/* 38 - & */ '&', +/* 39 - ' */ '\'', +/* 40 - ( */ '(', +/* 41 - ) */ ')', +/* 42 - * */ '*', +/* 43 - + */ '+', +/* 44 - , */ ',', +/* 45 - - */ '-', +/* 46 - . */ '.', +/* 47 - / */ '/', +/* 48 - 0 */ 0, +/* 49 - 1 */ 0, +/* 50 - 2 */ 0, +/* 51 - 3 */ 0, +/* 52 - 4 */ 0, +/* 53 - 5 */ 0, +/* 54 - 6 */ 0, +/* 55 - 7 */ 0, +/* 56 - 8 */ 0, +/* 57 - 9 */ 0, +/* 58 - : */ ':', +/* 59 - ; */ ';', +/* 60 - < */ '<', +/* 61 - = */ '=', +/* 62 - > */ '>', +/* 63 - ? */ '?', +/* 64 - @ */ '@', +/* 65 - A */ 'A', +/* 66 - B */ 'B', +/* 67 - C */ 'C', +/* 68 - D */ 'D', +/* 69 - E */ 'E', +/* 70 - F */ 'F', +/* 71 - G */ 'G', +/* 72 - H */ 'H', +/* 73 - I */ 'I', +/* 74 - J */ 'J', +/* 75 - K */ 'K', +/* 76 - L */ 'L', +/* 77 - M */ 'M', +/* 78 - N */ 'N', +/* 79 - O */ 'O', +/* 80 - P */ 'P', +/* 81 - Q */ 'Q', +/* 82 - R */ 'R', +/* 83 - S */ 'S', +/* 84 - T */ 'T', +/* 85 - U */ 'U', +/* 86 - V */ 'V', +/* 87 - W */ 'W', +/* 88 - X */ 'X', +/* 89 - Y */ 'Y', +/* 90 - Z */ 'Z', +/* 91 - [ */ '[', +/* 92 - \ */ '\\', +/* 93 - ] */ ']', +/* 94 - ^ */ '^', +/* 95 - _ */ '_', +/* 96 - ` */ '`', +/* 97 - a */ 'a', +/* 98 - b */ 0x08, +/* 99 - c */ 'c', +/* 100 - d */ 'd', +/* 101 - e */ 'e', +/* 102 - f */ 0x0C, +/* 103 - g */ 'g', +/* 104 - h */ 'h', +/* 105 - i */ 'i', +/* 106 - j */ 'j', +/* 107 - k */ 'k', +/* 108 - l */ 'l', +/* 109 - m */ 'm', +/* 110 - n */ 0x0A, +/* 111 - o */ 'o', +/* 112 - p */ 'p', +/* 113 - q */ 'q', +/* 114 - r */ 0x0D, +/* 115 - s */ 's', +/* 116 - t */ 0x09, +/* 117 - u */ 0, +/* 118 - v */ 0x0B, +/* 119 - w */ 'w', +/* 120 - x */ 0, +/* 121 - y */ 'y', +/* 122 - z */ 'z', +/* 123 - { */ '{', +/* 124 - | */ '|', +/* 125 - } */ '}', +/* 126 - ~ */ '~', +/* 127 - Delete */ 0 +}; + +template +Lexer::Lexer(VM* vm, JSParserBuiltinMode builtinMode) + : m_isReparsingFunction(false) + , m_vm(vm) + , m_parsingBuiltinFunction(builtinMode == JSParserBuiltinMode::Builtin) +{ +} + +static inline JSTokenType tokenTypeForIntegerLikeToken(double doubleValue) +{ + if ((doubleValue || !std::signbit(doubleValue)) && static_cast(doubleValue) == doubleValue) + return INTEGER; + return DOUBLE; +} + +template +Lexer::~Lexer() +{ +} + +template +String Lexer::invalidCharacterMessage() const +{ + switch (m_current) { + case 0: + return ASCIILiteral("Invalid character: '\\0'"); + case 10: + return ASCIILiteral("Invalid character: '\\n'"); + case 11: + return ASCIILiteral("Invalid character: '\\v'"); + case 13: + return ASCIILiteral("Invalid character: '\\r'"); + case 35: + return ASCIILiteral("Invalid character: '#'"); + case 64: + return ASCIILiteral("Invalid character: '@'"); + case 96: + return ASCIILiteral("Invalid character: '`'"); + default: + return String::format("Invalid character '\\u%04u'", static_cast(m_current)); + } +} + +template +ALWAYS_INLINE const T* Lexer::currentSourcePtr() const +{ + ASSERT(m_code <= m_codeEnd); + return m_code; +} + +template +void Lexer::setCode(const SourceCode& source, ParserArena* arena) +{ + m_arena = &arena->identifierArena(); + + m_lineNumber = source.firstLine(); + m_lastToken = -1; + + const String& sourceString = source.provider()->source(); + + if (!sourceString.isNull()) + setCodeStart(sourceString.impl()); + else + m_codeStart = 0; + + m_source = &source; + m_sourceOffset = source.startOffset(); + m_codeStartPlusOffset = m_codeStart + source.startOffset(); + m_code = m_codeStartPlusOffset; + m_codeEnd = m_codeStart + source.endOffset(); + m_error = false; + m_atLineStart = true; + m_lineStart = m_code; + m_lexErrorMessage = String(); + + m_buffer8.reserveInitialCapacity(initialReadBufferCapacity); + m_buffer16.reserveInitialCapacity((m_codeEnd - m_code) / 2); + m_bufferForRawTemplateString16.reserveInitialCapacity(initialReadBufferCapacity); + + if (LIKELY(m_code < m_codeEnd)) + m_current = *m_code; + else + m_current = 0; + ASSERT(currentOffset() == source.startOffset()); +} + +template +template ALWAYS_INLINE void Lexer::internalShift() +{ + m_code += shiftAmount; + ASSERT(currentOffset() >= currentLineStartOffset()); + m_current = *m_code; +} + +template +ALWAYS_INLINE void Lexer::shift() +{ + // At one point timing showed that setting m_current to 0 unconditionally was faster than an if-else sequence. + m_current = 0; + ++m_code; + if (LIKELY(m_code < m_codeEnd)) + m_current = *m_code; +} + +template +ALWAYS_INLINE bool Lexer::atEnd() const +{ + ASSERT(!m_current || m_code < m_codeEnd); + return UNLIKELY(UNLIKELY(!m_current) && m_code == m_codeEnd); +} + +template +ALWAYS_INLINE T Lexer::peek(int offset) const +{ + ASSERT(offset > 0 && offset < 5); + const T* code = m_code + offset; + return (code < m_codeEnd) ? *code : 0; +} + +struct ParsedUnicodeEscapeValue { + ParsedUnicodeEscapeValue(UChar32 value) + : m_value(value) + { + ASSERT(isValid()); + } + + enum SpecialValueType { Incomplete = -2, Invalid = -1 }; + ParsedUnicodeEscapeValue(SpecialValueType type) + : m_value(type) + { + } + + bool isValid() const { return m_value >= 0; } + bool isIncomplete() const { return m_value == Incomplete; } + + UChar32 value() const + { + ASSERT(isValid()); + return m_value; + } + +private: + UChar32 m_value; +}; + +template ParsedUnicodeEscapeValue Lexer::parseUnicodeEscape() +{ + if (m_current == '{') { + shift(); + UChar32 codePoint = 0; + do { + if (!isASCIIHexDigit(m_current)) + return m_current ? ParsedUnicodeEscapeValue::Invalid : ParsedUnicodeEscapeValue::Incomplete; + codePoint = (codePoint << 4) | toASCIIHexValue(m_current); + if (codePoint > UCHAR_MAX_VALUE) + return ParsedUnicodeEscapeValue::Invalid; + shift(); + } while (m_current != '}'); + shift(); + return codePoint; + } + + auto character2 = peek(1); + auto character3 = peek(2); + auto character4 = peek(3); + if (UNLIKELY(!isASCIIHexDigit(m_current) || !isASCIIHexDigit(character2) || !isASCIIHexDigit(character3) || !isASCIIHexDigit(character4))) + return (m_code + 4) >= m_codeEnd ? ParsedUnicodeEscapeValue::Incomplete : ParsedUnicodeEscapeValue::Invalid; + auto result = convertUnicode(m_current, character2, character3, character4); + shift(); + shift(); + shift(); + shift(); + return result; +} + +template +void Lexer::shiftLineTerminator() +{ + ASSERT(isLineTerminator(m_current)); + + m_positionBeforeLastNewline = currentPosition(); + T prev = m_current; + shift(); + + // Allow both CRLF and LFCR. + if (prev + m_current == '\n' + '\r') + shift(); + + ++m_lineNumber; +} + +template +ALWAYS_INLINE bool Lexer::lastTokenWasRestrKeyword() const +{ + return m_lastToken == CONTINUE || m_lastToken == BREAK || m_lastToken == RETURN || m_lastToken == THROW; +} + +static NEVER_INLINE bool isNonLatin1IdentStart(UChar c) +{ + return U_GET_GC_MASK(c) & U_GC_L_MASK; +} + +static ALWAYS_INLINE bool isLatin1(LChar) +{ + return true; +} + +static ALWAYS_INLINE bool isLatin1(UChar c) +{ + return c < 256; +} + +static ALWAYS_INLINE bool isLatin1(UChar32 c) +{ + return !(c & ~0xFF); +} + +static inline bool isIdentStart(LChar c) +{ + return typesOfLatin1Characters[c] == CharacterIdentifierStart; +} + +static inline bool isIdentStart(UChar32 c) +{ + return isLatin1(c) ? isIdentStart(static_cast(c)) : isNonLatin1IdentStart(c); +} + +static NEVER_INLINE bool isNonLatin1IdentPart(UChar32 c) +{ + // FIXME: ES6 says this should be based on the Unicode property ID_Continue now instead. + return (U_GET_GC_MASK(c) & (U_GC_L_MASK | U_GC_MN_MASK | U_GC_MC_MASK | U_GC_ND_MASK | U_GC_PC_MASK)) || c == 0x200C || c == 0x200D; +} + +static ALWAYS_INLINE bool isIdentPart(LChar c) +{ + // Character types are divided into two groups depending on whether they can be part of an + // identifier or not. Those whose type value is less or equal than CharacterNumber can be + // part of an identifier. (See the CharacterType definition for more details.) + return typesOfLatin1Characters[c] <= CharacterNumber; +} + +static ALWAYS_INLINE bool isIdentPart(UChar32 c) +{ + return isLatin1(c) ? isIdentPart(static_cast(c)) : isNonLatin1IdentPart(c); +} + +static ALWAYS_INLINE bool isIdentPart(UChar c) +{ + return isIdentPart(static_cast(c)); +} + +template ALWAYS_INLINE bool isIdentPartIncludingEscapeTemplate(const CharacterType* code, const CharacterType* codeEnd) +{ + if (isIdentPart(code[0])) + return true; + + // Shortest sequence handled below is \u{0}, which is 5 characters. + if (!(code[0] == '\\' && codeEnd - code >= 5 && code[1] == 'u')) + return false; + + if (code[2] == '{') { + UChar32 codePoint = 0; + const CharacterType* pointer; + for (pointer = &code[3]; pointer < codeEnd; ++pointer) { + auto digit = *pointer; + if (!isASCIIHexDigit(digit)) + break; + codePoint = (codePoint << 4) | toASCIIHexValue(digit); + if (codePoint > UCHAR_MAX_VALUE) + return false; + } + return isIdentPart(codePoint) && pointer < codeEnd && *pointer == '}'; + } + + // Shortest sequence handled below is \uXXXX, which is 6 characters. + if (codeEnd - code < 6) + return false; + + auto character1 = code[2]; + auto character2 = code[3]; + auto character3 = code[4]; + auto character4 = code[5]; + return isASCIIHexDigit(character1) && isASCIIHexDigit(character2) && isASCIIHexDigit(character3) && isASCIIHexDigit(character4) + && isIdentPart(Lexer::convertUnicode(character1, character2, character3, character4)); +} + +static ALWAYS_INLINE bool isIdentPartIncludingEscape(const LChar* code, const LChar* codeEnd) +{ + return isIdentPartIncludingEscapeTemplate(code, codeEnd); +} + +static ALWAYS_INLINE bool isIdentPartIncludingEscape(const UChar* code, const UChar* codeEnd) +{ + return isIdentPartIncludingEscapeTemplate(code, codeEnd); +} + +static inline LChar singleEscape(int c) +{ + if (c < 128) { + ASSERT(static_cast(c) < ARRAY_SIZE(singleCharacterEscapeValuesForASCII)); + return singleCharacterEscapeValuesForASCII[c]; + } + return 0; +} + +template +inline void Lexer::record8(int c) +{ + ASSERT(c >= 0); + ASSERT(c <= 0xFF); + m_buffer8.append(static_cast(c)); +} + +template +inline void assertCharIsIn8BitRange(T c) +{ + UNUSED_PARAM(c); + ASSERT(c >= 0); + ASSERT(c <= 0xFF); +} + +template <> +inline void assertCharIsIn8BitRange(UChar c) +{ + UNUSED_PARAM(c); + ASSERT(c <= 0xFF); +} + +template <> +inline void assertCharIsIn8BitRange(LChar) +{ +} + +template +inline void Lexer::append8(const T* p, size_t length) +{ + size_t currentSize = m_buffer8.size(); + m_buffer8.grow(currentSize + length); + LChar* rawBuffer = m_buffer8.data() + currentSize; + + for (size_t i = 0; i < length; i++) { + T c = p[i]; + assertCharIsIn8BitRange(c); + rawBuffer[i] = c; + } +} + +template +inline void Lexer::append16(const LChar* p, size_t length) +{ + size_t currentSize = m_buffer16.size(); + m_buffer16.grow(currentSize + length); + UChar* rawBuffer = m_buffer16.data() + currentSize; + + for (size_t i = 0; i < length; i++) + rawBuffer[i] = p[i]; +} + +template +inline void Lexer::record16(T c) +{ + m_buffer16.append(c); +} + +template +inline void Lexer::record16(int c) +{ + ASSERT(c >= 0); + ASSERT(c <= static_cast(USHRT_MAX)); + m_buffer16.append(static_cast(c)); +} + +template inline void Lexer::recordUnicodeCodePoint(UChar32 codePoint) +{ + ASSERT(codePoint >= 0); + ASSERT(codePoint <= UCHAR_MAX_VALUE); + if (U_IS_BMP(codePoint)) + record16(codePoint); + else { + UChar codeUnits[2] = { U16_LEAD(codePoint), U16_TRAIL(codePoint) }; + append16(codeUnits, 2); + } +} + +#if !ASSERT_DISABLED +bool isSafeBuiltinIdentifier(VM& vm, const Identifier* ident) +{ + if (!ident) + return true; + /* Just block any use of suspicious identifiers. This is intended to + * be used as a safety net while implementing builtins. + */ + // FIXME: How can a debug-only assertion be a safety net? + if (*ident == vm.propertyNames->builtinNames().callPublicName()) + return false; + if (*ident == vm.propertyNames->builtinNames().applyPublicName()) + return false; + if (*ident == vm.propertyNames->eval) + return false; + if (*ident == vm.propertyNames->Function) + return false; + return true; +} +#endif + +template <> +template ALWAYS_INLINE JSTokenType Lexer::parseIdentifier(JSTokenData* tokenData, unsigned lexerFlags, bool strictMode) +{ + const ptrdiff_t remaining = m_codeEnd - m_code; + if ((remaining >= maxTokenLength) && !(lexerFlags & LexerFlagsIgnoreReservedWords)) { + JSTokenType keyword = parseKeyword(tokenData); + if (keyword != IDENT) { + ASSERT((!shouldCreateIdentifier) || tokenData->ident); + return keyword == RESERVED_IF_STRICT && !strictMode ? IDENT : keyword; + } + } + + bool isPrivateName = m_current == '@' && m_parsingBuiltinFunction; + if (isPrivateName) + shift(); + + const LChar* identifierStart = currentSourcePtr(); + unsigned identifierLineStart = currentLineStartOffset(); + + while (isIdentPart(m_current)) + shift(); + + if (UNLIKELY(m_current == '\\')) { + setOffsetFromSourcePtr(identifierStart, identifierLineStart); + return parseIdentifierSlowCase(tokenData, lexerFlags, strictMode); + } + + const Identifier* ident = 0; + + if (shouldCreateIdentifier || m_parsingBuiltinFunction) { + int identifierLength = currentSourcePtr() - identifierStart; + ident = makeIdentifier(identifierStart, identifierLength); + if (m_parsingBuiltinFunction) { + if (!isSafeBuiltinIdentifier(*m_vm, ident) && !isPrivateName) { + m_lexErrorMessage = makeString("The use of '", ident->string(), "' is disallowed in builtin functions."); + return ERRORTOK; + } + if (isPrivateName) + ident = m_vm->propertyNames->getPrivateName(*ident); + else if (*ident == m_vm->propertyNames->undefinedKeyword) + tokenData->ident = &m_vm->propertyNames->undefinedPrivateName; + if (!ident) + return INVALID_PRIVATE_NAME_ERRORTOK; + } + tokenData->ident = ident; + } else + tokenData->ident = 0; + + if (UNLIKELY((remaining < maxTokenLength) && !(lexerFlags & LexerFlagsIgnoreReservedWords)) && !isPrivateName) { + ASSERT(shouldCreateIdentifier); + if (remaining < maxTokenLength) { + const HashTableValue* entry = JSC::mainTable.entry(*ident); + ASSERT((remaining < maxTokenLength) || !entry); + if (!entry) + return IDENT; + JSTokenType token = static_cast(entry->lexerValue()); + return (token != RESERVED_IF_STRICT) || strictMode ? token : IDENT; + } + return IDENT; + } + + return IDENT; +} + +template <> +template ALWAYS_INLINE JSTokenType Lexer::parseIdentifier(JSTokenData* tokenData, unsigned lexerFlags, bool strictMode) +{ + const ptrdiff_t remaining = m_codeEnd - m_code; + if ((remaining >= maxTokenLength) && !(lexerFlags & LexerFlagsIgnoreReservedWords)) { + JSTokenType keyword = parseKeyword(tokenData); + if (keyword != IDENT) { + ASSERT((!shouldCreateIdentifier) || tokenData->ident); + return keyword == RESERVED_IF_STRICT && !strictMode ? IDENT : keyword; + } + } + + bool isPrivateName = m_current == '@' && m_parsingBuiltinFunction; + if (isPrivateName) + shift(); + + const UChar* identifierStart = currentSourcePtr(); + int identifierLineStart = currentLineStartOffset(); + + UChar orAllChars = 0; + + while (isIdentPart(m_current)) { + orAllChars |= m_current; + shift(); + } + + if (UNLIKELY(m_current == '\\')) { + ASSERT(!isPrivateName); + setOffsetFromSourcePtr(identifierStart, identifierLineStart); + return parseIdentifierSlowCase(tokenData, lexerFlags, strictMode); + } + + bool isAll8Bit = false; + + if (!(orAllChars & ~0xff)) + isAll8Bit = true; + + const Identifier* ident = 0; + + if (shouldCreateIdentifier || m_parsingBuiltinFunction) { + int identifierLength = currentSourcePtr() - identifierStart; + if (isAll8Bit) + ident = makeIdentifierLCharFromUChar(identifierStart, identifierLength); + else + ident = makeIdentifier(identifierStart, identifierLength); + if (m_parsingBuiltinFunction) { + if (!isSafeBuiltinIdentifier(*m_vm, ident) && !isPrivateName) { + m_lexErrorMessage = makeString("The use of '", ident->string(), "' is disallowed in builtin functions."); + return ERRORTOK; + } + if (isPrivateName) + ident = m_vm->propertyNames->getPrivateName(*ident); + else if (*ident == m_vm->propertyNames->undefinedKeyword) + tokenData->ident = &m_vm->propertyNames->undefinedPrivateName; + if (!ident) + return INVALID_PRIVATE_NAME_ERRORTOK; + } + tokenData->ident = ident; + } else + tokenData->ident = 0; + + if (UNLIKELY((remaining < maxTokenLength) && !(lexerFlags & LexerFlagsIgnoreReservedWords)) && !isPrivateName) { + ASSERT(shouldCreateIdentifier); + if (remaining < maxTokenLength) { + const HashTableValue* entry = JSC::mainTable.entry(*ident); + ASSERT((remaining < maxTokenLength) || !entry); + if (!entry) + return IDENT; + JSTokenType token = static_cast(entry->lexerValue()); + return (token != RESERVED_IF_STRICT) || strictMode ? token : IDENT; + } + return IDENT; + } + + return IDENT; +} + +template template JSTokenType Lexer::parseIdentifierSlowCase(JSTokenData* tokenData, unsigned lexerFlags, bool strictMode) +{ + auto identifierStart = currentSourcePtr(); + bool bufferRequired = false; + + while (true) { + if (LIKELY(isIdentPart(m_current))) { + shift(); + continue; + } + if (LIKELY(m_current != '\\')) + break; + + // \uXXXX unicode characters. + bufferRequired = true; + if (identifierStart != currentSourcePtr()) + m_buffer16.append(identifierStart, currentSourcePtr() - identifierStart); + shift(); + if (UNLIKELY(m_current != 'u')) + return atEnd() ? UNTERMINATED_IDENTIFIER_ESCAPE_ERRORTOK : INVALID_IDENTIFIER_ESCAPE_ERRORTOK; + shift(); + auto character = parseUnicodeEscape(); + if (UNLIKELY(!character.isValid())) + return character.isIncomplete() ? UNTERMINATED_IDENTIFIER_UNICODE_ESCAPE_ERRORTOK : INVALID_IDENTIFIER_UNICODE_ESCAPE_ERRORTOK; + if (UNLIKELY(m_buffer16.size() ? !isIdentPart(character.value()) : !isIdentStart(character.value()))) + return INVALID_IDENTIFIER_UNICODE_ESCAPE_ERRORTOK; + if (shouldCreateIdentifier) + recordUnicodeCodePoint(character.value()); + identifierStart = currentSourcePtr(); + } + + int identifierLength; + const Identifier* ident = nullptr; + if (shouldCreateIdentifier) { + if (!bufferRequired) { + identifierLength = currentSourcePtr() - identifierStart; + ident = makeIdentifier(identifierStart, identifierLength); + } else { + if (identifierStart != currentSourcePtr()) + m_buffer16.append(identifierStart, currentSourcePtr() - identifierStart); + ident = makeIdentifier(m_buffer16.data(), m_buffer16.size()); + } + + tokenData->ident = ident; + } else + tokenData->ident = nullptr; + + m_buffer16.shrink(0); + + if (LIKELY(!(lexerFlags & LexerFlagsIgnoreReservedWords))) { + ASSERT(shouldCreateIdentifier); + const HashTableValue* entry = JSC::mainTable.entry(*ident); + if (!entry) + return IDENT; + JSTokenType token = static_cast(entry->lexerValue()); + return (token != RESERVED_IF_STRICT) || strictMode ? token : IDENT; + } + + return IDENT; +} + +static ALWAYS_INLINE bool characterRequiresParseStringSlowCase(LChar character) +{ + return character < 0xE; +} + +static ALWAYS_INLINE bool characterRequiresParseStringSlowCase(UChar character) +{ + return character < 0xE || character > 0xFF; +} + +template +template ALWAYS_INLINE typename Lexer::StringParseResult Lexer::parseString(JSTokenData* tokenData, bool strictMode) +{ + int startingOffset = currentOffset(); + int startingLineStartOffset = currentLineStartOffset(); + int startingLineNumber = lineNumber(); + T stringQuoteCharacter = m_current; + shift(); + + const T* stringStart = currentSourcePtr(); + + while (m_current != stringQuoteCharacter) { + if (UNLIKELY(m_current == '\\')) { + if (stringStart != currentSourcePtr() && shouldBuildStrings) + append8(stringStart, currentSourcePtr() - stringStart); + shift(); + + LChar escape = singleEscape(m_current); + + // Most common escape sequences first. + if (escape) { + if (shouldBuildStrings) + record8(escape); + shift(); + } else if (UNLIKELY(isLineTerminator(m_current))) + shiftLineTerminator(); + else if (m_current == 'x') { + shift(); + if (!isASCIIHexDigit(m_current) || !isASCIIHexDigit(peek(1))) { + m_lexErrorMessage = ASCIILiteral("\\x can only be followed by a hex character sequence"); + return (atEnd() || (isASCIIHexDigit(m_current) && (m_code + 1 == m_codeEnd))) ? StringUnterminated : StringCannotBeParsed; + } + T prev = m_current; + shift(); + if (shouldBuildStrings) + record8(convertHex(prev, m_current)); + shift(); + } else { + setOffset(startingOffset, startingLineStartOffset); + setLineNumber(startingLineNumber); + m_buffer8.shrink(0); + return parseStringSlowCase(tokenData, strictMode); + } + stringStart = currentSourcePtr(); + continue; + } + + if (UNLIKELY(characterRequiresParseStringSlowCase(m_current))) { + setOffset(startingOffset, startingLineStartOffset); + setLineNumber(startingLineNumber); + m_buffer8.shrink(0); + return parseStringSlowCase(tokenData, strictMode); + } + + shift(); + } + + if (currentSourcePtr() != stringStart && shouldBuildStrings) + append8(stringStart, currentSourcePtr() - stringStart); + if (shouldBuildStrings) { + tokenData->ident = makeIdentifier(m_buffer8.data(), m_buffer8.size()); + m_buffer8.shrink(0); + } else + tokenData->ident = 0; + + return StringParsedSuccessfully; +} + +template +template ALWAYS_INLINE auto Lexer::parseComplexEscape(EscapeParseMode escapeParseMode, bool strictMode, T stringQuoteCharacter) -> StringParseResult +{ + if (m_current == 'x') { + shift(); + if (!isASCIIHexDigit(m_current) || !isASCIIHexDigit(peek(1))) { + m_lexErrorMessage = ASCIILiteral("\\x can only be followed by a hex character sequence"); + return StringCannotBeParsed; + } + T prev = m_current; + shift(); + if (shouldBuildStrings) + record16(convertHex(prev, m_current)); + shift(); + return StringParsedSuccessfully; + } + + if (m_current == 'u') { + shift(); + + if (escapeParseMode == EscapeParseMode::String && m_current == stringQuoteCharacter) { + if (shouldBuildStrings) + record16('u'); + return StringParsedSuccessfully; + } + + auto character = parseUnicodeEscape(); + if (character.isValid()) { + if (shouldBuildStrings) + recordUnicodeCodePoint(character.value()); + return StringParsedSuccessfully; + } + + m_lexErrorMessage = ASCIILiteral("\\u can only be followed by a Unicode character sequence"); + return character.isIncomplete() ? StringUnterminated : StringCannotBeParsed; + } + + if (strictMode) { + if (isASCIIDigit(m_current)) { + // The only valid numeric escape in strict mode is '\0', and this must not be followed by a decimal digit. + int character1 = m_current; + shift(); + if (character1 != '0' || isASCIIDigit(m_current)) { + m_lexErrorMessage = ASCIILiteral("The only valid numeric escape in strict mode is '\\0'"); + return StringCannotBeParsed; + } + if (shouldBuildStrings) + record16(0); + return StringParsedSuccessfully; + } + } else { + if (isASCIIOctalDigit(m_current)) { + // Octal character sequences + T character1 = m_current; + shift(); + if (isASCIIOctalDigit(m_current)) { + // Two octal characters + T character2 = m_current; + shift(); + if (character1 >= '0' && character1 <= '3' && isASCIIOctalDigit(m_current)) { + if (shouldBuildStrings) + record16((character1 - '0') * 64 + (character2 - '0') * 8 + m_current - '0'); + shift(); + } else { + if (shouldBuildStrings) + record16((character1 - '0') * 8 + character2 - '0'); + } + } else { + if (shouldBuildStrings) + record16(character1 - '0'); + } + return StringParsedSuccessfully; + } + } + + if (!atEnd()) { + if (shouldBuildStrings) + record16(m_current); + shift(); + return StringParsedSuccessfully; + } + + m_lexErrorMessage = ASCIILiteral("Unterminated string constant"); + return StringUnterminated; +} + +template +template auto Lexer::parseStringSlowCase(JSTokenData* tokenData, bool strictMode) -> StringParseResult +{ + T stringQuoteCharacter = m_current; + shift(); + + const T* stringStart = currentSourcePtr(); + + while (m_current != stringQuoteCharacter) { + if (UNLIKELY(m_current == '\\')) { + if (stringStart != currentSourcePtr() && shouldBuildStrings) + append16(stringStart, currentSourcePtr() - stringStart); + shift(); + + LChar escape = singleEscape(m_current); + + // Most common escape sequences first + if (escape) { + if (shouldBuildStrings) + record16(escape); + shift(); + } else if (UNLIKELY(isLineTerminator(m_current))) + shiftLineTerminator(); + else { + StringParseResult result = parseComplexEscape(EscapeParseMode::String, strictMode, stringQuoteCharacter); + if (result != StringParsedSuccessfully) + return result; + } + + stringStart = currentSourcePtr(); + continue; + } + // Fast check for characters that require special handling. + // Catches 0, \n, \r, 0x2028, and 0x2029 as efficiently + // as possible, and lets through all common ASCII characters. + if (UNLIKELY(((static_cast(m_current) - 0xE) & 0x2000))) { + // New-line or end of input is not allowed + if (atEnd() || isLineTerminator(m_current)) { + m_lexErrorMessage = ASCIILiteral("Unexpected EOF"); + return atEnd() ? StringUnterminated : StringCannotBeParsed; + } + // Anything else is just a normal character + } + shift(); + } + + if (currentSourcePtr() != stringStart && shouldBuildStrings) + append16(stringStart, currentSourcePtr() - stringStart); + if (shouldBuildStrings) + tokenData->ident = makeIdentifier(m_buffer16.data(), m_buffer16.size()); + else + tokenData->ident = 0; + + m_buffer16.shrink(0); + return StringParsedSuccessfully; +} + +#if ENABLE(ES6_TEMPLATE_LITERAL_SYNTAX) +// While the lexer accepts (not ) sequence +// as one line terminator and increments one line number, +// TemplateLiteral considers it as two line terminators and . +// +// TemplateLiteral normalizes line terminators as follows. +// +// => +// => +// => +// <\u2028> => <\u2028> +// <\u2029> => <\u2029> +// +// So, should be normalized to . +// However, the lexer should increment the line number only once for . +// +// To achieve this, LineNumberAdder holds the current status of line terminator sequence. +// When TemplateLiteral lexer encounters a line terminator, it notifies to LineNumberAdder. +// LineNumberAdder maintains the status and increments the line number when it's necessary. +// For example, LineNumberAdder increments the line number only once for and . +template +class LineNumberAdder { +public: + LineNumberAdder(int& lineNumber) + : m_lineNumber(lineNumber) + { + } + + void clear() + { + m_previous = 0; + } + + void add(CharacterType character) + { + ASSERT(Lexer::isLineTerminator(character)); + if ((character + m_previous) == ('\n' + '\r')) + m_previous = 0; + else { + ++m_lineNumber; + m_previous = character; + } + } + +private: + int& m_lineNumber; + CharacterType m_previous { 0 }; +}; + +template +template typename Lexer::StringParseResult Lexer::parseTemplateLiteral(JSTokenData* tokenData, RawStringsBuildMode rawStringsBuildMode) +{ + const T* stringStart = currentSourcePtr(); + const T* rawStringStart = currentSourcePtr(); + + LineNumberAdder lineNumberAdder(m_lineNumber); + + while (m_current != '`') { + if (UNLIKELY(m_current == '\\')) { + lineNumberAdder.clear(); + if (stringStart != currentSourcePtr() && shouldBuildStrings) + append16(stringStart, currentSourcePtr() - stringStart); + shift(); + + LChar escape = singleEscape(m_current); + + // Most common escape sequences first. + if (escape) { + if (shouldBuildStrings) + record16(escape); + shift(); + } else if (UNLIKELY(isLineTerminator(m_current))) { + if (m_current == '\r') { + lineNumberAdder.add(m_current); + shift(); + if (m_current == '\n') { + lineNumberAdder.add(m_current); + shift(); + } + } else { + lineNumberAdder.add(m_current); + shift(); + } + } else { + bool strictMode = true; + StringParseResult result = parseComplexEscape(EscapeParseMode::Template, strictMode, '`'); + if (result != StringParsedSuccessfully) + return result; + } + + stringStart = currentSourcePtr(); + continue; + } + + if (m_current == '$' && peek(1) == '{') + break; + + // Fast check for characters that require special handling. + // Catches 0, \n, \r, 0x2028, and 0x2029 as efficiently + // as possible, and lets through all common ASCII characters. + if (UNLIKELY(((static_cast(m_current) - 0xE) & 0x2000))) { + // End of input is not allowed. + // Unlike String, line terminator is allowed. + if (atEnd()) { + m_lexErrorMessage = ASCIILiteral("Unexpected EOF"); + return atEnd() ? StringUnterminated : StringCannotBeParsed; + } + + if (isLineTerminator(m_current)) { + if (m_current == '\r') { + // Normalize , to . + if (shouldBuildStrings) { + if (stringStart != currentSourcePtr()) + append16(stringStart, currentSourcePtr() - stringStart); + if (rawStringStart != currentSourcePtr() && rawStringsBuildMode == RawStringsBuildMode::BuildRawStrings) + m_bufferForRawTemplateString16.append(rawStringStart, currentSourcePtr() - rawStringStart); + + record16('\n'); + if (rawStringsBuildMode == RawStringsBuildMode::BuildRawStrings) + m_bufferForRawTemplateString16.append('\n'); + } + lineNumberAdder.add(m_current); + shift(); + if (m_current == '\n') { + lineNumberAdder.add(m_current); + shift(); + } + stringStart = currentSourcePtr(); + rawStringStart = currentSourcePtr(); + } else { + lineNumberAdder.add(m_current); + shift(); + } + continue; + } + // Anything else is just a normal character + } + + lineNumberAdder.clear(); + shift(); + } + + bool isTail = m_current == '`'; + + if (shouldBuildStrings) { + if (currentSourcePtr() != stringStart) + append16(stringStart, currentSourcePtr() - stringStart); + if (rawStringStart != currentSourcePtr() && rawStringsBuildMode == RawStringsBuildMode::BuildRawStrings) + m_bufferForRawTemplateString16.append(rawStringStart, currentSourcePtr() - rawStringStart); + } + + if (shouldBuildStrings) { + tokenData->cooked = makeIdentifier(m_buffer16.data(), m_buffer16.size()); + // Line terminator normalization (e.g. => ) should be applied to both the raw and cooked representations. + if (rawStringsBuildMode == RawStringsBuildMode::BuildRawStrings) + tokenData->raw = makeIdentifier(m_bufferForRawTemplateString16.data(), m_bufferForRawTemplateString16.size()); + else + tokenData->raw = makeEmptyIdentifier(); + } else { + tokenData->cooked = makeEmptyIdentifier(); + tokenData->raw = makeEmptyIdentifier(); + } + tokenData->isTail = isTail; + + m_buffer16.shrink(0); + m_bufferForRawTemplateString16.shrink(0); + + if (isTail) { + // Skip ` + shift(); + } else { + // Skip $ and { + shift(); + shift(); + } + + return StringParsedSuccessfully; +} +#endif + +template +ALWAYS_INLINE void Lexer::parseHex(double& returnValue) +{ + // Optimization: most hexadecimal values fit into 4 bytes. + uint32_t hexValue = 0; + int maximumDigits = 7; + + do { + hexValue = (hexValue << 4) + toASCIIHexValue(m_current); + shift(); + --maximumDigits; + } while (isASCIIHexDigit(m_current) && maximumDigits >= 0); + + if (maximumDigits >= 0) { + returnValue = hexValue; + return; + } + + // No more place in the hexValue buffer. + // The values are shifted out and placed into the m_buffer8 vector. + for (int i = 0; i < 8; ++i) { + int digit = hexValue >> 28; + if (digit < 10) + record8(digit + '0'); + else + record8(digit - 10 + 'a'); + hexValue <<= 4; + } + + while (isASCIIHexDigit(m_current)) { + record8(m_current); + shift(); + } + + returnValue = parseIntOverflow(m_buffer8.data(), m_buffer8.size(), 16); +} + +template +ALWAYS_INLINE bool Lexer::parseBinary(double& returnValue) +{ + // Optimization: most binary values fit into 4 bytes. + uint32_t binaryValue = 0; + const unsigned maximumDigits = 32; + int digit = maximumDigits - 1; + // Temporary buffer for the digits. Makes easier + // to reconstruct the input characters when needed. + LChar digits[maximumDigits]; + + do { + binaryValue = (binaryValue << 1) + (m_current - '0'); + digits[digit] = m_current; + shift(); + --digit; + } while (isASCIIBinaryDigit(m_current) && digit >= 0); + + if (!isASCIIDigit(m_current) && digit >= 0) { + returnValue = binaryValue; + return true; + } + + for (int i = maximumDigits - 1; i > digit; --i) + record8(digits[i]); + + while (isASCIIBinaryDigit(m_current)) { + record8(m_current); + shift(); + } + + if (isASCIIDigit(m_current)) + return false; + + returnValue = parseIntOverflow(m_buffer8.data(), m_buffer8.size(), 2); + return true; +} + +template +ALWAYS_INLINE bool Lexer::parseOctal(double& returnValue) +{ + // Optimization: most octal values fit into 4 bytes. + uint32_t octalValue = 0; + const unsigned maximumDigits = 10; + int digit = maximumDigits - 1; + // Temporary buffer for the digits. Makes easier + // to reconstruct the input characters when needed. + LChar digits[maximumDigits]; + + do { + octalValue = octalValue * 8 + (m_current - '0'); + digits[digit] = m_current; + shift(); + --digit; + } while (isASCIIOctalDigit(m_current) && digit >= 0); + + if (!isASCIIDigit(m_current) && digit >= 0) { + returnValue = octalValue; + return true; + } + + for (int i = maximumDigits - 1; i > digit; --i) + record8(digits[i]); + + while (isASCIIOctalDigit(m_current)) { + record8(m_current); + shift(); + } + + if (isASCIIDigit(m_current)) + return false; + + returnValue = parseIntOverflow(m_buffer8.data(), m_buffer8.size(), 8); + return true; +} + +template +ALWAYS_INLINE bool Lexer::parseDecimal(double& returnValue) +{ + // Optimization: most decimal values fit into 4 bytes. + uint32_t decimalValue = 0; + + // Since parseOctal may be executed before parseDecimal, + // the m_buffer8 may hold ascii digits. + if (!m_buffer8.size()) { + const unsigned maximumDigits = 10; + int digit = maximumDigits - 1; + // Temporary buffer for the digits. Makes easier + // to reconstruct the input characters when needed. + LChar digits[maximumDigits]; + + do { + decimalValue = decimalValue * 10 + (m_current - '0'); + digits[digit] = m_current; + shift(); + --digit; + } while (isASCIIDigit(m_current) && digit >= 0); + + if (digit >= 0 && m_current != '.' && (m_current | 0x20) != 'e') { + returnValue = decimalValue; + return true; + } + + for (int i = maximumDigits - 1; i > digit; --i) + record8(digits[i]); + } + + while (isASCIIDigit(m_current)) { + record8(m_current); + shift(); + } + + return false; +} + +template +ALWAYS_INLINE void Lexer::parseNumberAfterDecimalPoint() +{ + record8('.'); + while (isASCIIDigit(m_current)) { + record8(m_current); + shift(); + } +} + +template +ALWAYS_INLINE bool Lexer::parseNumberAfterExponentIndicator() +{ + record8('e'); + shift(); + if (m_current == '+' || m_current == '-') { + record8(m_current); + shift(); + } + + if (!isASCIIDigit(m_current)) + return false; + + do { + record8(m_current); + shift(); + } while (isASCIIDigit(m_current)); + return true; +} + +template +ALWAYS_INLINE bool Lexer::parseMultilineComment() +{ + while (true) { + while (UNLIKELY(m_current == '*')) { + shift(); + if (m_current == '/') { + shift(); + return true; + } + } + + if (atEnd()) + return false; + + if (isLineTerminator(m_current)) { + shiftLineTerminator(); + m_terminator = true; + } else + shift(); + } +} + +template +bool Lexer::nextTokenIsColon() +{ + const T* code = m_code; + while (code < m_codeEnd && (isWhiteSpace(*code) || isLineTerminator(*code))) + code++; + + return code < m_codeEnd && *code == ':'; +} + +template +void Lexer::setTokenPosition(JSToken* tokenRecord) +{ + JSTokenData* tokenData = &tokenRecord->m_data; + tokenData->line = lineNumber(); + tokenData->offset = currentOffset(); + tokenData->lineStartOffset = currentLineStartOffset(); + ASSERT(tokenData->offset >= tokenData->lineStartOffset); +} + +template +JSTokenType Lexer::lex(JSToken* tokenRecord, unsigned lexerFlags, bool strictMode) +{ + JSTokenData* tokenData = &tokenRecord->m_data; + JSTokenLocation* tokenLocation = &tokenRecord->m_location; + m_lastTockenLocation = JSTokenLocation(tokenRecord->m_location); + + ASSERT(!m_error); + ASSERT(m_buffer8.isEmpty()); + ASSERT(m_buffer16.isEmpty()); + + JSTokenType token = ERRORTOK; + m_terminator = false; + +start: + while (isWhiteSpace(m_current)) + shift(); + + if (atEnd()) + return EOFTOK; + + tokenLocation->startOffset = currentOffset(); + ASSERT(currentOffset() >= currentLineStartOffset()); + tokenRecord->m_startPosition = currentPosition(); + + CharacterType type; + if (LIKELY(isLatin1(m_current))) + type = static_cast(typesOfLatin1Characters[m_current]); + else if (isNonLatin1IdentStart(m_current)) + type = CharacterIdentifierStart; + else if (isLineTerminator(m_current)) + type = CharacterLineTerminator; + else + type = CharacterInvalid; + + switch (type) { + case CharacterGreater: + shift(); + if (m_current == '>') { + shift(); + if (m_current == '>') { + shift(); + if (m_current == '=') { + shift(); + token = URSHIFTEQUAL; + break; + } + token = URSHIFT; + break; + } + if (m_current == '=') { + shift(); + token = RSHIFTEQUAL; + break; + } + token = RSHIFT; + break; + } + if (m_current == '=') { + shift(); + token = GE; + break; + } + token = GT; + break; + case CharacterEqual: { +#if ENABLE(ES6_ARROWFUNCTION_SYNTAX) + if (peek(1) == '>') { + token = ARROWFUNCTION; + tokenData->line = lineNumber(); + tokenData->offset = currentOffset(); + tokenData->lineStartOffset = currentLineStartOffset(); + ASSERT(tokenData->offset >= tokenData->lineStartOffset); + shift(); + shift(); + break; + } +#endif + shift(); + if (m_current == '=') { + shift(); + if (m_current == '=') { + shift(); + token = STREQ; + break; + } + token = EQEQ; + break; + } + token = EQUAL; + break; + } + case CharacterLess: + shift(); + if (m_current == '!' && peek(1) == '-' && peek(2) == '-') { + //