Skip to content
This repository was archived by the owner on Mar 29, 2023. It is now read-only.

Commit ad70067

Browse files
fix: ast for constants
1 parent ea69b15 commit ad70067

17 files changed

+765
-148
lines changed

src/ast.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const Position = require("./ast/position");
1414
* - [Location](#location)
1515
* - [Position](#position)
1616
* - [Node](#node)
17+
* - [Constant](#constant)
1718
* - [Identifier](#identifier)
1819
* - [Reference](#reference)
1920
* - [TypeReference](#classreference)
@@ -67,6 +68,8 @@ const Position = require("./ast/position");
6768
* - [Nowdoc](#nowdoc)
6869
* - [Encapsed](#encapsed)
6970
* - [Statement](#statement)
71+
* - [ConstantStatement](#constantstatement)
72+
* - [ClassConstant](#classconstant)
7073
* - [Return](#return)
7174
* - [Label](#label)
7275
* - [Continue](#continue)
@@ -98,8 +101,6 @@ const Position = require("./ast/position");
98101
* - [Class](#class)
99102
* - [Interface](#interface)
100103
* - [Trait](#trait)
101-
* - [Constant](#constant)
102-
* - [ClassConstant](#classconstant)
103104
* - [Function](#function)
104105
* - [Method](#method)
105106
* - [Parameter](#parameter)
@@ -359,6 +360,7 @@ AST.prototype.prepare = function(kind, docs, parser) {
359360
require("./ast/commentblock"),
360361
require("./ast/commentline"),
361362
require("./ast/constant"),
363+
require("./ast/constantstatement"),
362364
require("./ast/continue"),
363365
require("./ast/declaration"),
364366
require("./ast/declare"),

src/ast/classconstant.js

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,48 @@
55
*/
66
"use strict";
77

8-
const Constant = require("./constant");
8+
const ConstantStatement = require("./constantstatement");
99
const KIND = "classconstant";
1010

11+
const IS_UNDEFINED = "";
12+
const IS_PUBLIC = "public";
13+
const IS_PROTECTED = "protected";
14+
const IS_PRIVATE = "private";
15+
1116
/**
1217
* Defines a class/interface/trait constant
1318
* @constructor ClassConstant
14-
* @extends {Constant}
15-
* @property {boolean} isStatic
19+
* @extends {ConstantStatement}
1620
* @property {string} visibility
1721
*/
18-
module.exports = Constant.extends(KIND, function ClassConstant(
19-
name,
20-
value,
22+
const ClassConstant = ConstantStatement.extends(KIND, function ClassConstant(
23+
kind,
24+
items,
2125
flags,
2226
docs,
2327
location
2428
) {
25-
Constant.apply(this, [name, value, docs, location]);
26-
this.kind = KIND;
29+
ConstantStatement.apply(this, [kind || KIND, items, docs, location]);
2730
this.parseFlags(flags);
2831
});
32+
33+
/**
34+
* Generic flags parser
35+
* @param {Integer[]} flags
36+
* @return {void}
37+
*/
38+
ClassConstant.prototype.parseFlags = function(flags) {
39+
if (flags[0] === -1) {
40+
this.visibility = IS_UNDEFINED;
41+
} else if (flags[0] === null) {
42+
this.visibility = null;
43+
} else if (flags[0] === 0) {
44+
this.visibility = IS_PUBLIC;
45+
} else if (flags[0] === 1) {
46+
this.visibility = IS_PROTECTED;
47+
} else if (flags[0] === 2) {
48+
this.visibility = IS_PRIVATE;
49+
}
50+
};
51+
52+
module.exports = ClassConstant;

src/ast/constant.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,23 @@
55
*/
66
"use strict";
77

8-
const Declaration = require("./declaration");
8+
const Node = require("./node");
99
const KIND = "constant";
1010

1111
/**
12-
* Defines a namespace constant
12+
* Defines a constant
1313
* @constructor Constant
14-
* @extends {Declaration}
15-
* @property {Node|null} value
14+
* @extends {Node}
15+
* @property {string} name
16+
* @property {Node|string|number|boolean|null} value
1617
*/
17-
module.exports = Declaration.extends(KIND, function Constant(
18+
module.exports = Node.extends(KIND, function Constant(
1819
name,
1920
value,
2021
docs,
2122
location
2223
) {
23-
Declaration.apply(this, [KIND, name, docs, location]);
24+
Node.apply(this, [KIND, docs, location]);
25+
this.name = name;
2426
this.value = value;
2527
});

src/ast/constantstatement.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright (C) 2018 Glayzzle (BSD3 License)
3+
* @authors https://github.com/glayzzle/php-parser/graphs/contributors
4+
* @url http://glayzzle.com
5+
*/
6+
"use strict";
7+
8+
const Statement = require("./statement");
9+
const KIND = "constantstatement";
10+
11+
/**
12+
* Declares a constants into the current scope
13+
* @constructor ConstantStatement
14+
* @extends {Statement}
15+
* @property {Constant[]} items
16+
*/
17+
module.exports = Statement.extends(KIND, function ConstantStatement(
18+
kind,
19+
items,
20+
docs,
21+
location
22+
) {
23+
Statement.apply(this, [kind || KIND, docs, location]);
24+
this.items = items;
25+
});

src/parser/class.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ module.exports = {
165165
if (this.expect(this.tok.T_CONST)) {
166166
this.next();
167167
}
168-
return this.read_list(
168+
const result = this.node("classconstant");
169+
const items = this.read_list(
169170
/**
170171
* Reads a constant declaration
171172
*
@@ -175,7 +176,7 @@ module.exports = {
175176
* @return {Constant} [:link:](AST.md#constant)
176177
*/
177178
function read_constant_declaration() {
178-
const result = this.node("classconstant");
179+
const result = this.node("constant");
179180
let name = null;
180181
let value = null;
181182
if (
@@ -190,10 +191,12 @@ module.exports = {
190191
if (this.expect("=")) {
191192
value = this.next().read_expr();
192193
}
193-
return result(name, value, flags);
194+
return result(name, value);
194195
},
195196
","
196197
);
198+
199+
return result(null, items, flags);
197200
},
198201
/**
199202
* Read member flags

src/parser/statement.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,12 @@ module.exports = {
5151
return this.read_trait();
5252
case this.tok.T_USE:
5353
return this.read_use_statement();
54-
case this.tok.T_CONST:
55-
return this.next().read_const_list();
54+
case this.tok.T_CONST: {
55+
const result = this.node("constantstatement");
56+
const items = this.next().read_const_list();
57+
this.expectEndOfStatement();
58+
return result(null, items);
59+
}
5660
case this.tok.T_NAMESPACE:
5761
return this.read_namespace();
5862
case this.tok.T_HALT_COMPILER: {
@@ -109,7 +113,6 @@ module.exports = {
109113
",",
110114
false
111115
);
112-
this.expectEndOfStatement();
113116
return result;
114117
},
115118
/**

0 commit comments

Comments
 (0)