Skip to content

Commit

Permalink
fix(parser): tweaked bit masks
Browse files Browse the repository at this point in the history
  • Loading branch information
KFlash committed Jun 23, 2019
1 parent 7aab914 commit 2b623cd
Show file tree
Hide file tree
Showing 9 changed files with 1,098 additions and 195 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "meriyah",
"version": "0.5.1",
"version": "0.5.2",
"description": "A 100% compliant, self-hosted javascript parser with high focus on both performance and stability",
"main": "dist/meriyah.umd.js",
"module": "dist/meriyah.esm.js",
Expand Down
1 change: 0 additions & 1 deletion src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,6 @@ export function validateAndDeclareLabel(parser: ParserState, labels: any, name:
labels['$' + name] = 1;
}


export function finishNode<T extends Node>(
parser: ParserState,
context: Context,
Expand Down
28 changes: 14 additions & 14 deletions src/estree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@ export interface Position {
*/
export interface Program extends _Node<'Program'> {
sourceType: 'script' | 'module';
body: Array<Statement | ModuleDeclaration>;
comments?: Array<Comment>;
body: (Statement | ModuleDeclaration)[];
comments?: Comment[];
}

interface BaseFunction extends _Node<'Function'> {
params: Array<Pattern>;
params: Pattern[];
generator?: boolean;
async?: boolean;
body: BlockStatement | Expression;
Expand Down Expand Up @@ -186,8 +186,8 @@ export type Expression =
export interface EmptyStatement extends _Node<'EmptyStatement'> {}

export interface BlockStatement extends _Node<'BlockStatement'> {
body: Array<Statement>;
innerComments?: Array<Comment>;
body: Statement[];
innerComments?: Comment[];
}

export interface ExpressionStatement extends _Node<'ExpressionStatement'> {
Expand Down Expand Up @@ -221,7 +221,7 @@ export interface WithStatement extends _Node<'WithStatement'> {

export interface SwitchStatement extends _Node<'SwitchStatement'> {
discriminant: Expression;
cases: Array<SwitchCase>;
cases: SwitchCase[];
}

export interface ReturnStatement extends _Node<'ReturnStatement'> {
Expand Down Expand Up @@ -367,11 +367,11 @@ type Expression =
export interface ThisExpression extends _Expression<'ThisExpression'> {}

export interface ArrayExpression extends _Expression<'ArrayExpression'> {
elements: Array<Expression | SpreadElement>;
elements: (Expression | SpreadElement)[];
}

export interface ObjectExpression extends _Expression<'ObjectExpression'> {
properties: Array<Property>;
properties: Property[];
}

export interface Property extends _Node<'Property'> {
Expand All @@ -391,7 +391,7 @@ export interface FunctionExpression extends _Expression<'FunctionExpression'> {
}

export interface SequenceExpression extends _Expression<'SequenceExpression'> {
expressions: Array<Expression>;
expressions: Expression[];
}

export interface UnaryExpression extends _Expression<'UnaryExpression'> {
Expand Down Expand Up @@ -555,8 +555,8 @@ export interface YieldExpression extends _Expression<'YieldExpression'> {
}

export interface TemplateLiteral extends _Expression<'TemplateLiteral'> {
quasis: Array<TemplateElement>;
expressions: Array<Expression>;
quasis: TemplateElement[];
expressions: Expression[];
}

export interface TaggedTemplateExpression extends _Expression<'TaggedTemplateExpression'> {
Expand All @@ -576,11 +576,11 @@ export interface AssignmentProperty extends _Node<'Property'> {
}

export interface ObjectPattern extends _Pattern<'ObjectPattern'> {
properties: Array<AssignmentProperty>;
properties: AssignmentProperty[];
}

export interface ArrayPattern extends _Pattern<'ArrayPattern'> {
elements: Array<Pattern>;
elements: Pattern[];
}

export interface RestElement extends _Pattern<'RestElement'> {
Expand All @@ -593,7 +593,7 @@ export interface AssignmentPattern extends _Pattern<'AssignmentPattern'> {
}

export interface ClassBody extends _Node<'ClassBody'> {
body: Array<MethodDefinition>;
body: MethodDefinition[];
decorators?: Decorator[] | null;
}
export interface PrivateMemberExpression extends _Node<'FieldDefinition'> {
Expand Down
2 changes: 1 addition & 1 deletion src/meriyah.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ export function parse(source: string, options: Options | void): ESTree.Program {
return parseSource(source, options, Context.None);
}

export const version = '0.4.2';
export const version = '0.5.2';
52 changes: 26 additions & 26 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export function parseSource(source: string, options: Options | void, context: Co
body = parseModuleItemList(parser, context | Context.InGlobal | Context.TopLevel, scope);

if (context & Context.OptionsLexical) {
for (let key in parser.exportedBindings) {
for (const key in parser.exportedBindings) {
if (key !== '$default' && (scope.var[key] === undefined && scope.lexicals[key] === undefined)) {
report(parser, Errors.UndeclaredExportedBinding, key.slice(1));
}
Expand Down Expand Up @@ -268,7 +268,7 @@ export function parseStatementList(parser: ParserState, context: Context, scope:
while (parser.token === Token.StringLiteral) {
// "use strict" must be the exact literal without escape sequences or line continuation.
const { index, tokenIndex, tokenValue, linePos, columnPos, token } = parser;
let expr = parseLiteral1(parser, context, tokenIndex, linePos, columnPos);
const expr = parseLiteral1(parser, context, tokenIndex, linePos, columnPos);
if (index - tokenIndex < 13 && tokenValue === 'use strict') {
if ((parser.token & Token.IsAutoSemicolon) === Token.IsAutoSemicolon || parser.flags & Flags.NewLine) {
context |= Context.Strict;
Expand Down Expand Up @@ -323,7 +323,7 @@ export function parseModuleItemList(
while (parser.token === Token.StringLiteral) {
// "use strict" must be the exact literal without escape sequences or line continuation.
const { index, tokenIndex, tokenValue, linePos, columnPos, token } = parser;
let expr = parseLiteral(parser, context, tokenIndex, linePos, columnPos);
const expr = parseLiteral(parser, context, tokenIndex, linePos, columnPos);
if (index - tokenIndex < 13 && tokenValue === 'use strict') {
if ((parser.token & Token.IsAutoSemicolon) === Token.IsAutoSemicolon) {
context |= Context.Strict;
Expand Down Expand Up @@ -743,7 +743,7 @@ export function parseBlock(
while (parser.token !== Token.RightBrace) {
body.push(parseStatementListItem(
parser,
context & ~Context.TopLevel,
(context | Context.TopLevel) ^ Context.TopLevel,
scope,
{ $: labels },
parser.tokenIndex,
Expand Down Expand Up @@ -870,7 +870,7 @@ export function parseLabelledStatement(
)
: parseStatement(
parser,
context & ~Context.TopLevel,
(context | Context.TopLevel) ^ Context.TopLevel,
scope,
labels,
allowFuncDecl,
Expand Down Expand Up @@ -2028,9 +2028,9 @@ export function parseForStatement(

if (isVarDecl) {
if (token === Token.LetKeyword) {
let varStart = parser.tokenIndex;
let linePosStart = parser.linePos;
let columnPosStart = parser.columnPos;
const varStart = parser.tokenIndex;
const linePosStart = parser.linePos;
const columnPosStart = parser.columnPos;
init = parseIdentifier(parser, context, tokenIndex, linePos, columnPos);
if (parser.token & (Token.IsIdentifier | Token.IsPatternStart)) {
if (parser.token === Token.InKeyword) {
Expand Down Expand Up @@ -2065,9 +2065,9 @@ export function parseForStatement(
}
} else {
// 'var', 'const'
let varStart = parser.tokenIndex;
let linePosStart = parser.linePos;
let columnPosStart = parser.columnPos;
const varStart = parser.tokenIndex;
const linePosStart = parser.linePos;
const columnPosStart = parser.columnPos;
nextToken(parser, context);

const kind = KeywordDescTable[token & Token.Type] as 'var' | 'const';
Expand Down Expand Up @@ -2550,9 +2550,9 @@ function parseExportDeclaration(

// export default HoistableDeclaration[Default]
case Token.AsyncKeyword:
let idxBeforeAsync = parser.tokenIndex;
let lineBeforeAsync = parser.linePos;
let columnBeforeAsync = parser.columnPos;
const idxBeforeAsync = parser.tokenIndex;
const lineBeforeAsync = parser.linePos;
const columnBeforeAsync = parser.columnPos;

declaration = parseIdentifier(parser, context, idxBeforeAsync, lineBeforeAsync, columnBeforeAsync);
const { flags } = parser;
Expand Down Expand Up @@ -2694,8 +2694,8 @@ function parseExportDeclaration(

nextToken(parser, context); // Skips: '{'

let tmpExportedNames: string[] = [];
let tmpExportedBindings: string[] = [];
const tmpExportedNames: string[] = [];
const tmpExportedBindings: string[] = [];

while (parser.token & Token.IsIdentifier) {
const { tokenIndex, tokenValue, linePos, columnPos } = parser;
Expand Down Expand Up @@ -3307,7 +3307,7 @@ export function parseFunctionBody(
while (parser.token === Token.StringLiteral) {
// "use strict" must be the exact literal without escape sequences or line continuation.
const { index, tokenIndex, tokenValue, token } = parser;
let expr = parseLiteral(parser, context, parser.tokenIndex, parser.linePos, parser.columnPos);
const expr = parseLiteral(parser, context, parser.tokenIndex, parser.linePos, parser.columnPos);
if (index - tokenIndex < 13 && tokenValue === 'use strict') {
if ((parser.token & Token.IsAutoSemicolon) === Token.IsAutoSemicolon || parser.flags & Flags.NewLine) {
context |= Context.Strict;
Expand Down Expand Up @@ -3429,7 +3429,7 @@ export function parseLeftHandSideExpression(
// LeftHandSideExpression ::
// (PrimaryExpression | MemberExpression) ...

let expression = parsePrimaryExpressionExtended(
const expression = parsePrimaryExpressionExtended(
parser,
context,
BindingType.None,
Expand Down Expand Up @@ -4832,7 +4832,7 @@ function parseSpreadElement(
if (parser.token & (Token.Keyword | Token.IsIdentifier)) {
parser.assignable = AssignmentKind.Assignable;

let tokenValue = parser.tokenValue;
const tokenValue = parser.tokenValue;

argument = parsePrimaryExpressionExtended(parser, context, type, 0, 1, inGroup, tokenIndex, linePos, columnPos);

Expand Down Expand Up @@ -6247,9 +6247,9 @@ export function parseParenthesizedExpression(
let toplevelComma: 0 | 1 = 0;
let isComplex: 0 | 1 = 0;

let idxStart = parser.tokenIndex;
let lineStart = parser.linePos;
let columnStart = parser.columnPos;
const idxStart = parser.tokenIndex;
const lineStart = parser.linePos;
const columnStart = parser.columnPos;

parser.assignable = AssignmentKind.Assignable;

Expand Down Expand Up @@ -6776,9 +6776,9 @@ export function parseNewExpression(
// - `new (await foo);`
// - `new x(await foo);`
const id = parseIdentifier(parser, context | Context.AllowRegExp, start, line, column);
let startIdx = parser.tokenIndex;
let lineIdx = parser.linePos;
let columnIdx = parser.columnPos;
const startIdx = parser.tokenIndex;
const lineIdx = parser.linePos;
const columnIdx = parser.columnPos;

if (consumeOpt(parser, context, Token.Period)) {
if (context & Context.AllowNewTarget && parser.token === Token.Target) {
Expand Down Expand Up @@ -7369,7 +7369,7 @@ export function parseClassExpression(
* @param context Context masks
*/
export function parseDecorators(parser: ParserState, context: Context): ESTree.Decorator[] {
let list: ESTree.Decorator[] = [];
const list: ESTree.Decorator[] = [];

while (parser.token === Token.Decorator) {
list.push(parseDecoratorList(parser, context, parser.tokenIndex, parser.linePos, parser.columnPos));
Expand Down
10 changes: 5 additions & 5 deletions src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export function declareName(
): void {
if (scope === null) return;

let hashed = '$' + name;
const hashed = '$' + name;

if (bindingType & BindingType.Variable) {
let lex = scope.lexicals;
Expand Down Expand Up @@ -114,10 +114,10 @@ export function declareName(
lexicalVariables = lexicalVariables['$'];
}
} else {
let lex = scope.lexicals;
const lex = scope.lexicals;

if (dupeChecks) {
let lexParent = scope.lexicals['$'];
const lexParent = scope.lexicals['$'];

if (lexParent && lexParent.type & (ScopeType.ArgList | ScopeType.Catch) && lexParent[hashed]) {
report(parser, Errors.DuplicateBinding, name);
Expand Down Expand Up @@ -211,7 +211,7 @@ export function checkConflictingLexicalDeclarations(
scope: any,
checkParent: 0 | 1
): boolean {
for (let key in scope.lexicals) {
for (const key in scope.lexicals) {
if (key[0] === '$' && key.length > 1) {
if (scope.lexicals[key] > 1) report(parser, Errors.DuplicateBinding, key);

Expand Down Expand Up @@ -244,7 +244,7 @@ export function checkConflictingLexicalDeclarations(
*/

export function verifyArguments(parser: ParserState, lex: any): void {
for (let key in lex) {
for (const key in lex) {
if (key[0] === '$' && key.length > 1 && lex[key] > 1) {
report(parser, Errors.DuplicateBinding, key.slice(1));
}
Expand Down

0 comments on commit 2b623cd

Please sign in to comment.