forked from glayzzle/php-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswitch.js
More file actions
94 lines (93 loc) · 2.71 KB
/
switch.js
File metadata and controls
94 lines (93 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*!
* 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 switch statement
* ```ebnf
* switch ::= T_SWITCH '(' expr ')' switch_case_list
* ```
* @return {Switch}
* @see http://php.net/manual/en/control-structures.switch.php
*/
read_switch: function() {
this.expect(this.tok.T_SWITCH) && this.next();
var result = this.node('switch'), test, body, shortForm;
this.expect('(') && this.next();
test = this.read_expr();
this.expect(')') && this.next();
shortForm = (this.token === ':');
body = this.read_switch_case_list();
return result(test, body, shortForm);
}
/**
* ```ebnf
* switch_case_list ::= '{' ';'? case_list* '}' | ':' ';'? case_list* T_ENDSWITCH ';'
* ```
* @see https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L566
*/
,read_switch_case_list: function() {
// DETECT SWITCH MODE
var expect = null,
result = this.node('block'),
items = [];
if (this.token === '{') {
expect = '}';
} else if (this.token === ':') {
expect = this.tok.T_ENDSWITCH;
} else {
this.expect(['{', ':']);
}
// OPTIONNAL ';'
// https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L570
if (this.next().token === ';') {
this.next();
}
// IGNORE THE CLOSE TAG TOKEN WITH SHORT MODE
if (this.token === this.tok.T_CLOSE_TAG) {
this.next();
}
// EXTRACTING CASES
while(this.token !== this.EOF && this.token !== expect) {
items.push( this.read_case_list(expect) );
}
// CHECK END TOKEN
this.expect(expect) && this.next();
if (expect === this.tok.T_ENDSWITCH) {
this.expectEndOfStatement();
}
return result(null, items);
}
/**
* ```ebnf
* case_list ::= ((T_CASE expr) | T_DEFAULT) (':' | ';') inner_statement*
* ```
*/
,read_case_list: function(stopToken) {
var result = this.node('case'), test = null, body = null, items = [];
if (this.token === this.tok.T_CASE) {
test = this.next().read_expr();
} else if (this.token === this.tok.T_DEFAULT) {
// the defaut entry - no condition
this.next();
} else {
this.expect([this.tok.T_CASE, this.tok.T_DEFAULT]);
}
this.expect([':', ';']) && this.next();
body = this.node('block');
while(
this.token != this.EOF
&& this.token !== stopToken
&& this.token !== this.tok.T_CASE
&& this.token !== this.tok.T_DEFAULT
) {
items.push(this.read_inner_statement());
}
return result(
test, items.length > 0 ? body(null, items) : null
);
}
};