Skip to content

Commit

Permalink
Backtraces
Browse files Browse the repository at this point in the history
  • Loading branch information
Cypher1 committed May 13, 2024
1 parent fe4ab0c commit 60117bb
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 35 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions takolib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ smallvec = { version = "2.0.0-alpha.5" }
llamada = { path = "../llamada", features = [ ] }
better-std = { path = "../better-std", features = [ ] }
test_each_file = "0.3.2"
backtrace = "0.3.71"

[dev-dependencies]
strum = "0.26.2"
Expand Down
2 changes: 2 additions & 0 deletions takolib/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::ast::location::{Location, UserFacingLocation};
use crate::parser::ParseError;
use crate::primitives::typed_index::TypedIndex;
use thiserror::Error;
use backtrace::Backtrace;

#[derive(Error, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum TError {
Expand Down Expand Up @@ -40,6 +41,7 @@ impl std::fmt::Display for TError {

impl From<std::fmt::Error> for TError {
fn from(error: std::fmt::Error) -> Self {
error!("Internal Error: {error:?}\n{0:?}", Backtrace::new());
Self::InternalError {
message: error.to_string(),
location: None,
Expand Down
61 changes: 29 additions & 32 deletions takolib/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,7 @@ impl<'src, 'toks, T: Iterator<Item = &'toks Token>> ParseState<'src, 'toks, T> {

fn expr(&mut self, binding: Symbol) -> Result<NodeId, TError> {
let _scope = self.depth.clone();
let Ok(mut token) = self.peek().copied() else {
return Err(ParseError::UnexpectedEof.into());
};
let mut token = self.peek().copied()?;
token.kind = self.get_kind(&token);
let location = token.location();
trace!("{indent}Expr: {token:?} (binding {binding:?})", indent=self.indent());
Expand Down Expand Up @@ -611,36 +609,35 @@ impl<'src, 'toks, T: Iterator<Item = &'toks Token>> ParseState<'src, 'toks, T> {
}

fn repeated(&mut self, closer: Symbol) -> Result<SmallVec<NodeId, 2>, TError> {
if self.operator_is(closer).is_ok() {
return Ok(smallvec![]);
}
let mut left = self.any_expr()?;
let mut args = smallvec![];
if self.operator_is(closer).is_err() {
let mut left = self.any_expr()?;
while self.operator_is(closer).is_err() {
let Ok(token) = self.peek().copied() else {
return Err(ParseError::UnexpectedEof.into());
};
let location = token.location();
let op = if self.require(TokenType::Op(Symbol::Comma)).is_ok() {
Symbol::Comma
} else if self.require(TokenType::Op(Symbol::Sequence)).is_ok() {
Symbol::Sequence // TODO: This isn't well supported
} else {
Symbol::Comma
};
if self.require(TokenType::Op(closer)).is_ok() {
break;
}

let right = self.any_expr()?;
left = self.ast.add_op(
Op {
op,
args: smallvec![left, right],
},
location,
);
while self.operator_is(closer).is_err() {
let token = self.peek().copied()?;
let location = token.location();
let op = if self.operator_is(Symbol::Comma).is_ok() {
Symbol::Comma
} else if self.operator_is(Symbol::Sequence).is_ok() {
Symbol::Sequence // TODO: This isn't well supported
} else {
Symbol::Comma
};
if self.operator_is(closer).is_ok() {
break;
}
args.push(left);

let right = self.any_expr()?;
left = self.ast.add_op(
Op {
op,
args: smallvec![left, right],
},
location,
);
}
args.push(left);
return Ok(args);
}

Expand Down Expand Up @@ -720,10 +717,10 @@ pub fn parse(filepath: &Path, contents: &str, tokens: &[Token]) -> Result<Ast, T
// let mut rng = rand::thread_rng();
// std::thread::sleep(std::time::Duration::from_secs(rng.gen_range(0..10)));
if let Some(token) = state.tokens.next() {
Err(ParseError::UnparsedTokens {
return Err(ParseError::UnparsedTokens {
token: token.kind,
location: token.location(),
})?;
}.into());
}
Ok(state.ast)
}
Expand Down
6 changes: 3 additions & 3 deletions takolib/src/test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Integration tests
use log::{debug, info};
use log::{error, info};
use std::fs;
use std::path::Path;
use test_each_file::test_each_path;
Expand Down Expand Up @@ -94,7 +94,7 @@ fn parse_example_files(file: &Path) {
Err(TError::InternalError { message, location }) => {
let st = location.map(|l| l.start.into()).unwrap_or(0);
let end = std::cmp::min(st + 50, contents.len());
debug!("ERROR AT:\n{}", &contents[st..end]);
error!("AT:\n{}", &contents[st..end]);
assert_eq!(
setting.expect,
TestResult::InternalError,
Expand All @@ -106,7 +106,7 @@ fn parse_example_files(file: &Path) {
let location = e.location();
let st = location.map(|l| l.start.into()).unwrap_or(0);
let end = std::cmp::min(st + 50, contents.len());
debug!("ERROR AT:\n{}", &contents[st..end]);
error!("AT:\n{}", &contents[st..end]);
assert_eq!(
setting.expect,
TestResult::ParseError,
Expand Down

0 comments on commit 60117bb

Please sign in to comment.