From 3280c4844e09bed808f08ce6f42932c0cf33e4d6 Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Sat, 27 Sep 2025 14:25:50 +0800 Subject: init --- .gitignore | 1 + Cargo.lock | 129 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 7 +++ src/ast.rs | 1 + src/lib.rs | 3 ++ src/main.rs | 7 +++ src/parser.rs | 52 ++++++++++++++++++++++ src/tokenizer.rs | 34 +++++++++++++++ 8 files changed, 234 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/ast.rs create mode 100644 src/lib.rs create mode 100644 src/main.rs create mode 100644 src/parser.rs create mode 100644 src/tokenizer.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..e08df41 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,129 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "fastrand" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" + +[[package]] +name = "java2flowchart" +version = "0.1.0" +dependencies = [ + "phf", +] + +[[package]] +name = "phf" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1562dc717473dbaa4c1f85a36410e03c047b2e7df7f45ee938fbef64ae7fadf" +dependencies = [ + "phf_macros", + "phf_shared", + "serde", +] + +[[package]] +name = "phf_generator" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "135ace3a761e564ec88c03a77317a7c6b80bb7f7135ef2544dbe054243b89737" +dependencies = [ + "fastrand", + "phf_shared", +] + +[[package]] +name = "phf_macros" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "812f032b54b1e759ccd5f8b6677695d5268c588701effba24601f6932f8269ef" +dependencies = [ + "phf_generator", + "phf_shared", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "phf_shared" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e57fef6bc5981e38c2ce2d63bfa546861309f875b8a75f092d1d54ae2d64f266" +dependencies = [ + "siphasher", +] + +[[package]] +name = "proc-macro2" +version = "1.0.101" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89ae43fd86e4158d6db51ad8e2b80f313af9cc74f5c0e03ccb87de09998732de" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "serde" +version = "1.0.227" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80ece43fc6fbed4eb5392ab50c07334d3e577cbf40997ee896fe7af40bba4245" +dependencies = [ + "serde_core", +] + +[[package]] +name = "serde_core" +version = "1.0.227" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a576275b607a2c86ea29e410193df32bc680303c82f31e275bbfcafe8b33be5" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.227" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51e694923b8824cf0e9b382adf0f60d4e05f348f357b38833a3fa5ed7c2ede04" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "siphasher" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56199f7ddabf13fe5074ce809e7d3f42b42ae711800501b5b16ea82ad029c39d" + +[[package]] +name = "syn" +version = "2.0.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ede7c438028d4436d71104916910f5bb611972c5cfd7f89b8300a8186e6fada6" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f63a545481291138910575129486daeaf8ac54aee4387fe7906919f7830c7d9d" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..f249668 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "java2flowchart" +version = "0.1.0" +edition = "2024" + +[dependencies] +phf = { version = "0.13.1", features = ["macros"] } 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> { + 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 +} 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) +} -- cgit v1.2.3