Skip to content

Commit

Permalink
Merge branch 'master' into issue-201
Browse files Browse the repository at this point in the history
  • Loading branch information
ichiriac authored Nov 10, 2018
2 parents e3861f1 + e6752a9 commit 15fbf83
Show file tree
Hide file tree
Showing 51 changed files with 7,283 additions and 1,160 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = {
sourceType: "module"
},
plugins: ["prettier"],
extends: ["eslint:recommended", "prettier"],
extends: ["eslint:recommended"],
env: {
browser: true,
node: true,
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"dist"
],
"scripts": {
"lint": "prettier --write src/*.js src/**/*.js",
"lint": "eslint 'src/**/*.js'",
"pretest": "npm run lint",
"test": "jest",
"prebuild": "npm run test",
Expand Down Expand Up @@ -58,7 +58,8 @@
"babel-loader": "^7",
"babel-preset-es2015": "^6",
"coveralls": "^3",
"eslint-plugin-prettier": "^2.6.2",
"eslint": "^5.8.0",
"eslint-plugin-prettier": "^3.0.0",
"jest": "^22.4",
"jsdoc": "^3.5.5",
"jsdoc-template": "github:braintree/jsdoc-template",
Expand Down
8 changes: 6 additions & 2 deletions src/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const Position = require("./ast/position");
* - [Location](#location)
* - [Position](#position)
* - [Node](#node)
* - [EncapsedPart](#encapsedpart)
* - [Constant](#constant)
* - [Identifier](#identifier)
* - [Reference](#reference)
* - [TypeReference](#classreference)
Expand Down Expand Up @@ -67,6 +69,8 @@ const Position = require("./ast/position");
* - [Nowdoc](#nowdoc)
* - [Encapsed](#encapsed)
* - [Statement](#statement)
* - [ConstantStatement](#constantstatement)
* - [ClassConstant](#classconstant)
* - [Return](#return)
* - [Label](#label)
* - [Continue](#continue)
Expand Down Expand Up @@ -98,8 +102,6 @@ const Position = require("./ast/position");
* - [Class](#class)
* - [Interface](#interface)
* - [Trait](#trait)
* - [Constant](#constant)
* - [ClassConstant](#classconstant)
* - [Function](#function)
* - [Method](#method)
* - [Parameter](#parameter)
Expand Down Expand Up @@ -359,13 +361,15 @@ AST.prototype.prepare = function(kind, docs, parser) {
require("./ast/commentblock"),
require("./ast/commentline"),
require("./ast/constant"),
require("./ast/constantstatement"),
require("./ast/continue"),
require("./ast/declaration"),
require("./ast/declare"),
require("./ast/do"),
require("./ast/echo"),
require("./ast/empty"),
require("./ast/encapsed"),
require("./ast/encapsedpart"),
require("./ast/entry"),
require("./ast/error"),
require("./ast/eval"),
Expand Down
40 changes: 32 additions & 8 deletions src/ast/classconstant.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,48 @@
*/
"use strict";

const Constant = require("./constant");
const ConstantStatement = require("./constantstatement");
const KIND = "classconstant";

const IS_UNDEFINED = "";
const IS_PUBLIC = "public";
const IS_PROTECTED = "protected";
const IS_PRIVATE = "private";

/**
* Defines a class/interface/trait constant
* @constructor ClassConstant
* @extends {Constant}
* @property {boolean} isStatic
* @extends {ConstantStatement}
* @property {string} visibility
*/
module.exports = Constant.extends(KIND, function ClassConstant(
name,
value,
const ClassConstant = ConstantStatement.extends(KIND, function ClassConstant(
kind,
items,
flags,
docs,
location
) {
Constant.apply(this, [name, value, docs, location]);
this.kind = KIND;
ConstantStatement.apply(this, [kind || KIND, items, docs, location]);
this.parseFlags(flags);
});

/**
* Generic flags parser
* @param {Integer[]} flags
* @return {void}
*/
ClassConstant.prototype.parseFlags = function(flags) {
if (flags[0] === -1) {
this.visibility = IS_UNDEFINED;
} else if (flags[0] === null) {
this.visibility = null;
} else if (flags[0] === 0) {
this.visibility = IS_PUBLIC;
} else if (flags[0] === 1) {
this.visibility = IS_PROTECTED;
} else if (flags[0] === 2) {
this.visibility = IS_PRIVATE;
}
};

module.exports = ClassConstant;
14 changes: 8 additions & 6 deletions src/ast/constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@
*/
"use strict";

const Declaration = require("./declaration");
const Node = require("./node");
const KIND = "constant";

/**
* Defines a namespace constant
* Defines a constant
* @constructor Constant
* @extends {Declaration}
* @property {Node|null} value
* @extends {Node}
* @property {string} name
* @property {Node|string|number|boolean|null} value
*/
module.exports = Declaration.extends(KIND, function Constant(
module.exports = Node.extends(KIND, function Constant(
name,
value,
docs,
location
) {
Declaration.apply(this, [KIND, name, docs, location]);
Node.apply(this, [KIND, docs, location]);
this.name = name;
this.value = value;
});
25 changes: 25 additions & 0 deletions src/ast/constantstatement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright (C) 2018 Glayzzle (BSD3 License)
* @authors https://github.com/glayzzle/php-parser/graphs/contributors
* @url http://glayzzle.com
*/
"use strict";

const Statement = require("./statement");
const KIND = "constantstatement";

/**
* Declares a constants into the current scope
* @constructor ConstantStatement
* @extends {Statement}
* @property {Constant[]} items
*/
module.exports = Statement.extends(KIND, function ConstantStatement(
kind,
items,
docs,
location
) {
Statement.apply(this, [kind || KIND, docs, location]);
this.items = items;
});
26 changes: 26 additions & 0 deletions src/ast/encapsedpart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright (C) 2018 Glayzzle (BSD3 License)
* @authors https://github.com/glayzzle/php-parser/graphs/contributors
* @url http://glayzzle.com
*/
"use strict";

const Expression = require("./expression");
const KIND = "encapsedpart";

/**
* Part of `Encapsed` node
* @constructor EncapsedPart
* @extends {Expression}
* @property {Expression} what
*/
module.exports = Expression.extends(KIND, function EncapsedPart(
expression,
curly,
docs,
location
) {
Expression.apply(this, [KIND, docs, location]);
this.expression = expression;
this.curly = curly;
});
2 changes: 2 additions & 0 deletions src/ast/parentreference.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ const KIND = "parentreference";
* @extends {Reference}
*/
const ParentReference = Reference.extends(KIND, function ParentReference(
raw,
docs,
location
) {
Reference.apply(this, [KIND, docs, location]);
this.raw = raw;
});
module.exports = ParentReference;
2 changes: 2 additions & 0 deletions src/ast/selfreference.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ const KIND = "selfreference";
* @extends {Reference}
*/
const SelfReference = Reference.extends(KIND, function SelfReference(
raw,
docs,
location
) {
Reference.apply(this, [KIND, docs, location]);
this.raw = raw;
});
module.exports = SelfReference;
2 changes: 2 additions & 0 deletions src/ast/staticreference.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ const KIND = "staticreference";
* @extends {Reference}
*/
const StaticReference = Reference.extends(KIND, function StaticReference(
raw,
docs,
location
) {
Reference.apply(this, [KIND, docs, location]);
this.raw = raw;
});
module.exports = StaticReference;
14 changes: 13 additions & 1 deletion src/ast/typereference.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,25 @@ const KIND = "typereference";
*/
const TypeReference = Reference.extends(KIND, function TypeReference(
name,
raw,
docs,
location
) {
Reference.apply(this, [KIND, docs, location]);
this.name = name;
this.raw = raw;
});

TypeReference.types = ["int", "float", "string", "bool", "object", "array"];
TypeReference.types = [
"int",
"float",
"string",
"bool",
"object",
"array",
"callable",
"iterable",
"void"
];

module.exports = TypeReference;
2 changes: 2 additions & 0 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,9 @@ parser.prototype.node = function(name) {
docs = this._docs.slice(this._docIndex);
this._docIndex = this._docs.length;
if (this.debug) {
// eslint-disable-next-line no-console
console.log(new Error("Append docs on " + name));
// eslint-disable-next-line no-console
console.log(docs);
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/parser/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ module.exports = {
if (this.expect(this.tok.T_CONST)) {
this.next();
}
return this.read_list(
const result = this.node("classconstant");
const items = this.read_list(
/**
* Reads a constant declaration
*
Expand All @@ -175,7 +176,7 @@ module.exports = {
* @return {Constant} [:link:](AST.md#constant)
*/
function read_constant_declaration() {
const result = this.node("classconstant");
const result = this.node("constant");
let name = null;
let value = null;
if (
Expand All @@ -190,10 +191,12 @@ module.exports = {
if (this.expect("=")) {
value = this.next().read_expr();
}
return result(name, value, flags);
return result(name, value);
},
","
);

return result(null, items, flags);
},
/**
* Read member flags
Expand Down
27 changes: 21 additions & 6 deletions src/parser/expr.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
"use strict";

module.exports = {
read_expr: function() {
read_expr: function(expr) {
const result = this.node();
const expr = this.read_expr_item();
if (!expr) {
expr = this.read_expr_item();
}
// binary operations
if (this.token === "|")
return result("bin", "|", expr, this.next().read_expr());
Expand Down Expand Up @@ -63,8 +65,21 @@ module.exports = {
return result("bin", ">=", expr, this.next().read_expr());
if (this.token === this.tok.T_SPACESHIP)
return result("bin", "<=>", expr, this.next().read_expr());
if (this.token === this.tok.T_INSTANCEOF)
return result("bin", "instanceof", expr, this.next().read_expr());
if (this.token === this.tok.T_INSTANCEOF) {
expr = result(
"bin",
"instanceof",
expr,
this.next().read_class_name_reference()
);
if (
this.token !== ";" &&
this.token !== this.tok.T_INLINE_HTML &&
this.token !== this.EOF
) {
expr = this.read_expr(expr);
}
}

// extra operations :
// $username = $_GET['user'] ?? 'nobody';
Expand Down Expand Up @@ -417,9 +432,9 @@ module.exports = {
expr = this.read_scalar();
if (expr.kind === "array" && expr.shortForm && this.token === "=") {
// list assign
let list = this.node("list")(expr.items, true);
const list = this.node("list")(expr.items, true);
if (expr.loc) list.loc = expr.loc;
let right = this.next().read_expr();
const right = this.next().read_expr();
return result("assign", list, right, "=");
} else {
// see #189 - swap docs on nodes
Expand Down
Loading

0 comments on commit 15fbf83

Please sign in to comment.