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 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<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 {
state: Node,
}
impl Parser {
fn new() -> Self {
Self { }
Self {
state: Node::Empty,
}
}
fn parse(&self, _tokens: Vec<Token>) -> Result<Tree, Error> {
// if tokens.is_empty() {
// }
Ok(Tree{})
fn parse(mut self, tokens: Vec<Token>) -> Result<Node, Error> {
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),
}
}
#[derive(Debug)]
pub struct Tree {
Ok(self.state)
}
}
trait Node {
fn with(self, t: Token) -> Result<Box<dyn Node>, Error>;
#[derive(Debug)]
pub enum Node {
Empty,
Command {
name: String,
args: Vec<String>,
},
}
// 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<Tree, Error> {
pub fn parse<S: AsRef<str>>(text: S) -> Result<Node, Error> {
let tokens = lex(text)?;
let parser = Parser::new();
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