-
Notifications
You must be signed in to change notification settings - Fork 23
Print only minimum brackets (new feature), decimal number parsing (fix) #4
base: master
Are you sure you want to change the base?
Changes from all commits
59ee37b
fb69e3b
bea8b47
d0a0417
d29a346
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 = ""; | ||
|
|
@@ -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 + " "; | ||
|
|
||
| if (needBrackets(this, this.right)) { | ||
| str += "(" + this.right.print("", "") + ")"; | ||
| } else { | ||
| str += this.right.print("", ""); | ||
| } | ||
|
|
||
| return str; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Blank line before |
||
| }; | ||
|
|
||
| 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; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Blank line before |
||
| }; | ||
|
|
||
| ast.UpdateExpressionNode.prototype.print = function(indent, indentChar) { | ||
| var str = this.argument.print("", ""); | ||
|
|
||
| if (needBrackets(this, this.argument)) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Blank line before |
||
| str = "(" + str + ")"; | ||
| } | ||
|
|
||
| if (this.prefix) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Blank line before |
||
| return "(" + this.operator + this.argument.print("", "") + ")"; | ||
| str = this.operator + str; | ||
| } else { | ||
| return "(" + this.argument.print("", "") + this.operator + ")"; | ||
| str = str + this.operator; | ||
| } | ||
|
|
||
| return str; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Blank line before |
||
| }; | ||
|
|
||
| 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 + " "; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Blank line before |
||
| }; | ||
|
|
||
| ast.ConditionalExpressionNode.prototype.print = function(indent, indentChar) { | ||
|
|
@@ -497,3 +599,4 @@ | |
| return this.value; | ||
| }; | ||
| })(ecmascript); | ||
|
|
||
There was a problem hiding this comment.
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.