Skip to content

Commit

Permalink
fix(parser): moved enums to common.ts for improved readability
Browse files Browse the repository at this point in the history
  • Loading branch information
KFlash committed May 7, 2019
1 parent 7d62c7a commit 09683b4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 33 deletions.
15 changes: 13 additions & 2 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const enum Context {
OptionsWebCompat = 1 << 8,
OptionsRaw = 1 << 9,
Strict = 1 << 10,
Module = 1 << 11,
Module = 1 << 11, // Current code should be parsed as a module body
InSwitch = 1 << 12,
InGlobal = 1 << 13,
TopLevel = 1 << 14,
Expand Down Expand Up @@ -71,7 +71,7 @@ export const enum BindingOrigin {
Arrow = 1 << 1,
ForStatement = 1 << 2,
Statement = 1 << 3,
Export = 1 << 4
Export = 1 << 4
}

export const enum AssignmentKind {
Expand Down Expand Up @@ -105,6 +105,17 @@ export const enum Flags {
SimpleParameterList = 1 << 7
}

export const enum ParseFunctionFlag {
None = 0,
DisallowGenerator = 1 << 0,
RequireIdentifier = 1 << 1,
}

export const enum LabelledFunctionStatement {
Disallow,
Allow,
}

/**
* The parser interface.
*/
Expand Down
9 changes: 7 additions & 2 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ export const enum Errors {
InvalidStatementStart,
StrictDelete,
InvalidPatternTail,
ForLoopInvalidLHS
ForLoopInvalidLHS,
AsyncFunctionInSingleStatementContext,
InvalidTernaryYield
}

/*@internal*/
Expand Down Expand Up @@ -180,6 +182,8 @@ export const errorMessages: {
[Errors.YieldInParameter]: 'Yield expression not allowed in formal parameter',
[Errors.InvalidExponentationLHS]:
'Unary expressions as the left operand of an exponentation expression must be disambiguated with parentheses',
[Errors.AsyncFunctionInSingleStatementContext]:
'Async functions can only be declared at the top level or inside a block',
[Errors.UnterminatedRegExp]: 'Unterminated regular expression',
[Errors.UnexpectedTokenRegExpFlag]: 'Unexpected regular expression flag',
[Errors.DuplicateRegExpFlag]: "Duplicate regular expression flag '%0'",
Expand Down Expand Up @@ -289,7 +293,8 @@ export const errorMessages: {
[Errors.InvalidDecoratorSemicolon]: 'Decorators must not be followed by a semicolon',
[Errors.InvalidStatementStart]: 'A statement can not start with object destructuring assignment',
[Errors.StrictDelete]: 'Calling delete on expression not allowed in strict mode',
[Errors.InvalidPatternTail]: 'Pattern can not have a tail'
[Errors.InvalidPatternTail]: 'Pattern can not have a tail',
[Errors.InvalidTernaryYield]: 'Can not have a `yield` expression on the left side of a ternary'
};

export class ParseError extends SyntaxError {
Expand Down
48 changes: 19 additions & 29 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,15 @@ import {
reinterpretToPattern,
DestructuringKind,
AssignmentKind,
LabelledFunctionStatement,
ParseFunctionFlag,
BindingType,
validateIdentifier,
isStrictReservedWord,
optionalBit,
consumeSemicolon
} from './common';

export const enum LabelledFunctionStatement {
Disallow,
Allow
}

export const enum ParseFunctionFlag {
IsNormal = 0,
DisAllowGenerator = 1 << 0,
RequireIdentifier = 1 << 1
}

export const enum DecoratorFlag {
IsNormal = 0,
InExportDecl = 1 << 0,
InClassBody = 1 << 1
}

/**
* Create a new parser instance
*/
Expand Down Expand Up @@ -301,7 +286,7 @@ export function parseStatementListItem(parser: ParserState, context: Context): a
switch (parser.token) {
// HoistableDeclaration[?Yield, ~Default]
case Token.FunctionKeyword:
return parseFunctionDeclaration(parser, context, ParseFunctionFlag.IsNormal, /* isAsync */ 0);
return parseFunctionDeclaration(parser, context, ParseFunctionFlag.None, /* isAsync */ 0);
// @decorator
case Token.Decorator:
if (context & Context.Module) return parseDecorators(parser, context | Context.InDecoratorContext);
Expand Down Expand Up @@ -456,7 +441,7 @@ export function parseExpressionOrLabelledStatement(

const { token } = parser;

let expr: any;
let expr: ESTree.Expression;

switch (token) {
case Token.LetKeyword:
Expand Down Expand Up @@ -514,7 +499,13 @@ export function parseExpressionOrLabelledStatement(
* 2. LeftHandSideExpression = AssignmentExpression
*
*/
expr = parseAssignmentExpression(parser, context, expr);
expr = parseAssignmentExpression(parser, context, expr as
| ESTree.AssignmentExpression
| ESTree.Identifier
| ESTree.Literal
| ESTree.BinaryExpression
| ESTree.LogicalExpression
| ESTree.ConditionalExpression);

/** Sequence expression
*
Expand Down Expand Up @@ -646,7 +637,7 @@ export function parseLabelledStatement(
(context & Context.Strict) === 0 &&
context & Context.OptionsWebCompat &&
parser.token === Token.FunctionKeyword
? parseFunctionDeclaration(parser, context, ParseFunctionFlag.DisAllowGenerator, /* isAsync */ 0)
? parseFunctionDeclaration(parser, context, ParseFunctionFlag.DisallowGenerator, /* isAsync */ 0)
: parseStatement(parser, (context | Context.TopLevel) ^ Context.TopLevel, allowFuncDecl)
};
}
Expand Down Expand Up @@ -675,8 +666,8 @@ export function parseAsyncStatement(
if (!asyncNewLine) {
// async function ...
if (parser.token === Token.FunctionKeyword) {
if (!allowFuncDecl) report(parser, Errors.Unexpected);
return parseFunctionDeclaration(parser, context, ParseFunctionFlag.IsNormal, /* isAsync */ 1);
if (!allowFuncDecl) report(parser, Errors.AsyncFunctionInSingleStatementContext);
return parseFunctionDeclaration(parser, context, ParseFunctionFlag.None, /* isAsync */ 1);
}

// async Identifier => ...
Expand Down Expand Up @@ -851,7 +842,7 @@ export function parseConsequentOrAlternate(
(context & Context.OptionsWebCompat) === 0 ||
parser.token !== Token.FunctionKeyword
? parseStatement(parser, (context | Context.TopLevel) ^ Context.TopLevel, LabelledFunctionStatement.Disallow)
: parseFunctionDeclaration(parser, context, ParseFunctionFlag.DisAllowGenerator, /* isAsync */ 0);
: parseFunctionDeclaration(parser, context, ParseFunctionFlag.DisallowGenerator, /* isAsync */ 0);
}

/**
Expand Down Expand Up @@ -1845,12 +1836,12 @@ function parseExportDeclaration(
declaration = parseVariableStatement(parser, context, BindingType.Variable, BindingOrigin.Export);
break;
case Token.FunctionKeyword:
declaration = parseFunctionDeclaration(parser, context, ParseFunctionFlag.IsNormal, /* isAsync */ 0);
declaration = parseFunctionDeclaration(parser, context, ParseFunctionFlag.None, /* isAsync */ 0);
break;
case Token.AsyncKeyword:
nextToken(parser, context);
if ((parser.flags & Flags.NewLine) === 0 && parser.token === Token.FunctionKeyword) {
declaration = parseFunctionDeclaration(parser, context, ParseFunctionFlag.IsNormal, /* isAsync */ 1);
declaration = parseFunctionDeclaration(parser, context, ParseFunctionFlag.None, /* isAsync */ 1);
break;
}
// falls through
Expand Down Expand Up @@ -2143,8 +2134,7 @@ export function parseYieldExpressionOrIdentifier(parser: ParserState, context: C
// 'yield' ([no line terminator] '*'? AssignmentExpression)?
nextToken(parser, context | Context.AllowRegExp);
if (context & Context.InArgList) report(parser, Errors.YieldInParameter);
if (parser.token === Token.QuestionMark)
report(parser, Errors.UnexpectedToken, 'Can not have a `yield` expression on the left side of a ternary');
if (parser.token === Token.QuestionMark) report(parser, Errors.InvalidTernaryYield);
parser.flags |= Flags.SeenYield;
let argument: ESTree.Expression | null = null;
let delegate = false; // yield*
Expand Down Expand Up @@ -2867,7 +2857,7 @@ export function parseFunctionDeclaration(
nextToken(parser, context | Context.AllowRegExp);
let isGenerator: 0 | 1 = 0;
if (parser.token === Token.Multiply) {
if (flags & ParseFunctionFlag.DisAllowGenerator) report(parser, Errors.Unexpected);
if (flags & ParseFunctionFlag.DisallowGenerator) report(parser, Errors.Unexpected);
nextToken(parser, context);
isGenerator = 1;
}
Expand Down

0 comments on commit 09683b4

Please sign in to comment.