/*!
* Defines a list of helper functions for parsing
* Copyright (C) 2017 Glayzzle (BSD3 License)
* @authors https://github.com/glayzzle/php-parser/graphs/contributors
* @url http://glayzzle.com
*/
"use strict";
module.exports = {
/**
* Reads a short form of tokens
* @param {Number} token - The ending token
* @return {Block}
*/
read_short_form: function(token) {
var body = this.node('block'), items = [];
Eif (this.expect(':')) this.next();
while(this.token != this.EOF && this.token !== token) {
items.push(this.read_inner_statement());
}
Eif (this.expect(token)) this.next();
this.expectEndOfStatement();
return body(null, items);
}
/**
* Helper : reads a list of tokens / sample : T_STRING ',' T_STRING ...
* ```ebnf
* list ::= separator? ( item separator )* item
* ```
*/
,read_list: function(item, separator, preserveFirstSeparator) {
var result = [];
if (this.token == separator) {
Eif (preserveFirstSeparator) result.push('');
this.next();
}
if (typeof (item) === "function") {
do {
result.push(item.apply(this, []));
if (this.token != separator) {
break;
}
} while(this.next().token != this.EOF);
} else {
if (this.expect(item)) {
result.push(this.text());
} else {
return [];
}
while (this.next().token != this.EOF) {
if (this.token != separator) break;
// trim current separator & check item
Iif (this.next().token != item) break;
result.push(this.text());
}
}
return result;
}
/**
* Reads a list of names separated by a comma
*
* ```ebnf
* name_list ::= namespace (',' namespace)*
* ```
*
* Sample code :
* ```php
* <?php class foo extends bar, baz { }
* ```
*
* @see https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L726
* @return {Identifier[]}
*/
,read_name_list: function() {
return this.read_list(
this.read_namespace_name, ',', false
);
}
/**
* Reads a list of variables declarations
*
* ```ebnf
* variable_declaration ::= T_VARIABLE ('=' expr)?*
* variable_declarations ::= variable_declaration (',' variable_declaration)*
* ```
*
* Sample code :
* ```php
* <?php class foo extends bar, baz { }
* ```
* @return {Variable[]|Assign[]} Returns an array composed by a list of variables, or
* assign values
*/
,read_variable_declarations: function() {
return this.read_list(function() {
var node = this.node('assign'),
value = null,
variable = this.node('variable');
// plain variable name
Eif (this.expect(this.tok.T_VARIABLE)) {
var name = this.text().substring(1);
this.next();
variable = variable(name, false);
} else {
variable = variable('#ERR', false);
}
if (this.token === '=') {
return node(variable, this.next().read_expr());
} else {
return variable;
}
}, ',');
}
};
|