mirror of
https://forge.katzen.cafe/katzen-cafe/iowo.git
synced 2024-11-05 07:16:23 +01:00
Compare commits
3 commits
cee9b97dbf
...
958857cb58
Author | SHA1 | Date | |
---|---|---|---|
958857cb58 | |||
883b0c804e | |||
f7d05ead2c |
|
@ -62,13 +62,15 @@ pub enum SyntaxKind {
|
|||
PARSE_ERR,
|
||||
|
||||
// Meta SyntaxKinds
|
||||
ROOT,
|
||||
EOF,
|
||||
}
|
||||
|
||||
impl pawarser::parser::SyntaxElement for SyntaxKind {
|
||||
const EOF: Self = Self::EOF;
|
||||
const SYNTAX_EOF: Self = Self::EOF;
|
||||
|
||||
const ERROR: Self = Self::PARSE_ERR;
|
||||
const SYNTAX_ERROR: Self = Self::PARSE_ERR;
|
||||
const SYNTAX_ROOT: Self = Self::ROOT;
|
||||
}
|
||||
|
||||
impl From<SyntaxKind> for rowan::SyntaxKind {
|
||||
|
|
|
@ -26,9 +26,10 @@ where
|
|||
+ Eq,
|
||||
{
|
||||
/// EOF value. This will be used by the rest of the parser library to represent an EOF.
|
||||
const EOF: Self;
|
||||
const SYNTAX_EOF: Self;
|
||||
/// Error value. This will be used as a placeholder for associated respective errors.
|
||||
const ERROR: Self;
|
||||
const SYNTAX_ERROR: Self;
|
||||
const SYNTAX_ROOT: Self;
|
||||
}
|
||||
|
||||
pub struct Parser<'src, SyntaxKind: SyntaxElement, SyntaxErr: SyntaxError> {
|
||||
|
@ -119,6 +120,9 @@ impl<'src, 'toks, SyntaxKind: SyntaxElement, SyntaxErr: SyntaxError>
|
|||
let mut errors: Vec<SyntaxErr> = Vec::new();
|
||||
raw_toks.reverse();
|
||||
|
||||
// always have an implicit root node to avoid [`GreenNodeBuilder::finish()`] panicking due to multiple root elements.
|
||||
builder.start_node(SyntaxKind::SYNTAX_ROOT.into());
|
||||
|
||||
for i in 0..events.len() {
|
||||
match mem::replace(&mut events[i], Event::tombstone()) {
|
||||
Event::Start {
|
||||
|
@ -170,7 +174,7 @@ impl<'src, 'toks, SyntaxKind: SyntaxElement, SyntaxErr: SyntaxError>
|
|||
NodeKind::Syntax(kind) => builder.start_node(kind.into()),
|
||||
NodeKind::Error(err) => {
|
||||
errors.push(err);
|
||||
builder.start_node(SyntaxKind::ERROR.into())
|
||||
builder.start_node(SyntaxKind::SYNTAX_ERROR.into())
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
@ -184,6 +188,9 @@ impl<'src, 'toks, SyntaxKind: SyntaxElement, SyntaxErr: SyntaxError>
|
|||
}
|
||||
}
|
||||
|
||||
// finish SYNTAX_ROOT
|
||||
builder.finish_node();
|
||||
|
||||
ParserOutput {
|
||||
green_node: builder.finish(),
|
||||
errors,
|
||||
|
|
|
@ -34,7 +34,7 @@ impl<'src, SyntaxKind: SyntaxElement> Input<'src, SyntaxKind> {
|
|||
|
||||
pub fn kind(&self, idx: usize) -> SyntaxKind {
|
||||
let Some(meaningful_idx) = self.meaningful_toks.get(idx) else {
|
||||
return SyntaxKind::EOF;
|
||||
return SyntaxKind::SYNTAX_EOF;
|
||||
};
|
||||
|
||||
self.raw.get(*meaningful_idx).unwrap().0
|
||||
|
|
|
@ -31,34 +31,40 @@ fn debug_print_output<SyntaxKind: SyntaxElement, SyntaxErr: SyntaxError>(
|
|||
lvl: i32,
|
||||
errs: &mut Vec<&SyntaxErr>,
|
||||
) -> std::fmt::Result {
|
||||
for _ in 0..lvl {
|
||||
f.write_str(" ")?;
|
||||
if f.alternate() {
|
||||
for _ in 0..lvl {
|
||||
f.write_str(" ")?;
|
||||
}
|
||||
}
|
||||
let maybe_newline = if f.alternate() { "\n" } else { " " };
|
||||
|
||||
match node {
|
||||
NodeOrToken::Node(n) => {
|
||||
let kind: SyntaxKind = node.kind().into();
|
||||
if kind != SyntaxKind::ERROR {
|
||||
writeln!(f, "{:?} {{", kind)?;
|
||||
if kind != SyntaxKind::SYNTAX_ERROR {
|
||||
write!(f, "{:?} {{{maybe_newline}", kind)?;
|
||||
} else {
|
||||
let err = errs
|
||||
.pop()
|
||||
.expect("all error syntax nodes should correspond to an error");
|
||||
|
||||
writeln!(f, "{:?}: {err:?} {{", kind)?;
|
||||
write!(f, "{:?}: {err:?} {{{maybe_newline}", kind)?;
|
||||
}
|
||||
for c in n.children() {
|
||||
debug_print_output::<SyntaxKind, SyntaxErr>(c, f, lvl + 1, errs)?;
|
||||
}
|
||||
for _ in 0..lvl {
|
||||
f.write_str(" ")?;
|
||||
|
||||
if f.alternate() {
|
||||
for _ in 0..lvl {
|
||||
f.write_str(" ")?;
|
||||
}
|
||||
}
|
||||
f.write_str("}\n")
|
||||
write!(f, "}}{maybe_newline}")
|
||||
}
|
||||
NodeOrToken::Token(t) => {
|
||||
writeln!(
|
||||
write!(
|
||||
f,
|
||||
"{:?} {:?};",
|
||||
"{:?} {:?};{maybe_newline}",
|
||||
Into::<SyntaxKind>::into(t.kind()),
|
||||
t.text()
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue