Skip to content

Commit

Permalink
feat: improve Declare node and introduce DeclareDirective node
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Nov 12, 2018
1 parent 940e0d0 commit a0338ae
Show file tree
Hide file tree
Showing 11 changed files with 594 additions and 120 deletions.
2 changes: 2 additions & 0 deletions src/ast.js
Expand Up @@ -14,6 +14,7 @@ const Position = require("./ast/position");
* - [Location](#location)
* - [Position](#position)
* - [Node](#node)
* - [DeclareDirective](#declaredirective)
* - [EncapsedPart](#encapsedpart)
* - [Constant](#constant)
* - [Identifier](#identifier)
Expand Down Expand Up @@ -365,6 +366,7 @@ AST.prototype.prepare = function(kind, docs, parser) {
require("./ast/continue"),
require("./ast/declaration"),
require("./ast/declare"),
require("./ast/declaredirective"),
require("./ast/do"),
require("./ast/echo"),
require("./ast/empty"),
Expand Down
6 changes: 3 additions & 3 deletions src/ast/declare.js
Expand Up @@ -12,19 +12,19 @@ const KIND = "declare";
* The declare construct is used to set execution directives for a block of code
* @constructor Declare
* @extends {Block}
* @property {Expression[]} what
* @property {Array[]} directives
* @property {String} mode
* @see http://php.net/manual/en/control-structures.declare.php
*/
const Declare = Block.extends(KIND, function Declare(
what,
directives,
body,
mode,
docs,
location
) {
Block.apply(this, [KIND, body, docs, location]);
this.what = what;
this.directives = directives;
this.mode = mode;
});

Expand Down
27 changes: 27 additions & 0 deletions src/ast/declaredirective.js
@@ -0,0 +1,27 @@
/**
* Copyright (C) 2018 Glayzzle (BSD3 License)
* @authors https://github.com/glayzzle/php-parser/graphs/contributors
* @url http://glayzzle.com
*/
"use strict";

const Node = require("./node");
const KIND = "declaredirective";

/**
* Defines a constant
* @constructor DeclareDirective
* @extends {Node}
* @property {Identifier} name
* @property {Node|string|number|boolean|null} value
*/
module.exports = Node.extends(KIND, function DeclareDirective(
key,
value,
docs,
location
) {
Node.apply(this, [KIND, docs, location]);
this.key = key;
this.value = value;
});
24 changes: 14 additions & 10 deletions src/parser/statement.js
Expand Up @@ -118,20 +118,24 @@ module.exports = {
/**
* Reads a list of constants declaration
* ```ebnf
* declare_list ::= T_STRING '=' expr (',' T_STRING '=' expr)*
* declare_list ::= IDENTIFIER '=' expr (',' IDENTIFIER '=' expr)*
* ```
* @retrurn {Object}
* @retrurn {Array}
*/
read_declare_list: function() {
const result = {};
const result = [];
while (this.token != this.EOF && this.token !== ")") {
this.expect(this.tok.T_STRING);
const name = this.text().toLowerCase();
if (this.next().expect("=")) {
result[name] = this.next().read_expr();
} else {
result[name] = null;
const directive = this.node("declaredirective");
let key = this.node("identifier");
const name = this.text();
this.next();
key = key(name);
let value = null;
if (this.expect("=")) {
value = this.next().read_expr();
}
result.push(directive(key, value));
if (this.token !== ",") break;
this.next();
}
Expand Down Expand Up @@ -299,7 +303,7 @@ module.exports = {
const body = [];
let mode;
this.next().expect("(") && this.next();
const what = this.read_declare_list();
const directives = this.read_declare_list();
this.expect(")") && this.next();
if (this.token === ":") {
this.next();
Expand All @@ -325,7 +329,7 @@ module.exports = {
this.expect(";") && this.next();
mode = this.ast.declare.MODE_NONE;
}
return result(what, body, mode);
return result(directives, body, mode);
}

case this.tok.T_TRY:
Expand Down
71 changes: 52 additions & 19 deletions test/snapshot/__snapshots__/acid.test.js.snap
Expand Up @@ -80,6 +80,58 @@ Program {
},
Declare {
"children": Array [],
"directives": Array [
DeclareDirective {
"key": Identifier {
"kind": "identifier",
"loc": Location {
"end": Position {
"column": 20,
"line": 4,
"offset": 63,
},
"source": "strict_types",
"start": Position {
"column": 8,
"line": 4,
"offset": 51,
},
},
"name": "strict_types",
},
"kind": "declaredirective",
"loc": Location {
"end": Position {
"column": 22,
"line": 4,
"offset": 65,
},
"source": "strict_types=1",
"start": Position {
"column": 8,
"line": 4,
"offset": 51,
},
},
"value": Number {
"kind": "number",
"loc": Location {
"end": Position {
"column": 22,
"line": 4,
"offset": 65,
},
"source": "1",
"start": Position {
"column": 21,
"line": 4,
"offset": 64,
},
},
"value": "1",
},
},
],
"kind": "declare",
"loc": Location {
"end": Position {
Expand All @@ -95,25 +147,6 @@ Program {
},
},
"mode": "none",
"what": Object {
"strict_types": Number {
"kind": "number",
"loc": Location {
"end": Position {
"column": 22,
"line": 4,
"offset": 65,
},
"source": "1",
"start": Position {
"column": 21,
"line": 4,
"offset": 64,
},
},
"value": "1",
},
},
},
ExpressionStatement {
"expression": Include {
Expand Down

0 comments on commit a0338ae

Please sign in to comment.