diff --git a/Cargo.lock b/Cargo.lock index 113216c..e585234 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -259,6 +259,7 @@ version = "0.1.0" dependencies = [ "dirs", "mlua", + "peg", "rustyline", "tracing", "tracing-appender", @@ -303,6 +304,33 @@ version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "692fcb63b64b1758029e0a96ee63e049ce8c5948587f2f7208df04625e5f6b56" +[[package]] +name = "peg" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07c0b841ea54f523f7aa556956fbd293bcbe06f2e67d2eb732b7278aaf1d166a" +dependencies = [ + "peg-macros", + "peg-runtime", +] + +[[package]] +name = "peg-macros" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5aa52829b8decbef693af90202711348ab001456803ba2a98eb4ec8fb70844c" +dependencies = [ + "peg-runtime", + "proc-macro2", + "quote", +] + +[[package]] +name = "peg-runtime" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c719dcf55f09a3a7e764c6649ab594c18a177e3599c467983cdf644bfc0a4088" + [[package]] name = "pin-project-lite" version = "0.2.7" diff --git a/Cargo.toml b/Cargo.toml index f6e91a5..1719df6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ whoami = "1.2.0" # Utils for fetching user data rustyline = "9.0.0" # Read user input tracing = "0.1.29" # tracing-appender = "0.2.0" # Logging +peg = "0.7.0" # Parsing [dependencies.mlua] # Lua interpreter version = "0.6.6" # diff --git a/src/lib.rs b/src/lib.rs index c23c1d3..e32050a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,2 +1,3 @@ pub mod core; pub mod log; // TODO: rename this module +pub mod parser; diff --git a/src/parser/mod.rs b/src/parser/mod.rs new file mode 100644 index 0000000..360cc6b --- /dev/null +++ b/src/parser/mod.rs @@ -0,0 +1,3 @@ +//! NeoSH command parser + +pub mod structure; diff --git a/src/parser/structure.rs b/src/parser/structure.rs new file mode 100644 index 0000000..b962c57 --- /dev/null +++ b/src/parser/structure.rs @@ -0,0 +1,52 @@ +/// Command is what user sends. Consists of [CommandUnit] +pub struct Command(Vec); + +// TODO: Operator as an enum +/// Part of a [Command]. Can be either an [Instruction] or an Operator +pub enum CommandUnit { + Instruction(Instruction), + Operator(String), +} + +/// Instruction is a something that is called with [InstructionUnit] +pub struct Instruction { + callable: String, + children: Vec, +} + +/// Either an argument or a [Flag] +pub enum InstructionUnit { + Arg(String), + Flag(Flag), +} + +/// Flag parameters +/// +/// name - what comes after `--` or `-` +/// value - what we pass to the flag +/// length - see [FlagLength] +/// storage - see [FlagStorage] +pub struct Flag { + name: String, + value: Option, + length: FlagLength, + storage: FlagStorage, +} + +/// Flag length type +/// +/// Short - `-` +/// Long - `--` +pub enum FlagLength { + Short, + Long, +} + +/// How flag storages it's value +// Inner - --a=b +// Outer - --a b +pub enum FlagStorage { + Inner, + Outer, +} +