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

Commit 5b205cd

Browse files
authored
Merge pull request glayzzle#242 from glayzzle/feat-declare-and-declare-directive
feat: improve `Declare` node and introduce `DeclareDirective` node
2 parents d6886c4 + a0338ae commit 5b205cd

11 files changed

+594
-120
lines changed

src/ast.js

Lines changed: 2 additions & 0 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+
* - [DeclareDirective](#declaredirective)
1718
* - [EncapsedPart](#encapsedpart)
1819
* - [Constant](#constant)
1920
* - [Identifier](#identifier)
@@ -365,6 +366,7 @@ AST.prototype.prepare = function(kind, docs, parser) {
365366
require("./ast/continue"),
366367
require("./ast/declaration"),
367368
require("./ast/declare"),
369+
require("./ast/declaredirective"),
368370
require("./ast/do"),
369371
require("./ast/echo"),
370372
require("./ast/empty"),

src/ast/declare.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@ const KIND = "declare";
1212
* The declare construct is used to set execution directives for a block of code
1313
* @constructor Declare
1414
* @extends {Block}
15-
* @property {Expression[]} what
15+
* @property {Array[]} directives
1616
* @property {String} mode
1717
* @see http://php.net/manual/en/control-structures.declare.php
1818
*/
1919
const Declare = Block.extends(KIND, function Declare(
20-
what,
20+
directives,
2121
body,
2222
mode,
2323
docs,
2424
location
2525
) {
2626
Block.apply(this, [KIND, body, docs, location]);
27-
this.what = what;
27+
this.directives = directives;
2828
this.mode = mode;
2929
});
3030

src/ast/declaredirective.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 Node = require("./node");
9+
const KIND = "declaredirective";
10+
11+
/**
12+
* Defines a constant
13+
* @constructor DeclareDirective
14+
* @extends {Node}
15+
* @property {Identifier} name
16+
* @property {Node|string|number|boolean|null} value
17+
*/
18+
module.exports = Node.extends(KIND, function DeclareDirective(
19+
key,
20+
value,
21+
docs,
22+
location
23+
) {
24+
Node.apply(this, [KIND, docs, location]);
25+
this.key = key;
26+
this.value = value;
27+
});

src/parser/statement.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,20 +118,24 @@ module.exports = {
118118
/**
119119
* Reads a list of constants declaration
120120
* ```ebnf
121-
* declare_list ::= T_STRING '=' expr (',' T_STRING '=' expr)*
121+
* declare_list ::= IDENTIFIER '=' expr (',' IDENTIFIER '=' expr)*
122122
* ```
123-
* @retrurn {Object}
123+
* @retrurn {Array}
124124
*/
125125
read_declare_list: function() {
126-
const result = {};
126+
const result = [];
127127
while (this.token != this.EOF && this.token !== ")") {
128128
this.expect(this.tok.T_STRING);
129-
const name = this.text().toLowerCase();
130-
if (this.next().expect("=")) {
131-
result[name] = this.next().read_expr();
132-
} else {
133-
result[name] = null;
129+
const directive = this.node("declaredirective");
130+
let key = this.node("identifier");
131+
const name = this.text();
132+
this.next();
133+
key = key(name);
134+
let value = null;
135+
if (this.expect("=")) {
136+
value = this.next().read_expr();
134137
}
138+
result.push(directive(key, value));
135139
if (this.token !== ",") break;
136140
this.next();
137141
}
@@ -299,7 +303,7 @@ module.exports = {
299303
const body = [];
300304
let mode;
301305
this.next().expect("(") && this.next();
302-
const what = this.read_declare_list();
306+
const directives = this.read_declare_list();
303307
this.expect(")") && this.next();
304308
if (this.token === ":") {
305309
this.next();
@@ -325,7 +329,7 @@ module.exports = {
325329
this.expect(";") && this.next();
326330
mode = this.ast.declare.MODE_NONE;
327331
}
328-
return result(what, body, mode);
332+
return result(directives, body, mode);
329333
}
330334

331335
case this.tok.T_TRY:

test/snapshot/__snapshots__/acid.test.js.snap

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,58 @@ Program {
8080
},
8181
Declare {
8282
"children": Array [],
83+
"directives": Array [
84+
DeclareDirective {
85+
"key": Identifier {
86+
"kind": "identifier",
87+
"loc": Location {
88+
"end": Position {
89+
"column": 20,
90+
"line": 4,
91+
"offset": 63,
92+
},
93+
"source": "strict_types",
94+
"start": Position {
95+
"column": 8,
96+
"line": 4,
97+
"offset": 51,
98+
},
99+
},
100+
"name": "strict_types",
101+
},
102+
"kind": "declaredirective",
103+
"loc": Location {
104+
"end": Position {
105+
"column": 22,
106+
"line": 4,
107+
"offset": 65,
108+
},
109+
"source": "strict_types=1",
110+
"start": Position {
111+
"column": 8,
112+
"line": 4,
113+
"offset": 51,
114+
},
115+
},
116+
"value": Number {
117+
"kind": "number",
118+
"loc": Location {
119+
"end": Position {
120+
"column": 22,
121+
"line": 4,
122+
"offset": 65,
123+
},
124+
"source": "1",
125+
"start": Position {
126+
"column": 21,
127+
"line": 4,
128+
"offset": 64,
129+
},
130+
},
131+
"value": "1",
132+
},
133+
},
134+
],
83135
"kind": "declare",
84136
"loc": Location {
85137
"end": Position {
@@ -95,25 +147,6 @@ Program {
95147
},
96148
},
97149
"mode": "none",
98-
"what": Object {
99-
"strict_types": Number {
100-
"kind": "number",
101-
"loc": Location {
102-
"end": Position {
103-
"column": 22,
104-
"line": 4,
105-
"offset": 65,
106-
},
107-
"source": "1",
108-
"start": Position {
109-
"column": 21,
110-
"line": 4,
111-
"offset": 64,
112-
},
113-
},
114-
"value": "1",
115-
},
116-
},
117150
},
118151
ExpressionStatement {
119152
"expression": Include {

0 commit comments

Comments
 (0)