-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathif.js
More file actions
116 lines (114 loc) · 3.01 KB
/
if.js
File metadata and controls
116 lines (114 loc) · 3.01 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
* Copyright (C) 2018 Glayzzle (BSD3 License)
* @authors https://github.com/glayzzle/php-parser/graphs/contributors
* @url http://glayzzle.com
*/
"use strict";
module.exports = {
/*
* Reads an IF statement
*
* ```ebnf
* if ::= T_IF '(' expr ')' ':' ...
* ```
*/
read_if() {
const result = this.node("if");
const test = this.next().read_if_expr();
let body;
let alternate = null;
let shortForm = false;
if (this.token === ":") {
shortForm = true;
body = this.node("block");
this.next();
const items = [];
while (this.token !== this.EOF && this.token !== this.tok.T_ENDIF) {
if (this.token === this.tok.T_ELSEIF) {
alternate = this.read_elseif_short();
break;
} else if (this.token === this.tok.T_ELSE) {
alternate = this.read_else_short();
break;
}
items.push(this.read_inner_statement());
}
if (
items.length === 0 &&
this.extractDoc &&
this._docs.length > this._docIndex
) {
items.push(this.node("noop")());
}
body = body(null, items);
this.expect(this.tok.T_ENDIF) && this.next();
this.expectEndOfStatement();
} else {
body = this.read_statement();
if (this.token === this.tok.T_ELSEIF) {
alternate = this.read_if();
} else if (this.token === this.tok.T_ELSE) {
alternate = this.next().read_statement();
}
}
return result(test, body, alternate, shortForm);
},
/*
* reads an if expression : '(' expr ')'
*/
read_if_expr() {
this.expect("(") && this.next();
const result = this.read_expr();
this.expect(")") && this.next();
return result;
},
/*
* reads an elseif (expr): statements
*/
read_elseif_short() {
let alternate = null;
const result = this.node("if");
const test = this.next().read_if_expr();
const body = this.node("block");
if (this.expect(":")) this.next();
const items = [];
while (this.token != this.EOF && this.token !== this.tok.T_ENDIF) {
if (this.token === this.tok.T_ELSEIF) {
alternate = this.read_elseif_short();
break;
} else if (this.token === this.tok.T_ELSE) {
alternate = this.read_else_short();
break;
}
items.push(this.read_inner_statement());
}
if (
items.length === 0 &&
this.extractDoc &&
this._docs.length > this._docIndex
) {
items.push(this.node("noop")());
}
return result(test, body(null, items), alternate, true);
},
/*
*
*/
read_else_short() {
this.next();
const body = this.node("block");
if (this.expect(":")) this.next();
const items = [];
while (this.token != this.EOF && this.token !== this.tok.T_ENDIF) {
items.push(this.read_inner_statement());
}
if (
items.length === 0 &&
this.extractDoc &&
this._docs.length > this._docIndex
) {
items.push(this.node("noop")());
}
return body(null, items);
},
};