qmljs: update parser

Update the qtcreator qmljs parser to the
one of Qt 5.12. It supports EcmaScript 7.

Task-number: QTCREATORBUG-20341
Change-Id: I0d1cff71402ba17e22cde6b46c65614e162280de
Reviewed-by: Erik Verbruggen <erik.verbruggen@qt.io>
This commit is contained in:
Marco Benelli
2018-10-16 15:32:58 +02:00
parent fe8a372773
commit 4646acad0d
46 changed files with 10604 additions and 5872 deletions

View File

@@ -31,6 +31,27 @@ QT_QML_BEGIN_NAMESPACE
namespace QmlJS { namespace AST {
FunctionExpression *asAnonymousFunctionDefinition(Node *n)
{
if (!n)
return nullptr;
FunctionExpression *f = n->asFunctionDefinition();
if (!f || !f->name.isNull())
return nullptr;
return f;
}
ClassExpression *asAnonymousClassDefinition(Node *n)
{
if (!n)
return nullptr;
ClassExpression *c = n->asClassDefinition();
if (!c || !c->name.isNull())
return nullptr;
return c;
}
void Node::accept(Visitor *visitor)
{
if (visitor->preVisit(this)) {
@@ -47,22 +68,42 @@ void Node::accept(Node *node, Visitor *visitor)
ExpressionNode *Node::expressionCast()
{
return 0;
return nullptr;
}
BinaryExpression *Node::binaryExpressionCast()
{
return 0;
return nullptr;
}
Statement *Node::statementCast()
{
return 0;
return nullptr;
}
UiObjectMember *Node::uiObjectMemberCast()
{
return 0;
return nullptr;
}
LeftHandSideExpression *Node::leftHandSideExpressionCast()
{
return nullptr;
}
Pattern *Node::patternCast()
{
return nullptr;
}
FunctionExpression *Node::asFunctionDefinition()
{
return nullptr;
}
ClassExpression *Node::asClassDefinition()
{
return nullptr;
}
ExpressionNode *ExpressionNode::expressionCast()
@@ -70,6 +111,42 @@ ExpressionNode *ExpressionNode::expressionCast()
return this;
}
FormalParameterList *ExpressionNode::reparseAsFormalParameterList(MemoryPool *pool)
{
AST::ExpressionNode *expr = this;
AST::FormalParameterList *f = nullptr;
if (AST::Expression *commaExpr = AST::cast<AST::Expression *>(expr)) {
f = commaExpr->left->reparseAsFormalParameterList(pool);
if (!f)
return nullptr;
expr = commaExpr->right;
}
AST::ExpressionNode *rhs = nullptr;
if (AST::BinaryExpression *assign = AST::cast<AST::BinaryExpression *>(expr)) {
if (assign->op != QSOperator::Assign)
return nullptr;
expr = assign->left;
rhs = assign->right;
}
AST::PatternElement *binding = nullptr;
if (AST::IdentifierExpression *idExpr = AST::cast<AST::IdentifierExpression *>(expr)) {
binding = new (pool) AST::PatternElement(idExpr->name, rhs);
binding->identifierToken = idExpr->identifierToken;
} else if (AST::Pattern *p = expr->patternCast()) {
SourceLocation loc;
QString s;
if (!p->convertLiteralToAssignmentPattern(pool, &loc, &s))
return nullptr;
binding = new (pool) AST::PatternElement(p, rhs);
binding->identifierToken = p->firstSourceLocation();
}
if (!binding)
return nullptr;
return new (pool) AST::FormalParameterList(f, binding);
}
BinaryExpression *BinaryExpression::binaryExpressionCast()
{
return this;
@@ -93,6 +170,16 @@ void NestedExpression::accept0(Visitor *visitor)
visitor->endVisit(this);
}
FunctionExpression *NestedExpression::asFunctionDefinition()
{
return expression->asFunctionDefinition();
}
ClassExpression *NestedExpression::asClassDefinition()
{
return expression->asClassDefinition();
}
void ThisExpression::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
@@ -133,6 +220,15 @@ void FalseLiteral::accept0(Visitor *visitor)
visitor->endVisit(this);
}
void SuperLiteral::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
}
visitor->endVisit(this);
}
void StringLiteral::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
@@ -141,6 +237,16 @@ void StringLiteral::accept0(Visitor *visitor)
visitor->endVisit(this);
}
void TemplateLiteral::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
if (next)
accept(next, visitor);
}
visitor->endVisit(this);
}
void NumericLiteral::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
@@ -157,17 +263,27 @@ void RegExpLiteral::accept0(Visitor *visitor)
visitor->endVisit(this);
}
void ArrayLiteral::accept0(Visitor *visitor)
void ArrayPattern::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
if (visitor->visit(this))
accept(elements, visitor);
accept(elision, visitor);
}
visitor->endVisit(this);
}
void ObjectLiteral::accept0(Visitor *visitor)
bool ArrayPattern::isValidArrayLiteral(SourceLocation *errorLocation) const {
for (PatternElementList *it = elements; it != nullptr; it = it->next) {
PatternElement *e = it->element;
if (e && e->bindingTarget != nullptr) {
if (errorLocation)
*errorLocation = e->firstSourceLocation();
return false;
}
}
return true;
}
void ObjectPattern::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
accept(properties, visitor);
@@ -176,18 +292,170 @@ void ObjectLiteral::accept0(Visitor *visitor)
visitor->endVisit(this);
}
void ElementList::accept0(Visitor *visitor)
/*
This is the grammar for AssignmentPattern that we need to convert the literal to:
AssignmentPattern:
ObjectAssignmentPattern
ArrayAssignmentPattern
ArrayAssignmentPattern:
[ ElisionOpt AssignmentRestElementOpt ]
[ AssignmentElementList ]
[ AssignmentElementList , ElisionOpt AssignmentRestElementOpt ]
AssignmentElementList:
AssignmentElisionElement
AssignmentElementList , AssignmentElisionElement
AssignmentElisionElement:
ElisionOpt AssignmentElement
AssignmentRestElement:
... DestructuringAssignmentTarget
ObjectAssignmentPattern:
{}
{ AssignmentPropertyList }
{ AssignmentPropertyList, }
AssignmentPropertyList:
AssignmentProperty
AssignmentPropertyList , AssignmentProperty
AssignmentProperty:
IdentifierReference InitializerOpt_In
PropertyName:
AssignmentElement
AssignmentElement:
DestructuringAssignmentTarget InitializerOpt_In
DestructuringAssignmentTarget:
LeftHandSideExpression
It was originally parsed with the following grammar:
ArrayLiteral:
[ ElisionOpt ]
[ ElementList ]
[ ElementList , ElisionOpt ]
ElementList:
ElisionOpt AssignmentExpression_In
ElisionOpt SpreadElement
ElementList , ElisionOpt AssignmentExpression_In
ElementList , Elisionopt SpreadElement
SpreadElement:
... AssignmentExpression_In
ObjectLiteral:
{}
{ PropertyDefinitionList }
{ PropertyDefinitionList , }
PropertyDefinitionList:
PropertyDefinition
PropertyDefinitionList , PropertyDefinition
PropertyDefinition:
IdentifierReference
CoverInitializedName
PropertyName : AssignmentExpression_In
MethodDefinition
PropertyName:
LiteralPropertyName
ComputedPropertyName
*/
bool ArrayPattern::convertLiteralToAssignmentPattern(MemoryPool *pool, SourceLocation *errorLocation, QString *errorMessage)
{
if (visitor->visit(this)) {
for (ElementList *it = this; it; it = it->next) {
accept(it->elision, visitor);
accept(it->expression, visitor);
if (parseMode == Binding)
return true;
for (auto *it = elements; it; it = it->next) {
if (!it->element)
continue;
if (it->element->type == PatternElement::SpreadElement && it->next) {
*errorLocation = it->element->firstSourceLocation();
*errorMessage = QString::fromLatin1("'...' can only appear as last element in a destructuring list.");
return false;
}
if (!it->element->convertLiteralToAssignmentPattern(pool, errorLocation, errorMessage))
return false;
}
parseMode = Binding;
return true;
}
bool ObjectPattern::convertLiteralToAssignmentPattern(MemoryPool *pool, SourceLocation *errorLocation, QString *errorMessage)
{
if (parseMode == Binding)
return true;
for (auto *it = properties; it; it = it->next) {
if (!it->property->convertLiteralToAssignmentPattern(pool, errorLocation, errorMessage))
return false;
}
parseMode = Binding;
return true;
}
bool PatternElement::convertLiteralToAssignmentPattern(MemoryPool *pool, SourceLocation *errorLocation, QString *errorMessage)
{
Q_ASSERT(type == Literal || type == SpreadElement);
Q_ASSERT(bindingIdentifier.isNull());
Q_ASSERT(bindingTarget == nullptr);
Q_ASSERT(bindingTarget == nullptr);
Q_ASSERT(initializer);
ExpressionNode *init = initializer;
initializer = nullptr;
LeftHandSideExpression *lhs = init->leftHandSideExpressionCast();
if (type == SpreadElement) {
if (!lhs) {
*errorLocation = init->firstSourceLocation();
*errorMessage = QString::fromLatin1("Invalid lhs expression after '...' in destructuring expression.");
return false;
}
} else {
type = PatternElement::Binding;
if (BinaryExpression *b = init->binaryExpressionCast()) {
if (b->op != QSOperator::Assign) {
*errorLocation = b->operatorToken;
*errorMessage = QString::fromLatin1("Invalid assignment operation in destructuring expression");
return false;
}
lhs = b->left->leftHandSideExpressionCast();
initializer = b->right;
Q_ASSERT(lhs);
} else {
lhs = init->leftHandSideExpressionCast();
}
if (!lhs) {
*errorLocation = init->firstSourceLocation();
*errorMessage = QString::fromLatin1("Destructuring target is not a left hand side expression.");
return false;
}
}
visitor->endVisit(this);
if (auto *i = cast<IdentifierExpression *>(lhs)) {
bindingIdentifier = i->name;
identifierToken = i->identifierToken;
return true;
}
bindingTarget = lhs;
if (auto *p = lhs->patternCast()) {
if (!p->convertLiteralToAssignmentPattern(pool, errorLocation, errorMessage))
return false;
}
return true;
}
bool PatternProperty::convertLiteralToAssignmentPattern(MemoryPool *pool, SourceLocation *errorLocation, QString *errorMessage)
{
Q_ASSERT(type != SpreadElement);
if (type == Binding)
return true;
if (type == Getter || type == Setter) {
*errorLocation = firstSourceLocation();
*errorMessage = QString::fromLatin1("Invalid getter/setter in destructuring expression.");
return false;
}
Q_ASSERT(type == Literal);
return PatternElement::convertLiteralToAssignmentPattern(pool, errorLocation, errorMessage);
}
void Elision::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
@@ -197,38 +465,6 @@ void Elision::accept0(Visitor *visitor)
visitor->endVisit(this);
}
void PropertyNameAndValue::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
accept(name, visitor);
accept(value, visitor);
}
visitor->endVisit(this);
}
void PropertyGetterSetter::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
accept(name, visitor);
accept(formals, visitor);
accept(functionBody, visitor);
}
visitor->endVisit(this);
}
void PropertyAssignmentList::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
for (PropertyAssignmentList *it = this; it; it = it->next) {
accept(it->assignment, visitor);
}
}
visitor->endVisit(this);
}
void IdentifierPropertyName::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
@@ -253,6 +489,28 @@ void NumericLiteralPropertyName::accept0(Visitor *visitor)
visitor->endVisit(this);
}
namespace {
struct LocaleWithoutZeroPadding : public QLocale
{
LocaleWithoutZeroPadding()
: QLocale(QLocale::C)
{
setNumberOptions(QLocale::OmitLeadingZeroInExponent | QLocale::OmitGroupSeparator);
}
};
}
QString NumericLiteralPropertyName::asString()const
{
// Can't use QString::number here anymore as it does zero padding by default now.
// In C++11 this initialization is thread-safe (6.7 [stmt.dcl] p4)
static LocaleWithoutZeroPadding locale;
// Because of https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83562 we can't use thread_local
// for the locale variable and therefore rely on toString(double) to be thread-safe.
return locale.toString(id, 'g', 16);
}
void ArrayMemberExpression::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
@@ -482,15 +740,6 @@ void VariableDeclarationList::accept0(Visitor *visitor)
visitor->endVisit(this);
}
void VariableDeclaration::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
accept(expression, visitor);
}
visitor->endVisit(this);
}
void EmptyStatement::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
@@ -543,17 +792,6 @@ void ForStatement::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
accept(initialiser, visitor);
accept(condition, visitor);
accept(expression, visitor);
accept(statement, visitor);
}
visitor->endVisit(this);
}
void LocalForStatement::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
accept(declarations, visitor);
accept(condition, visitor);
accept(expression, visitor);
@@ -566,18 +804,7 @@ void LocalForStatement::accept0(Visitor *visitor)
void ForEachStatement::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
accept(initialiser, visitor);
accept(expression, visitor);
accept(statement, visitor);
}
visitor->endVisit(this);
}
void LocalForEachStatement::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
accept(declaration, visitor);
accept(lhs, visitor);
accept(expression, visitor);
accept(statement, visitor);
}
@@ -610,6 +837,16 @@ void ReturnStatement::accept0(Visitor *visitor)
visitor->endVisit(this);
}
void YieldExpression::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
accept(expression, visitor);
}
visitor->endVisit(this);
}
void WithStatement::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
@@ -703,6 +940,7 @@ void TryStatement::accept0(Visitor *visitor)
void Catch::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
accept(patternElement, visitor);
accept(statement, visitor);
}
@@ -738,57 +976,182 @@ void FunctionExpression::accept0(Visitor *visitor)
visitor->endVisit(this);
}
FunctionExpression *FunctionExpression::asFunctionDefinition()
{
return this;
}
QStringList FormalParameterList::formals() const
{
QStringList formals;
int i = 0;
for (const FormalParameterList *it = this; it; it = it->next) {
if (it->element) {
QString name = it->element->bindingIdentifier.toString();
int duplicateIndex = formals.indexOf(name);
if (duplicateIndex >= 0) {
// change the name of the earlier argument to enforce the lookup semantics from the spec
formals[duplicateIndex] += QLatin1String("#") + QString::number(i);
}
formals += name;
}
++i;
}
return formals;
}
QStringList FormalParameterList::boundNames() const
{
QStringList names;
for (const FormalParameterList *it = this; it; it = it->next) {
if (it->element)
it->element->boundNames(&names);
}
return names;
}
void FormalParameterList::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
// ###
accept(element, visitor);
if (next)
accept(next, visitor);
}
visitor->endVisit(this);
}
void FunctionBody::accept0(Visitor *visitor)
FormalParameterList *FormalParameterList::finish(QmlJS::MemoryPool *pool)
{
if (visitor->visit(this)) {
accept(elements, visitor);
}
FormalParameterList *front = next;
next = nullptr;
visitor->endVisit(this);
int i = 0;
for (const FormalParameterList *it = this; it; it = it->next) {
if (it->element && it->element->bindingIdentifier.isEmpty())
it->element->bindingIdentifier = pool->newString(QLatin1String("arg#") + QString::number(i));
++i;
}
return front;
}
void Program::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
accept(elements, visitor);
accept(statements, visitor);
}
visitor->endVisit(this);
}
void SourceElements::accept0(Visitor *visitor)
void ImportSpecifier::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
for (SourceElements *it = this; it; it = it->next) {
accept(it->element, visitor);
}
visitor->endVisit(this);
}
void ImportsList::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
for (ImportsList *it = this; it; it = it->next) {
accept(it->importSpecifier, visitor);
}
}
visitor->endVisit(this);
}
void FunctionSourceElement::accept0(Visitor *visitor)
void NamedImports::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
accept(declaration, visitor);
accept(importsList, visitor);
}
visitor->endVisit(this);
}
void StatementSourceElement::accept0(Visitor *visitor)
void FromClause::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
accept(statement, visitor);
}
visitor->endVisit(this);
}
void NameSpaceImport::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
}
visitor->endVisit(this);
}
void ImportClause::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
accept(nameSpaceImport, visitor);
accept(namedImports, visitor);
}
visitor->endVisit(this);
}
void ImportDeclaration::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
accept(importClause, visitor);
accept(fromClause, visitor);
}
visitor->endVisit(this);
}
void ExportSpecifier::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
}
visitor->endVisit(this);
}
void ExportsList::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
for (ExportsList *it = this; it; it = it->next) {
accept(it->exportSpecifier, visitor);
}
}
visitor->endVisit(this);
}
void ExportClause::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
accept(exportsList, visitor);
}
visitor->endVisit(this);
}
void ExportDeclaration::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
accept(fromClause, visitor);
accept(exportClause, visitor);
accept(variableStatementOrDeclaration, visitor);
}
visitor->endVisit(this);
}
void ESModule::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
accept(body, visitor);
}
visitor->endVisit(this);
@@ -916,18 +1279,9 @@ void UiImport::accept0(Visitor *visitor)
visitor->endVisit(this);
}
void UiQualifiedPragmaId::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
}
visitor->endVisit(this);
}
void UiPragma::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
accept(pragmaType, visitor);
}
visitor->endVisit(this);
@@ -970,6 +1324,153 @@ void UiEnumMemberList::accept0(Visitor *visitor)
visitor->endVisit(this);
}
void TaggedTemplate::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
accept(base, visitor);
accept(templateLiteral, visitor);
}
visitor->endVisit(this);
}
void PatternElement::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
accept(bindingTarget, visitor);
accept(initializer, visitor);
}
visitor->endVisit(this);
}
void PatternElement::boundNames(QStringList *names)
{
if (bindingTarget) {
if (PatternElementList *e = elementList())
e->boundNames(names);
else if (PatternPropertyList *p = propertyList())
p->boundNames(names);
} else {
names->append(bindingIdentifier.toString());
}
}
void PatternElementList::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
accept(elision, visitor);
accept(element, visitor);
if (next)
accept(next, visitor);
}
visitor->endVisit(this);
}
void PatternElementList::boundNames(QStringList *names)
{
for (PatternElementList *it = this; it; it = it->next) {
if (it->element)
it->element->boundNames(names);
}
}
void PatternProperty::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
accept(name, visitor);
accept(bindingTarget, visitor);
accept(initializer, visitor);
}
visitor->endVisit(this);
}
void PatternProperty::boundNames(QStringList *names)
{
PatternElement::boundNames(names);
}
void PatternPropertyList::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
accept(property, visitor);
if (next)
accept(next, visitor);
}
visitor->endVisit(this);
}
void PatternPropertyList::boundNames(QStringList *names)
{
for (PatternPropertyList *it = this; it; it = it->next)
it->property->boundNames(names);
}
void ComputedPropertyName::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
accept(expression, visitor);
}
visitor->endVisit(this);
}
void ClassExpression::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
accept(heritage, visitor);
accept(elements, visitor);
}
visitor->endVisit(this);
}
ClassExpression *ClassExpression::asClassDefinition()
{
return this;
}
void ClassDeclaration::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
accept(heritage, visitor);
accept(elements, visitor);
}
visitor->endVisit(this);
}
void ClassElementList::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
accept(property, visitor);
if (next)
accept(next, visitor);
}
visitor->endVisit(this);
}
ClassElementList *ClassElementList::finish()
{
ClassElementList *front = next;
next = nullptr;
return front;
}
Pattern *Pattern::patternCast()
{
return this;
}
LeftHandSideExpression *LeftHandSideExpression::leftHandSideExpressionCast()
{
return this;
}
} } // namespace QmlJS::AST
QT_QML_END_NAMESPACE