Skip to content

Commit

Permalink
better subshells
Browse files Browse the repository at this point in the history
  • Loading branch information
harryscholes committed Oct 26, 2024
1 parent e3accb1 commit a1ccbae
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 236 deletions.
56 changes: 56 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ edition = "2021"

[dev-dependencies]
tempdir = "0.3.7"

[dependencies]
thiserror = "1.0.65"
2 changes: 1 addition & 1 deletion src/ast.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::grammar::Token;

#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub enum Ast {
Command { command: Token, args: Vec<Token> },
Pipe { left: Box<Ast>, right: Box<Ast> },
Expand Down
17 changes: 17 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use crate::grammar::Token;

#[derive(thiserror::Error, Debug, PartialEq)]
pub enum Error {
#[error("parse error near `{0}`")]
Parse(Token),
}

impl From<Error> for std::io::Error {
fn from(e: Error) -> std::io::Error {
match e {
Error::Parse(token) => {
std::io::Error::new(std::io::ErrorKind::InvalidInput, token.to_string())
}
}
}
}
6 changes: 6 additions & 0 deletions src/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ pub enum Token {
// - Variable
}

impl std::fmt::Display for Token {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:?}", self.as_ref())
}
}

impl AsRef<OsStr> for Token {
fn as_ref(&self) -> &OsStr {
match self {
Expand Down
21 changes: 5 additions & 16 deletions src/lex.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use std::io;

use crate::{grammar::Token, input};
use crate::{error::Error, grammar::Token, input};

pub struct Lexer;

impl Lexer {
pub fn lex(line: &str) -> io::Result<Vec<Token>> {
pub fn lex(line: &str) -> Result<Vec<Token>, Error> {
let mut tokens = vec![];
let mut token = String::new();
let mut escape = false;
Expand Down Expand Up @@ -46,10 +44,7 @@ impl Lexer {
if iter.peek() == Some(&'|') {
iter.next();
if iter.peek() == Some(&'|') {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"parse error near `|`",
));
Err(Error::Parse(Token::Pipe))?;
}
tokens.push(Token::Or);
} else {
Expand All @@ -62,10 +57,7 @@ impl Lexer {
if iter.peek() == Some(&'>') {
iter.next();
if iter.peek() == Some(&'>') {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"parse error near `>`",
));
Err(Error::Parse(Token::RedirectAppend))?;
}
tokens.push(Token::RedirectAppend);
} else {
Expand All @@ -78,10 +70,7 @@ impl Lexer {
if iter.peek() == Some(&'&') {
iter.next();
if iter.peek() == Some(&'&') {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"parse error near `&`",
));
Err(Error::Parse(Token::Background))?;
}
tokens.push(Token::And);
} else {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod ast;
pub mod error;
pub mod exec;
pub mod grammar;
pub mod lex;
Expand Down
Loading

0 comments on commit a1ccbae

Please sign in to comment.