Skip to content

Commit 658f396

Browse files
committed
handle optional level argument on break & continue statements
1 parent 9ad99b6 commit 658f396

File tree

7 files changed

+66
-50
lines changed

7 files changed

+66
-50
lines changed

docs/AST.md

Lines changed: 46 additions & 38 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)
@@ -117,44 +155,6 @@ Prepares an AST node
117155

118156
Returns **[Function](#function)**
119157

120-
# AST
121-
122-
The AST builder class
123-
124-
**Parameters**
125-
126-
- `withPositions`
127-
- `withSource`
128-
129-
**Properties**
130-
131-
- `withPositions` **[Boolean](#boolean)** Should locate any node (by default false)
132-
- `withSource` **[Boolean](#boolean)** Should extract the node original code (by default false)
133-
134-
## position
135-
136-
Create a position node from specified parser
137-
including it's lexer current state
138-
139-
**Parameters**
140-
141-
- `Parser`
142-
- `parser`
143-
144-
Returns **[Position](#position)**
145-
146-
## prepare
147-
148-
Prepares an AST node
149-
150-
**Parameters**
151-
152-
- `kind` **([String](#string) | null)** Defines the node type
153-
(if null, the kind must be passed at the function call)
154-
- `parser` **Parser** The parser instance (use for extracting locations)
155-
156-
Returns **[Function](#function)**
157-
158158
# Array
159159

160160
**Extends Expression**
@@ -224,6 +224,10 @@ Defines a boolean value (true/false)
224224

225225
A break statement
226226

227+
**Properties**
228+
229+
- `level` **([Number](#number) \| [Null](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null))**
230+
227231
# Call
228232

229233
**Extends Statement**
@@ -368,6 +372,10 @@ A constant reference
368372

369373
A continue statement
370374

375+
**Properties**
376+
377+
- `level` **([Number](#number) \| [Null](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null))**
378+
371379
# Declaration
372380

373381
**Extends Statement**

src/ast/break.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ var KIND = 'break';
1111
* A break statement
1212
* @constructor Break
1313
* @extends {Node}
14+
* @property {Number|Null} level
1415
*/
15-
var Break = Node.extends(function Break(location) {
16+
var Break = Node.extends(function Break(level, location) {
1617
Node.apply(this, [KIND, location]);
18+
this.level = level;
1719
});
1820

1921
module.exports = Break;

src/ast/continue.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ var KIND = 'continue';
1111
* A continue statement
1212
* @constructor Continue
1313
* @extends {Node}
14+
* @property {Number|Null} level
1415
*/
15-
var Continue = Node.extends(function Continue(location) {
16+
var Continue = Node.extends(function Continue(level, location) {
1617
Node.apply(this, [KIND, location]);
18+
this.level = level;
1719
});
1820

1921
module.exports = Continue;

src/parser/scalar.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ module.exports = {
9090
this.next().expect([this.tok.T_LNUMBER, this.tok.T_DNUMBER]);
9191
value += this.text();
9292
}
93-
result = result(value);
9493
this.next();
94+
result = result(value);
9595
return result;
9696

9797
// CONSTANTS

src/parser/statement.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,14 +203,18 @@ module.exports = {
203203
return result(expr);
204204

205205
case this.tok.T_BREAK:
206-
var result = this.node('break');
207-
this.next().expectEndOfStatement();
208-
return result();
209-
210206
case this.tok.T_CONTINUE:
211-
var result = this.node('continue');
212-
this.next().expectEndOfStatement();
213-
return result();
207+
var result = this.node(
208+
this.token === this.tok.T_CONTINUE ? 'continue' : 'break'
209+
), level = null;
210+
if(this.next().token === this.tok.T_LNUMBER) {
211+
level = this.node('number');
212+
var value = this.text();
213+
this.next();
214+
level = level(value);
215+
}
216+
this.expectEndOfStatement();
217+
return result(level);
214218

215219
case this.tok.T_GLOBAL:
216220
var result = this.node('global');

test/loopTests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ describe('Test loops statements (for, while)', function() {
5454
'echo $i;',
5555
'for(;;):',
5656
'echo $ok;',
57-
'continue;;',
57+
'continue 5;;',
5858
'endfor;'
5959
].join('\n'), {
6060
parser: { debug: false }

test/switchTests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ describe('Test SWITCH statements', function() {
77
' case 1:',
88
' case $var:',
99
' $foo = false;',
10-
' break;',
10+
' break 10;',
1111
' case FOO:',
1212
' $foo = true;',
1313
' break;',

0 commit comments

Comments
 (0)