summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2025-09-27 14:25:50 +0800
committerlittleblack111 <littleblack11111@gmail.com>2025-09-27 14:25:50 +0800
commit3280c4844e09bed808f08ce6f42932c0cf33e4d6 (patch)
tree84ed1ec40db0184b273faabdb7c7be522271eb43 /src
init
Diffstat (limited to 'src')
-rw-r--r--src/ast.rs1
-rw-r--r--src/lib.rs3
-rw-r--r--src/main.rs7
-rw-r--r--src/parser.rs52
-rw-r--r--src/tokenizer.rs34
5 files changed, 97 insertions, 0 deletions
diff --git a/src/ast.rs b/src/ast.rs
new file mode 100644
index 0000000..8b13789
--- /dev/null
+++ b/src/ast.rs
@@ -0,0 +1 @@
+
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..a553e33
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1,3 @@
+pub mod ast;
+pub mod parser;
+pub mod tokenizer;
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..8667027
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,7 @@
+use java2flowchart::parser::{delimit, parse};
+
+fn main() {
+ let test = "if (a) {} else {}";
+
+ println!("{:#?}", parse(delimit(test)));
+}
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<Expression<'_>> {
+ let mut expressions = Vec::new();
+
+ for str in strs {
+ let mut exp: Option<Expression> = 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
+}
diff --git a/src/tokenizer.rs b/src/tokenizer.rs
new file mode 100644
index 0000000..085c1d3
--- /dev/null
+++ b/src/tokenizer.rs
@@ -0,0 +1,34 @@
+use phf::{Map, phf_map};
+
+#[derive(Clone, Copy, Debug)]
+pub enum If {
+ If,
+ Else,
+}
+
+#[derive(Clone, Copy, Debug)]
+pub enum Loop {
+ For,
+ While,
+}
+
+pub enum Keyword {
+ If(If),
+ Loop(Loop),
+ Throw,
+ IO,
+}
+
+pub static KEYWORDS: Map<&'static str, Keyword> = phf_map! {
+ "if" => Keyword::If(If::If),
+ "else" => Keyword::If(If::Else),
+ "for" => Keyword::Loop(Loop::For),
+ "while" => Keyword::Loop(Loop::While),
+ "throw" => Keyword::Throw,
+ "println" => Keyword::IO,
+ "print" => Keyword::IO,
+};
+
+pub fn parse(keyword: &str) -> Option<&Keyword> {
+ KEYWORDS.get(keyword)
+}