Diag tweaks and lexer fix (needs core fix)

This commit is contained in:
TerraMaster85 2024-01-16 23:01:12 -05:00
parent e70ff6ef9d
commit d9fc775d56
8 changed files with 54 additions and 125 deletions

1
Cargo.lock generated
View File

@ -55,6 +55,7 @@ dependencies = [
name = "kabel" name = "kabel"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"colored",
"is-terminal", "is-terminal",
"libkabel", "libkabel",
"tracing-subscriber", "tracing-subscriber",

View File

@ -4,108 +4,4 @@
"aaaaaa
"aaaaaa

View File

@ -10,4 +10,5 @@ description = "A dead-simple scripting language for use in games"
[dependencies] [dependencies]
libkabel = { version = "0.1", path = "../libkabel", features = ["pretty-emitter"] } libkabel = { version = "0.1", path = "../libkabel", features = ["pretty-emitter"] }
is-terminal = "0.4" is-terminal = "0.4"
tracing-subscriber = "0.3" tracing-subscriber = "0.3"
colored = "2"

View File

@ -5,6 +5,7 @@ use std::io::{self, Read};
use libkabel::diagnostics::emitters::Emitter; use libkabel::diagnostics::emitters::Emitter;
use libkabel::error::KError; use libkabel::error::KError;
use libkabel::lexer::token::Token; use libkabel::lexer::token::Token;
use colored::Colorize;
fn main() -> Result<(), Box<dyn Error>> { fn main() -> Result<(), Box<dyn Error>> {
let argv: Vec<String> = env::args().collect(); let argv: Vec<String> = env::args().collect();
@ -19,7 +20,7 @@ fn main() -> Result<(), Box<dyn Error>> {
} }
Err(e) => { Err(e) => {
eprintln!( eprintln!(
"Error: Tried opening `{}' and got unexpected error: {}", "error: Tried opening file `{}' and got unexpected error: {}",
argv[1], argv[1],
e.kind() e.kind()
); );
@ -38,7 +39,14 @@ fn main() -> Result<(), Box<dyn Error>> {
Err(e) => { Err(e) => {
match e { match e {
KError::InternalError(e) => { KError::InternalError(e) => {
eprintln!("ICE! Please report this to the kabel developers, along with what you were doing: {:?}", e); eprintln!("{} {}",
"error: Internal Kabel error!".red(),
"THIS IS NOT YOUR FAULT.".bold().red());
eprintln!("{}", "error: Please report this error to the kabel developers along with your".red());
eprintln!("{}", "fail: program's complete source code. Either");
eprintln!("{}", "fail: - Open an Issue at https://git.e3t.cc/tm85/kabel, or".red());
eprintln!("{}", "fail: - E-Mail the developers at kabel@e3t.cc".red());
eprintln!("{} {:?}", "fail: Error message follows:".red(), e);
} }
KError::UserError(diags) => { KError::UserError(diags) => {
//eprintln!("{}", libkabel::diagnostics::emitters::basic::BasicEmitter::emit(diags, text_source)); //eprintln!("{}", libkabel::diagnostics::emitters::basic::BasicEmitter::emit(diags, text_source));

View File

@ -8,4 +8,4 @@ pub(crate) mod util;
pub trait Emitter { pub trait Emitter {
type Output; type Output;
fn emit(diag: Vec<Diagnostic>, source: String, source_name: String) -> Self::Output; fn emit(diag: Vec<Diagnostic>, source: String, source_name: String) -> Self::Output;
} }

View File

@ -50,7 +50,7 @@ impl Emitter for PrettyEmitter {
let line_hdr_padding = line.to_string().len() + 1; let line_hdr_padding = line.to_string().len() + 1;
writeln!(output, "{}{}", " ".repeat(line_hdr_padding), "|".bright_blue().bold()).unwrap(); writeln!(output, "{}{}", " ".repeat(line_hdr_padding), "|".bright_blue().bold()).unwrap();
writeln!(output, "{}{} {} {}", line.to_string().bright_blue().bold(), " ".repeat(line_no_padding - line.to_string().len()), "|".bright_blue().bold(), get_line(line, &source).unwrap()).unwrap(); writeln!(output, "{}{} {} {}", line.to_string().bright_blue().bold(), " ".repeat(line_no_padding - line.to_string().len()), "|".bright_blue().bold(), get_line(line, &source).unwrap_or("<line unavailable>")).unwrap();
@ -107,4 +107,4 @@ impl Emitter for PrettyEmitter {
output output
} }
} }

View File

@ -1,10 +1,10 @@
pub fn pos_to_line_col(pos: usize, source: &str) -> (usize, usize) { pub fn pos_to_line_col(pos: usize, source: &str) -> (usize, usize) {
let mut line = 0; let mut line = 1;
let mut col = 1; let mut col = 1;
for (n, c) in source.chars().enumerate() { for (n, c) in source.chars().enumerate() {
if c == '\n' { if c == '\n' {
line += 1; line += 1;
col = 0; col = 1;
} else { } else {
col += 1; col += 1;
} }
@ -17,4 +17,4 @@ pub fn pos_to_line_col(pos: usize, source: &str) -> (usize, usize) {
pub fn get_line(line: usize, source: &str) -> Option<&str> { pub fn get_line(line: usize, source: &str) -> Option<&str> {
source.lines().nth(line) source.lines().nth(line)
} }

View File

@ -38,22 +38,45 @@ pub fn lexer(text_source: &str) -> Result<Vec<Token>, KError> {
} }
State::Stringing => { State::Stringing => {
// If next char is an unescaped quote // If next char is an unescaped quote
if let Some(c_peek) = chars.peek() { // TODO: when possible, make this 1 `if'. Ability to
if c != '\\' && *c_peek == '\"' { // do that remains unimplemented, hence the stupid copied
chars.next(); // code below.
pos += 1; if c != '\n' {
if let Some(c_peek) = chars.peek() {
if c != '\\' && *c_peek == '\"' {
chars.next();
pos += 1;
current_token.push(c); current_token.push(c);
let tok_cpy = current_token.clone(); let tok_cpy = current_token.clone();
lexed.push(Token::Literal(Span::new(span_start, pos), Literal::Str(tok_cpy))); lexed.push(Token::Literal(Span::new(span_start, pos), Literal::Str(tok_cpy)));
state = State::BuildingToken; state = State::BuildingToken;
current_token = String::new(); current_token = String::new();
} else {
current_token.push(c);
}
} else { } else {
current_token.push(c); return Err(KError::UserError(vec![
Diagnostic {
diag_type: DiagnosticType::Error,
message: "unterminated string literal".to_string(),
spans: vec![
SpanWithLabel {
span: Span::new(span_start-1, span_start-1),
span_type: DiagnosticType::SecondaryError,
label: Some("string began here".to_string())
},
SpanWithLabel {
span: Span::new(pos, pos),
span_type: DiagnosticType::Error,
label: Some("expected end quote here".to_string())
}
]
}
]));
} }
} else { } else {
return Err(KError::UserError(vec![ return Err(KError::UserError(vec![
Diagnostic { Diagnostic {
diag_type: DiagnosticType::Error, diag_type: DiagnosticType::Error,