Ignore:
Timestamp:
Jun 25, 2015, 11:49:20 PM (10 years ago)
Author:
[email protected]
Message:

Source/JavaScriptCore:

[ES6] Implement ES6 arrow function syntax. Parser of arrow function with execution as common function.
https://bugs.webkit.org/show_bug.cgi?id=144955

Reviewed by Yusuke Suzuki.

Added support of ES6 arrow function. Changes were made according to following spec http://wiki.ecmascript.org/doku.php?id=harmony:arrow_function_syntax. Patch does not include any arrow function specific behavior e.g. lexical bind this, arguments and etc.

This patch implements the simplest cases of arrow function declaration:

parameters () => 10 + 20
parameter x => x + 20
parameters (x, y) => x + y
function with block x => { return x*10; }

Not implemented:

bind of the this, arguments, super and etc.
exception in case of trying to use 'new' with arrow function

Patch by Aleksandr Skachkov <[email protected]> on 2015-06-26

  • parser/ASTBuilder.h:

(JSC::ASTBuilder::createFunctionExpr):
(JSC::ASTBuilder::createArrowFunctionExpr):
(JSC::ASTBuilder::createGetterOrSetterProperty):
(JSC::ASTBuilder::createFuncDeclStatement):

  • parser/Lexer.cpp:

(JSC::Lexer<T>::setTokenPosition):
(JSC::Lexer<T>::lex):

  • parser/Lexer.h:

(JSC::Lexer::lastTokenLocation):
(JSC::Lexer::setTerminator):

  • parser/Parser.cpp:

(JSC::Parser<LexerType>::parseInner):
(JSC::Parser<LexerType>::parseSourceElements):
(JSC::Parser<LexerType>::parseArrowFunctionSingleExpressionBody):
(JSC::Parser<LexerType>::parseSwitchClauses):
(JSC::Parser<LexerType>::parseSwitchDefaultClause):
(JSC::Parser<LexerType>::parseBlockStatement):
(JSC::Parser<LexerType>::parseFunctionBody):
(JSC::stringForFunctionMode):
(JSC::Parser<LexerType>::parseFunctionParameters):
(JSC::Parser<LexerType>::parseFunctionInfo):
(JSC::Parser<LexerType>::parseFunctionDeclaration):
(JSC::Parser<LexerType>::parseClass):
(JSC::Parser<LexerType>::parseAssignmentExpression):
(JSC::Parser<LexerType>::parsePropertyMethod):
(JSC::Parser<LexerType>::parseGetterSetter):
(JSC::Parser<LexerType>::parseArrowFunctionExpression):

  • parser/Parser.h:

(JSC::Parser::locationBeforeLastToken):
(JSC::Parser::isEndOfArrowFunction):
(JSC::Parser::isArrowFunctionParamters):
(JSC::Parser::setEndOfStatement):

  • parser/ParserFunctionInfo.h:
  • parser/ParserTokens.h:
  • parser/SourceCode.h:

(JSC::SourceCode::subArrowExpression):

  • parser/SourceProviderCacheItem.h:

(JSC::SourceProviderCacheItem::endFunctionToken):
(JSC::SourceProviderCacheItem::SourceProviderCacheItem):

  • parser/SyntaxChecker.h:

(JSC::SyntaxChecker::createArrowFunctionExpr):
(JSC::SyntaxChecker::setFunctionNameStart):

LayoutTests:

[ES6] Implement ES6 arrow function syntax. Parser of arrow function with execution as common function
https://bugs.webkit.org/show_bug.cgi?id=144955

Reviewed by Yusuke Suzuki.

Added arrow function tests

Patch by Aleksandr Skachkov <[email protected]> on 2015-06-26

  • js/arrowfunction-asparamter-1-expected.txt: Added.
  • js/arrowfunction-asparamter-1.html: Added.
  • js/arrowfunction-asparamter-2-expected.txt: Added.
  • js/arrowfunction-asparamter-2.html: Added.
  • js/arrowfunction-associativity-1-expected.txt: Added.
  • js/arrowfunction-associativity-1.html: Added.
  • js/arrowfunction-associativity-2-expected.txt: Added.
  • js/arrowfunction-associativity-2.html: Added.
  • js/arrowfunction-block-1-expected.txt: Added.
  • js/arrowfunction-block-1.html: Added.
  • js/arrowfunction-block-2-expected.txt: Added.
  • js/arrowfunction-block-2.html: Added.
  • js/arrowfunction-syntax-endings-expected.txt: Added.
  • js/arrowfunction-syntax-endings.html: Added.
  • js/arrowfunction-syntax-errors-expected.txt: Added.
  • js/arrowfunction-syntax-errors.html: Added.
  • js/arrowfunction-syntax-expected.txt: Added.
  • js/arrowfunction-syntax.html: Added.
  • js/script-tests/arrowfunction-asparamter-1.js: Added.
  • js/script-tests/arrowfunction-asparamter-2.js: Added.
  • js/script-tests/arrowfunction-associativity-1.js: Added.
  • js/script-tests/arrowfunction-associativity-2.js: Added.
  • js/script-tests/arrowfunction-block-1.js: Added.
  • js/script-tests/arrowfunction-block-2.js: Added.
  • js/script-tests/arrowfunction-syntax-endings.js: Added.
  • js/script-tests/arrowfunction-syntax-errors.js: Added.
  • js/script-tests/arrowfunction-syntax.js: Added.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/parser/Parser.h

    r185699 r185989  
    8181
    8282enum SourceElementsMode { CheckForStrictMode, DontCheckForStrictMode };
     83#if ENABLE(ES6_ARROWFUNCTION_SYNTAX)
     84enum FunctionParseType { StandardFunctionParseType, ArrowFunctionParseType };
     85#else
     86enum FunctionParseType { StandardFunctionParseType};
     87#endif
    8388enum FunctionRequirements { FunctionNoRequirements, FunctionNeedsName };
    8489enum FunctionParseMode {
     
    8792    SetterMode,
    8893    MethodMode,
     94#if ENABLE(ES6_ARROWFUNCTION_SYNTAX)
     95    ArrowFunctionMode
     96#endif
    8997};
    9098enum DeconstructionKind {
     
    431439
    432440    JSTextPosition positionBeforeLastNewline() const { return m_lexer->positionBeforeLastNewline(); }
     441    JSTokenLocation locationBeforeLastToken() const { return m_lexer->lastTokenLocation(); }
    433442    Vector<RefPtr<UniquedStringImpl>>&& closedVariables() { return WTF::move(m_closedVariables); }
    434443
     
    612621        return m_token.m_type == IDENT && *m_token.m_data.ident == m_vm->propertyNames->of;
    613622    }
     623   
     624#if ENABLE(ES6_ARROWFUNCTION_SYNTAX)
     625    ALWAYS_INLINE bool isEndOfArrowFunction()
     626    {
     627        return match(SEMICOLON) || match(COMMA) || match(CLOSEPAREN) || match(CLOSEBRACE) || match(CLOSEBRACKET) || match(EOFTOK) || m_lexer->prevTerminator();
     628    }
     629   
     630    ALWAYS_INLINE bool isArrowFunctionParamters()
     631    {
     632        bool isArrowFunction = false;
     633       
     634        if (match(EOFTOK))
     635            return isArrowFunction;
     636       
     637        SavePoint saveArrowFunctionPoint = createSavePoint();
     638       
     639        if (consume(OPENPAREN)) {
     640            bool isArrowFunctionParamters = true;
     641           
     642            while (consume(IDENT)) {
     643                if (consume(COMMA)) {
     644                    if (!match(IDENT)) {
     645                        isArrowFunctionParamters = false;
     646                        break;
     647                    }
     648                } else
     649                    break;
     650            }
     651           
     652            if (isArrowFunctionParamters) {
     653                if (consume(CLOSEPAREN) && match(ARROWFUNCTION))
     654                    isArrowFunction = true;
     655            }
     656        } else if (consume(IDENT) && match(ARROWFUNCTION))
     657            isArrowFunction = true;
     658
     659        restoreSavePoint(saveArrowFunctionPoint);
     660       
     661        return isArrowFunction;
     662    }
     663#endif
    614664   
    615665    ALWAYS_INLINE unsigned tokenStart()
     
    716766        return result;
    717767    }
    718    
    719     template <class TreeBuilder> TreeSourceElements parseSourceElements(TreeBuilder&, SourceElementsMode);
     768
     769    template <class TreeBuilder> TreeSourceElements parseSourceElements(TreeBuilder&, SourceElementsMode, FunctionParseType);
    720770    template <class TreeBuilder> TreeStatement parseStatement(TreeBuilder&, const Identifier*& directive, unsigned* directiveLiteralLength = 0);
    721771#if ENABLE(ES6_CLASS_SYNTAX)
     
    757807    template <class TreeBuilder> TreeExpression parsePropertyMethod(TreeBuilder& context, const Identifier* methodName);
    758808    template <class TreeBuilder> TreeProperty parseGetterSetter(TreeBuilder&, bool strict, PropertyNode::Type, unsigned getterOrSetterStartOffset, ConstructorKind = ConstructorKind::None, SuperBinding = SuperBinding::NotNeeded);
    759     template <class TreeBuilder> ALWAYS_INLINE TreeFunctionBody parseFunctionBody(TreeBuilder&, int functionKeywordStart, int functionNameStart, int parametersStart, ConstructorKind);
     809    template <class TreeBuilder> ALWAYS_INLINE TreeFunctionBody parseFunctionBody(TreeBuilder&, int functionKeywordStart, int functionNameStart, int parametersStart, ConstructorKind, FunctionParseType);
    760810    template <class TreeBuilder> ALWAYS_INLINE TreeFormalParameterList parseFormalParameters(TreeBuilder&);
    761811    enum VarDeclarationListContext { ForLoopContext, VarDeclarationContext };
     
    763813    template <class TreeBuilder> NEVER_INLINE TreeConstDeclList parseConstDeclarationList(TreeBuilder&);
    764814
     815#if ENABLE(ES6_ARROWFUNCTION_SYNTAX)
     816    template <class TreeBuilder> TreeStatement parseArrowFunctionSingleExpressionBody(TreeBuilder&, FunctionParseType);
     817    template <class TreeBuilder> TreeExpression parseArrowFunctionExpression(TreeBuilder&);
     818#endif
     819
    765820    template <class TreeBuilder> NEVER_INLINE TreeDeconstructionPattern createBindingPattern(TreeBuilder&, DeconstructionKind, const Identifier&, int depth, JSToken);
    766821    template <class TreeBuilder> NEVER_INLINE TreeDeconstructionPattern parseDeconstructionPattern(TreeBuilder&, DeconstructionKind, int depth = 0);
     
    768823    template <class TreeBuilder> NEVER_INLINE TreeExpression parseDefaultValueForDeconstructionPattern(TreeBuilder&);
    769824
    770     template <class TreeBuilder> NEVER_INLINE bool parseFunctionInfo(TreeBuilder&, FunctionRequirements, FunctionParseMode, bool nameIsInContainingScope, ConstructorKind, SuperBinding, int functionKeywordStart, ParserFunctionInfo<TreeBuilder>&);
    771    
    772     template <class TreeBuilder> NEVER_INLINE int parseFunctionParameters(TreeBuilder&, FunctionRequirements, FunctionParseMode, bool, AutoPopScopeRef&, ParserFunctionInfo<TreeBuilder>&);
     825    template <class TreeBuilder> NEVER_INLINE bool parseFunctionInfo(TreeBuilder&, FunctionRequirements, FunctionParseMode, bool nameIsInContainingScope, ConstructorKind, SuperBinding, int functionKeywordStart, ParserFunctionInfo<TreeBuilder>&, FunctionParseType);
     826   
     827    template <class TreeBuilder> NEVER_INLINE int parseFunctionParameters(TreeBuilder&, FunctionParseMode, ParserFunctionInfo<TreeBuilder>&);
    773828
    774829#if ENABLE(ES6_CLASS_SYNTAX)
    775830    template <class TreeBuilder> NEVER_INLINE TreeClassExpression parseClass(TreeBuilder&, FunctionRequirements, ParserClassInfo<TreeBuilder>&);
    776831#endif
     832
    777833#if ENABLE(ES6_TEMPLATE_LITERAL_SYNTAX)
    778834    template <class TreeBuilder> NEVER_INLINE typename TreeBuilder::TemplateString parseTemplateString(TreeBuilder& context, bool isTemplateHead, typename LexerType::RawStringsBuildMode, bool& elementIsTail);
     
    794850    }
    795851   
     852
     853#if ENABLE(ES6_ARROWFUNCTION_SYNTAX)
     854    void setEndOfStatement()
     855    {
     856        m_lexer->setTokenPosition(&m_token);
     857    }
     858#endif
     859
    796860    bool canRecurse()
    797861    {
Note: See TracChangeset for help on using the changeset viewer.