Skip to content

Commit

Permalink
Concretized ParsingError<T> to Span, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
VonTum committed Aug 26, 2023
1 parent 049cb84 commit a53fbe9
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/code_generation/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

use crate::{ast::*, errors::ParsingError};
use crate::{ast::*, errors::ErrorCollector};


#[derive(Debug)]
Expand Down Expand Up @@ -138,7 +138,7 @@ impl Flattened {
result
}

pub fn typecheck(&mut self, errors : &mut Vec<ParsingError<Span>>) {
pub fn typecheck(&mut self, errors : &mut ErrorCollector) {

}
}
Expand Down
6 changes: 3 additions & 3 deletions src/dev_aid/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use lsp_server::{Response, Message, Connection};

use lsp_types::notification::Notification;

use crate::{parser::{perform_full_semantic_parse, FullParseResult}, dev_aid::syntax_highlighting::create_token_ide_info, ast::{IdentifierType, Span}, errors::ParsingError};
use crate::{parser::{perform_full_semantic_parse, FullParseResult}, dev_aid::syntax_highlighting::create_token_ide_info, ast::{IdentifierType, Span}, errors::ErrorCollector};

use super::syntax_highlighting::{IDETokenType, IDEIdentifierType, IDEToken};

Expand Down Expand Up @@ -258,9 +258,9 @@ fn cvt_span_to_lsp_range(ch_sp : Span, token_positions : &[std::ops::Range<Posit
}
}

fn send_errors_warnings(connection: &Connection, errs : Vec<ParsingError<Span>>, file_uri: Url, token_positions : &[std::ops::Range<Position>]) -> Result<(), Box<dyn Error + Sync + Send>> {
fn send_errors_warnings(connection: &Connection, errors : ErrorCollector, file_uri: Url, token_positions : &[std::ops::Range<Position>]) -> Result<(), Box<dyn Error + Sync + Send>> {
let mut diag_vec : Vec<Diagnostic> = Vec::new();
for err in errs {
for err in errors.errors {
diag_vec.push(Diagnostic::new_simple(cvt_span_to_lsp_range(err.error.position, token_positions), err.error.reason));
}

Expand Down
2 changes: 1 addition & 1 deletion src/dev_aid/syntax_highlighting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ pub fn syntax_highlight_file(file_path : &str) {

let token_offsets = generate_character_offsets(&file_text, &full_parse.tokens);

for err in errors {
for err in errors.errors {
err.pretty_print_error(&file_path, &file_text, &token_offsets);
}

Expand Down
18 changes: 9 additions & 9 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ use ariadne::*;

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

pub struct ErrorInfo<T> {
pub position : T,
pub struct ErrorInfo {
pub position : Span,
pub reason : String
}

pub struct ParsingError<T> {
pub error : ErrorInfo<T>,
pub infos : Vec<ErrorInfo<T>>
pub struct ParsingError {
pub error : ErrorInfo,
pub infos : Vec<ErrorInfo>
}

impl<'a> ParsingError<Span> {
impl ParsingError {
pub fn pretty_print_error(&self, file_name : &str, file_text : &str, character_ranges : &[Range<usize>]) {
// Generate & choose some colours for each of our elements
let err_color = Color::Red;
Expand Down Expand Up @@ -50,7 +50,7 @@ impl<'a> ParsingError<Span> {
}
}

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

Expand All @@ -73,7 +73,7 @@ pub fn join_expected_list(expected : &[TokenTypeIdx]) -> String {

// Class that collects and manages errors and warnings
pub struct ErrorCollector {
pub errors : Vec<ParsingError<Span>>
pub errors : Vec<ParsingError>
}

impl<'a> ErrorCollector {
Expand All @@ -85,7 +85,7 @@ impl<'a> ErrorCollector {
self.errors.push(ParsingError{error : error_info(position, reason), infos : Vec::new()});
}

pub fn error_with_info<S : Into<String>>(&mut self, position : Span, reason : S, infos : Vec<ErrorInfo<Span>>) {
pub fn error_with_info<S : Into<String>>(&mut self, position : Span, reason : S, infos : Vec<ErrorInfo>) {
self.errors.push(ParsingError{error : error_info(position, reason), infos : infos});
}
}
5 changes: 2 additions & 3 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ pub struct FullParseResult {
pub ast : ASTRoot
}

pub fn perform_full_semantic_parse<'txt>(file_text : &'txt str) -> (FullParseResult, Vec<ParsingError<Span>>) {
pub fn perform_full_semantic_parse<'txt>(file_text : &'txt str) -> (FullParseResult, ErrorCollector) {
let mut errors = ErrorCollector::new();

let tokens = tokenize(file_text, &mut errors);
Expand All @@ -692,10 +692,9 @@ pub fn perform_full_semantic_parse<'txt>(file_text : &'txt str) -> (FullParseRes

let ast = parse(&token_hierarchy, file_text, tokens.len(), &mut errors);

let errs = errors.errors;
(FullParseResult{
tokens,
token_hierarchy,
ast,
}, errs)
}, errors)
}

0 comments on commit a53fbe9

Please sign in to comment.