From b28e107ea3fc432314037c05de5dd858199c22fe Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sun, 2 Apr 2023 20:39:44 -0500 Subject: [PATCH] how do you even write a parser lol --- src/main.rs | 8 ++++-- src/parse.rs | 79 ++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 79 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5762558..feeb3a6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -60,6 +60,9 @@ fn main() -> Result<()> { if event.code == key::ENTER { shell.output.newline()?; let s = shell.line.pop(); + info!("◇ {}", s); + let tree = parse::parse(&s)?; + debug!(" {:?}", tree); let parts: Vec<&str> = s.split_whitespace().collect(); if parts.len() > 0 { @@ -69,10 +72,9 @@ fn main() -> Result<()> { } else { vec![] }; - debug!("◇ {} {}", cmd.clone(), args.join(" ")); match shell.eval(cmd.clone(), args.clone()) { - Ok(true) => info!("▷ {} {}", cmd, args.join(" ")), - Ok(false) => warn!("▷ {} {}", cmd, args.join(" ")), + Ok(true) => debug!(" ok"), + Ok(false) => warn!(" fail"), Err(e) => { error!("▷ {} {} ● {}", cmd, args.join(" "), e); println!("error: {}", e); diff --git a/src/parse.rs b/src/parse.rs index 2acc442..37617e3 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -1,5 +1,11 @@ -#[derive(Debug)] -pub enum Error {} +use thiserror::Error; +use log::debug; + +#[derive(Debug, Error)] +pub enum Error { + #[error("Unexpected Token")] + UnexpectedToken, +} #[allow(dead_code)] #[derive(Debug, PartialEq)] @@ -98,16 +104,79 @@ impl<'text> Lexer<'text> { } } +struct Command { + // name: String, + args: Vec, +} + +impl Node for Command { + fn with(mut self, t: Token) -> Result, Error> { + match t { + Token::Ident(s) => { + self.args.push(s); + Ok(Box::new(self)) + }, + _ => Err(Error::UnexpectedToken), + } + } +} + struct Parser { } -struct Tree { +impl Parser { + fn new() -> Self { + Self { } + } + fn parse(&self, _tokens: Vec) -> Result { + // if tokens.is_empty() { + // } + Ok(Tree{}) + } } -fn parse>(text: S) -> Result { - Ok(Tree{}) +#[derive(Debug)] +pub struct Tree { + +} + +trait Node { + fn with(self, t: Token) -> Result, Error>; +} + +// I don't know how to write a parser lol + +// ls +// Command{name: "ls"} + +// echo one two three +// Command{name: "echo", args: [Lit("one"), Lit("two"), Lit("three")]} + +// echo *.rs +// Command{name: "echo", args: [Glob("*.rs")]} +// Command{name: "echo", args: [Path("main.rs"), Path("parse.rs"), ...]} + +// cat main.rs | wc -l +// Pipe{ +// src: Command{name: "cat", args: [Lit("main.rs")]} +// dest: Command{name: "wc", args: [Lit("-l")]} +// } + +// echo one ; echo two +// Sequence{ +// children: [ +// Command{Name: +// ] +// } + + +pub fn parse>(text: S) -> Result { + let tokens = lex(text)?; + let parser = Parser::new(); + debug!(" {:?}", tokens); + parser.parse(tokens) } #[cfg(test)]