-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathcontrol_parser.rs
228 lines (194 loc) · 7.55 KB
/
control_parser.rs
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
use plc_ast::{
ast::{AstFactory, AstNode, AstStatement},
control_statements::{CaseStatement, ConditionalBlock, ForLoopStatement, IfStatement, LoopStatement},
};
use plc_diagnostics::diagnostics::Diagnostic;
// Copyright (c) 2020 Ghaith Hachem and Mathias Rieder
use crate::{
expect_token,
lexer::Token::*,
parser::{parse_any_in_region, parse_body_in_region},
};
use super::ParseSession;
use super::{parse_expression, parse_reference, parse_statement};
pub fn parse_control_statement(lexer: &mut ParseSession) -> AstNode {
match lexer.token {
KeywordIf => parse_if_statement(lexer),
KeywordFor => parse_for_statement(lexer),
KeywordWhile => parse_while_statement(lexer),
KeywordRepeat => parse_repeat_statement(lexer),
KeywordCase => parse_case_statement(lexer),
KeywordReturn => parse_return_statement(lexer),
KeywordContinue => parse_continue_statement(lexer),
KeywordExit => parse_exit_statement(lexer),
_ => parse_statement(lexer),
}
}
fn parse_return_statement(lexer: &mut ParseSession) -> AstNode {
let location = lexer.location();
lexer.advance();
AstFactory::create_return_statement(None, location, lexer.next_id())
}
fn parse_exit_statement(lexer: &mut ParseSession) -> AstNode {
let location = lexer.location();
lexer.advance();
AstFactory::create_exit_statement(location, lexer.next_id())
}
fn parse_continue_statement(lexer: &mut ParseSession) -> AstNode {
let location = lexer.location();
lexer.advance();
AstFactory::create_continue_statement(location, lexer.next_id())
}
fn parse_if_statement(lexer: &mut ParseSession) -> AstNode {
let start = lexer.range().start;
lexer.advance(); //If
let mut conditional_blocks = vec![];
while lexer.last_token == KeywordElseIf || lexer.last_token == KeywordIf {
let condition = parse_expression(lexer);
expect_token!(
lexer,
KeywordThen,
AstFactory::create_empty_statement(lexer.location(), lexer.next_id())
);
lexer.advance();
let condition_block = ConditionalBlock {
condition: Box::new(condition),
body: parse_body_in_region(lexer, vec![KeywordEndIf, KeywordElseIf, KeywordElse]),
};
conditional_blocks.push(condition_block);
}
let mut else_block = Vec::new();
if lexer.last_token == KeywordElse {
else_block.append(&mut parse_body_in_region(lexer, vec![KeywordEndIf]));
}
let end = lexer.last_range.end;
let stmt = IfStatement { blocks: conditional_blocks, else_block, end_location: lexer.last_location() };
AstFactory::create_if_statement(
stmt,
lexer.source_range_factory.create_range(start..end),
lexer.next_id(),
)
}
fn parse_for_statement(lexer: &mut ParseSession) -> AstNode {
let start = lexer.range().start;
lexer.advance(); // FOR
let counter_expression = parse_reference(lexer);
expect_token!(
lexer,
KeywordAssignment,
AstFactory::create_empty_statement(lexer.location(), lexer.next_id())
);
lexer.advance();
let start_expression = parse_expression(lexer);
expect_token!(lexer, KeywordTo, AstFactory::create_empty_statement(lexer.location(), lexer.next_id()));
lexer.advance();
let end_expression = parse_expression(lexer);
let step = if lexer.token == KeywordBy {
lexer.advance(); // BY
Some(parse_expression(lexer))
} else {
None
};
lexer.try_consume_or_report(KeywordDo); // DO
let stmt = ForLoopStatement {
counter: Box::new(counter_expression),
start: Box::new(start_expression),
end: Box::new(end_expression),
by_step: step.map(Box::new),
body: parse_body_in_region(lexer, vec![KeywordEndFor]),
end_location: lexer.last_location(),
};
AstFactory::create_for_loop(
stmt,
lexer.source_range_factory.create_range(start..lexer.last_range.end),
lexer.next_id(),
)
}
fn parse_while_statement(lexer: &mut ParseSession) -> AstNode {
let start = lexer.range().start;
lexer.advance(); //WHILE
let condition = parse_expression(lexer);
lexer.try_consume_or_report(KeywordDo);
let stmt = LoopStatement {
condition: Box::new(condition),
body: parse_body_in_region(lexer, vec![KeywordEndWhile]),
end_location: lexer.last_location(),
};
AstFactory::create_while_statement(
stmt,
lexer.source_range_factory.create_range(start..lexer.last_range.end),
lexer.next_id(),
)
}
fn parse_repeat_statement(lexer: &mut ParseSession) -> AstNode {
let start = lexer.range().start;
lexer.advance(); //REPEAT
let body = parse_body_in_region(lexer, vec![KeywordUntil, KeywordEndRepeat]); //UNTIL
let condition = if lexer.last_token == KeywordUntil {
parse_any_in_region(lexer, vec![KeywordEndRepeat], parse_expression)
} else {
AstFactory::create_empty_statement(lexer.location(), lexer.next_id())
};
let stmt = LoopStatement { condition: Box::new(condition), body, end_location: lexer.last_location() };
AstFactory::create_repeat_statement(
stmt,
lexer.source_range_factory.create_range(start..lexer.last_range.end),
lexer.next_id(),
)
}
fn parse_case_statement(lexer: &mut ParseSession) -> AstNode {
let start = lexer.range().start;
lexer.advance(); // CASE
let selector = parse_expression(lexer);
expect_token!(lexer, KeywordOf, AstFactory::create_empty_statement(lexer.location(), lexer.next_id()));
lexer.advance();
let mut case_blocks = Vec::new();
if lexer.token != KeywordEndCase && lexer.token != KeywordElse {
let body = parse_body_in_region(lexer, vec![KeywordEndCase, KeywordElse]);
let mut current_condition = None;
let mut current_body = vec![];
for statement in body {
if let AstNode { stmt: AstStatement::CaseCondition(condition), .. } = statement {
if let Some(condition) = current_condition {
let block = ConditionalBlock { condition, body: current_body };
case_blocks.push(block);
current_body = vec![];
}
current_condition = Some(condition);
} else {
//If no current condition is available, log a diagnostic and add an empty condition
if current_condition.is_none() {
lexer.accept_diagnostic(
Diagnostic::new("Missing Case-Condition")
.with_error_code("E012")
.with_location(lexer.location()),
);
current_condition =
Some(Box::new(AstFactory::create_empty_statement(lexer.location(), lexer.next_id())));
}
current_body.push(statement);
}
}
if let Some(condition) = current_condition {
let block = ConditionalBlock { condition, body: current_body };
case_blocks.push(block);
}
}
let else_block = if lexer.last_token == KeywordElse {
parse_body_in_region(lexer, vec![KeywordEndCase])
} else {
vec![]
};
let end = lexer.last_range.end;
let stmt = CaseStatement {
selector: Box::new(selector),
case_blocks,
else_block,
end_location: lexer.last_location(),
};
AstFactory::create_case_statement(
stmt,
lexer.source_range_factory.create_range(start..end),
lexer.next_id(),
)
}