use alloc::string::String;
use core::fmt;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
InvalidName {
name: String,
},
NoRules,
DuplicateRule {
name: String,
},
HiddenStart {
name: String,
},
UndefinedSymbol {
symbol: String,
rule: String,
},
EmptyString {
rule: String,
},
EmptyChoice {
rule: String,
},
SymbolInToken {
symbol: String,
rule: String,
},
MatchesEmpty {
rule: String,
},
IndirectRecursion {
rule: String,
},
WordNotToken {
name: String,
},
InvalidExternal {
name: String,
},
InvalidInline {
name: String,
},
InvalidSupertype {
name: String,
},
UnknownNode {
name: String,
},
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidName { name } => write!(
f,
"`{name}` is not a valid name: names start with an ASCII letter or `_` \
and continue with ASCII letters, digits, or `_`"
),
Self::NoRules => {
f.write_str("the grammar has no rules; tree-sitter needs at least one")
}
Self::DuplicateRule { name } => write!(f, "rule `{name}` is defined more than once"),
Self::HiddenStart { name } => write!(
f,
"the start rule `{name}` is hidden; tree-sitter requires the first rule to be visible"
),
Self::UndefinedSymbol { symbol, rule } => {
write!(f, "`{rule}` refers to undefined symbol `{symbol}`")
}
Self::EmptyString { rule } => match rule.split_once(": ") {
Some((name, problem)) => {
write!(f, "`{name}` contains an invalid pattern: {problem}")
}
None => write!(f, "`{rule}` contains an empty string or pattern"),
},
Self::EmptyChoice { rule } => {
write!(f, "`{rule}` contains a choice with no alternatives")
}
Self::SymbolInToken { symbol, rule } => write!(
f,
"`{rule}` uses symbol `{symbol}` inside a token; token contents must be \
strings and patterns"
),
Self::MatchesEmpty { rule } => write!(
f,
"rule `{rule}` can match the empty string where tree-sitter needs a token: \
it is referred to by another rule, or it repeats something that can match nothing"
),
Self::IndirectRecursion { rule } => write!(
f,
"rule `{rule}` is part of a cycle of rules that can each be just the next one"
),
Self::WordNotToken { name } => write!(
f,
"word rule `{name}` is not a token: its body must be a string, a pattern, \
or a token used nowhere else"
),
Self::InvalidExternal { name } => write!(
f,
"external `{name}` shares its name with a rule that is not a token"
),
Self::InvalidInline { name } => write!(f, "rule `{name}` cannot be inlined"),
Self::InvalidSupertype { name } => write!(
f,
"supertype `{name}` must be a rule whose every alternative is a single node"
),
Self::UnknownNode { name } => write!(
f,
"the tree has a node named `{name}`, which is not in the grammar"
),
}
}
}
impl core::error::Error for Error {}
#[cfg(test)]
mod tests {
use alloc::string::ToString;
use super::*;
#[test]
fn test_display_names_the_offending_items() {
let e = Error::UndefinedSymbol {
symbol: "item".into(),
rule: "list".into(),
};
assert_eq!(e.to_string(), "`list` refers to undefined symbol `item`");
let e = Error::MatchesEmpty {
rule: "args".into(),
};
assert!(
e.to_string()
.starts_with("rule `args` can match the empty string")
);
assert!(Error::NoRules.to_string().contains("no rules"));
}
#[test]
fn test_display_tells_empty_from_invalid_patterns() {
let empty = Error::EmptyString { rule: "x".into() };
assert_eq!(empty.to_string(), "`x` contains an empty string or pattern");
let invalid = Error::EmptyString {
rule: "x: unterminated group at byte 0 of `(`".into(),
};
assert_eq!(
invalid.to_string(),
"`x` contains an invalid pattern: unterminated group at byte 0 of `(`"
);
}
#[test]
fn test_every_variant_displays_something() {
let s = || String::from("x");
let all = [
Error::InvalidName { name: s() },
Error::NoRules,
Error::DuplicateRule { name: s() },
Error::HiddenStart { name: s() },
Error::UndefinedSymbol {
symbol: s(),
rule: s(),
},
Error::EmptyString { rule: s() },
Error::EmptyChoice { rule: s() },
Error::SymbolInToken {
symbol: s(),
rule: s(),
},
Error::MatchesEmpty { rule: s() },
Error::IndirectRecursion { rule: s() },
Error::WordNotToken { name: s() },
Error::InvalidExternal { name: s() },
Error::InvalidInline { name: s() },
Error::InvalidSupertype { name: s() },
Error::UnknownNode { name: s() },
];
for e in all {
assert!(e.to_string().contains('x') || matches!(e, Error::NoRules));
}
}
}