the new parser is doing parsing now

main
Jordan Orelli 11 months ago
parent 7004de4e82
commit aa6bcedcf0

@ -1,5 +1,5 @@
use crate::error::Error; use crate::error::Error;
pub use log::{debug, info, set_logger, set_max_level, warn, LevelFilter}; pub use log::{debug, error, info, set_logger, set_max_level, warn, LevelFilter};
use std::{ use std::{
fs::File, fs::File,

@ -15,6 +15,7 @@ mod syntax;
use crate::log::*; use crate::log::*;
use prompt::Prompt; use prompt::Prompt;
use shell::Shell; use shell::Shell;
use syntax::Eval;
use std::io::Write; use std::io::Write;
@ -55,9 +56,22 @@ fn main() -> Result<()> {
shell.output.newline()?; shell.output.newline()?;
let s = shell.line.pop(); let s = shell.line.pop();
info!("◇ {}", s); info!("◇ {}", s);
let tree = parse::parse(&s)?; // let tree = parse::parse(&s)?;
match syntax::x(&s) {
Ok(tree) => {
debug!(" {:?}", tree); debug!(" {:?}", tree);
shell.exec(tree.into())?; let mut state = syntax::State::new();
if let Err(e) = tree.eval(&mut state) {
error!("{e:?}");
println!("Error: {e:?}");
}
}
Err(e) => {
error!("{e:?}");
println!("Error: {e:?}");
}
}
// shell.exec(tree.into())?;
// Some commands don't leave the terminal in a clean state, so we use reset // Some commands don't leave the terminal in a clean state, so we use reset
// to ensure that our input and output modes are what we expect them to be. // to ensure that our input and output modes are what we expect them to be.
shell.reset()?; shell.reset()?;

@ -69,10 +69,8 @@ impl Iterator for ChildIter {
type Item = Cursor; type Item = Cursor;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
println!("next");
let children = self.target.children.borrow(); let children = self.target.children.borrow();
let v = children.get(self.idx)?; let v = children.get(self.idx)?;
println!("next has: {v:?}");
self.idx += 1; self.idx += 1;
Some(Cursor { Some(Cursor {
target: Rc::clone(v), target: Rc::clone(v),
@ -176,7 +174,6 @@ impl<'text> Parser<'text> {
} }
fn step(&mut self) -> Result<bool, ParseError> { fn step(&mut self) -> Result<bool, ParseError> {
println!("parser step...");
match self.cursor.value() { match self.cursor.value() {
Value::Start => self.step_start(), Value::Start => self.step_start(),
Value::Statement => self.step_statement(), Value::Statement => self.step_statement(),
@ -186,30 +183,24 @@ impl<'text> Parser<'text> {
fn step_start(&mut self) -> Result<bool, ParseError> { fn step_start(&mut self) -> Result<bool, ParseError> {
assert!(matches!(self.cursor.value(), Value::Start)); assert!(matches!(self.cursor.value(), Value::Start));
println!("step start node");
match self.source.peek()? { match self.source.peek()? {
Some(Token::Word(_)) => { Some(Token::Word(_)) => {
self.cursor.push(Value::Statement)?; self.cursor.push(Value::Statement)?;
let token = self.source.next().unwrap()?; let token = self.source.next().unwrap()?;
self.cursor.push(Value::Terminal(token))?; self.cursor.push(Value::Terminal(token))?;
self.cursor.up()?; self.cursor.up()?;
println!("start node returns true...");
Ok(true) Ok(true)
} }
Some(Token::Glob(_)) => { Some(Token::Glob(_)) => {
let token = self.source.next().unwrap()?; let token = self.source.next().unwrap()?;
Err(ParseError::UnexpectedToken(token)) Err(ParseError::UnexpectedToken(token))
} }
None => { None => Ok(false),
println!("start node has ... nothin peeked in source");
Ok(false)
}
} }
} }
fn step_statement(&mut self) -> Result<bool, ParseError> { fn step_statement(&mut self) -> Result<bool, ParseError> {
assert!(matches!(self.cursor.value(), Value::Statement)); assert!(matches!(self.cursor.value(), Value::Statement));
println!("step statement node");
match self.source.peek()? { match self.source.peek()? {
Some(Token::Word(_) | Token::Glob(_)) => { Some(Token::Word(_) | Token::Glob(_)) => {
let token = self.source.next().unwrap()?; let token = self.source.next().unwrap()?;

@ -5,7 +5,7 @@ use crate::{
}; };
use std::{collections::HashSet, process}; use std::{collections::HashSet, process};
struct State {} pub struct State {}
impl State { impl State {
pub fn new() -> Self { pub fn new() -> Self {
@ -13,12 +13,12 @@ impl State {
} }
} }
trait Eval { pub trait Eval {
fn eval(&self, ctx: &mut State) -> Result<Value, ExecError>; fn eval(&self, ctx: &mut State) -> Result<Value, ExecError>;
} }
#[derive(Debug)] #[derive(Debug)]
enum Element { pub enum Element {
Block(Block), Block(Block),
Command(Command), Command(Command),
Literal(Value), Literal(Value),
@ -36,7 +36,7 @@ impl Eval for Element {
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
enum Value { pub enum Value {
None, None,
Text(String), Text(String),
ExitStatus(process::ExitStatus), ExitStatus(process::ExitStatus),
@ -61,7 +61,7 @@ impl Value {
} }
#[derive(Debug)] #[derive(Debug)]
struct Block { pub struct Block {
commands: Vec<Command>, commands: Vec<Command>,
} }
@ -84,7 +84,7 @@ impl Eval for Block {
} }
#[derive(Debug)] #[derive(Debug)]
struct Command { pub struct Command {
name: Box<Element>, name: Box<Element>,
args: Vec<Element>, args: Vec<Element>,
} }
@ -123,11 +123,9 @@ impl TreeBuilder {
fn descend(&mut self, source: &mut parse2::Cursor) -> Result<Element, ParseError> { fn descend(&mut self, source: &mut parse2::Cursor) -> Result<Element, ParseError> {
let e = match source.value() { let e = match source.value() {
parse2::Value::Start => { parse2::Value::Start => {
println!("start node");
let mut root = Block::new(); let mut root = Block::new();
let children = source.iter_children(); let children = source.iter_children();
for mut child in children { for mut child in children {
println!("child");
let e = self.descend(&mut child)?; let e = self.descend(&mut child)?;
match e { match e {
Element::Command(cmd) => root.commands.push(cmd), Element::Command(cmd) => root.commands.push(cmd),
@ -137,7 +135,6 @@ impl TreeBuilder {
Element::Block(root) Element::Block(root)
} }
parse2::Value::Statement => { parse2::Value::Statement => {
println!("statement node");
let mut children = source.iter_children(); let mut children = source.iter_children();
let mut first = children.next().unwrap(); let mut first = children.next().unwrap();
let name = self.descend(&mut first)?; let name = self.descend(&mut first)?;
@ -152,14 +149,12 @@ impl TreeBuilder {
Element::Command(cmd) Element::Command(cmd)
} }
parse2::Value::Terminal(Token::Word(word)) => { parse2::Value::Terminal(Token::Word(word)) => {
println!("terminal word node");
Element::Literal(Value::Text(word.to_string())) Element::Literal(Value::Text(word.to_string()))
} }
parse2::Value::Terminal(Token::Glob(_)) => { parse2::Value::Terminal(Token::Glob(_)) => {
todo!() todo!()
} }
}; };
println!("visited: {:?}", source.target.id);
self.visited.insert(source.target.id); self.visited.insert(source.target.id);
Ok(e) Ok(e)
} }
@ -171,11 +166,10 @@ fn build(mut source: parse2::Cursor) -> Result<Element, ParseError> {
builder.descend(&mut source) builder.descend(&mut source)
} }
fn x(source: &str) -> Result<Element, ParseError> { pub fn x(source: &str) -> Result<Element, ParseError> {
let tokens = Lexer::new(source); let tokens = Lexer::new(source);
let parser = parse2::Parser::new(tokens); let parser = parse2::Parser::new(tokens);
let mut parse_tree = parser.parse()?; let mut parse_tree = parser.parse()?;
println!("parse tree: {parse_tree:?}");
let mut builder = TreeBuilder::new(); let mut builder = TreeBuilder::new();
builder.descend(&mut parse_tree) builder.descend(&mut parse_tree)
} }
@ -187,9 +181,10 @@ mod test {
#[test] #[test]
fn hi() -> Result<(), ParseError> { fn hi() -> Result<(), ParseError> {
let e = x("ls one two three ; ls four")?; let e = x("ls one two three")?;
print!("{:?}", e); print!("{:?}", e);
Ok(()) todo!()
//Ok(())
} }
} }

Loading…
Cancel
Save