how do you even write a parser lol

parse-tree
Jordan Orelli 2 years ago
parent 5980bc59da
commit b28e107ea3

@ -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);

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

Loading…
Cancel
Save