diff options
Diffstat (limited to 'src/qml/parser')
-rw-r--r-- | src/qml/parser/qqmljs.g | 72 | ||||
-rw-r--r-- | src/qml/parser/qqmljsast_p.h | 61 | ||||
-rw-r--r-- | src/qml/parser/qqmljslexer.cpp | 82 |
3 files changed, 136 insertions, 79 deletions
diff --git a/src/qml/parser/qqmljs.g b/src/qml/parser/qqmljs.g index 8ac7633ae0..28dbb53ff5 100644 --- a/src/qml/parser/qqmljs.g +++ b/src/qml/parser/qqmljs.g @@ -49,6 +49,7 @@ %token T_DIVIDE_EQ "/=" T_DO "do" T_DOT "." %token T_ELSE "else" T_EQ "=" T_EQ_EQ "==" %token T_EQ_EQ_EQ "===" T_FINALLY "finally" T_FOR "for" +%token T_FUNCTION_STAR "function *" %token T_FUNCTION "function" T_GE ">=" T_GT ">" %token T_GT_GT ">>" T_GT_GT_EQ ">>=" T_GT_GT_GT ">>>" %token T_GT_GT_GT_EQ ">>>=" T_IDENTIFIER "identifier" T_IF "if" @@ -122,6 +123,7 @@ --%left T_PLUS T_MINUS %nonassoc T_IDENTIFIER T_COLON T_SIGNAL T_PROPERTY T_READONLY T_ON T_SET T_GET T_OF T_STATIC T_FROM T_AS %nonassoc REDUCE_HERE +%right T_THEN T_ELSE %start TopLevel @@ -1394,18 +1396,18 @@ UiObjectMember: T_READONLY T_PROPERTY UiPropertyType QmlIdentifier T_COLON Expre } break; ./ -UiObjectMember: FunctionDeclarationWithTypes; +UiObjectMember: GeneratorDeclaration; /. case $rule_number: { - sym(1).Node = new (pool) AST::UiSourceElement(sym(1).Node); + auto node = new (pool) AST::UiSourceElement(sym(1).Node); + sym(1).Node = node; } break; ./ -UiObjectMember: GeneratorExpression; +UiObjectMember: FunctionDeclarationWithTypes; /. case $rule_number: { - auto node = new (pool) AST::UiSourceElement(sym(1).Node); - sym(1).Node = node; + sym(1).Node = new (pool) AST::UiSourceElement(sym(1).Node); } break; ./ @@ -3232,7 +3234,7 @@ ExpressionStatementLookahead: ; int token = lookaheadToken(lexer); if (token == T_LBRACE) pushToken(T_FORCE_BLOCK); - else if (token == T_FUNCTION || token == T_CLASS || token == T_LET || token == T_CONST) + else if (token == T_FUNCTION || token == T_FUNCTION_STAR || token == T_CLASS || token == T_LET || token == T_CONST) pushToken(T_FORCE_DECLARATION); } break; ./ @@ -3980,27 +3982,14 @@ GeneratorRBrace: T_RBRACE; } break; ./ -GeneratorDeclaration: Function T_STAR BindingIdentifier GeneratorLParen FormalParameters T_RPAREN FunctionLBrace GeneratorBody GeneratorRBrace; -/. - case $rule_number: { - AST::FunctionDeclaration *node = new (pool) AST::FunctionDeclaration(stringRef(3), sym(5).FormalParameterList, sym(8).StatementList); - node->functionToken = loc(1); - node->identifierToken = loc(3); - node->lparenToken = loc(4); - node->rparenToken = loc(6); - node->lbraceToken = loc(7); - node->rbraceToken = loc(9); - node->isGenerator = true; - sym(1).Node = node; - } break; -./ +FunctionStar: T_FUNCTION_STAR %prec REDUCE_HERE; -GeneratorDeclaration_Default: GeneratorDeclaration; -GeneratorDeclaration_Default: Function T_STAR GeneratorLParen FormalParameters T_RPAREN FunctionLBrace GeneratorBody GeneratorRBrace; +GeneratorDeclaration: FunctionStar BindingIdentifier GeneratorLParen FormalParameters T_RPAREN FunctionLBrace GeneratorBody GeneratorRBrace; /. case $rule_number: { - AST::FunctionDeclaration *node = new (pool) AST::FunctionDeclaration(QStringRef(), sym(4).FormalParameterList, sym(7).StatementList); + AST::FunctionDeclaration *node = new (pool) AST::FunctionDeclaration(stringRef(2), sym(4).FormalParameterList, sym(7).StatementList); node->functionToken = loc(1); + node->identifierToken = loc(2); node->lparenToken = loc(3); node->rparenToken = loc(5); node->lbraceToken = loc(6); @@ -4010,27 +3999,28 @@ GeneratorDeclaration_Default: Function T_STAR GeneratorLParen FormalParameters T } break; ./ -GeneratorExpression: T_FUNCTION T_STAR BindingIdentifier GeneratorLParen FormalParameters T_RPAREN FunctionLBrace GeneratorBody GeneratorRBrace; +GeneratorDeclaration_Default: GeneratorDeclaration; +GeneratorDeclaration_Default: FunctionStar GeneratorLParen FormalParameters T_RPAREN FunctionLBrace GeneratorBody GeneratorRBrace; /. case $rule_number: { - AST::FunctionExpression *node = new (pool) AST::FunctionExpression(stringRef(3), sym(5).FormalParameterList, sym(8).StatementList); + AST::FunctionDeclaration *node = new (pool) AST::FunctionDeclaration(QStringRef(), sym(3).FormalParameterList, sym(6).StatementList); node->functionToken = loc(1); - if (!stringRef(3).isNull()) - node->identifierToken = loc(3); - node->lparenToken = loc(4); - node->rparenToken = loc(6); - node->lbraceToken = loc(7); - node->rbraceToken = loc(9); + node->lparenToken = loc(2); + node->rparenToken = loc(4); + node->lbraceToken = loc(5); + node->rbraceToken = loc(7); node->isGenerator = true; sym(1).Node = node; } break; ./ -GeneratorExpression: T_FUNCTION T_STAR GeneratorLParen FormalParameters T_RPAREN FunctionLBrace GeneratorBody GeneratorRBrace; +GeneratorExpression: T_FUNCTION_STAR BindingIdentifier GeneratorLParen FormalParameters T_RPAREN FunctionLBrace GeneratorBody GeneratorRBrace; /. case $rule_number: { - AST::FunctionExpression *node = new (pool) AST::FunctionExpression(QStringRef(), sym(4).FormalParameterList, sym(7).StatementList); + AST::FunctionExpression *node = new (pool) AST::FunctionExpression(stringRef(2), sym(4).FormalParameterList, sym(7).StatementList); node->functionToken = loc(1); + if (!stringRef(2).isNull()) + node->identifierToken = loc(2); node->lparenToken = loc(3); node->rparenToken = loc(5); node->lbraceToken = loc(6); @@ -4040,6 +4030,20 @@ GeneratorExpression: T_FUNCTION T_STAR GeneratorLParen FormalParameters T_RPAREN } break; ./ +GeneratorExpression: T_FUNCTION_STAR GeneratorLParen FormalParameters T_RPAREN FunctionLBrace GeneratorBody GeneratorRBrace; +/. + case $rule_number: { + AST::FunctionExpression *node = new (pool) AST::FunctionExpression(QStringRef(), sym(3).FormalParameterList, sym(6).StatementList); + node->functionToken = loc(1); + node->lparenToken = loc(2); + node->rparenToken = loc(4); + node->lbraceToken = loc(5); + node->rbraceToken = loc(7); + node->isGenerator = true; + sym(1).Node = node; + } break; +./ + GeneratorBody: FunctionBody; YieldExpression: T_YIELD; @@ -4419,7 +4423,7 @@ ExportDeclarationLookahead: ; /. case $rule_number: { int token = lookaheadToken(lexer); - if (token == T_FUNCTION || token == T_CLASS) + if (token == T_FUNCTION || token == T_FUNCTION_STAR || token == T_CLASS) pushToken(T_FORCE_DECLARATION); } break; ./ diff --git a/src/qml/parser/qqmljsast_p.h b/src/qml/parser/qqmljsast_p.h index 39194068bf..6c28903066 100644 --- a/src/qml/parser/qqmljsast_p.h +++ b/src/qml/parser/qqmljsast_p.h @@ -307,6 +307,14 @@ public: int kind = Kind_Undefined; }; +template<typename T> +T lastListElement(T head) +{ + auto current = head; + while (current->next) + current = current->next; + return current; +} class QML_PARSER_EXPORT UiQualifiedId: public Node { @@ -338,7 +346,7 @@ public: { return identifierToken; } SourceLocation lastSourceLocation() const override - { return next ? next->lastSourceLocation() : identifierToken; } + { return lastListElement(this)->identifierToken; } // attributes UiQualifiedId *next; @@ -397,7 +405,7 @@ public: { return typeId->firstSourceLocation(); } SourceLocation lastSourceLocation() const override - { return next ? next->lastSourceLocation() : typeId->lastSourceLocation(); } + { return lastListElement(this)->typeId->lastSourceLocation(); } inline TypeArgumentList *finish() { @@ -678,7 +686,10 @@ public: { return literalToken; } SourceLocation lastSourceLocation() const override - { return next ? next->lastSourceLocation() : (expression ? expression->lastSourceLocation() : literalToken); } + { + auto last = lastListElement(this); + return (last->expression ? last->expression->lastSourceLocation() : last->literalToken); + } void accept0(Visitor *visitor) override; @@ -800,7 +811,7 @@ public: { return commaToken; } SourceLocation lastSourceLocation() const override - { return next ? next->lastSourceLocation() : commaToken; } + { return lastListElement(this)->commaToken; } inline Elision *finish () { @@ -961,7 +972,10 @@ public: { return elision ? elision->firstSourceLocation() : element->firstSourceLocation(); } SourceLocation lastSourceLocation() const override - { return next ? next->lastSourceLocation() : (element ? element->lastSourceLocation() : elision->lastSourceLocation()); } + { + auto last = lastListElement(this); + return last->element ? last->element->lastSourceLocation() : last->elision->lastSourceLocation(); + } Elision *elision = nullptr; PatternElement *element = nullptr; @@ -1036,7 +1050,7 @@ public: { return property->firstSourceLocation(); } SourceLocation lastSourceLocation() const override - { return next ? next->lastSourceLocation() : property->lastSourceLocation(); } + { return lastListElement(this)->property->lastSourceLocation(); } PatternProperty *property; PatternPropertyList *next; @@ -1645,7 +1659,9 @@ public: { return statement->firstSourceLocation(); } SourceLocation lastSourceLocation() const override - { return next ? next->lastSourceLocation() : statement->lastSourceLocation(); } + { + return lastListElement(this)->statement->lastSourceLocation(); + } inline StatementList *finish () { @@ -2138,7 +2154,9 @@ public: { return clause->firstSourceLocation(); } SourceLocation lastSourceLocation() const override - { return next ? next->lastSourceLocation() : clause->lastSourceLocation(); } + { + return lastListElement(this)->clause->lastSourceLocation(); + } inline CaseClauses *finish () { @@ -2429,7 +2447,9 @@ public: { return element->firstSourceLocation(); } SourceLocation lastSourceLocation() const override - { return next ? next->lastSourceLocation() : element->lastSourceLocation(); } + { + return lastListElement(this)->element->lastSourceLocation(); + } FormalParameterList *finish(MemoryPool *pool); @@ -2606,7 +2626,9 @@ public: { return importSpecifierToken; } SourceLocation lastSourceLocation() const override - { return next ? next->lastSourceLocation() : importSpecifierToken; } + { + return lastListElement(this)->importSpecifierToken; + } // attributes SourceLocation importSpecifierToken; @@ -2843,7 +2865,7 @@ public: SourceLocation firstSourceLocation() const override { return exportSpecifier->firstSourceLocation(); } SourceLocation lastSourceLocation() const override - { return next ? next->lastSourceLocation() : exportSpecifier->lastSourceLocation(); } + { return lastListElement(this)->exportSpecifier->lastSourceLocation(); } // attributes ExportSpecifier *exportSpecifier; @@ -3036,7 +3058,7 @@ public: { return member->firstSourceLocation(); } SourceLocation lastSourceLocation() const override - { return next ? next->lastSourceLocation() : member->lastSourceLocation(); } + { return lastListElement(this)->member->lastSourceLocation(); } UiObjectMemberList *finish() { @@ -3115,7 +3137,7 @@ public: { return headerItem->firstSourceLocation(); } SourceLocation lastSourceLocation() const override - { return next ? next->lastSourceLocation() : headerItem->lastSourceLocation(); } + { return lastListElement(this)->headerItem->lastSourceLocation(); } // attributes Node *headerItem; @@ -3179,7 +3201,7 @@ public: { return member->firstSourceLocation(); } SourceLocation lastSourceLocation() const override - { return next ? next->lastSourceLocation() : member->lastSourceLocation(); } + { return lastListElement(this)->member->lastSourceLocation(); } UiArrayMemberList *finish() { @@ -3240,7 +3262,10 @@ public: { return colonToken.isValid() ? identifierToken : propertyTypeToken; } SourceLocation lastSourceLocation() const override - { return next ? next->lastSourceLocation() : (colonToken.isValid() ? propertyTypeToken : identifierToken); } + { + auto last = lastListElement(this); + return (last->colonToken.isValid() ? last->propertyTypeToken : last->identifierToken); + } inline UiParameterList *finish () { @@ -3493,8 +3518,10 @@ public: { return memberToken; } SourceLocation lastSourceLocation() const override - { return next ? next->lastSourceLocation() : - valueToken.isValid() ? valueToken : memberToken; } + { + auto last = lastListElement(this); + return last->valueToken.isValid() ? last->valueToken : last->memberToken; + } void accept0(Visitor *visitor) override; diff --git a/src/qml/parser/qqmljslexer.cpp b/src/qml/parser/qqmljslexer.cpp index 1e0ac72bd1..443e1a7476 100644 --- a/src/qml/parser/qqmljslexer.cpp +++ b/src/qml/parser/qqmljslexer.cpp @@ -492,6 +492,42 @@ int Lexer::scanToken() again: _validTokenText = false; + // handle comment can be called after a '/' has been read + // and returns true if it actually encountered a comment + auto handleComment = [this](){ + if (_char == QLatin1Char('*')) { + scanChar(); + while (_codePtr <= _endPtr) { + if (_char == QLatin1Char('*')) { + scanChar(); + if (_char == QLatin1Char('/')) { + scanChar(); + + if (_engine) { + _engine->addComment(tokenOffset() + 2, _codePtr - _tokenStartPtr - 1 - 4, + tokenStartLine(), tokenStartColumn() + 2); + } + + return true; + } + } else { + scanChar(); + } + } + } else if (_char == QLatin1Char('/')) { + while (_codePtr <= _endPtr && !isLineTerminator()) { + scanChar(); + } + if (_engine) { + _engine->addComment(tokenOffset() + 2, _codePtr - _tokenStartPtr - 1 - 2, + tokenStartLine(), tokenStartColumn() + 2); + } + return true; + } + return false; + }; + + while (_char.isSpace()) { if (isLineTerminator()) { if (_restrictedKeyword) { @@ -599,35 +635,9 @@ again: case ':': return T_COLON; case '/': - if (_char == QLatin1Char('*')) { - scanChar(); - while (_codePtr <= _endPtr) { - if (_char == QLatin1Char('*')) { - scanChar(); - if (_char == QLatin1Char('/')) { - scanChar(); - - if (_engine) { - _engine->addComment(tokenOffset() + 2, _codePtr - _tokenStartPtr - 1 - 4, - tokenStartLine(), tokenStartColumn() + 2); - } - - goto again; - } - } else { - scanChar(); - } - } - } else if (_char == QLatin1Char('/')) { - while (_codePtr <= _endPtr && !isLineTerminator()) { - scanChar(); - } - if (_engine) { - _engine->addComment(tokenOffset() + 2, _codePtr - _tokenStartPtr - 1 - 2, - tokenStartLine(), tokenStartColumn() + 2); - } + if (handleComment()) goto again; - } if (_char == QLatin1Char('=')) { + else if (_char == QLatin1Char('=')) { scanChar(); return T_DIVIDE_EQ; } @@ -829,6 +839,21 @@ again: if (!identifierWithEscapeChars) kind = classify(_tokenStartPtr, _tokenLength, parseModeFlags()); + if (kind == T_FUNCTION) { + continue_skipping: + while (_codePtr < _endPtr && _char.isSpace()) + scanChar(); + if (_char == QLatin1Char('*')) { + _tokenLength = _codePtr - _tokenStartPtr - 1; + kind = T_FUNCTION_STAR; + scanChar(); + } else if (_char == QLatin1Char('/')) { + scanChar(); + if (handleComment()) + goto continue_skipping; + } + } + if (_engine) { if (kind == T_IDENTIFIER && identifierWithEscapeChars) _tokenSpell = _engine->newStringRef(_tokenText); @@ -1407,6 +1432,7 @@ static const int uriTokens[] = { QQmlJSGrammar::T_FINALLY, QQmlJSGrammar::T_FOR, QQmlJSGrammar::T_FUNCTION, + QQmlJSGrammar::T_FUNCTION_STAR, QQmlJSGrammar::T_IF, QQmlJSGrammar::T_IN, QQmlJSGrammar::T_OF, |