From 3280c4844e09bed808f08ce6f42932c0cf33e4d6 Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Sat, 27 Sep 2025 14:25:50 +0800 Subject: init --- src/parser.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/parser.rs (limited to 'src/parser.rs') diff --git a/src/parser.rs b/src/parser.rs new file mode 100644 index 0000000..a6ec7cf --- /dev/null +++ b/src/parser.rs @@ -0,0 +1,52 @@ +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 Expression<'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 parse(strs: Vec<&str>) -> Vec> { + let mut expressions = Vec::new(); + + for str in strs { + let mut exp: Option = 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(Expression::Decision((str, Scope::If(t.to_owned())))), + Loop(t) => Some(Expression::Decision((str, Scope::Loop(t.to_owned())))), + Throw => Some(Expression::IO(str)), + IO => Some(Expression::IO(str)), + } + } + } + + match exp { + Some(exp) => expressions.push(exp), + None => expressions.push(Expression::Process(str)), + } + } + + expressions +} -- cgit v1.2.3