From 3597f4f4d785481011bd580d73457ab6f82e045a Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Tue, 25 Nov 2014 01:21:10 -0800 Subject: [PATCH] Improve comments for both parsers and bring more in sync. --- src/compiler/parser.ts | 42 ++++++++++++++++++----------------- src/services/syntax/parser.ts | 41 +++++++++++++++++++--------------- 2 files changed, 45 insertions(+), 38 deletions(-) diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index dabd00c3432..4219ad40ef4 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -1678,9 +1678,9 @@ module ts { return parseInitializer(/*inParameter*/ true); } - function parseSignature(kind: SyntaxKind, returnToken: SyntaxKind, returnTokenRequired: boolean, isGenerator: boolean): ParsedSignature { + function parseSignature(kind: SyntaxKind, returnToken: SyntaxKind, returnTokenRequired: boolean, yieldAndGeneratorParameterContext: boolean): ParsedSignature { var signature = {}; - fillSignature(kind, returnToken, returnTokenRequired, isGenerator, signature); + fillSignature(kind, returnToken, returnTokenRequired, yieldAndGeneratorParameterContext, signature); return signature; } @@ -1688,14 +1688,14 @@ module ts { kind: SyntaxKind, returnToken: SyntaxKind, returnTokenRequired: boolean, - isGenerator: boolean, + yieldAndGeneratorParameterContext: boolean, signature: ParsedSignature): void { if (kind === SyntaxKind.ConstructSignature) { parseExpected(SyntaxKind.NewKeyword); } signature.typeParameters = parseTypeParameters(); - signature.parameters = parseParameterList(/*yieldContext:*/ isGenerator, /*generatorParameterContext:*/ isGenerator); + signature.parameters = parseParameterList(yieldAndGeneratorParameterContext); if (returnTokenRequired) { parseExpected(returnToken); @@ -1706,9 +1706,11 @@ module ts { } } - // Because we use this for index signatures as well, we sometimes use - // parentheses, and sometimes use brackets. - function parseParameterList(yieldContext: boolean, generatorParameterContext: boolean) { + // Note: after careful analysis of the grammar, it does not appear to be possible to + // have 'Yield' And 'GeneratorParameter' not in sync. i.e. any production calling + // this FormalParameters production either always sets both to true, or always sets + // both to false. As such we only have a single parameter to represent both. + function parseParameterList(yieldAndGeneratorParameterContext: boolean) { // FormalParameters[Yield,GeneratorParameter] : // ... // @@ -1727,8 +1729,8 @@ module ts { var savedYieldContext = inYieldContext(); var savedGeneratorParameterContext = inGeneratorParameterContext(); - setYieldContext(yieldContext); - setGeneratorParameterContext(generatorParameterContext); + setYieldContext(yieldAndGeneratorParameterContext); + setGeneratorParameterContext(yieldAndGeneratorParameterContext); var result = parseDelimitedList(ParsingContext.Parameters, parseParameter); parseExpected(SyntaxKind.CloseParenToken); @@ -1744,7 +1746,7 @@ module ts { function parseSignatureMember(kind: SyntaxKind, returnToken: SyntaxKind): SignatureDeclaration { var node = createNode(kind); - fillSignature(kind, returnToken, /* returnTokenRequired */ false, /*isGenerator:*/ false, node); + fillSignature(kind, returnToken, /* returnTokenRequired */ false, /*yieldAndGeneratorParameterContext:*/ false, node); parseSemicolon(); return finishNode(node); } @@ -1773,7 +1775,7 @@ module ts { // Method signatues don't exist in expression contexts. So they have neither // [Yield] nor [GeneratorParameter] - fillSignature(SyntaxKind.CallSignature, SyntaxKind.ColonToken, /* returnTokenRequired */ false, /*isGenerator:*/ false, method); + fillSignature(SyntaxKind.CallSignature, SyntaxKind.ColonToken, /* returnTokenRequired */ false, /*yieldAndGeneratorParameterContext:*/ false, method); parseSemicolon(); return finishNode(method); @@ -1857,7 +1859,7 @@ module ts { function parseFunctionType(typeKind: SyntaxKind): SignatureDeclaration { var node = createNode(typeKind); fillSignature(typeKind === SyntaxKind.FunctionType ? SyntaxKind.CallSignature : SyntaxKind.ConstructSignature, - SyntaxKind.EqualsGreaterThanToken, /* returnTokenRequired */ true, /*isGenerator:*/ false, node); + SyntaxKind.EqualsGreaterThanToken, /* returnTokenRequired */ true, /*yieldAndGeneratorParameterContext:*/ false, node); return finishNode(node); } @@ -2230,7 +2232,7 @@ module ts { if (triState === Tristate.True) { // Arrow function are never generators. - var sig = parseSignature(SyntaxKind.CallSignature, SyntaxKind.ColonToken, /* returnTokenRequired */ false, /*isGenerator:*/ false); + var sig = parseSignature(SyntaxKind.CallSignature, SyntaxKind.ColonToken, /* returnTokenRequired */ false, /*yieldAndGeneratorParameterContext:*/ false); // If we have an arrow, then try to parse the body. // Even if not, try to parse if we have an opening brace, just in case we're in an error state. @@ -2334,7 +2336,7 @@ module ts { function tryParseSignatureIfArrowOrBraceFollows(): ParsedSignature { return tryParse(() => { // Arrow functions are never generators. - var sig = parseSignature(SyntaxKind.CallSignature, SyntaxKind.ColonToken, /* returnTokenRequired */ false, /*isGenerator:*/ false); + var sig = parseSignature(SyntaxKind.CallSignature, SyntaxKind.ColonToken, /* returnTokenRequired */ false, /*yieldAndGeneratorParameterContext:*/ false); // Parsing a signature isn't enough. // Parenthesized arrow signatures often look like other valid expressions. @@ -2726,7 +2728,7 @@ module ts { if (isGenerator) { node.flags |= NodeFlags.Generator; } - var sig = parseSignature(SyntaxKind.CallSignature, SyntaxKind.ColonToken, /* returnTokenRequired */ false, /*isGenerator:*/ isGenerator); + var sig = parseSignature(SyntaxKind.CallSignature, SyntaxKind.ColonToken, /* returnTokenRequired */ false, /*yieldAndGeneratorParameterContext:*/ isGenerator); var body = parseFunctionBlock(isGenerator, /* ignoreMissingOpenBrace */ false); // do not propagate property name as name for function expression @@ -2794,7 +2796,7 @@ module ts { parseExpected(SyntaxKind.FunctionKeyword); var isGenerator = parseOptional(SyntaxKind.AsteriskToken); var name = isGenerator ? doInYieldContext(parseOptionalIdentifier) : parseOptionalIdentifier(); - var sig = parseSignature(SyntaxKind.CallSignature, SyntaxKind.ColonToken, /* returnTokenRequired */ false, /*isGenerator:*/ isGenerator); + var sig = parseSignature(SyntaxKind.CallSignature, SyntaxKind.ColonToken, /* returnTokenRequired */ false, /*yieldAndGeneratorParameterContext:*/ isGenerator); var body = parseFunctionBlock(/*allowYield:*/ isGenerator, /* ignoreMissingOpenBrace */ false); return makeFunctionExpression(SyntaxKind.FunctionExpression, pos, name, sig, body, isGenerator ? NodeFlags.Generator : undefined); @@ -3282,7 +3284,7 @@ module ts { } node.name = parseIdentifier(); - fillSignature(SyntaxKind.CallSignature, SyntaxKind.ColonToken, /* returnTokenRequired */ false, /*isGenerator:*/ isGenerator, node); + fillSignature(SyntaxKind.CallSignature, SyntaxKind.ColonToken, /* returnTokenRequired */ false, /*yieldAndGeneratorParameterContext:*/ isGenerator, node); node.body = parseFunctionBlockOrSemicolon(isGenerator); return finishNode(node); } @@ -3291,7 +3293,7 @@ module ts { var node = createNode(SyntaxKind.Constructor, pos); setModifiers(node, modifiers); parseExpected(SyntaxKind.ConstructorKeyword); - fillSignature(SyntaxKind.CallSignature, SyntaxKind.ColonToken, /* returnTokenRequired */ false, /*isGenerator:*/ false, node); + fillSignature(SyntaxKind.CallSignature, SyntaxKind.ColonToken, /* returnTokenRequired */ false, /*yieldAndGeneratorParameterContext:*/ false, node); node.body = parseFunctionBlockOrSemicolon(/*isGenerator:*/ false); return finishNode(node); } @@ -3317,7 +3319,7 @@ module ts { method.flags = flags; } method.name = name; - fillSignature(SyntaxKind.CallSignature, SyntaxKind.ColonToken, /* returnTokenRequired */ false, /*isGenerator:*/ isGenerator, method); + fillSignature(SyntaxKind.CallSignature, SyntaxKind.ColonToken, /* returnTokenRequired */ false, /*yieldAndGeneratorParameterContext:*/ isGenerator, method); method.body = parseFunctionBlockOrSemicolon(isGenerator); return finishNode(method); } @@ -3339,7 +3341,7 @@ module ts { var node = createNode(kind, fullStart); setModifiers(node, modifiers); node.name = parsePropertyName(); - fillSignature(SyntaxKind.CallSignature, SyntaxKind.ColonToken, /* returnTokenRequired */ false, /*isGenerator:*/ false, node); + fillSignature(SyntaxKind.CallSignature, SyntaxKind.ColonToken, /* returnTokenRequired */ false, /*yieldAndGeneratorParameterContext:*/ false, node); node.body = parseFunctionBlockOrSemicolon(/*isGenerator:*/ false); return finishNode(node); } diff --git a/src/services/syntax/parser.ts b/src/services/syntax/parser.ts index b02233544be..53f4608ff57 100644 --- a/src/services/syntax/parser.ts +++ b/src/services/syntax/parser.ts @@ -1190,14 +1190,14 @@ module TypeScript.Parser { function parseGetAccessor(modifiers: ISyntaxToken[], getKeyword: ISyntaxToken): GetAccessorSyntax { return new GetAccessorSyntax(contextFlags, modifiers, consumeToken(getKeyword), parsePropertyName(), - parseCallSignature(/*requireCompleteTypeParameterList:*/ false, /*yieldContext:*/ false, /*generatorParameter:*/ false), + parseCallSignature(/*requireCompleteTypeParameterList:*/ false, /*yieldAndGeneratorParameterContext:*/ false), parseFunctionBody(/*isGenerator:*/ false)); } function parseSetAccessor(modifiers: ISyntaxToken[], setKeyword: ISyntaxToken): SetAccessorSyntax { return new SetAccessorSyntax(contextFlags, modifiers, consumeToken(setKeyword), parsePropertyName(), - parseCallSignature(/*requireCompleteTypeParameterList:*/ false, /*yieldContext:*/ false, /*generatorParameterContext:*/ false), + parseCallSignature(/*requireCompleteTypeParameterList:*/ false, /*yieldAndGeneratorParameterContext:*/ false), parseFunctionBody(/*isGenerator:*/ false)); } @@ -1326,7 +1326,7 @@ module TypeScript.Parser { return new ConstructorDeclarationSyntax(contextFlags, parseModifiers(), eatToken(SyntaxKind.ConstructorKeyword), - parseCallSignature(/*requireCompleteTypeParameterList:*/ false, /*yieldContext:*/ false, /*generatorParameterContext:*/ false), + parseCallSignature(/*requireCompleteTypeParameterList:*/ false, /*yieldAndGeneratorParameterContext:*/ false), parseFunctionBody(/*isGenerator:*/ false)); } @@ -1339,7 +1339,7 @@ module TypeScript.Parser { modifiers, asteriskToken, propertyName, - parseCallSignature(/*requireCompleteTypeParameterList:*/ false, /*yieldContext:*/ isGenerator, /*generatorParameterContext:*/ isGenerator), + parseCallSignature(/*requireCompleteTypeParameterList:*/ false, /*yieldAndGeneratorParameterContext:*/ isGenerator), parseFunctionBody(isGenerator)); } @@ -1381,7 +1381,7 @@ module TypeScript.Parser { functionKeyword, asteriskToken, eatIdentifierToken(), - parseCallSignature(/*requireCompleteTypeParameterList:*/ false, /*yieldContext:*/ isGenerator, /*generatorParameterContext:*/ isGenerator), + parseCallSignature(/*requireCompleteTypeParameterList:*/ false, /*yieldAndGeneratorParameterContext:*/ isGenerator), parseFunctionBody(isGenerator)); } @@ -1485,7 +1485,7 @@ module TypeScript.Parser { // A call signature for a type member can both use 'yield' as a parameter name, and // does not have parameter initializers. So we can pass 'false' for both [Yield] // and [GeneratorParameter]. - return parseCallSignature(/*requireCompleteTypeParameterList:*/ false, /*yieldContext:*/ false, /*generatorParameterContext:*/ false); + return parseCallSignature(/*requireCompleteTypeParameterList:*/ false, /*yieldAndGeneratorParameterContext:*/ false); } else if (isConstructSignature()) { return parseConstructSignature(); @@ -1513,7 +1513,7 @@ module TypeScript.Parser { // Construct signatures have no [Yield] or [GeneratorParameter] restrictions. return new ConstructSignatureSyntax(contextFlags, eatToken(SyntaxKind.NewKeyword), - parseCallSignature(/*requireCompleteTypeParameterList:*/ false, /*yieldContext:*/ false, /*generatorParameterContext:*/ false)); + parseCallSignature(/*requireCompleteTypeParameterList:*/ false, /*yieldAndGeneratorParameterContext:*/ false)); } function parseIndexSignature(): IndexSignatureSyntax { @@ -1529,7 +1529,7 @@ module TypeScript.Parser { return new MethodSignatureSyntax(contextFlags, propertyName, questionToken, - parseCallSignature(/*requireCompleteTypeParameterList:*/ false, /*yieldContext:*/ false, /*generatorParameterContext:*/ false)); + parseCallSignature(/*requireCompleteTypeParameterList:*/ false, /*yieldAndGeneratorParameterContext:*/ false)); } function parsePropertySignature(propertyName: IPropertyNameSyntax, questionToken: ISyntaxToken): PropertySignatureSyntax { @@ -3022,7 +3022,7 @@ module TypeScript.Parser { functionKeyword, asteriskToken, asteriskToken ? enterYieldContextAnd(eatOptionalIdentifierToken) : eatOptionalIdentifierToken(), - parseCallSignature(/*requireCompleteTypeParameterList:*/ false, /*yield:*/ isGenerator, /*generatorParameter:*/ isGenerator), + parseCallSignature(/*requireCompleteTypeParameterList:*/ false, /*yieldAndGeneratorParameterContext:*/ isGenerator), parseFunctionBody(isGenerator)); } @@ -3144,7 +3144,7 @@ module TypeScript.Parser { // 2.If the [Yield] grammar parameter is not present for CoverParenthesizedExpressionAndArrowParameterList[Yield] // return the result of parsing the lexical token stream matched by CoverParenthesizedExpressionAndArrowParameterList // using ArrowFormalParameters as the goal symbol. - var callSignature = parseCallSignature(/*requireCompleteTypeParameterList:*/ true, /*yield:*/ inYieldContext(), /*generatorParameter:*/ inYieldContext()); + var callSignature = parseCallSignature(/*requireCompleteTypeParameterList:*/ true, /*yieldAndGeneratorParameterContext:*/ inYieldContext()); if (requireArrow && currentToken().kind !== SyntaxKind.EqualsGreaterThanToken) { return undefined; @@ -3538,7 +3538,7 @@ module TypeScript.Parser { return new FunctionPropertyAssignmentSyntax(contextFlags, asteriskToken, propertyName, - parseCallSignature(/*requireCompleteTypeParameterList:*/ false, /*yield:*/ isGenerator, /*generatorParameter:*/ isGenerator), + parseCallSignature(/*requireCompleteTypeParameterList:*/ false, /*yieldAndGeneratorParameterContext:*/ isGenerator), parseFunctionBody(isGenerator)); } @@ -3596,10 +3596,10 @@ module TypeScript.Parser { return statements; } - function parseCallSignature(requireCompleteTypeParameterList: boolean, yieldContext: boolean, generatorParameterContext: boolean): CallSignatureSyntax { + function parseCallSignature(requireCompleteTypeParameterList: boolean, yieldAndGeneratorParameterContext: boolean): CallSignatureSyntax { return new CallSignatureSyntax(contextFlags, tryParseTypeParameterList(requireCompleteTypeParameterList), - parseParameterList(yieldContext, generatorParameterContext), + parseParameterList(yieldAndGeneratorParameterContext), parseOptionalTypeAnnotation(/*allowStringLiteral:*/ false)); } @@ -3649,7 +3649,11 @@ module TypeScript.Parser { return new ConstraintSyntax(contextFlags, eatToken(SyntaxKind.ExtendsKeyword), parseTypeOrExpression()); } - function parseParameterList(yieldContext: boolean, generatorParameterContext: boolean): ParameterListSyntax { + // Note: after careful analysis of the grammar, it does not appear to be possible to + // have 'Yield' And 'GeneratorParameter' not in sync. i.e. any production calling + // this FormalParameters production either always sets both to true, or always sets + // both to false. As such we only have a single parameter to represent both. + function parseParameterList(yieldAndGeneratorParameterContext: boolean): ParameterListSyntax { // FormalParameters[Yield,GeneratorParameter] : // ... // @@ -3665,11 +3669,12 @@ module TypeScript.Parser { // [+GeneratorParameter]BindingIdentifier[Yield]Initializer[In]opt // [~GeneratorParameter]BindingIdentifier[?Yield]Initializer[In, ?Yield]opt + var savedYieldContext = inYieldContext(); var savedGeneratorParameterContext = inGeneratorParameterContext(); - setYieldContext(yieldContext); - setGeneratorParameterContext(generatorParameterContext); + setYieldContext(yieldAndGeneratorParameterContext); + setGeneratorParameterContext(yieldAndGeneratorParameterContext); var openParenToken: ISyntaxToken; var result = new ParameterListSyntax(contextFlags, @@ -3936,7 +3941,7 @@ module TypeScript.Parser { // aren't in the [Yield] or [GeneratorParameter] context. return new FunctionTypeSyntax(contextFlags, tryParseTypeParameterList(/*requireCompleteTypeParameterList:*/ false), - parseParameterList(/*yield:*/ false, /*generatorParameter:*/ false), + parseParameterList(/*yieldAndGeneratorParameterContext:*/ false), eatToken(SyntaxKind.EqualsGreaterThanToken), parseType()); } @@ -3946,7 +3951,7 @@ module TypeScript.Parser { return new ConstructorTypeSyntax(contextFlags, eatToken(SyntaxKind.NewKeyword), tryParseTypeParameterList(/*requireCompleteTypeParameterList:*/ false), - parseParameterList(/*yield:*/ false, /*generatorParameter:*/ false), + parseParameterList(/*yieldAndGeneratorParameterContext:*/ false), eatToken(SyntaxKind.EqualsGreaterThanToken), parseType()); }