#[non_exhaustive]pub struct Grammar {
pub name: String,
pub rules: BTreeMap<String, Production>,
pub supertypes: HashSet<String>,
pub extras: HashSet<String>,
pub subtypes: HashMap<String, HashSet<String>>,
pub yield_sets: HashMap<String, HashSet<String>>,
pub node_type_children: HashMap<String, HashSet<String>>,
pub external_alias_map: HashMap<String, String>,
pub inline_brace_rules: HashSet<String>,
}Expand description
A grammar’s production-rule table, deserialized from grammar.json.
Only the fields the emitter consumes are decoded; precedences, conflicts, externals, and other parser-only metadata are ignored.
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.name: StringGrammar name (e.g. "rust", "typescript").
rules: BTreeMap<String, Production>Map from rule name (a vertex kind on the schema side) to production. Entries are kept in lexical order so iteration is deterministic.
supertypes: HashSet<String>Supertypes declared in the grammar’s supertypes field. A
supertype is a rule whose body is a CHOICE of SYMBOL
references; tree-sitter parsers report a node’s kind as one
of the subtypes (e.g. identifier, typed_parameter) rather
than the supertype name (parameter), so the emitter needs to
know that a child kind in a subtype set should match the
supertype name when a SYMBOL references it.
extras: HashSet<String>Tree-sitter extras rules: the named symbols (typically comments)
that tree-sitter skips at parse time but records as children of the
surrounding vertex. They appear nowhere in the production grammar,
so the rule walker cannot reconcile them against the cursor — the
emit pass therefore drains them as a side channel: at vertex entry
and between REPEAT iterations any leading extras-kind edges are
consumed and emitted directly. The set is populated at
Grammar::from_bytes by collecting every SYMBOL { name } and
named ALIAS { value, named: true } under the top-level extras
array. Pattern-only extras (e.g. \s whitespace) are not vertex
kinds and are excluded.
subtypes: HashMap<String, HashSet<String>>Precomputed subtyping closure: subtypes[symbol_name] is the
set of vertex kinds that satisfy a SYMBOL symbol_name
reference on the schema side.
Built once at Grammar::from_bytes time by walking each
hidden rule (_-prefixed), declared supertype, and named
ALIAS { value: K, ... } production to its leaf SYMBOLs and
recording the closure. This replaces the prior heuristic
kind_satisfies_symbol that walked the rule body on every
query: lookups are now O(1) and the relation is exactly the
transitive closure of “is reachable via hidden / supertype /
alias dispatch”, with no over-expansion through non-hidden
non-supertype rule references.
yield_sets: HashMap<String, HashSet<String>>Precomputed Yield sets: yield_sets[rule_name] is the set of
concrete vertex kinds that can appear as the first named
child when that rule’s production is taken.
Defined inductively:
Yield(SYMBOL S)where S is hidden/supertype =Yield(rules[S])Yield(SYMBOL S)where S is concrete ={S}Yield(SEQ [M1, ...])=Yield(M1)(only first member)Yield(CHOICE [M1, ..., Mn])=⋃ Yield(Mi)Yield(OPTIONAL { c })=Yield(c) ∪ {ε}Yield(BLANK)={ε}- Wrappers (PREC*, TOKEN, FIELD, REPEAT, etc.) =
Yield(content) Yield(STRING)=Yield(PATTERN)=∅Yield(ALIAS { value: V, named: true })={V}
Epsilon is represented as the empty string "".
node_type_children: HashMap<String, HashSet<String>>Child kinds allowed per parent kind, derived from node-types.json.
Maps parent kind to the set of ALL named child kinds that tree-sitter’s
parser can produce for that parent (from both children.types and
fields.*.types). Used by augment_subtypes_from_node_types to
close the grammar/parser divergence gap.
external_alias_map: HashMap<String, String>Anonymous ALIAS values for external scanner tokens. Maps external
symbol name (e.g. _ternary_qmark) to the ALIAS value string
(e.g. "?"). Built by scanning grammar.json rule bodies for
ALIAS { content: SYMBOL S, named: false, value: V } where S
has no grammar rule.
inline_brace_rules: HashSet<String>Rules whose {/} STRING tokens are inline delimiters (e.g.
string interpolation) rather than block scopes. Identified
structurally: a rule whose SEQ contains { and } but no
REPEAT/REPEAT1 between them.
Implementations§
Source§impl Grammar
impl Grammar
Sourcepub fn from_bytes(protocol: &str, bytes: &[u8]) -> Result<Self, ParseError>
pub fn from_bytes(protocol: &str, bytes: &[u8]) -> Result<Self, ParseError>
Parse a grammar’s grammar.json bytes.
Builds the subtyping closure as part of construction so every
downstream lookup is O(1). The closure is the least relation
containing (K, K) for every rule key K and closed under:
- hidden-rule expansion: if
Sis hidden and a SYMBOLSmay reach SYMBOLK, thenK ⊑ S. - supertype expansion: if
Sis in the grammar’s supertypes block andKis one ofS’s alternatives, thenK ⊑ S. - alias renaming: if a rule body contains
ALIAS { content: SYMBOL R, value: A, named: true }whereRreaches kindK(orK = Rwhen no further hop), thenA ⊑ RandK ⊑ A.
§Errors
Returns ParseError::EmitFailed when the bytes are not a
valid grammar.json document.
Sourcepub fn from_bytes_with_node_types(
protocol: &str,
grammar_bytes: &[u8],
node_types_bytes: Option<&[u8]>,
) -> Result<Self, ParseError>
pub fn from_bytes_with_node_types( protocol: &str, grammar_bytes: &[u8], node_types_bytes: Option<&[u8]>, ) -> Result<Self, ParseError>
Parse a grammar from both grammar.json and optionally
node-types.json bytes.
§Errors
Returns ParseError::EmitFailed when grammar_bytes is
not a valid grammar.json document.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Grammar
impl<'de> Deserialize<'de> for Grammar
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Auto Trait Implementations§
impl Freeze for Grammar
impl RefUnwindSafe for Grammar
impl Send for Grammar
impl Sync for Grammar
impl Unpin for Grammar
impl UnsafeUnpin for Grammar
impl UnwindSafe for Grammar
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<D> OwoColorize for D
impl<D> OwoColorize for D
Source§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
Source§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
Source§fn black(&self) -> FgColorDisplay<'_, Black, Self>
fn black(&self) -> FgColorDisplay<'_, Black, Self>
Source§fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
Source§fn red(&self) -> FgColorDisplay<'_, Red, Self>
fn red(&self) -> FgColorDisplay<'_, Red, Self>
Source§fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
Source§fn green(&self) -> FgColorDisplay<'_, Green, Self>
fn green(&self) -> FgColorDisplay<'_, Green, Self>
Source§fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
Source§fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
Source§fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
Source§fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
Source§fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
Source§fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
Source§fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
Source§fn white(&self) -> FgColorDisplay<'_, White, Self>
fn white(&self) -> FgColorDisplay<'_, White, Self>
Source§fn on_white(&self) -> BgColorDisplay<'_, White, Self>
fn on_white(&self) -> BgColorDisplay<'_, White, Self>
Source§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
Source§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
Source§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
Source§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
Source§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
Source§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
Source§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
Source§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
Source§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
Source§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
Source§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
Source§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
Source§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
Source§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
Source§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
Source§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
Source§fn bold(&self) -> BoldDisplay<'_, Self>
fn bold(&self) -> BoldDisplay<'_, Self>
Source§fn dimmed(&self) -> DimDisplay<'_, Self>
fn dimmed(&self) -> DimDisplay<'_, Self>
Source§fn italic(&self) -> ItalicDisplay<'_, Self>
fn italic(&self) -> ItalicDisplay<'_, Self>
Source§fn underline(&self) -> UnderlineDisplay<'_, Self>
fn underline(&self) -> UnderlineDisplay<'_, Self>
Source§fn blink(&self) -> BlinkDisplay<'_, Self>
fn blink(&self) -> BlinkDisplay<'_, Self>
Source§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
Source§fn reversed(&self) -> ReversedDisplay<'_, Self>
fn reversed(&self) -> ReversedDisplay<'_, Self>
Source§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
Source§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg or
a color-specific method, such as OwoColorize::green, Read moreSource§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg or
a color-specific method, such as OwoColorize::on_yellow, Read more