Skip to content
This repository was archived by the owner on Jan 1, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ecmascript.jison
Original file line number Diff line number Diff line change
Expand Up @@ -1553,7 +1553,7 @@ function parseRegularExpressionLiteral(literal) {
}

function parseNumericLiteral(literal) {
if (literal.charAt(0) === "0") {
if (literal.charAt(0) === "0" && Number(literal)%1 === 0) {
if (literal.charAt(1).toLowerCase() === "x") {
return parseInt(literal, 16);
} else {
Expand Down
115 changes: 109 additions & 6 deletions print.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,60 @@
"use strict";

var nonCommutative = [ "-" , "/" , "%" ];
var precedence = [ // from less to most preferent
[ "=", "+=", "-=", "*=", "/=", "%=", "<<=", ">>=", ">>>=", "&=", "^=", "|=" ],
"||",
"&&",
"|",
"^",
"&",
[ "<", "<=", ">", ">=", "in", "instanceof" ],
[ "<<", ">>", ">>>" ],
[ "+", "-" ],
[ "*", "/", "%" ],
[ "!", "~", "+unary", "-unary", "typeof", "void", "delete" ],
[ "++", "--" ]
];

(function(parser) {
var ast = parser.ast;

function needBrackets(parentToken, childToken, childPrecedesParent) {
var firstOp = parentToken.operator;
if (parentToken.type === "UnaryExpression" &&
(parentToken.operator === "-" || parentToken.operator === "+")) {
firstOp += "unary";
}
var secondOp = childToken.operator;
if (childToken.type == "UnaryExpression" &&
(childToken.operator === "-" || childToken.operator === "+")) {
secondOp += "unary";
}
if (!childPrecedesParent && childToken.type == "BinaryExpression") {
for (var i=0; i<nonCommutative.length; i++) {
if (firstOp == nonCommutative[i]) {
return true;
}
}
}
var firstPos = precedence.length;
var secondPos = precedence.length;
for (var i = 0; i < precedence.length; i++) {
for (var j = 0; j < precedence[i].length; j++) {
var operation = precedence[i][j];
if (firstOp === operation) {
firstPos = i;
}

if (secondOp === operation) {
secondPos = i;
}
}
}

return firstPos > secondPos;
}

ast.ProgramNode.prototype.print = function(indent, indentChar) {
var elements = this.body;
var str = "";
Expand Down Expand Up @@ -392,29 +444,79 @@

if (operator === "delete" || operator === "void" || operator === "typeof") {
return operator + " (" + this.argument.print("", "") + ")";
} else {
} else if (needBrackets(this, this.argument)) {
return operator + "(" + this.argument.print("", "") + ")";
} else {
return operator + this.argument.print("", "");
}
};

ast.BinaryExpressionNode.prototype.print = function(indent, indentChar) {
return "(" + this.left.print("", "") + ") " + this.operator + " (" + this.right.print("", "") + ")";
var str = "";

if (needBrackets(this, this.left, true)) {
str += "(" + this.left.print("", "") + ")";
} else {
str += this.left.print("", "");
}

str += " " + this.operator + " ";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blank line after the ending curly brace.


if (needBrackets(this, this.right)) {
str += "(" + this.right.print("", "") + ")";
} else {
str += this.right.print("", "");
}

return str;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blank line before return

};

ast.AssignmentExpressionNode.prototype.print = function(indent, indentChar) {
return this.left.print("", "") + " " + this.operator + " (" + this.right.print("", "") + ")";
var str = this.left.print("", "") + " " + this.operator + " ";

if (needBrackets(this, this.right)) {
str += "(" + this.right.print("", "") + ")";
} else {
str += this.right.print("", "");
}

return str;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blank line before return

};

ast.UpdateExpressionNode.prototype.print = function(indent, indentChar) {
var str = this.argument.print("", "");

if (needBrackets(this, this.argument)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blank line before if

str = "(" + str + ")";
}

if (this.prefix) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blank line before if

return "(" + this.operator + this.argument.print("", "") + ")";
str = this.operator + str;
} else {
return "(" + this.argument.print("", "") + this.operator + ")";
str = str + this.operator;
}

return str;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blank line before return

};

ast.LogicalExpressionNode.prototype.print = function(indent, indentChar) {
return "(" + this.left.print("", "") + ") " + this.operator + " (" + this.right.print("", "") + ")";
var str = "";

if (needBrackets(this, this.left, true)) {
str += "(" + this.left.print("", "") + ")";
} else {
str += this.left.print("", "");
}

str += " " + this.operator + " ";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blank line before and after this line.


if (needBrackets(this, this.right)) {
str += "(" + this.right.print("", "") + ")";
} else {
str += this.right.print("", "");
}

return str;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blank line before return.

};

ast.ConditionalExpressionNode.prototype.print = function(indent, indentChar) {
Expand Down Expand Up @@ -497,3 +599,4 @@
return this.value;
};
})(ecmascript);