Starting to keep track of locations of errors in file

This commit is contained in:
greg 2019-01-05 17:18:10 -08:00
parent 2efac109ef
commit 8eda74c9a5
3 changed files with 33 additions and 13 deletions

View File

@ -19,7 +19,7 @@ impl Fold for RecursiveDescentFn {
let new_block: syn::Block = parse_quote! {
{
let next_token_before_parse = self.peek_with_token_offset();
let next_token_before_parse = self.token_handler.peek_token();
let record = ParseRecord {
production_name: stringify!(#ident).to_string(),
next_token: format!("{}", next_token_before_parse.to_string_with_metadata()),
@ -34,7 +34,7 @@ impl Fold for RecursiveDescentFn {
}
match result {
Err(ParseError { token: None, msg }) => {
let next_token_after_parse = self.peek_with_token_offset();
let next_token_after_parse = self.token_handler.peek_token();
println!("HERE? {:?}", next_token_after_parse);
Err(ParseError { token: Some(next_token_after_parse), msg })
},

View File

@ -124,7 +124,13 @@ fn parsing(handle: &mut Schala, input: Vec<tokenizing::Token>, comp: Option<&mut
Some(ref x) => println!("Bad parsing debug option: {}", x),
};
});
ast.map_err(|err| err.msg)
ast.map_err(|error: parsing::ParseError| {
let location_frag = match error.token {
Some(tok) => format!(" at line: {} column: {}", tok.offset.0, tok.offset.1),
None => "".into()
};
format!("'{}'{}", error.msg, location_frag)
})
}
fn symbol_table(handle: &mut Schala, input: ast::AST, comp: Option<&mut UnfinishedComputation>) -> Result<ast::AST, String> {

View File

@ -23,6 +23,13 @@ impl ParseError {
token: None
})
}
fn new_with_token<T>(msg: &str, t: Token) -> ParseResult<T>{
Err(ParseError {
msg: msg.to_string(),
token: Some(t)
})
}
}
pub type ParseResult<T> = Result<T, ParseError>;
@ -47,19 +54,26 @@ struct ParserRestrictions {
struct TokenHandler {
tokens: Peekable<IntoIter<Token>>,
end_of_file: (usize, usize),
}
impl TokenHandler {
fn new(tokens: Vec<Token>) -> TokenHandler {
let end_of_file = match tokens.last() {
None => (0, 0),
Some(s) => {
s.offset.clone()
}
};
let tokens = tokens.into_iter().peekable();
TokenHandler { tokens }
TokenHandler { tokens, end_of_file }
}
fn peek(&mut self) -> TokenKind {
self.tokens.peek().map(|ref t| { t.kind.clone() }).unwrap_or(TokenKind::EOF)
}
fn peek_with_token_offset(&mut self) -> Token {
self.tokens.peek().map(|t: &Token| { t.clone()}).unwrap_or(Token { kind: TokenKind::EOF, offset: (0,0)})
fn peek_token(&mut self) -> Token {
self.tokens.peek().map(|t: &Token| { t.clone()}).unwrap_or(Token { kind: TokenKind::EOF, offset: self.end_of_file})
}
fn next(&mut self) -> TokenKind {
self.tokens.next().map(|ref t| { t.kind.clone() }).unwrap_or(TokenKind::EOF)
@ -79,9 +93,6 @@ impl Parser {
fn peek(&mut self) -> TokenKind {
self.token_handler.peek()
}
fn peek_with_token_offset(&mut self) -> Token {
self.token_handler.peek_with_token_offset()
}
fn next(&mut self) -> TokenKind {
self.token_handler.next()
}
@ -544,7 +555,7 @@ impl Parser {
fn precedence_expr(&mut self, precedence: i32) -> ParseResult<Expression> {
let record = ParseRecord {
production_name: "precedence_expr".to_string(),
next_token: format!("{}", self.peek_with_token_offset().to_string_with_metadata()),
next_token: format!("{}", self.token_handler.peek_token().to_string_with_metadata()),
level: self.parse_level,
};
self.parse_level += 1;
@ -994,7 +1005,10 @@ impl Parser {
#[recursive_descent_method]
fn literal(&mut self) -> ParseResult<Expression> {
use self::ExpressionType::*;
match self.peek() {
let tok = self.token_handler.peek_token();
let kind = tok.kind.clone();
match kind {
DigitGroup(_) | HexLiteral(_) | BinNumberSigil | Period => self.number_literal(),
Keyword(Kw::True) => {
self.next();
@ -1006,9 +1020,9 @@ impl Parser {
},
StrLiteral(s) => {
self.next();
Ok(Expression(StringLiteral(s), None))
Ok(Expression(StringLiteral(s.clone()), None))
}
e => ParseError::new(&format!("Expected a literal expression, got {:?}", e)),
e => ParseError::new_with_token(&format!("Expected a literal expression, got {:?}", e), tok),
}
}