Skip to content

Commit 622724d

Browse files
committed
release 2.0.0-pre8
1 parent ae74d55 commit 622724d

File tree

6 files changed

+136
-98
lines changed

6 files changed

+136
-98
lines changed

RELEASE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- Shell nodes are now exposed by encapsed nodes only
99
- Typescript definition file
1010
- Avoid identifier nodes on namespaces & use statements
11+
- Fix precedence on bin, unary, retif nodes
1112

1213
## 1.0.0 : (2017-01-03)
1314

dist/php-parser.js

Lines changed: 81 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! php-parser - BSD3 License - 2017-02-17 */
1+
/*! php-parser - BSD3 License - 2017-02-26 */
22

33
require=(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
44
// shim for using process in browser
@@ -357,6 +357,21 @@ AST.prototype.prepare = function(kind, parser) {
357357
}
358358
var result = Object.create(node.prototype);
359359
node.apply(result, args);
360+
if (
361+
result.kind === 'bin' &&
362+
result.right &&
363+
typeof result.right.precedence === 'function'
364+
) {
365+
var out = result.right.precedence(result);
366+
if (out) { // shift with precedence
367+
result = out;
368+
}
369+
} else if (result.kind === 'unary') {
370+
var out = result.precedence(result.what);
371+
if (out) { // shift with precedence
372+
result = out;
373+
}
374+
}
360375
return result;
361376
};
362377
};
@@ -525,8 +540,8 @@ var binOperatorsPrecedence = [
525540
['or'],
526541
['xor'],
527542
['and'],
528-
// TODO: assignment
529-
// TODO: ternary ? :
543+
// TODO: assignment / not sure that PHP allows this with expressions
544+
['retif'],
530545
['??'],
531546
['||'],
532547
['&&'],
@@ -538,21 +553,13 @@ var binOperatorsPrecedence = [
538553
['<<', '>>'],
539554
['+', '-', '.'],
540555
['*', '/', '%'],
541-
// TODO: unary !
556+
['!'],
542557
['instanceof'],
543-
// TODO: unary ++, --, ~, @, typecasts
558+
// TODO: typecasts
544559
// TODO: [ (array)
545560
// TODO: clone, new
546561
];
547562

548-
// define nodes shifting
549-
var precedence = {};
550-
binOperatorsPrecedence.forEach(function (list, index) {
551-
list.forEach(function (operator) {
552-
precedence[operator] = index + 1;
553-
});
554-
});
555-
556563
/*
557564
x OP1 (y OP2 z)
558565
z OP1 (x OP2 y)
@@ -568,28 +575,30 @@ z OP2 (x OP1 y)
568575
*/
569576
var Bin = Operation.extends(function Bin(type, left, right, location) {
570577
Operation.apply(this, [KIND, location]);
571-
if (right && right.kind === 'bin') {
572-
var lLevel = precedence[type];
573-
var rLevel = precedence[right.type];
574-
if (lLevel && rLevel && rLevel < lLevel) {
575-
// shift precedence
576-
var buffer = right.right;
577-
right.right = right.left;
578-
right.left = left;
579-
left = buffer;
580-
buffer = right.type;
581-
right.type = type;
582-
type = buffer;
583-
buffer = left;
584-
left = right;
585-
right = buffer;
586-
}
587-
}
588578
this.type = type;
589579
this.left = left;
590580
this.right = right;
591581
});
592582

583+
Bin.prototype.precedence = function(node) {
584+
var lLevel = Bin.precedence[node.type];
585+
var rLevel = Bin.precedence[this.type];
586+
if (lLevel && rLevel && rLevel < lLevel) {
587+
// shift precedence
588+
node.right = this.left;
589+
this.left = node;
590+
return this;
591+
}
592+
};
593+
594+
// define nodes shifting
595+
Bin.precedence = {};
596+
binOperatorsPrecedence.forEach(function (list, index) {
597+
list.forEach(function (operator) {
598+
Bin.precedence[operator] = index + 1;
599+
});
600+
});
601+
593602
module.exports = Bin;
594603

595604
},{"./operation":57}],6:[function(require,module,exports){
@@ -1862,16 +1871,12 @@ var KIND = 'namespace';
18621871
* The main program node
18631872
* @constructor Namespace
18641873
* @extends {Block}
1865-
* @property {Identifier} name
1874+
* @property {String} name
18661875
* @property {Boolean} withBrackets
18671876
*/
18681877
var Namespace = Block.extends(function Namespace(name, children, withBrackets, location) {
18691878
Block.apply(this, [KIND, children, location]);
1870-
if (name instanceof Identifier) {
1871-
this.name = name;
1872-
} else {
1873-
this.name = new Identifier(name);
1874-
}
1879+
this.name = name;
18751880
this.withBrackets = withBrackets || false;
18761881
});
18771882

@@ -2256,6 +2261,8 @@ module.exports = PropertyLookup;
22562261

22572262
var Statement = require('./statement');
22582263
var KIND = 'retif';
2264+
var Bin = require('./bin');
2265+
var PRECEDENCE = Bin.precedence[KIND];
22592266

22602267
/**
22612268
* Defines a short if statement that returns a value
@@ -2272,9 +2279,26 @@ var RetIf = Statement.extends(function RetIf(test, trueExpr, falseExpr, location
22722279
this.falseExpr = falseExpr;
22732280
});
22742281

2282+
/**
2283+
* Handles precedence over items
2284+
*/
2285+
RetIf.prototype.precedence = function(node) {
2286+
var what = node.kind === 'bin' ? node.type : node.kind;
2287+
var lLevel = Bin.precedence[what];
2288+
if (lLevel && PRECEDENCE < lLevel) {
2289+
if (node.kind === 'bin') {
2290+
node.right = this.test;
2291+
this.test = node;
2292+
return this;
2293+
} else {
2294+
throw new Error('@todo ' + node.kind);
2295+
}
2296+
}
2297+
};
2298+
22752299
module.exports = RetIf;
22762300

2277-
},{"./statement":70}],68:[function(require,module,exports){
2301+
},{"./bin":5,"./statement":70}],68:[function(require,module,exports){
22782302
/*!
22792303
* Copyright (C) 2017 Glayzzle (BSD3 License)
22802304
* @authors https://github.com/glayzzle/php-parser/graphs/contributors
@@ -2660,6 +2684,18 @@ var Unary = Operation.extends(function Unary(type, what, location) {
26602684
this.what = what;
26612685
});
26622686

2687+
Unary.prototype.precedence = function(node) {
2688+
if (node.kind === 'bin') {
2689+
this.what = node.left;
2690+
node.left = this;
2691+
return node;
2692+
} else if (node.kind === 'retif') {
2693+
this.what = node.test;
2694+
node.test = this;
2695+
return node;
2696+
}
2697+
};
2698+
26632699
module.exports = Unary;
26642700

26652701
},{"./operation":57}],83:[function(require,module,exports){
@@ -2697,7 +2733,7 @@ var KIND = 'usegroup';
26972733
* Defines a use statement (with a list of use items)
26982734
* @constructor UseGroup
26992735
* @extends {Statement}
2700-
* @property {Identifier|null} name
2736+
* @property {String|null} name
27012737
* @property {String|null} type - Possible value : function, const
27022738
* @property {UseItem[]} item
27032739
* @see {Namespace}
@@ -2726,7 +2762,7 @@ var KIND = 'useitem';
27262762
* Defines a use statement (from namespace)
27272763
* @constructor UseItem
27282764
* @extends {Statement}
2729-
* @property {Identifier} name
2765+
* @property {String} name
27302766
* @property {String|null} type - Possible value : function, const
27312767
* @property {String|null} alias
27322768
* @see {Namespace}
@@ -6569,12 +6605,12 @@ module.exports = {
65696605
this.currentNamespace = name;
65706606
var body = this.nextWithComments().read_top_statements();
65716607
this.expect(this.EOF);
6572-
return result(name, body, false);
6608+
return result(name.name, body, false);
65736609
} else if (this.token == '{') {
65746610
this.currentNamespace = name;
65756611
var body = this.nextWithComments().read_top_statements();
65766612
this.expect('}') && this.nextWithComments();
6577-
return result(name, body, true);
6613+
return result(name.name, body, true);
65786614
} else if (this.token === '(') {
65796615
// resolve ambuiguity between namespace & function call
65806616
name.resolution = this.ast.identifier.RELATIVE_NAME;
@@ -6635,7 +6671,7 @@ module.exports = {
66356671
if (this.token === ',') {
66366672
items = items.concat(this.next().read_use_declarations(false));
66376673
} else if (this.token === '{') {
6638-
name = items[0].name;
6674+
name = items[0].name.name;
66396675
items = this.next().read_use_declarations(type === null);
66406676
this.expect('}') && this.next();
66416677
}
@@ -6655,7 +6691,7 @@ module.exports = {
66556691
if (typed) type = this.read_use_type();
66566692
var name = this.read_namespace_name();
66576693
var alias = this.read_use_alias();
6658-
return result(name, alias, type);
6694+
return result(name.name, alias, type);
66596695
}
66606696
/**
66616697
* Reads a list of use declarations
@@ -7235,9 +7271,9 @@ module.exports = {
72357271
return result(args);
72367272

72377273
case this.tok.T_INLINE_HTML:
7238-
var result = this.node('inline')(this.text());
7274+
var result = this.node('inline'), value = this.text();
72397275
this.next();
7240-
return result;
7276+
return result(value);
72417277

72427278
case this.tok.T_UNSET:
72437279
var result = this.node('unset');

0 commit comments

Comments
 (0)