Skip to content

Commit

Permalink
Added GlobalContext for error/warning reporting and other tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
VonTum committed Aug 19, 2023
1 parent f53e5f9 commit 4b24f6b
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 110 deletions.
7 changes: 4 additions & 3 deletions src/ast.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@

use num_bigint::BigUint;

use crate::{tokenizer::{TokenTypeIdx, TokenExtraInfo}, errors::{ParsingError, error_basic, error_basic_str}};
use crate::tokenizer::{TokenTypeIdx, TokenExtraInfo};
use core::ops::Range;

use std::collections::HashMap;

// Token span. Indices are INCLUSIVE
#[derive(Clone,Copy,Debug,PartialEq,Eq)]
pub struct Span(pub usize, pub usize);
Expand Down Expand Up @@ -302,6 +300,9 @@ impl IterIdentifiers for Module {
}
}




/*#[derive(Debug)]
pub enum NamespaceElement {
Type(TypeExpression),
Expand Down
58 changes: 5 additions & 53 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::ast::cvt_span_to_char_span;
use ariadne::*;

use crate::tokenizer::{TokenTypeIdx, get_token_type_name};
use crate::parser::TokenTreeNode;

pub struct ErrorInfo<T> {
pub position : T,
Expand Down Expand Up @@ -64,23 +63,16 @@ pub fn cvt_token_error_to_str_error(err : ParsingError<Span>, token_spans : &[Ch
ParsingError{error : cvt_token_err_info_to_str(err.error, token_spans), infos : info_vec}
}

pub fn error_info<T>(position : T, reason : String) -> ErrorInfo<T> {
ErrorInfo{position : position, reason : reason}
}
pub fn error_info_str<T>(position : T, reason : &str) -> ErrorInfo<T> {
ErrorInfo{position : position, reason : reason.to_owned()}
pub fn error_info<T, S : Into<String>>(position : T, reason : S) -> ErrorInfo<T> {
ErrorInfo{position : position, reason : reason.into()}
}

pub fn error_basic<T>(position : T, reason : String) -> ParsingError<T> {
pub fn error_basic<T, S : Into<String>>(position : T, reason : S) -> ParsingError<T> {
ParsingError{error : error_info(position, reason), infos : Vec::new()}
}

pub fn error_basic_str<T>(position : T, reason : &str) -> ParsingError<T> {
ParsingError{error : error_info(position, reason.to_owned()), infos : Vec::new()}
}

pub fn error_with_info<T>(position : T, reason : String, infos : Vec<ErrorInfo<T>>) -> ParsingError<T> {
ParsingError{error : error_info(position, reason), infos : infos}
pub fn error_with_info<T, S : Into<String>, V : Into<Vec<ErrorInfo<T>>>>(position : T, reason : S, infos : V) -> ParsingError<T> {
ParsingError{error : error_info(position, reason), infos : infos.into()}
}

pub fn join_expected_list(expected : &[TokenTypeIdx]) -> String {
Expand All @@ -100,43 +92,3 @@ pub fn join_expected_list(expected : &[TokenTypeIdx]) -> String {
result
}

pub fn error_unclosed_bracket(open_pos : usize, open_typ : TokenTypeIdx, close_before_pos : usize) -> ParsingError<Span> {
let open_name = get_token_type_name(open_typ);
let reason = format!("Unclosed bracket {open_name}");
error_with_info(Span::from(open_pos), reason, vec![error_info_str(Span(close_before_pos, close_before_pos), "must be closed before this")])
}
pub fn error_unopened_bracket(close_pos : usize, close_typ : TokenTypeIdx, open_after_pos : usize) -> ParsingError<Span> {
let close_name = get_token_type_name(close_typ);
let reason = format!("Unopened bracket. Closing bracket {close_name} found but was not opened.");
error_with_info(Span::from(close_pos), reason, vec![error_info_str(Span(open_after_pos, open_after_pos), "must be opened in scope after this")])
}

pub fn error_unexpected_token(expected : &[TokenTypeIdx], found : TokenTypeIdx, pos : usize, context : &str) -> ParsingError<Span> {
let expected_list_str = join_expected_list(expected);
error_unexpected_token_str(&expected_list_str, found, pos, context)
}

pub fn error_unexpected_token_str(expected_list_str : &str, found : TokenTypeIdx, pos : usize, context : &str) -> ParsingError<Span> {
let tok_typ_name = get_token_type_name(found);
error_basic(Span::from(pos), format!("Unexpected Token '{tok_typ_name}' while parsing {context}. Expected {expected_list_str}"))
}

pub fn error_unexpected_tree_node(expected : &[TokenTypeIdx], found : Option<&TokenTreeNode>, unexpected_eof_idx : usize, context : &str) -> ParsingError<Span> {
let expected_list_str = join_expected_list(expected);
error_unexpected_tree_node_str(&expected_list_str, found, unexpected_eof_idx, context)
}

pub fn error_unexpected_tree_node_str(expected_list_str : &str, found : Option<&TokenTreeNode>, unexpected_eof_idx : usize, context : &str) -> ParsingError<Span> {
match found {
None => {
error_basic(Span::from(unexpected_eof_idx), format!("Unexpected End of Scope while parsing {context}. Expected {expected_list_str}"))
}
Some(TokenTreeNode::PlainToken(tok, pos)) => {
error_unexpected_token_str(expected_list_str, tok.get_type(), *pos, context)
}
Some(TokenTreeNode::Block(typ, _, span)) => {
let tok_typ_name = get_token_type_name(*typ);
error_basic(*span, format!("Unexpected Block '{tok_typ_name}' while parsing {context}. Expected {expected_list_str}"))
}
}
}
30 changes: 30 additions & 0 deletions src/global_context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

use crate::ast::{Span, CharSpan, cvt_span_to_char_span};
use crate::errors::*;

pub struct GlobalContext<'a> {
pub errors : Vec<ParsingError<CharSpan>>,
pub token_spans : &'a [CharSpan]
}

impl<'a> GlobalContext<'a> {
// Error reporting
pub fn error(&mut self, err : ParsingError<Span>) {
let converted = cvt_token_error_to_str_error(err, &self.token_spans);
self.errors.push(converted);
}

// Helpers for basic errors
pub fn error_basic<S : Into<String>>(&mut self, position : Span, reason : S) {
let cvt_position = cvt_span_to_char_span(position, &self.token_spans);
self.errors.push(ParsingError{error : error_info(cvt_position, reason), infos : Vec::new()});
}

pub fn error_with_info<S : Into<String>, const N : usize>(&mut self, position : Span, reason : S, infos : [ErrorInfo<Span>; N]) {
let cvt_position = cvt_span_to_char_span(position, &self.token_spans);
let cvt_infos = infos.into_iter().map(|i| {
cvt_token_err_info_to_str(i, &self.token_spans)
}).collect();
self.errors.push(ParsingError{error : error_info(cvt_position, reason), infos : cvt_infos});
}
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod parser;
mod errors;
mod ast;
mod code_generation;
mod global_context;

mod dev_aid;

Expand Down
Loading

0 comments on commit 4b24f6b

Please sign in to comment.