Skip to content

Commit

Permalink
fix(parser): dedupe some code
Browse files Browse the repository at this point in the history
  • Loading branch information
KFlash committed Jun 27, 2019
1 parent 76c53f3 commit 42f1afa
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 44 deletions.
4 changes: 0 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,3 @@ yarn-error.log*
.rollupcache
.fuzz-output
.DS_Store
cherow-rewritten.js

# fuzz output
/packages/cherow/.fuzz-output/
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.6.1",
"version": "0.6.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
22 changes: 4 additions & 18 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,16 +346,9 @@ export class ParseError extends SyntaxError {
public line: number;
public column: number;
public description: string;
constructor(startindex: number, line: number, column: number, source: string, type: Errors, ...params: string[]) {
constructor(startindex: number, line: number, column: number, type: Errors, ...params: string[]) {
let message =
'[' + line + ':' + column + ']: ' + errorMessages[type].replace(/%(\d+)/g, (_: string, i: number) => params[i]);
const lines = source.split('\n');
message = message + '\n' + lines[line - 1] + '\n';
for (let i = 0; i < column; i++) {
message += ' ';
}
message += '^\n';

super(`${message}`);

this.index = startindex;
Expand All @@ -375,7 +368,7 @@ export class ParseError extends SyntaxError {
* @returns {never}
*/
export function report(parser: ParserState, type: Errors, ...params: string[]): never {
throw new ParseError(parser.index, parser.line, parser.column, parser.source, type, ...params);
throw new ParseError(parser.index, parser.line, parser.column, type, ...params);
}

/**
Expand All @@ -390,13 +383,6 @@ export function report(parser: ParserState, type: Errors, ...params: string[]):
* @param {...string[]} params
* @returns {never}
*/
export function reportAt(
parser: ParserState,
index: number,
line: number,
column: number,
type: Errors,
...params: string[]
): never {
throw new ParseError(index, line, column, parser.source, type, ...params);
export function reportAt(index: number, line: number, column: number, type: Errors, ...params: string[]): never {
throw new ParseError(index, line, column, type, ...params);
}
12 changes: 4 additions & 8 deletions src/lexer/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,10 @@ export function parseEscape(parser: ParserState, context: Context, first: number

// Line continuations
case Chars.CarriageReturn: {
const { index } = parser;

if (index < parser.end) {
const ch = parser.source.charCodeAt(index);

if (ch === Chars.LineFeed) {
parser.nextCP = ch;
parser.index = index + 1;
if (parser.index < parser.end) {
if (parser.nextCP === Chars.LineFeed) {
parser.index = parser.index + 1;
parser.nextCP = parser.source.charCodeAt(parser.index);
}
}
}
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.6.1';
export const version = '0.6.2';
13 changes: 5 additions & 8 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1964,7 +1964,6 @@ function parseVariableDeclaration(
context & Context.Strict))
) {
reportAt(
parser,
tokenIndex,
parser.line,
parser.index - 3,
Expand Down Expand Up @@ -2351,7 +2350,6 @@ function parseImportNamespaceSpecifier(
validateBindingIdentifier(parser, context, BindingType.Const, parser.token, 0);
} else {
reportAt(
parser,
tokenIndex,
parser.line,
parser.index,
Expand Down Expand Up @@ -3272,7 +3270,7 @@ export function parseAwaitExpressionOrIdentifier(
if (inNewExpression) {
report(parser, Errors.InvalidAwaitIdent);
} else if (context & Context.InArgList) {
reportAt(parser, parser.index, parser.line, parser.index, Errors.AwaitInParameter);
reportAt(parser.index, parser.line, parser.index, Errors.AwaitInParameter);
}

nextToken(parser, context | Context.AllowRegExp);
Expand Down Expand Up @@ -3334,10 +3332,10 @@ export function parseFunctionBody(
// in the body of a function with non-simple parameter list, on
// 29/7/2015. https://goo.gl/ueA7Ln
if (parser.flags & Flags.SimpleParameterList) {
reportAt(parser, parser.index, parser.line, parser.tokenIndex, Errors.IllegalUseStrict);
reportAt(parser.index, parser.line, parser.tokenIndex, Errors.IllegalUseStrict);
}
if (parser.flags & Flags.Octals) {
reportAt(parser, parser.index, parser.line, parser.tokenIndex, Errors.StrictOctalLiteral);
reportAt(parser.index, parser.line, parser.tokenIndex, Errors.StrictOctalLiteral);
}
}
}
Expand Down Expand Up @@ -4575,7 +4573,7 @@ export function parseArrayExpressionOrPattern(

if (consumeOpt(parser, context | Context.AllowRegExp, Token.Assign)) {
if (parser.assignable & AssignmentKind.NotAssignable) {
reportAt(parser, parser.index, parser.line, parser.index - 3, Errors.InvalidLHS);
reportAt(parser.index, parser.line, parser.index - 3, Errors.InvalidLHS);
} else if (context & Context.OptionsLexical) {
declareName(parser, context, scope, tokenValue, type, 0, 0);
if (origin & BindingOrigin.Export) {
Expand Down Expand Up @@ -5944,7 +5942,6 @@ export function parseObjectLiteralOrPattern(
);
} else {
reportAt(
parser,
index,
line,
index,
Expand Down Expand Up @@ -7626,7 +7623,7 @@ function parseClassElementList(
kind |= PropertyKind.ClassField;
context = context | Context.InClass;
} else {
report(parser, Errors.UnexpectedToken, KeywordDescTable[parser.token & Token.Type]);
report(parser, Errors.Unexpected);
}

if (kind & (PropertyKind.Generator | PropertyKind.Async | PropertyKind.GetSet)) {
Expand Down
8 changes: 4 additions & 4 deletions src/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,17 +174,17 @@ export const enum Token {
EscapedFutureReserved = 119 | IsIdentifier,
ReservedIfStrict = 120 | IsIdentifier,

PrivateName = 121 | IsIdentifier,
PrivateName = 121 | IsIdentifier,
BigIntLiteral = 122,
WhiteSpace = 124,
WhiteSpace = 124,
Illegal = 129,
CarriageReturn = 130,
PrivateField = 131,
PrivateField = 131,
Template = 132,
Decorator = 133,
Target = 134 | IsIdentifier,
LineFeed = 135,
EscapedIdentifier = 136,
EscapedIdentifier = 136,
}

export const KeywordDescTable = [
Expand Down

0 comments on commit 42f1afa

Please sign in to comment.