Skip to content

Commit

Permalink
feat(parser): added label tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
KFlash committed May 25, 2019
1 parent 0858e3b commit 930f825
Show file tree
Hide file tree
Showing 8 changed files with 946 additions and 112 deletions.
34 changes: 26 additions & 8 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export const enum AssignmentKind {
export const enum DestructuringKind {
None = 0,
MustDestruct = 1 << 3,
CannotDestruct = 1 << 4,
CannotDestruct = 1 << 4,
// Only destructible if assignable
AssignableDestruct = 1 << 5,
// `__proto__` is a special case and only valid to parse if destructible
Expand All @@ -106,12 +106,6 @@ export const enum Flags {
Yield = 1 << 9,
}

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

export const enum FunctionStatement {
Disallow,
Allow,
Expand All @@ -122,7 +116,6 @@ export const enum FunctionStatement {
*/
export interface ParserState {
source: string;

flags: Flags;
index: number;
line: number;
Expand Down Expand Up @@ -307,3 +300,28 @@ export function isStrictReservedWord(parser: ParserState, context: Context, t: T
export function isPropertyWithPrivateFieldKey(expr: any): boolean {
return !expr.property ? false : expr.property.type === 'PrivateName';
}

export function isValidLabel(parser: ParserState, labels: any, label: string, requireIterationStatement: 0 | 1): boolean {

let isInvalid = requireIterationStatement;

do {
if (labels['€' + label]) {
if (isInvalid) report(parser, Errors.InvalidNestedStatement);
return true;
}
if (isInvalid && labels.loop) isInvalid = 0;
labels = labels['€'];
} while (labels);

return false;
}
export function validateAndTrackLabel(parser: ParserState, labels: any, name: string) {
let set = labels;
do {
if (set['€' + name]) report(parser, Errors.LabelRedeclaration, name);
set = set['€'];
} while (set);

labels['€' + name] = true;
}
18 changes: 16 additions & 2 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,14 @@ export const enum Errors {
InvalidStaticClassFieldConstructor,
InvalidClassFieldConstructor,
InvalidClassFieldArgEval,
InvalidGeneratorFunction
InvalidGeneratorFunction,
AsyncRestrictedProd,
UnexpectedCharAfterObjLit,
InvalidObjLitKey,
InvalidKeyToken,
LabelRedeclaration,
InvalidNestedStatement,
UnknownLabel
}

/*@internal*/
Expand Down Expand Up @@ -202,6 +209,7 @@ export const errorMessages: {
[Errors.InvalidLHSInit]: 'Cannot assign to lhs, not destructible with this initializer',
[Errors.InvalidGeneratorGetter]: 'A getter cannot be a generator',
[Errors.InvalidComputedPropName]: 'A computed property name must be followed by a colon or paren',
[Errors.InvalidObjLitKey]: 'Object literal keys that are strings or numbers must be a method or have a colon',
[Errors.InvalidAsyncGetter]: 'Found `* async x(){}` but this should be `async * x(){}`',
[Errors.InvalidGetSetGenerator]: 'Getters and setters can not be generators',
[Errors.InvalidGenMethodShorthand]: "'%0' can not be generator method",
Expand Down Expand Up @@ -308,7 +316,13 @@ export const errorMessages: {
[Errors.InvalidClassFieldConstructor]: 'Classes may not have a field called constructor',
[Errors.InvalidStaticClassFieldConstructor]: 'Classes may not have a private element named constructor',
[Errors.InvalidClassFieldArgEval]: 'A class field initializer may not contain arguments',
[Errors.InvalidGeneratorFunction]: 'Generators can only be declared at the top level or inside a block'
[Errors.InvalidGeneratorFunction]: 'Generators can only be declared at the top level or inside a block',
[Errors.AsyncRestrictedProd]: 'Async methods are a restricted production and cannot have a newline following it',
[Errors.UnexpectedCharAfterObjLit]: 'Unexpected character after object literal property name',
[Errors.InvalidKeyToken]: 'Invalid key token',
[Errors.LabelRedeclaration]: "Label '%0' has already been declared",
[Errors.InvalidNestedStatement]: 'continue statement must be nested within an iteration statement',
[Errors.UnknownLabel]: "Undefined label '%0'"
};

export class ParseError extends SyntaxError {
Expand Down
Loading

0 comments on commit 930f825

Please sign in to comment.