lang: add highlighting to errors
This commit is contained in:
parent
29cdcfbe0c
commit
4bcaf945d7
6 changed files with 128 additions and 15 deletions
|
@ -16,6 +16,7 @@ rowan = "0.15.15"
|
|||
drop_bomb = "0.1.5"
|
||||
enumset = "1.1.3"
|
||||
indoc = "2"
|
||||
owo-colors = {version = "4", features = ["supports-colors"]}
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
use enumset::enum_set;
|
||||
|
||||
use crate::lst_parser::syntax_kind::SyntaxKind;
|
||||
|
||||
use super::syntax_kind::TokenSet;
|
||||
|
||||
pub struct Input<'src, 'toks> {
|
||||
raw: &'toks Vec<(SyntaxKind, &'src str)>,
|
||||
/// indices of the "meaningful" tokens (not whitespace etc)
|
||||
|
@ -10,14 +14,19 @@ pub struct Input<'src, 'toks> {
|
|||
newlines: Vec<usize>,
|
||||
}
|
||||
|
||||
pub const MEANINGLESS_TOKS: TokenSet = enum_set!(SyntaxKind::WHITESPACE | SyntaxKind::NEWLINE);
|
||||
|
||||
impl<'src, 'toks> Input<'src, 'toks> {
|
||||
pub fn new(raw_toks: &'toks Vec<(SyntaxKind, &'src str)>) -> Self {
|
||||
let meaningful = raw_toks
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(i, tok)| match tok.0 {
|
||||
SyntaxKind::WHITESPACE | SyntaxKind::NEWLINE => None,
|
||||
_ => Some(i),
|
||||
.filter_map(|(i, tok)| {
|
||||
if MEANINGLESS_TOKS.contains(tok.0) {
|
||||
None
|
||||
} else {
|
||||
Some(i)
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
let newlines = raw_toks
|
||||
|
|
|
@ -1,7 +1,11 @@
|
|||
use owo_colors::{unset_override, OwoColorize};
|
||||
use rowan::{GreenNode, GreenNodeBuilder, GreenNodeData, GreenTokenData, Language, NodeOrToken};
|
||||
use std::mem;
|
||||
|
||||
use crate::lst_parser::syntax_kind::{Lang, SyntaxKind};
|
||||
use crate::lst_parser::{
|
||||
input::MEANINGLESS_TOKS,
|
||||
syntax_kind::{Lang, SyntaxKind},
|
||||
};
|
||||
|
||||
use super::{error::SyntaxError, events::Event};
|
||||
|
||||
|
@ -14,47 +18,109 @@ impl std::fmt::Debug for Output {
|
|||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let mut errs: Vec<&SyntaxError> = self.errors.iter().collect();
|
||||
errs.reverse();
|
||||
debug_print_green_node(NodeOrToken::Node(&self.green_node), f, 0, &mut errs)
|
||||
|
||||
debug_print_green_node(NodeOrToken::Node(&self.green_node), f, 0, &mut errs, false)
|
||||
}
|
||||
}
|
||||
|
||||
fn debug_print_green_node(
|
||||
node: NodeOrToken<&GreenNodeData, &GreenTokenData>,
|
||||
f: &mut std::fmt::Formatter<'_>,
|
||||
f: &mut dyn std::fmt::Write,
|
||||
lvl: i32,
|
||||
errs: &mut Vec<&SyntaxError>,
|
||||
colored: bool,
|
||||
) -> std::fmt::Result {
|
||||
for _ in 0..lvl {
|
||||
f.write_str(" ")?;
|
||||
}
|
||||
|
||||
match node {
|
||||
if !colored {
|
||||
owo_colors::set_override(false);
|
||||
} else {
|
||||
owo_colors::set_override(true);
|
||||
}
|
||||
|
||||
let r = match node {
|
||||
NodeOrToken::Node(n) => {
|
||||
let kind = Lang::kind_from_raw(node.kind());
|
||||
if kind != SyntaxKind::PARSE_ERR {
|
||||
writeln!(f, "{:?} {{", Lang::kind_from_raw(node.kind()))?;
|
||||
writeln!(
|
||||
f,
|
||||
"{:?} {}",
|
||||
Lang::kind_from_raw(node.kind()).bright_yellow().bold(),
|
||||
"{".yellow()
|
||||
)?;
|
||||
} else {
|
||||
let err = errs
|
||||
.pop()
|
||||
.expect("all error syntax nodes should correspond to an error");
|
||||
.expect("all error syntax nodes should correspond to an error")
|
||||
.bright_red();
|
||||
|
||||
writeln!(f, "{:?}: {err:?} {{", kind)?;
|
||||
writeln!(
|
||||
f,
|
||||
"{:?}{} {err:?} {}",
|
||||
kind.bright_red().bold(),
|
||||
":".red(),
|
||||
"{".bright_red().bold()
|
||||
)?;
|
||||
}
|
||||
for c in n.children() {
|
||||
debug_print_green_node(c, f, lvl + 1, errs)?;
|
||||
debug_print_green_node(c, f, lvl + 1, errs, colored)?;
|
||||
}
|
||||
for _ in 0..lvl {
|
||||
f.write_str(" ")?;
|
||||
}
|
||||
f.write_str("}\n")
|
||||
if kind != SyntaxKind::PARSE_ERR {
|
||||
write!(f, "{}", "}\n".yellow())
|
||||
} else {
|
||||
write!(f, "{}", "}\n".bright_red().bold())
|
||||
}
|
||||
}
|
||||
NodeOrToken::Token(t) => {
|
||||
writeln!(f, "{:?} {:?};", Lang::kind_from_raw(t.kind()), t.text())
|
||||
let tok = Lang::kind_from_raw(t.kind());
|
||||
if MEANINGLESS_TOKS.contains(tok) {
|
||||
writeln!(
|
||||
f,
|
||||
"{:?} {:?}{}",
|
||||
Lang::kind_from_raw(t.kind()).white(),
|
||||
t.text().white(),
|
||||
";".white()
|
||||
)
|
||||
} else {
|
||||
writeln!(
|
||||
f,
|
||||
"{:?} {:?}{}",
|
||||
Lang::kind_from_raw(t.kind()).bright_cyan().bold(),
|
||||
t.text().green(),
|
||||
";".yellow()
|
||||
)
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if !colored {
|
||||
owo_colors::unset_override();
|
||||
}
|
||||
|
||||
r
|
||||
}
|
||||
|
||||
impl Output {
|
||||
pub fn debug_colored(&self) -> String {
|
||||
let mut out = String::new();
|
||||
let mut errs: Vec<&SyntaxError> = self.errors.iter().collect();
|
||||
errs.reverse();
|
||||
|
||||
let _ = debug_print_green_node(
|
||||
NodeOrToken::Node(&self.green_node),
|
||||
&mut out,
|
||||
0,
|
||||
&mut errs,
|
||||
true,
|
||||
);
|
||||
|
||||
out
|
||||
}
|
||||
pub fn from_parser_output(
|
||||
mut raw_toks: Vec<(SyntaxKind, &str)>,
|
||||
(mut events, errs): (Vec<Event>, Vec<SyntaxError>),
|
||||
|
|
|
@ -23,7 +23,7 @@ fn main() {
|
|||
let p_out = dbg!(parser.finish());
|
||||
let o = Output::from_parser_output(toks, p_out);
|
||||
|
||||
println!("Out: {:?}", o);
|
||||
println!("{}", o.debug_colored());
|
||||
|
||||
// let parse_res = parser::parse(&f);
|
||||
// println!("parse: {:?}", parse_res);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue