php-parser
Advanced tools
Comparing version
{ | ||
"name": "php-parser", | ||
"version": "2.0.0-pre7", | ||
"version": "2.0.0-pre8", | ||
"description": "Parse PHP code and returns its AST", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -11,2 +11,3 @@ # Releases | ||
- Avoid identifier nodes on namespaces & use statements | ||
- Fix precedence on bin, unary, retif nodes | ||
@@ -13,0 +14,0 @@ ## 1.0.0 : (2017-01-03) |
@@ -175,2 +175,17 @@ /*! | ||
node.apply(result, args); | ||
if ( | ||
result.kind === 'bin' && | ||
result.right && | ||
typeof result.right.precedence === 'function' | ||
) { | ||
var out = result.right.precedence(result); | ||
if (out) { // shift with precedence | ||
result = out; | ||
} | ||
} else if (result.kind === 'unary') { | ||
var out = result.precedence(result.what); | ||
if (out) { // shift with precedence | ||
result = out; | ||
} | ||
} | ||
return result; | ||
@@ -177,0 +192,0 @@ }; |
@@ -16,4 +16,4 @@ /*! | ||
['and'], | ||
// TODO: assignment | ||
// TODO: ternary ? : | ||
// TODO: assignment / not sure that PHP allows this with expressions | ||
['retif'], | ||
['??'], | ||
@@ -30,5 +30,5 @@ ['||'], | ||
['*', '/', '%'], | ||
// TODO: unary ! | ||
['!'], | ||
['instanceof'], | ||
// TODO: unary ++, --, ~, @, typecasts | ||
// TODO: typecasts | ||
// TODO: [ (array) | ||
@@ -38,10 +38,2 @@ // TODO: clone, new | ||
// define nodes shifting | ||
var precedence = {}; | ||
binOperatorsPrecedence.forEach(function (list, index) { | ||
list.forEach(function (operator) { | ||
precedence[operator] = index + 1; | ||
}); | ||
}); | ||
/* | ||
@@ -62,19 +54,2 @@ x OP1 (y OP2 z) | ||
Operation.apply(this, [KIND, location]); | ||
if (right && right.kind === 'bin') { | ||
var lLevel = precedence[type]; | ||
var rLevel = precedence[right.type]; | ||
if (lLevel && rLevel && rLevel < lLevel) { | ||
// shift precedence | ||
var buffer = right.right; | ||
right.right = right.left; | ||
right.left = left; | ||
left = buffer; | ||
buffer = right.type; | ||
right.type = type; | ||
type = buffer; | ||
buffer = left; | ||
left = right; | ||
right = buffer; | ||
} | ||
} | ||
this.type = type; | ||
@@ -85,2 +60,21 @@ this.left = left; | ||
Bin.prototype.precedence = function(node) { | ||
var lLevel = Bin.precedence[node.type]; | ||
var rLevel = Bin.precedence[this.type]; | ||
if (lLevel && rLevel && rLevel < lLevel) { | ||
// shift precedence | ||
node.right = this.left; | ||
this.left = node; | ||
return this; | ||
} | ||
}; | ||
// define nodes shifting | ||
Bin.precedence = {}; | ||
binOperatorsPrecedence.forEach(function (list, index) { | ||
list.forEach(function (operator) { | ||
Bin.precedence[operator] = index + 1; | ||
}); | ||
}); | ||
module.exports = Bin; |
@@ -10,2 +10,4 @@ /*! | ||
var KIND = 'retif'; | ||
var Bin = require('./bin'); | ||
var PRECEDENCE = Bin.precedence[KIND]; | ||
@@ -27,2 +29,19 @@ /** | ||
/** | ||
* Handles precedence over items | ||
*/ | ||
RetIf.prototype.precedence = function(node) { | ||
var what = node.kind === 'bin' ? node.type : node.kind; | ||
var lLevel = Bin.precedence[what]; | ||
if (lLevel && PRECEDENCE < lLevel) { | ||
if (node.kind === 'bin') { | ||
node.right = this.test; | ||
this.test = node; | ||
return this; | ||
} else { | ||
throw new Error('@todo ' + node.kind); | ||
} | ||
} | ||
}; | ||
module.exports = RetIf; |
@@ -24,2 +24,14 @@ /*! | ||
Unary.prototype.precedence = function(node) { | ||
if (node.kind === 'bin') { | ||
this.what = node.left; | ||
node.left = this; | ||
return node; | ||
} else if (node.kind === 'retif') { | ||
this.what = node.test; | ||
node.test = this; | ||
return node; | ||
} | ||
}; | ||
module.exports = Unary; |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
2221259
0.14%16509
0.41%10
-9.09%