Implement object literal parsing; Instantiate classes from object literals

Essentially, if the contextual type is a class with a constructor that takes zero arguments or doesn't have a constructor at all, an object literal can be used to initialize a new instance of that class with preset values.
This commit is contained in:
dcodeIO
2018-07-14 04:00:04 +02:00
parent 72cb1e9008
commit 9e508de69a
15 changed files with 946 additions and 12 deletions

View File

@ -3073,6 +3073,51 @@ export class Parser extends DiagnosticEmitter {
}
return Node.createArrayLiteralExpression(elementExpressions, tn.range(startPos, tn.pos));
}
// ObjectLiteralExpression
case Token.OPENBRACE: {
let startPos = tn.tokenPos;
let names = new Array<IdentifierExpression>();
let values = new Array<Expression>();
let name: IdentifierExpression;
while (!tn.skip(Token.CLOSEBRACE)) {
if (!tn.skipIdentifier()) {
if (!tn.skip(Token.STRINGLITERAL)) {
this.error(
DiagnosticCode.Identifier_expected,
tn.range(),
);
return null;
}
name = Node.createIdentifierExpression(tn.readString(), tn.range());
name.set(CommonFlags.QUOTED);
} else {
name = Node.createIdentifierExpression(tn.readIdentifier(), tn.range());
}
if (!tn.skip(Token.COLON)) {
this.error(
DiagnosticCode._0_expected,
tn.range(), ":"
);
return null;
}
let value = this.parseExpression(tn, Precedence.COMMA + 1);
if (!value) return null;
names.push(name);
values.push(value);
if (!tn.skip(Token.COMMA)) {
if (tn.skip(Token.CLOSEBRACE)) {
break;
} else {
this.error(
DiagnosticCode._0_expected,
tn.range(), "}"
);
return null;
}
}
}
return Node.createObjectLiteralExpression(names, values, tn.range(startPos, tn.pos));
}
// AssertionExpression (unary prefix)
case Token.LESSTHAN: {
let toType = this.parseType(tn);