diff --git a/src/parse.rs b/src/parse.rs index 37617e3..e9acd8c 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -1,5 +1,7 @@ -use thiserror::Error; +use crate::shell::Shell; use log::debug; +use std::fmt; +use thiserror::Error; #[derive(Debug, Error)] pub enum Error { @@ -104,46 +106,42 @@ 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 { - + state: Node, } impl Parser { fn new() -> Self { - Self { } + Self { + state: Node::Empty, + } } - fn parse(&self, _tokens: Vec) -> Result { - // if tokens.is_empty() { - // } - Ok(Tree{}) + fn parse(mut self, tokens: Vec) -> Result { + for token in tokens { + match (self.state, token) { + (Node::Empty, Token::Ident(name)) => { + self.state = Node::Command{name, args: vec!()}; + }, + (Node::Command{name, mut args}, Token::Ident(s)) => { + args.push(s); + self.state = Node::Command{name, args}; + }, + _ => return Err(Error::UnexpectedToken), + } + } + Ok(self.state) } -} - -#[derive(Debug)] -pub struct Tree { } -trait Node { - fn with(self, t: Token) -> Result, Error>; +#[derive(Debug)] +pub enum Node { + Empty, + Command { + name: String, + args: Vec, + }, } // I don't know how to write a parser lol @@ -167,12 +165,11 @@ trait Node { // echo one ; echo two // Sequence{ // children: [ -// Command{Name: +// Command{Name: // ] // } - -pub fn parse>(text: S) -> Result { +pub fn parse>(text: S) -> Result { let tokens = lex(text)?; let parser = Parser::new(); debug!(" {:?}", tokens); @@ -255,9 +252,3 @@ mod tests { */ - - - - - - diff --git a/src/shell.rs b/src/shell.rs index f4eaf07..f071765 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -136,10 +136,4 @@ impl Shell { } } } - - // pub fn parse(&mut self) -> (String, Vec<&str>) { - // let buf = self.line.show(); - // let tree = parse(buf); - // (String::new(), vec!()) - // } }