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
|
use crate::tokenizer::{self, If, Keyword::*, Loop};
pub const EXPRESSION_DELIMITERS: &[char] = &[
';', '{', '}',
];
#[derive(Clone, Copy, Debug)]
pub enum Scope {
If(If),
Loop(Loop),
}
#[derive(Debug)]
pub enum Expr<'a> {
StartOrStop(bool),
Decision((&'a str, Scope)),
IO(&'a str),
Process(&'a str),
}
pub fn delimit(str: &str) -> Vec<&str> {
str.split(|c| EXPRESSION_DELIMITERS.contains(&c))
.filter(|c| !c.is_empty())
.collect()
}
pub fn process(strs: Vec<&str>) -> Vec<Expr<'_>> {
let mut exprs = Vec::new();
for str in strs {
let mut exp: Option<Expr> = None;
// TODO: fix one line if else as not delimited by {}
for s in str.split_whitespace() {
if let Some(token) = tokenizer::parse(s) {
exp = match token {
If(t) => Some(Expr::Decision((str, Scope::If(t.to_owned())))),
Loop(t) => Some(Expr::Decision((str, Scope::Loop(t.to_owned())))),
Throw => Some(Expr::IO(str)),
IO => Some(Expr::IO(str)),
}
}
}
match exp {
Some(exp) => exprs.push(exp),
None => exprs.push(Expr::Process(str)),
}
}
exprs
}
pub fn parse(str: &str) -> Vec<Expr<'_>> {
process(delimit(str))
}
|