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