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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use crate::*;
#[tracable_parser]
#[packrat_parser]
pub(crate) fn loop_statement(s: Span) -> IResult<Span, LoopStatement> {
alt((
loop_statement_forever,
loop_statement_repeat,
loop_statement_while,
loop_statement_for,
loop_statement_do_while,
loop_statement_foreach,
))(s)
}
#[tracable_parser]
#[packrat_parser]
pub(crate) fn loop_statement_forever(s: Span) -> IResult<Span, LoopStatement> {
let (s, a) = keyword("forever")(s)?;
let (s, b) = statement_or_null(s)?;
Ok((
s,
LoopStatement::Forever(Box::new(LoopStatementForever { nodes: (a, b) })),
))
}
#[tracable_parser]
#[packrat_parser]
pub(crate) fn loop_statement_repeat(s: Span) -> IResult<Span, LoopStatement> {
let (s, a) = keyword("repeat")(s)?;
let (s, b) = paren(expression)(s)?;
let (s, c) = statement_or_null(s)?;
Ok((
s,
LoopStatement::Repeat(Box::new(LoopStatementRepeat { nodes: (a, b, c) })),
))
}
#[tracable_parser]
#[packrat_parser]
pub(crate) fn loop_statement_while(s: Span) -> IResult<Span, LoopStatement> {
let (s, a) = keyword("while")(s)?;
let (s, b) = paren(expression)(s)?;
let (s, c) = statement_or_null(s)?;
Ok((
s,
LoopStatement::While(Box::new(LoopStatementWhile { nodes: (a, b, c) })),
))
}
#[tracable_parser]
#[packrat_parser]
pub(crate) fn loop_statement_for(s: Span) -> IResult<Span, LoopStatement> {
let (s, a) = keyword("for")(s)?;
let (s, b) = paren(tuple((
opt(for_initialization),
symbol(";"),
opt(expression),
symbol(";"),
opt(for_step),
)))(s)?;
let (s, c) = statement_or_null(s)?;
Ok((
s,
LoopStatement::For(Box::new(LoopStatementFor { nodes: (a, b, c) })),
))
}
#[tracable_parser]
#[packrat_parser]
pub(crate) fn loop_statement_do_while(s: Span) -> IResult<Span, LoopStatement> {
let (s, a) = keyword("do")(s)?;
let (s, b) = statement_or_null(s)?;
let (s, c) = keyword("while")(s)?;
let (s, d) = paren(expression)(s)?;
let (s, e) = symbol(";")(s)?;
Ok((
s,
LoopStatement::DoWhile(Box::new(LoopStatementDoWhile {
nodes: (a, b, c, d, e),
})),
))
}
#[tracable_parser]
#[packrat_parser]
pub(crate) fn loop_statement_foreach(s: Span) -> IResult<Span, LoopStatement> {
let (s, a) = keyword("foreach")(s)?;
let (s, b) = paren(pair(
ps_or_hierarchical_array_identifier,
bracket(loop_variables),
))(s)?;
let (s, c) = statement(s)?;
Ok((
s,
LoopStatement::Foreach(Box::new(LoopStatementForeach { nodes: (a, b, c) })),
))
}
#[tracable_parser]
#[packrat_parser]
pub(crate) fn for_initialization(s: Span) -> IResult<Span, ForInitialization> {
alt((
map(list_of_variable_assignments, |x| {
ForInitialization::ListOfVariableAssignments(Box::new(x))
}),
for_initialization_declaration,
))(s)
}
#[recursive_parser]
#[tracable_parser]
#[packrat_parser]
pub(crate) fn for_initialization_declaration(s: Span) -> IResult<Span, ForInitialization> {
let (s, a) = list(symbol(","), for_variable_declaration)(s)?;
Ok((
s,
ForInitialization::Declaration(Box::new(ForInitializationDeclaration { nodes: (a,) })),
))
}
#[tracable_parser]
#[packrat_parser]
pub(crate) fn for_variable_declaration(s: Span) -> IResult<Span, ForVariableDeclaration> {
let (s, a) = opt(var)(s)?;
let (s, b) = data_type(s)?;
let (s, c) = list(
symbol(","),
triple(variable_identifier, symbol("="), expression),
)(s)?;
Ok((s, ForVariableDeclaration { nodes: (a, b, c) }))
}
#[tracable_parser]
#[packrat_parser]
pub(crate) fn var(s: Span) -> IResult<Span, Var> {
let (s, a) = keyword("var")(s)?;
Ok((s, Var { nodes: (a,) }))
}
#[recursive_parser]
#[tracable_parser]
#[packrat_parser]
pub(crate) fn for_step(s: Span) -> IResult<Span, ForStep> {
let (s, a) = list(symbol(","), for_step_assignment)(s)?;
Ok((s, ForStep { nodes: (a,) }))
}
#[tracable_parser]
#[packrat_parser]
pub(crate) fn for_step_assignment(s: Span) -> IResult<Span, ForStepAssignment> {
alt((
map(operator_assignment, |x| {
ForStepAssignment::OperatorAssignment(Box::new(x))
}),
map(inc_or_dec_expression, |x| {
ForStepAssignment::IncOrDecExpression(Box::new(x))
}),
map(function_subroutine_call, |x| {
ForStepAssignment::FunctionSubroutineCall(Box::new(x))
}),
))(s)
}
#[tracable_parser]
#[packrat_parser]
pub(crate) fn loop_variables(s: Span) -> IResult<Span, LoopVariables> {
let (s, a) = list(symbol(","), opt(index_variable_identifier))(s)?;
Ok((s, LoopVariables { nodes: (a,) }))
}