Diag tweaks and lexer fix (needs core fix)
This commit is contained in:
parent
e70ff6ef9d
commit
d9fc775d56
|
@ -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",
|
||||||
|
|
104
examples/try.kab
104
examples/try.kab
|
@ -4,108 +4,4 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
"aaaaaa
|
"aaaaaa
|
|
@ -11,3 +11,4 @@ description = "A dead-simple scripting language for use in games"
|
||||||
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"
|
||||||
|
|
|
@ -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));
|
||||||
|
|
|
@ -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();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,6 +38,10 @@ 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
|
||||||
|
// TODO: when possible, make this 1 `if'. Ability to
|
||||||
|
// do that remains unimplemented, hence the stupid copied
|
||||||
|
// code below.
|
||||||
|
if c != '\n' {
|
||||||
if let Some(c_peek) = chars.peek() {
|
if let Some(c_peek) = chars.peek() {
|
||||||
if c != '\\' && *c_peek == '\"' {
|
if c != '\\' && *c_peek == '\"' {
|
||||||
chars.next();
|
chars.next();
|
||||||
|
@ -53,7 +57,26 @@ pub fn lexer(text_source: &str) -> Result<Vec<Token>, KError> {
|
||||||
current_token.push(c);
|
current_token.push(c);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
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 {
|
||||||
return Err(KError::UserError(vec![
|
return Err(KError::UserError(vec![
|
||||||
Diagnostic {
|
Diagnostic {
|
||||||
diag_type: DiagnosticType::Error,
|
diag_type: DiagnosticType::Error,
|
||||||
|
|
Loading…
Reference in New Issue