mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-06-10 18:04:18 -05:00
Improve comments for both parsers and bring more in sync.
This commit is contained in:
@@ -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 = <ParsedSignature>{};
|
||||
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 = <SignatureDeclaration>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 = <SignatureDeclaration>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 = <ConstructorDeclaration>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 = <MethodDeclaration>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);
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
return new FunctionDeclarationSyntax(contextFlags,
|
||||
modifiers,
|
||||
functionKeyword,
|
||||
asteriskToken,
|
||||
asteriskToken,
|
||||
eatIdentifierToken(),
|
||||
parseCallSignature(/*requireCompleteTypeParameterList:*/ false, /*yieldAndGeneratorParameterContext:*/ isGenerator),
|
||||
parseFunctionBody(isGenerator));
|
||||
@@ -1485,7 +1485,7 @@ module TypeScript.Parser {
|
||||
|
||||
if (isCallSignature(/*tokenIndex:*/ 0)) {
|
||||
// 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]
|
||||
// does not have parameter initializers. So we can pass 'false' for both [Yield]
|
||||
// and [GeneratorParameter].
|
||||
return parseCallSignature(/*requireCompleteTypeParameterList:*/ false, /*yieldAndGeneratorParameterContext:*/ false);
|
||||
}
|
||||
@@ -1513,7 +1513,7 @@ module TypeScript.Parser {
|
||||
|
||||
function parseConstructSignature(): ConstructSignatureSyntax {
|
||||
// Construct signatures have no [Yield] or [GeneratorParameter] restrictions.
|
||||
return new ConstructSignatureSyntax(contextFlags,
|
||||
return new ConstructSignatureSyntax(contextFlags,
|
||||
eatToken(SyntaxKind.NewKeyword),
|
||||
parseCallSignature(/*requireCompleteTypeParameterList:*/ false, /*yieldAndGeneratorParameterContext:*/ false));
|
||||
}
|
||||
@@ -1529,7 +1529,7 @@ module TypeScript.Parser {
|
||||
// Method signatues don't exist in expression contexts. So they have neither
|
||||
// [Yield] nor [GeneratorParameter]
|
||||
return new MethodSignatureSyntax(contextFlags,
|
||||
propertyName,
|
||||
propertyName,
|
||||
questionToken,
|
||||
parseCallSignature(/*requireCompleteTypeParameterList:*/ false, /*yieldAndGeneratorParameterContext:*/ false));
|
||||
}
|
||||
@@ -3022,7 +3022,7 @@ module TypeScript.Parser {
|
||||
var isGenerator = asteriskToken !== undefined;
|
||||
return new FunctionExpressionSyntax(contextFlags,
|
||||
functionKeyword,
|
||||
asteriskToken,
|
||||
asteriskToken,
|
||||
asteriskToken ? enterYieldContextAnd(eatOptionalIdentifierToken) : eatOptionalIdentifierToken(),
|
||||
parseCallSignature(/*requireCompleteTypeParameterList:*/ false, /*yieldAndGeneratorParameterContext:*/ isGenerator),
|
||||
parseFunctionBody(isGenerator));
|
||||
@@ -3144,7 +3144,7 @@ module TypeScript.Parser {
|
||||
// return the result of parsing the lexical token stream matched by CoverParenthesizedExpressionAndArrowParameterList[Yield]
|
||||
// using ArrowFormalParameters[Yield, GeneratorParameter] as the goal symbol.
|
||||
// 2.If the [Yield] grammar parameter is not present for CoverParenthesizedExpressionAndArrowParameterList[Yield]
|
||||
// return the result of parsing the lexical token stream matched by CoverParenthesizedExpressionAndArrowParameterList
|
||||
// return the result of parsing the lexical token stream matched by CoverParenthesizedExpressionAndArrowParameterList
|
||||
// using ArrowFormalParameters as the goal symbol.
|
||||
var callSignature = parseCallSignature(/*requireCompleteTypeParameterList:*/ true, /*yieldAndGeneratorParameterContext:*/ inYieldContext());
|
||||
|
||||
@@ -3538,7 +3538,7 @@ module TypeScript.Parser {
|
||||
function parseFunctionPropertyAssignment(asteriskToken: ISyntaxToken, propertyName: IPropertyNameSyntax): FunctionPropertyAssignmentSyntax {
|
||||
var isGenerator = asteriskToken !== undefined;
|
||||
return new FunctionPropertyAssignmentSyntax(contextFlags,
|
||||
asteriskToken,
|
||||
asteriskToken,
|
||||
propertyName,
|
||||
parseCallSignature(/*requireCompleteTypeParameterList:*/ false, /*yieldAndGeneratorParameterContext:*/ isGenerator),
|
||||
parseFunctionBody(isGenerator));
|
||||
@@ -3596,10 +3596,10 @@ module TypeScript.Parser {
|
||||
setStrictModeContext(savedIsInStrictMode);
|
||||
|
||||
return statements;
|
||||
}
|
||||
}
|
||||
|
||||
function parseCallSignature(requireCompleteTypeParameterList: boolean, yieldAndGeneratorParameterContext: boolean): CallSignatureSyntax {
|
||||
return new CallSignatureSyntax(contextFlags,
|
||||
return new CallSignatureSyntax(contextFlags,
|
||||
tryParseTypeParameterList(requireCompleteTypeParameterList),
|
||||
parseParameterList(yieldAndGeneratorParameterContext),
|
||||
parseOptionalTypeAnnotation(/*allowStringLiteral:*/ false));
|
||||
@@ -3649,7 +3649,11 @@ module TypeScript.Parser {
|
||||
}
|
||||
|
||||
return new ConstraintSyntax(contextFlags, eatToken(SyntaxKind.ExtendsKeyword), parseTypeOrExpression());
|
||||
}
|
||||
}
|
||||
|
||||
// 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 {
|
||||
//
|
||||
// SingleNameBinding[Yield, GeneratorParameter] : See 13.2.3
|
||||
// [+GeneratorParameter]BindingIdentifier[Yield]Initializer[In]opt
|
||||
// [~GeneratorParameter]BindingIdentifier[?Yield]Initializer[In, ?Yield]opt
|
||||
|
||||
|
||||
var savedYieldContext = inYieldContext();
|
||||
var savedGeneratorParameterContext = inGeneratorParameterContext();
|
||||
|
||||
var savedGeneratorParameterContext = inGeneratorParameterContext();
|
||||
|
||||
setYieldContext(yieldAndGeneratorParameterContext);
|
||||
setGeneratorParameterContext(yieldAndGeneratorParameterContext);
|
||||
|
||||
@@ -3936,7 +3941,7 @@ module TypeScript.Parser {
|
||||
function parseFunctionType(): FunctionTypeSyntax {
|
||||
// Function types only exist in the type space and not the expression space. So they
|
||||
// aren't in the [Yield] or [GeneratorParameter] context.
|
||||
return new FunctionTypeSyntax(contextFlags,
|
||||
return new FunctionTypeSyntax(contextFlags,
|
||||
tryParseTypeParameterList(/*requireCompleteTypeParameterList:*/ false),
|
||||
parseParameterList(/*yieldAndGeneratorParameterContext:*/ false),
|
||||
eatToken(SyntaxKind.EqualsGreaterThanToken), parseType());
|
||||
@@ -3946,7 +3951,7 @@ module TypeScript.Parser {
|
||||
// Constructor types only exist in the type space and not the expression space. So they
|
||||
// aren't in the [Yield] or [GeneratorParameter] context.
|
||||
return new ConstructorTypeSyntax(contextFlags,
|
||||
eatToken(SyntaxKind.NewKeyword),
|
||||
eatToken(SyntaxKind.NewKeyword),
|
||||
tryParseTypeParameterList(/*requireCompleteTypeParameterList:*/ false),
|
||||
parseParameterList(/*yieldAndGeneratorParameterContext:*/ false),
|
||||
eatToken(SyntaxKind.EqualsGreaterThanToken), parseType());
|
||||
|
||||
Reference in New Issue
Block a user