Skip to content

Commit 1721238

Browse files
committed
implement namespace & use nodes (fix glayzzle#35)
1 parent 2fdd9fa commit 1721238

File tree

13 files changed

+458
-140
lines changed

13 files changed

+458
-140
lines changed

docs/AST.md

Lines changed: 111 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,44 @@
22

33
# AST
44

5+
The AST builder class
6+
7+
**Parameters**
8+
9+
- `withPositions`
10+
- `withSource`
11+
12+
**Properties**
13+
14+
- `withPositions` **[Boolean](#boolean)** Should locate any node (by default false)
15+
- `withSource` **[Boolean](#boolean)** Should extract the node original code (by default false)
16+
17+
## position
18+
19+
Create a position node from specified parser
20+
including it's lexer current state
21+
22+
**Parameters**
23+
24+
- `Parser`
25+
- `parser`
26+
27+
Returns **[Position](#position)**
28+
29+
## prepare
30+
31+
Prepares an AST node
32+
33+
**Parameters**
34+
35+
- `kind` **([String](#string) | null)** Defines the node type
36+
(if null, the kind must be passed at the function call)
37+
- `parser` **Parser** The parser instance (use for extracting locations)
38+
39+
Returns **[Function](#function)**
40+
41+
# AST
42+
543
## Class hierarchy
644

745
- [Location](#location)
@@ -21,6 +59,7 @@
2159
- [Variable](#variable)
2260
- [ConstRef](#constref)
2361
- [Operation](#operation)
62+
- [Coalesce](#coalesce)
2463
- [Post](#post)
2564
- [Literal](#literal)
2665
- [Boolean](#boolean)
@@ -33,7 +72,6 @@
3372
- [Eval](#eval)
3473
- [Exit](#exit)
3574
- [Clone](#clone)
36-
- [Coalesce](#coalesce)
3775
- [Include](#include)
3876
- [Assign](#assign)
3977
- [If](#if)
@@ -46,6 +84,8 @@
4684
- [Try](#try)
4785
- [Catch](#catch)
4886
- [Call](#call)
87+
- [UseGroup](#usegroup)
88+
- [UseItem](#useitem)
4989
- [Block](#block)
5090
- [Program](#program)
5191
- [Namespace](#namespace)
@@ -74,31 +114,17 @@
74114
- `withPositions`
75115
- `withSource`
76116

77-
## prepare
117+
## position
78118

79-
Prepares an AST node
119+
Create a position node from specified parser
120+
including it's lexer current state
80121

81122
**Parameters**
82123

83-
- `kind` **([String](#string) | null)** Defines the node type
84-
(if null, the kind must be passed at the function call)
85-
- `parser` **Parser** The parser instance (use for extracting locations)
86-
87-
Returns **[Function](#function)**
124+
- `Parser`
125+
- `parser`
88126

89-
# AST
90-
91-
The AST builder class
92-
93-
**Parameters**
94-
95-
- `withPositions`
96-
- `withSource`
97-
98-
**Properties**
99-
100-
- `withPositions` **[Boolean](#boolean)** Should locate any node (by default false)
101-
- `withSource` **[Boolean](#boolean)** Should extract the node original code (by default false)
127+
Returns **[Position](#position)**
102128

103129
## prepare
104130

@@ -240,7 +266,7 @@ Defines a clone call
240266

241267
# Coalesce
242268

243-
**Extends Statement**
269+
**Extends Operation**
244270

245271
Verify is the test property is defined and is not null, and returns
246272
is, otherwise returns the ifnull expression.
@@ -441,7 +467,33 @@ Defines an identifier node
441467
**Properties**
442468

443469
- `name` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)**
444-
- `fqn` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)**
470+
- `resolution` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)**
471+
472+
## UNQUALIFIED_NAME
473+
474+
This is an identifier without a namespace separator, such as Foo
475+
476+
Type: [String](#string)
477+
478+
## QUALIFIED_NAME
479+
480+
This is an identifier with a namespace separator, such as Foo\\Bar
481+
482+
Type: [String](#string)
483+
484+
## FULL_QUALIFIED_NAME
485+
486+
This is an identifier with a namespace separator that begins with
487+
a namespace separator, such as \\Foo\\Bar. The namespace \\Foo is also
488+
a fully qualified name.
489+
490+
Type: [String](#string)
491+
492+
## RELATIVE_NAME
493+
494+
This is an identifier starting with namespace, such as namespace\\Foo\\Bar.
495+
496+
Type: [String](#string)
445497

446498
# If
447499

@@ -776,6 +828,42 @@ Defines a trait usage
776828

777829
Deletes references to a list of variables
778830

831+
# UseGroup
832+
833+
**Extends Statement**
834+
835+
Defines a use statement (with a list of use items)
836+
837+
**Properties**
838+
839+
- `name` **([Identifier](#identifier) | null)**
840+
- `type` **([String](#string) | null)** Possible value : function, const
841+
- `item` **[Array](#array)<[UseItem](#useitem)>**
842+
843+
# UseItem
844+
845+
**Extends Statement**
846+
847+
Defines a use statement (from namespace)
848+
849+
**Properties**
850+
851+
- `name` **[Identifier](#identifier)**
852+
- `type` **([String](#string) | null)** Possible value : function, const
853+
- `alias` **([String](#string) | null)**
854+
855+
## TYPE_CONST
856+
857+
Importing a constant
858+
859+
Type: [String](#string)
860+
861+
## TYPE_FUNC
862+
863+
Importing a function
864+
865+
Type: [String](#string)
866+
779867
# Variable
780868

781869
**Extends Expression**

docs/parser.md

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -443,51 +443,81 @@ start ::= (namespace | top_statement)*
443443

444444
# read_namespace
445445

446+
Reads a namespace declaration block
447+
446448
```ebnf
447449
namespace ::= T_NAMESPACE namespace_name? '{'
448450
top_statements
449451
'}'
450452
| T_NAMESPACE namespace_name ';' top_statements
451453
```
452454

455+
Returns **Namespace**
456+
453457
# read_namespace_name
454458

455-
reading a namespace name
459+
Reads a namespace name
456460

457461
```ebnf
458462
namespace_name ::= T_NS_SEPARATOR? (T_STRING T_NS_SEPARATOR)* T_STRING
459463
```
460464

461-
# read_use_statements
465+
Returns **Identifier**
466+
467+
# read_use_statement
468+
469+
Reads a use statement
462470

463471
```ebnf
464-
use_statements ::=
465-
use_statements ',' use_statement
466-
| use_statement
472+
use_statement ::= T_USE
473+
use_type? use_declarations |
474+
use_type use_statement '{' use_declarations '}' |
475+
use_statement '{' use_declarations(=>typed) '}'
476+
';'
467477
```
468478

469-
# read_inline_use_declaration
479+
Returns **UseGroup**
480+
481+
# read_use_declaration
482+
483+
Reads a use declaration
470484

471485
```ebnf
472-
inline_use_declaration ::= ...
486+
use_declaration ::= use_type? namespace_name use_alias
473487
```
474488

475-
# read_use_statement_mixed
489+
Returns **UseItem**
490+
491+
# read_use_declarations
492+
493+
Reads a list of use declarations
476494

477495
```ebnf
478-
use_statement_mixed ::=
479-
use_statement (T_AS T_STRING | '{' read_inline_use_declaration '}' )
480-
(',' read_use_statement)*
496+
use_declarations ::= use_declaration (',' use_declaration)*
481497
```
482498

483-
# read_use_statement
499+
Returns **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<UseItem>**
500+
501+
# read_use_alias
502+
503+
Reads a use statement
484504

485505
```ebnf
486-
use_statement ::= (
487-
(T_FUNCTION | T_CONST)? namespace_name
488-
)
506+
use_alias ::= (T_AS T_STRING)?
507+
```
508+
509+
Returns **([String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) | null)**
510+
511+
# read_use_type
512+
513+
Reads the namespace type declaration
514+
515+
```ebnf
516+
use_type ::= (T_FUNCTION | T_CONST)?
489517
```
490518

519+
Returns **([String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) | null)** Possible values : function, const
520+
491521
# resolve_special_chars
492522

493523
Unescape special chars

src/ast.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var Position = require('./ast/position');
2727
* - [Variable](#variable)
2828
* - [ConstRef](#constref)
2929
* - [Operation](#operation)
30+
* - [Coalesce](#coalesce)
3031
* - [Post](#post)
3132
* - [Literal](#literal)
3233
* - [Boolean](#boolean)
@@ -39,7 +40,6 @@ var Position = require('./ast/position');
3940
* - [Eval](#eval)
4041
* - [Exit](#exit)
4142
* - [Clone](#clone)
42-
* - [Coalesce](#coalesce)
4343
* - [Include](#include)
4444
* - [Assign](#assign)
4545
* - [If](#if)
@@ -52,6 +52,8 @@ var Position = require('./ast/position');
5252
* - [Try](#try)
5353
* - [Catch](#catch)
5454
* - [Call](#call)
55+
* - [UseGroup](#usegroup)
56+
* - [UseItem](#useitem)
5557
* - [Block](#block)
5658
* - [Program](#program)
5759
* - [Namespace](#namespace)
@@ -86,6 +88,20 @@ var AST = function(withPositions, withSource) {
8688
this.withSource = withSource;
8789
};
8890

91+
/**
92+
* Create a position node from specified parser
93+
* including it's lexer current state
94+
* @param {Parser}
95+
* @return {Position}
96+
*/
97+
AST.prototype.position = function(parser) {
98+
return new Position(
99+
parser.lexer.yylloc.first_line,
100+
parser.lexer.yylloc.first_column,
101+
parser.lexer.yylloc.first_offset
102+
);
103+
};
104+
89105
/**
90106
* Prepares an AST node
91107
* @param {String|null} kind - Defines the node type
@@ -96,11 +112,7 @@ var AST = function(withPositions, withSource) {
96112
AST.prototype.prepare = function(kind, parser) {
97113
var start = null;
98114
if (this.withPositions || this.withSource) {
99-
start = new Position(
100-
parser.lexer.yylloc.first_line,
101-
parser.lexer.yylloc.first_column,
102-
parser.lexer.yylloc.first_offset
103-
);
115+
start = this.position(parser);
104116
}
105117
var self = this;
106118
// returns the node
@@ -199,6 +211,8 @@ AST.prototype.prepare = function(kind, parser) {
199211
require('./ast/traituse'),
200212
require('./ast/try'),
201213
require('./ast/unset'),
214+
require('./ast/usegroup'),
215+
require('./ast/useitem'),
202216
require('./ast/variable'),
203217
require('./ast/while')
204218
].forEach(function (ctor) {

src/ast/coalesce.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@
44
* @url http://glayzzle.com
55
*/
66

7-
var Statement = require('./statement');
7+
var Operation = require('./operation');
88
var KIND = 'coalesce';
99

1010
/**
1111
* Verify is the test property is defined and is not null, and returns
1212
* is, otherwise returns the ifnull expression.
1313
* @constructor Coalesce
14-
* @extends {Statement}
14+
* @extends {Operation}
1515
* @property {Expression} test - The expression to be testes
1616
* @property {Expression} ifnull - The returned expression if test is null
1717
* @see https://wiki.php.net/rfc/isset_ternary
1818
*/
19-
var Coalesce = Statement.extends(function Coalesce(test, ifnull, location) {
20-
Statement.apply(this, [KIND, location]);
19+
var Coalesce = Operation.extends(function Coalesce(test, ifnull, location) {
20+
Operation.apply(this, [KIND, location]);
2121
this.test = test;
2222
this.ifnull = ifnull;
2323
});

0 commit comments

Comments
 (0)