what even is this language sometimes

parse-tree
Jordan Orelli 2 years ago
parent b28e107ea3
commit 0c0bd77eea

@ -1,5 +1,7 @@
use thiserror::Error; use crate::shell::Shell;
use log::debug; use log::debug;
use std::fmt;
use thiserror::Error;
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum Error { pub enum Error {
@ -104,46 +106,42 @@ impl<'text> Lexer<'text> {
} }
} }
struct Command {
// name: String,
args: Vec<String>,
}
impl Node for Command {
fn with(mut self, t: Token) -> Result<Box<dyn Node>, Error> {
match t {
Token::Ident(s) => {
self.args.push(s);
Ok(Box::new(self))
},
_ => Err(Error::UnexpectedToken),
}
}
}
struct Parser { struct Parser {
state: Node,
} }
impl Parser { impl Parser {
fn new() -> Self { fn new() -> Self {
Self { } Self {
state: Node::Empty,
}
} }
fn parse(&self, _tokens: Vec<Token>) -> Result<Tree, Error> { fn parse(mut self, tokens: Vec<Token>) -> Result<Node, Error> {
// if tokens.is_empty() { for token in tokens {
// } match (self.state, token) {
Ok(Tree{}) (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 { #[derive(Debug)]
fn with(self, t: Token) -> Result<Box<dyn Node>, Error>; pub enum Node {
Empty,
Command {
name: String,
args: Vec<String>,
},
} }
// I don't know how to write a parser lol // I don't know how to write a parser lol
@ -171,8 +169,7 @@ trait Node {
// ] // ]
// } // }
pub fn parse<S: AsRef<str>>(text: S) -> Result<Node, Error> {
pub fn parse<S: AsRef<str>>(text: S) -> Result<Tree, Error> {
let tokens = lex(text)?; let tokens = lex(text)?;
let parser = Parser::new(); let parser = Parser::new();
debug!(" {:?}", tokens); debug!(" {:?}", tokens);
@ -255,9 +252,3 @@ mod tests {
*/ */

@ -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!())
// }
} }

Loading…
Cancel
Save