Skip to content

Commit

Permalink
fix: ast for constants
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Nov 6, 2018
1 parent ea69b15 commit 0d54ccb
Show file tree
Hide file tree
Showing 17 changed files with 775 additions and 161 deletions.
6 changes: 4 additions & 2 deletions src/ast.js
Expand Up @@ -14,6 +14,7 @@ const Position = require("./ast/position");
* - [Location](#location)
* - [Position](#position)
* - [Node](#node)
* - [Constant](#constant)
* - [Identifier](#identifier)
* - [Reference](#reference)
* - [TypeReference](#classreference)
Expand Down Expand Up @@ -67,6 +68,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 +101,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,6 +360,7 @@ 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"),
Expand Down
38 changes: 31 additions & 7 deletions src/ast/classconstant.js
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}
* @extends {ConstantStatement}
* @property {boolean} isStatic
* @property {string} visibility
*/
module.exports = Constant.extends(KIND, function ClassConstant(
name,
value,
const ClassConstant = ConstantStatement.extends(KIND, function ClassConstant(
items,
flags,
docs,
location
) {
Constant.apply(this, [name, value, docs, location]);
this.kind = KIND;
ConstantStatement.apply(this, [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;
9 changes: 5 additions & 4 deletions src/ast/constant.js
Expand Up @@ -5,21 +5,22 @@
*/
"use strict";

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

/**
* Defines a namespace constant
* @constructor Constant
* @extends {Declaration}
* @extends {Node}
* @property {Node|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;
});
24 changes: 24 additions & 0 deletions src/ast/constantstatement.js
@@ -0,0 +1,24 @@
/**
* 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 static variable into the current scope
* @constructor Static
* @extends {Statement}
* @property {Variable[]|Assign[]} items
*/
module.exports = Statement.extends(KIND, function ConstantStatement(
items,
docs,
location
) {
Statement.apply(this, [KIND, docs, location]);
this.items = items;
});
9 changes: 6 additions & 3 deletions src/parser/class.js
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(items, flags);
},
/**
* Read member flags
Expand Down
10 changes: 7 additions & 3 deletions src/parser/statement.js
Expand Up @@ -51,8 +51,13 @@ module.exports = {
return this.read_trait();
case this.tok.T_USE:
return this.read_use_statement();
case this.tok.T_CONST:
return this.next().read_const_list();
case this.tok.T_CONST: {
const result = this.node("constantstatement");
const items = this.next().read_const_list();
this.expectEndOfStatement();
console.log(result);
return result(items);
}
case this.tok.T_NAMESPACE:
return this.read_namespace();
case this.tok.T_HALT_COMPILER: {
Expand Down Expand Up @@ -109,7 +114,6 @@ module.exports = {
",",
false
);
this.expectEndOfStatement();
return result;
},
/**
Expand Down

0 comments on commit 0d54ccb

Please sign in to comment.