Skip to content

Commit

Permalink
try to fix semicolon can't become one of the two semicolons in the he…
Browse files Browse the repository at this point in the history
…ader of a for statement
  • Loading branch information
w-y committed Apr 27, 2017
1 parent 64b7955 commit afb3597
Show file tree
Hide file tree
Showing 10 changed files with 258 additions and 109 deletions.
10 changes: 10 additions & 0 deletions src/ast/FunctionDeclaration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const BaseNode = require('./Base');

function FunctionDeclarationNode(id, params, body, ...args) {
BaseNode.call(this, Object.assign({}, { type: 'FunctionDeclaration' }, ...args));
this.id = id;
this.params = params;
this.body = body;
}

exports.FunctionDeclarationNode = FunctionDeclarationNode;
23 changes: 23 additions & 0 deletions src/ast/IterationStatement.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
const BaseNode = require('./Base');

function checkForAutoSemicolonInsertion(
autoInsertionOffset, leftParenthesisRange, rightParenthesisRange, loc) {
if (leftParenthesisRange && rightParenthesisRange && autoInsertionOffset) {
// semicolon can't become one of the two semicolons
// in the header of a for statement

if (autoInsertionOffset > leftParenthesisRange[0] &&
autoInsertionOffset < rightParenthesisRange[1]) {
throw new (require('../error').ParseError)(
'semicolon can\'t become one of the two semicolons in the header of a for statement',
{
text: ';',
token: ';',
line: loc.first_line,
loc,
failedAutoSemicolon: true,
},
);
}
}
}

function IterationStatementNode(type, test, body, ...args) {
BaseNode.call(this, Object.assign({}, { type }, ...args));
this.test = test;
Expand Down Expand Up @@ -44,3 +66,4 @@ exports.ForInStatementNode = ForInStatementNode;

exports.ForOfStatementNode = ForOfStatementNode;

exports.checkForAutoSemicolonInsertion = checkForAutoSemicolonInsertion;
8 changes: 8 additions & 0 deletions src/ast/ReturnStatementNode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const BaseNode = require('./Base');

function ReturnStatementNode(params, ...args) {
BaseNode.call(this, Object.assign({}, { type: 'ReturnStatement' }, ...args));
this.params = params;
}

module.exports = ReturnStatementNode;
1 change: 1 addition & 0 deletions src/autoinsertion.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ let file = null;
let readbytes = 0;

function canApplyRule(source, ex) {
console.log(ex);
const token = ex.hash.token;
const range = ex.hash.loc.range;
let tokenOffset = range[1];
Expand Down
8 changes: 4 additions & 4 deletions src/bnf/FunctionDeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ module.exports = {
'FUNCTION BindingIdentifier ( FormalParameters ) { }',
],
handlers: [
'$$ = $1 + $2 + $3 + $4 + $5 + $6 + $7',
'$$ = $1 + $2 + $3 + $4 + $5 + $6',
'$$ = $1 + $2 + $3 + $4 + $5 + $6 + $7 + $8',
'$$ = $1 + $2 + $3 + $4 + $5 + $6 + $7',
'$$ = new (require(\'./ast/FunctionDeclaration\').FunctionDeclarationNode)($2, [], $6)',
'$$ = new (require(\'./ast/FunctionDeclaration\').FunctionDeclarationNode)($2, [], null)',
'$$ = new (require(\'./ast/FunctionDeclaration\').FunctionDeclarationNode)($2, $4, $7)',
'$$ = new (require(\'./ast/FunctionDeclaration\').FunctionDeclarationNode)($2, $4, null)',
],
subRules: [
require('./BindingIdentifier'),
Expand Down
112 changes: 79 additions & 33 deletions src/bnf/IterationStatement.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,96 @@ module.exports = {
name: 'IterationStatement',
conditions: [''],
rules: [
'do Statement while ( Expression_In ) ;',
'while ( Expression_In ) Statement',
'do Statement while LeftParenthesis Expression_In RightParenthesis ;',
'while LeftParenthesis Expression_In RightParenthesis Statement',

'for ( LexicalDeclaration Expression_In ; Expression_In ) Statement',
'for ( var VariableDeclarationList ; Expression_In ; Expression_In ) Statement',
'for LeftParenthesis LexicalDeclaration Expression_In ; Expression_In RightParenthesis Statement',
'for LeftParenthesis var VariableDeclarationList ; Expression_In ; Expression_In RightParenthesis Statement',

'for ( LeftHandSideExpression in Expression_In ) Statement',
'for ( ForDeclaration in Expression_In ) Statement',
'for LeftParenthesis LeftHandSideExpression in Expression_In RightParenthesis Statement',
'for LeftParenthesis ForDeclaration in Expression_In RightParenthesis Statement',

'for ( LeftHandSideExpression of AssignmentExpression_In ) Statement',
'for ( var ForBinding of AssignmentExpression_In ) Statement',
'for ( ForDeclaration of AssignmentExpression_In ) Statement',
'for LeftParenthesis LeftHandSideExpression of AssignmentExpression_In RightParenthesis Statement',
'for LeftParenthesis var ForBinding of AssignmentExpression_In RightParenthesis Statement',
'for LeftParenthesis ForDeclaration of AssignmentExpression_In RightParenthesis Statement',

'for ( Expression ; Expression_In ; Expression_In ) Statement',
'for ( Expression ; ; Expression_In ) Statement',
'for ( Expression ; ; ) Statement',
'for ( Expression ; Expression_In ; ) Statement',
'for LeftParenthesis Expression ; Expression_In ; Expression_In RightParenthesis Statement',
'for LeftParenthesis Expression ; ; Expression_In RightParenthesis Statement',
'for LeftParenthesis Expression ; ; RightParenthesis Statement',
'for LeftParenthesis Expression ; Expression_In ; RightParenthesis Statement',

'for ( ; Expression_In ; Expression_In ) Statement',
'for ( ; ; Expression_In ) Statement',
'for ( ; ; ) Statement',
'for ( ; Expression_In ; ) Statement',
'for LeftParenthesis ; Expression_In ; Expression_In RightParenthesis Statement',
'for LeftParenthesis ; ; Expression_In RightParenthesis Statement',
'for LeftParenthesis ; ; RightParenthesis Statement',
'for LeftParenthesis ; Expression_In ; RightParenthesis Statement',
],
handlers: [
'$$ = new (require(\'./ast/IterationStatement\').DoWhileStatementNode)($5, $2)',
'$$ = new (require(\'./ast/IterationStatement\').WhileStatementNode)($3, $5)',
`
require('./ast/IterationStatement').checkForAutoSemicolonInsertion(yy.autoInsertionOffset, $2.range, $7.range, yy.lexer.yylloc);
$$ = new (require('./ast/IterationStatement').ForStatementNode)($3, $4, $6, $8)
`,
`
require('./ast/IterationStatement').checkForAutoSemicolonInsertion(yy.autoInsertionOffset, $2.range, $9.range, yy.lexer.yylloc);
$$ = new (require('./ast/IterationStatement').ForStatementNode)($4, $6, $8, $10)
`,

'$$ = new (require(\'./ast/IterationStatement\').ForStatementNode)($3, $4, $6, $8)',
'$$ = new (require(\'./ast/IterationStatement\').ForStatementNode)($4, $6, $8, $10)',
`
require('./ast/IterationStatement').checkForAutoSemicolonInsertion(yy.autoInsertionOffset, $1.range, $6.range, yy.lexer.yylloc);
$$ = new (require('./ast/IterationStatement').ForInStatementNode)($3, $5, $7)
`,
`
require('./ast/IterationStatement').checkForAutoSemicolonInsertion(yy.autoInsertionOffset, $1.range, $6.range, yy.lexer.yylloc);
$$ = new (require('./ast/IterationStatement').ForInStatementNode)($3, $5, $7)
`,

'$$ = new (require(\'./ast/IterationStatement\').ForInStatementNode)($3, $5, $7)',
'$$ = new (require(\'./ast/IterationStatement\').ForInStatementNode)($3, $5, $7)',
`
require('./ast/IterationStatement').checkForAutoSemicolonInsertion(yy.autoInsertionOffset, $1.range, $6.range, yy.lexer.yylloc);
$$ = new (require('./ast/IterationStatement').ForOfStatementNode)($3, $5, $7)
`,
`
require('./ast/IterationStatement').checkForAutoSemicolonInsertion(yy.autoInsertionOffset, $1.range, $7.range, yy.lexer.yylloc);
$$ = new (require('./ast/IterationStatement').ForOfStatementNode)($4, $6, $8)
`,
`
require('./ast/IterationStatement').checkForAutoSemicolonInsertion(yy.autoInsertionOffset, $1.range, $6.range, yy.lexer.yylloc);
$$ = new (require('./ast/IterationStatement').ForOfStatementNode)($3, $5, $7)
`,

'$$ = new (require(\'./ast/IterationStatement\').ForOfStatementNode)($3, $5, $7)',
'$$ = new (require(\'./ast/IterationStatement\').ForOfStatementNode)($4, $6, $8)',
'$$ = new (require(\'./ast/IterationStatement\').ForOfStatementNode)($3, $5, $7)',
`
require('./ast/IterationStatement').checkForAutoSemicolonInsertion(yy.autoInsertionOffset, $1.range, $8.range, yy.lexer.yylloc);
$$ = new (require('./ast/IterationStatement').ForStatementNode)($3, $5, $7, $9)
`,
`
require('./ast/IterationStatement').checkForAutoSemicolonInsertion(yy.autoInsertionOffset, $1.range, $7.range, yy.lexer.yylloc);
$$ = new (require('./ast/IterationStatement').ForStatementNode)($3, null, $6, $7)
`,
`
require('./ast/IterationStatement').checkForAutoSemicolonInsertion(yy.autoInsertionOffset, $1.range, $6.range, yy.lexer.yylloc);
$$ = new (require('./ast/IterationStatement').ForStatementNode)($3, null, null, $7)
`,
`
require('./ast/IterationStatement').checkForAutoSemicolonInsertion(yy.autoInsertionOffset, $1.range, $7.range, yy.lexer.yylloc);
$$ = new (require('./ast/IterationStatement').ForStatementNode)($3, $5, null, $8)
`,

'$$ = new (require(\'./ast/IterationStatement\').ForStatementNode)($3, $5, $7, $9)',
'$$ = new (require(\'./ast/IterationStatement\').ForStatementNode)($3, null, $6, $7)',
'$$ = new (require(\'./ast/IterationStatement\').ForStatementNode)($3, null, null, $7)',
'$$ = new (require(\'./ast/IterationStatement\').ForStatementNode)($3, $5, null, $8)',

'$$ = new (require(\'./ast/IterationStatement\').ForStatementNode)(null, $4, $6, $8)',
'$$ = new (require(\'./ast/IterationStatement\').ForStatementNode)(null, null, $5, $7)',
'$$ = new (require(\'./ast/IterationStatement\').ForStatementNode)(null, null, null, $6)',
'$$ = new (require(\'./ast/IterationStatement\').ForStatementNode)(null, $4, null, $7)',
`
require('./ast/IterationStatement').checkForAutoSemicolonInsertion(yy.autoInsertionOffset, $1.range, $7.range, yy.lexer.yylloc);
$$ = new (require('./ast/IterationStatement').ForStatementNode)(null, $4, $6, $8)
`,
`
require('./ast/IterationStatement').checkForAutoSemicolonInsertion(yy.autoInsertionOffset, $1.range, $6.range, yy.lexer.yylloc);
$$ = new (require('./ast/IterationStatement').ForStatementNode)(null, null, $5, $7)
`,
`
require('./ast/IterationStatement').checkForAutoSemicolonInsertion(yy.autoInsertionOffset, $1.range, $5.range, yy.lexer.yylloc);
$$ = new (require('./ast/IterationStatement').ForStatementNode)(null, null, null, $6)
`,
`
require('./ast/IterationStatement').checkForAutoSemicolonInsertion(yy.autoInsertionOffset, $1.range, $6.range, yy.lexer.yylloc);
$$ = new (require('./ast/IterationStatement').ForStatementNode)(null, $4, null, $7)
`,
],
subRules: [
require('./Expression_In'),
Expand All @@ -59,5 +103,7 @@ module.exports = {
require('./AssignmentExpression_In'),
require('./ForBinding'),
require('./Expression'),
require('./LeftParenthesis'),
require('./RightParenthesis'),
],
};
12 changes: 12 additions & 0 deletions src/bnf/LeftParenthesis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
conditions: [''],
name: 'LeftParenthesis',
rules: [
'(',
],
handlers: [
'$$ = yy.lexer.yylloc',
],
subRules: [
],
};
4 changes: 2 additions & 2 deletions src/bnf/ReturnStatement.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ module.exports = {
'return Expression_In ;',
],
handlers: [
'$$ = $1 + $2;',
'$$ = $1 + $2 + $3;',
'$$ = new (require(\'./ast/ReturnStatementNode\'))(null)',
'$$ = new (require(\'./ast/ReturnStatementNode\'))($1)',
],
subRules: [
require('./Expression_In'),
Expand Down
12 changes: 12 additions & 0 deletions src/bnf/RightParenthesis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
conditions: [''],
name: 'RightParenthesis',
rules: [
')',
],
handlers: [
'$$ = yy.lexer.yylloc',
],
subRules: [
],
};
177 changes: 107 additions & 70 deletions src/parser.js

Large diffs are not rendered by default.

0 comments on commit afb3597

Please sign in to comment.