forked from glayzzle/php-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathif.js
More file actions
98 lines (96 loc) · 2.64 KB
/
if.js
File metadata and controls
98 lines (96 loc) · 2.64 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
/**
* 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: function() {
const result = this.node("if");
let body = null;
let alternate = null;
let shortForm = false;
let test = null;
test = this.next().read_if_expr();
if (this.token === ":") {
shortForm = true;
this.next();
body = this.node("block");
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());
}
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: function() {
this.expect("(") && this.next();
const result = this.read_expr();
this.expect(")") && this.next();
return result;
},
/**
* reads an elseif (expr): statements
*/
read_elseif_short: function() {
const result = this.node("if");
let alternate = null;
let test = null;
let body = null;
const items = [];
test = this.next().read_if_expr();
if (this.expect(":")) this.next();
body = this.node("block");
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());
}
body = body(null, items);
return result(test, body, alternate, true);
},
/**
*
*/
read_else_short: function() {
const body = this.node("block");
if (this.next().expect(":")) this.next();
const items = [];
while (this.token != this.EOF && this.token !== this.tok.T_ENDIF) {
items.push(this.read_inner_statement());
}
return body(null, items);
}
};