begin new error system, unfinished

This commit is contained in:
afonya2 2025-06-06 22:01:53 +02:00
parent 32c2a20697
commit 532fffe725
Signed by: afonya
GPG key ID: EBB9C4CAFAAFB2DC
6 changed files with 673 additions and 237 deletions

View file

@ -1,3 +1,6 @@
use std::process;
use crate::{errors::{create_error, print_error, ErrorSubType, ErrorType}, Context};
#[derive(Debug, PartialEq)]
pub enum TokenType {
STRING,
@ -46,7 +49,7 @@ fn is_sep(char: &str) -> bool {
return chars.contains(&check.as_str());
}*/
fn read_string(splitted: &Vec<&str>, pos: &mut usize, out: &mut Vec<Token>) {
fn read_string(splitted: &Vec<&str>, pos: &mut usize, out: &mut Vec<Token>, ctx: &Context) {
let mut str = String::from("");
let start_pos = *pos-1;
*pos += 5;
@ -61,12 +64,14 @@ fn read_string(splitted: &Vec<&str>, pos: &mut usize, out: &mut Vec<Token>) {
str += nchar;
}
if !success {
panic!("Unexpected end of string at {}", pos);
let err = create_error("Unexpected end of string", *pos, ErrorType::SyntaxError, ErrorSubType::UnexpectedEnd);
print_error(&err, &ctx);
process::exit(1);
}
*pos += 5;
out.push(Token { typ: TokenType::STRING, value: str, pos: start_pos });
}
fn read_comment(splitted: &Vec<&str>, pos: &mut usize, is_multiline: bool) {
fn read_comment(splitted: &Vec<&str>, pos: &mut usize, is_multiline: bool, ctx: &Context) {
let mut str = String::from("");
*pos += 1;
let mut success = !is_multiline;
@ -83,7 +88,9 @@ fn read_comment(splitted: &Vec<&str>, pos: &mut usize, is_multiline: bool) {
str += nchar;
}
if !success {
panic!("Unexpected end of comment at {}", pos);
let err = create_error("Unexpected end of comment", *pos, ErrorType::SyntaxError, ErrorSubType::UnexpectedEnd);
print_error(&err, &ctx);
process::exit(1);
}
*pos += 1;
}
@ -204,7 +211,7 @@ fn read_identifier(splitted: &Vec<&str>, pos: &mut usize, out: &mut Vec<Token>)
out.extend(pre_out);
}
pub fn lex(input: String) -> Vec<Token> {
pub fn lex(input: String, ctx: &Context) -> Vec<Token> {
let mut out: Vec<Token> = vec![];
let splitted: Vec<&str> = input.split("").collect();
let mut pos = 1;
@ -232,11 +239,11 @@ pub fn lex(input: String) -> Vec<Token> {
pos -= 1;
out.push(Token { typ: TokenType::NUMBER, value: num, pos: start_pos });
} else if splitted.len() >= pos+5 && splitted[(pos-1)..(pos+5)].join("") == "szaft\"" {
read_string(&splitted, &mut pos, &mut out);
read_string(&splitted, &mut pos, &mut out, ctx);
} else if splitted.len() >= pos+1 && splitted[(pos-1)..(pos+1)].join("") == "//" {
read_comment(&splitted, &mut pos, false);
read_comment(&splitted, &mut pos, false, ctx);
} else if splitted.len() >= pos+1 && splitted[(pos-1)..(pos+1)].join("") == "/*" {
read_comment(&splitted, &mut pos, true);
read_comment(&splitted, &mut pos, true, ctx);
} else if is_mul_operator(char, splitted[pos]) {
out.push(Token { typ: TokenType::OPERATOR, value: String::from(char) + splitted[pos], pos: pos-1 });
pos += 1;