ParseError always has token now

This commit is contained in:
greg 2019-01-08 01:04:46 -08:00
parent 09e2d8579d
commit ab8e24a276
3 changed files with 11 additions and 25 deletions

View File

@ -32,13 +32,7 @@ impl Fold for RecursiveDescentFn {
if self.parse_level != 0 {
self.parse_level -= 1;
}
match result {
Err(ParseError { token: None, msg }) => {
let next_token_after_parse = self.token_handler.peek();
Err(ParseError { token: Some(next_token_after_parse), msg })
},
_ => result
}
result
}
};
i.block = Box::new(new_block);

View File

@ -135,25 +135,20 @@ fn parsing(input: Vec<tokenizing::Token>, handle: &mut Schala, comp: Option<&mut
}
fn format_parse_error(error: parsing::ParseError, handle: &mut Schala) -> String {
//TODO make token no longer be Optoinal
if let Some(tok) = error.token {
let line_num = tok.offset.0;
let ch = tok.offset.1;
let line_from_program = handle.source_reference.get_line(line_num);
let location_pointer = format!("{}^", " ".repeat(ch));
let line_num = error.token.offset.0;
let ch = error.token.offset.1;
let line_from_program = handle.source_reference.get_line(line_num);
let location_pointer = format!("{}^", " ".repeat(ch));
let line_num_digits = format!("{}", line_num).chars().count();
let space_padding = " ".repeat(line_num_digits);
let line_num_digits = format!("{}", line_num).chars().count();
let space_padding = " ".repeat(line_num_digits);
format!(r#"
format!(r#"
{error_msg}
{space_padding} |
{line_num} | {}
{space_padding} | {}
"#, line_from_program, location_pointer, error_msg=error.msg, space_padding=space_padding, line_num=line_num)
} else {
format!("{}", error.msg)
}
}
fn symbol_table(input: ast::AST, handle: &mut Schala, comp: Option<&mut UnfinishedComputation>) -> Result<ast::AST, String> {

View File

@ -13,15 +13,12 @@ use crate::builtin::{BinOp, PrefixOp};
#[derive(Debug)]
pub struct ParseError {
pub msg: String,
pub token: Option<Token>
pub token: Token
}
impl ParseError {
fn new_with_token<T>(msg: &str, t: Token) -> ParseResult<T>{
Err(ParseError {
msg: msg.to_string(),
token: Some(t)
})
fn new_with_token<T>(msg: &str, token: Token) -> ParseResult<T>{
Err(ParseError { msg: msg.to_string(), token })
}
}