Skip to content

Commit

Permalink
feat(parser): support latest TC39 specs
Browse files Browse the repository at this point in the history
  • Loading branch information
KFlash committed Jun 4, 2019
1 parent 48b67c3 commit 82cb1f4
Show file tree
Hide file tree
Showing 10 changed files with 277 additions and 31 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@

## ESNext features

* [BigInt](https://github.com/tc39/proposal-bigint)
* [Decorators](https://github.com/tc39/proposal-decorators)
* [Class Public Instance Fields & Private Instance Fields](https://github.com/tc39/proposal-class-fields)
* [Hashbang Grammar](https://github.com/tc39/proposal-hashbang)
* [Import()](https://github.com/tc39/proposal-dynamic-import)
* [Private methods](https://github.com/tc39/proposal-private-methods)
* [Static class fields and private static methods](https://github.com/tc39/proposal-static-class-features/)

Expand Down
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.2.8",
"version": "0.2.9",
"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
10 changes: 9 additions & 1 deletion src/estree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ interface _Expression<T extends string> extends _Node<T> {}
interface T_Expression {
Identifier: Identifier;
Literal: Literal | RegExpLiteral;
BigIntLiteral: BigIntLiteral;
ThisExpression: ThisExpression;
ArrayExpression: ArrayExpression;
ObjectExpression: ObjectExpression;
Expand Down Expand Up @@ -72,6 +73,7 @@ interface T_Pattern {
export type Node =
| Identifier
| Literal
| BigIntLiteral
| Program
| Function
| SwitchCase
Expand Down Expand Up @@ -342,6 +344,7 @@ type Expression =
| ArrowFunctionExpression
| YieldExpression
| Literal
| BigIntLiteral
| UnaryExpression
| UpdateExpression
| BinaryExpression
Expand Down Expand Up @@ -470,9 +473,14 @@ export interface Identifier extends _Expression<'Identifier'>, _Pattern<'Identif
}

export interface Literal extends _Expression<'Literal'> {
value: string | boolean | null | number | RegExp | bigint;
raw?: string;
}

export interface BigIntLiteral extends _Expression<'BigIntLiteral'> {
value: boolean | number | string | null;
bigint: string;
raw?: string;
bigint?: string;
}

export interface RegExpLiteral extends _Expression<'Literal'> {
Expand Down
26 changes: 26 additions & 0 deletions src/grumpy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Context } from './common';
import { parseSource, Options } from './parser';
import * as ESTree from './estree';

/**
* Parse a script, optionally with various options.
*/
export function parseScript(source: string, options: Options | void): ESTree.Program {
return parseSource(source, options, Context.None);
}

/**
* Parse a module, optionally with various options.
*/
export function parseModule(source: string, options: Options | void): ESTree.Program {
return parseSource(source, options, Context.Strict | Context.Module);
}

/**
* Parse a module or a script, optionally with various options.
*/
export function parse(source: string, options: Options | void): ESTree.Program {
return parseSource(source, options, Context.None);
}

export const version = '0.0.1';
24 changes: 15 additions & 9 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3040,7 +3040,7 @@ export function parsePrimaryExpressionExtended(
return parseNewExpression(parser, context, inGroup, start);
case Token.BigIntLiteral:
parser.assignable = AssignmentKind.CannotAssign;
return parseBigIntLiteral(parser, context);
return parseBigIntLiteral(parser, context, start);
case Token.ImportKeyword:
return parseImportCallExpression(parser, context, inNewExpression, start);
default:
Expand Down Expand Up @@ -3096,15 +3096,21 @@ function parseImportCallExpression(
* @param parser Parser object
* @param context Context masks
*/
export function parseBigIntLiteral(parser: ParserState, context: Context): ESTree.Literal {
const { tokenRaw: raw, tokenValue: value } = parser;
export function parseBigIntLiteral(parser: ParserState, context: Context, start: number): ESTree.BigIntLiteral {
const { tokenRaw, tokenValue } = parser;
nextToken(parser, context);
return finishNode(parser, context, parser.index, {
type: 'Literal',
value,
bigint: raw,
raw
});
return context & Context.OptionsRaw
? finishNode(parser, context, start, {
type: 'BigIntLiteral',
value: tokenValue,
bigint: tokenRaw,
raw: tokenRaw
})
: finishNode(parser, context, start, {
type: 'BigIntLiteral',
value: tokenValue,
bigint: tokenRaw
});
}

/**
Expand Down
20 changes: 10 additions & 10 deletions test/parser/declarations/async-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,19 +187,19 @@ describe('Declarations - Async Function', () => {

for (const arg of [
'async function f() { var await = { await : async function foo() {} } }',
'async function f() { class x { foo(x=await y){} } }',
'async function f() { class x { foo(x=new (await y)()){} } }',
'async function f() { class x { foo(x=await y){} } }',
'async function f() { class x { foo(x=new (await y)()){} } }',
'async function f(async, await) { var x = await async; return x; }',
'async function f() { class x { foo(await y){} } }',
'function f() { class x { foo(x=await y){} } }',
'async function f() { class x { foo(await y){} } }',
'function f() { class x { foo(x=await y){} } }',
'async function foo() { async function bar(a = await baz()) {} }',
'async function wrap() {\n({a = await b} = obj) => a\n}',
'function f() { class x { foo(x=new (await y)()){} } }',
'function f() { class x { foo(x=new (await y)()){} } }',
'async function wrap() {\n(a = await b) => a\n}',
'async function f() { class x extends await { } }',
'function f() { class x { await y(){} } }',
'async function f() { class await { } }',
'function f() { class x { [await y](){} } }',
'async function f() { class x extends await { } }',
'function f() { class x { await y(){} } }',
'async function f() { class await { } }',
'function f() { class x { [await y](){} } }',
'async function * f() { for await ({0: a} = 1 of []); }',
'async function * f() { for await ({0: a = 1} = 1 of []); }',
'async function f() { class x extends feh(await) { } }',
Expand Down Expand Up @@ -436,7 +436,7 @@ describe('Declarations - Async Function', () => {
['async function wrap() {\n(a = await b) => a\n}', Context.None],
['async function wrap() {\n({a = await b} = obj) => a\n}', Context.None],
['function* wrap() {\nasync(a = yield b) => a\n}', Context.None],
//['async function f(){ (x = new x(await x)) => {} }', Context.None],
['async function f(){ (x = new x(await x)) => {} }', Context.None],
['async function arguments() { "use strict"; }', Context.None],
['async function fn(eval) { "use strict"; }', Context.None],
['async function method() { var await = 1; }', Context.None],
Expand Down
2 changes: 1 addition & 1 deletion test/parser/declarations/async-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ describe('Declarations - Async Generator', () => {
'yield / yield',
'+ yield',
'+ yield 3',
// 'yield\n*3',
'yield\n*3',
'var [yield] = [42];',
'var [await] = [42];',
'var {foo: yield} = {a: 42};',
Expand Down
52 changes: 46 additions & 6 deletions test/parser/declarations/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,41 @@ describe('Declarations - Function', () => {
['function foo(001, 003) { "use strict"; }', Context.None],
['function f([x=x()=x]){}', Context.None],
['function foo(001, 003) { "use strict"; }', Context.None],
['function foo(001, 003) { "use strict"; }', Context.None],
['function foo(001, 003) { "use strict"; }', Context.None],
['function foo(001, 003) { "use strict"; }', Context.None],
['function foo(001, 003) { "use strict"; }', Context.None],
['function foo(001, 003) { "use strict"; }', Context.None],
['function foo(001, 003) { "use strict"; }', Context.None]
['function f() { class await { } }', Context.Strict | Context.Module],
['function f() { class x extends await { } }', Context.Strict | Context.Module],
['function f() { class x extends await y { } }', Context.None],
['function f() { class x extends foo(await y) { } }', Context.None],
['function f() { class x { foo(await y){} } }', Context.None],
['function f() { class x { foo(x=await y){} } }', Context.None],
['function *f(){ class x { foo(x=new (yield)()){} } }', Context.None],
['function *f(){ class x { foo(x=yield y){} } }', Context.None],
['function *f(){ class x { foo(x=yield){} } }', Context.None],
['function *f(){ class x { foo(yield){} } }', Context.None],
['function *f(){ class x extends yield y { } }', Context.None],
['function *f(){ class x extends yield { } }', Context.None],
['function *f(){ class yield { } }', Context.None],
['function f(){ class x { [yield y](){} } }', Context.None],
['function f(){ class x { [yield](){} } }', Context.None],
['function f(){ class x { foo(x=new (yield)()){} } }', Context.None],
['function f(){ class x { foo(x=yield y){} } }', Context.None],
['function f(){ class x { foo(x=yield){} } }', Context.None],
['function f(){ class x { foo(yield){} } }', Context.None],
['function f(){ class x extends foo(yield y) { } }', Context.None],
['function f(){ class x extends foo(yield) { } }', Context.None],
['function f(){ class x extends yield y { } }', Context.None],
['function f(){ class x extends yield { } }', Context.None],
['function f(){ class yield { } }', Context.None],
['function f(){ class yield { } }', Context.None],
['function f(){ class x extends yield { } }', Context.None],
['function f(){ class x extends yield y { } }', Context.None],
['function f(){ class x extends foo(yield) { } }', Context.None],
['function f(){ class x extends foo(yield y) { } }', Context.None],
['function f(){ class x { foo(yield){} } }', Context.None],
['function f(){ class x { foo(x=yield){} } }', Context.None],
['function f(){ class x { foo(x=yield y){} } }', Context.None],
['function f(){ class x { foo(x=new (yield)()){} } }', Context.None],
['function f(){ class x { [yield](){} } }', Context.None],
['function f(){ class x { [yield y](){} } }', Context.None]
]);

for (const arg of [
Expand All @@ -187,6 +216,8 @@ describe('Declarations - Function', () => {
"function f(arg) {g(arg); g(function() {eval('arg = 42')}); g(arg)}",
"function f(arg) {g(arg); g(() => eval('arg = 42')); g(arg)}",
'function f(arg) {g(arg); arguments[0] = 42; g(arg)}',
'function *f(){ class x extends foo(yield y) { } }',
'function *f(){ class x extends foo(yield) { } }',
'function f(a, a) {}',
'function f([foo], b){}',
'function f([foo] = x, b){}',
Expand Down Expand Up @@ -243,6 +274,7 @@ describe('Declarations - Function', () => {
'function f([foo,bar]){}',
'function f([foo,bar] = x){}',
'function f([foo,,bar]){}',
'function f() { class x { foo(x=new (await)()){} } }',
"function f(arg) {g(arg); g(function() {eval('arg = 42')}); g(arg)}",
'function f(arg) {g(arg); g(() => arg = 42); g(arg)}',
"function f(arg) {g(arg); g(() => eval('arg = 42')); g(arg)}",
Expand Down Expand Up @@ -485,6 +517,14 @@ describe('Declarations - Function', () => {
'function f([foo=a,bar=b]){}',
'function f([foo=a,bar=b] = x){}',
'(function({x, ...y}) { })',
'function f() { class x { foo(x=await){} } }',
'function f() { class x { foo(await){} } }',
'function f() { class x extends foo(await) { } }',
'function f() { class x extends await { } }',
'function f() { class await { } }',
'function *f(){ class x { [yield y](){} } }',
'function *f(){ class x { [yield](){} } }',
'function *f(){ class x { yield(){} } }',
'async function* a() { for (let m in ((yield))) x; (r = a) => {} }'
]) {
it(`${arg}`, () => {
Expand Down
Loading

0 comments on commit 82cb1f4

Please sign in to comment.