bbnf_analysis/state/types.rs
1use std::collections::{HashMap, HashSet};
2
3use ls_types::*;
4
5use super::pretty::PrettyInfo;
6
7/// Information about a single production rule.
8#[derive(Debug, Clone)]
9pub struct RuleInfo {
10 /// The rule name (LHS nonterminal).
11 pub name: String,
12 /// Byte offset range of the LHS name.
13 pub name_span: (usize, usize),
14 /// Byte offset range of the entire rule (LHS = RHS ;).
15 pub full_span: (usize, usize),
16 /// Pretty-printed RHS for hover display.
17 pub rhs_text: String,
18 /// All nonterminal references in the RHS.
19 pub references: Vec<ReferenceInfo>,
20}
21
22/// A reference to a nonterminal in the RHS of a rule.
23#[derive(Debug, Clone)]
24pub struct ReferenceInfo {
25 /// The referenced nonterminal name.
26 pub name: String,
27 /// Byte offset range of this reference.
28 pub span: (usize, usize),
29}
30
31/// Semantic token data for a single token.
32#[derive(Debug, Clone)]
33pub struct SemanticTokenInfo {
34 pub span: (usize, usize),
35 pub token_type: u32,
36}
37
38/// Owned representation of an import directive.
39#[derive(Debug, Clone)]
40pub struct ImportInfo {
41 /// The path string from the import.
42 pub path: String,
43 /// Byte offset range of the entire import directive.
44 pub span: (usize, usize),
45 /// If `Some`, selective import; if `None`, glob import.
46 pub items: Option<Vec<String>>,
47}
48
49/// Owned representation of a @recover directive.
50#[derive(Debug, Clone)]
51pub struct RecoverInfo {
52 /// The name of the rule to wrap with recovery.
53 pub rule_name: String,
54 /// Byte offset range of the entire recover directive.
55 pub span: (usize, usize),
56 /// Byte offset range of the rule name within the directive.
57 pub rule_name_span: (usize, usize),
58 /// The sync expression source text (e.g. `";" | "}"` or `/[;\n]/`).
59 pub sync_expr_text: String,
60}
61
62/// Owned representation of a @no_collapse directive.
63#[derive(Debug, Clone)]
64pub struct NoCollapseInfo {
65 /// The name of the rule to prevent span collapse for.
66 pub rule_name: String,
67 /// Byte offset range of the entire directive.
68 pub span: (usize, usize),
69 /// Byte offset range of the rule name within the directive.
70 pub rule_name_span: (usize, usize),
71}
72
73/// Pre-analyzed document state -- all data is owned (no lifetimes).
74#[derive(Debug, Clone)]
75pub struct DocumentInfo {
76 pub rules: Vec<RuleInfo>,
77 pub diagnostics: Vec<Diagnostic>,
78 /// name -> index into `rules`
79 pub rule_index: HashMap<String, usize>,
80 /// Semantic tokens in document order.
81 pub semantic_tokens: Vec<SemanticTokenInfo>,
82 /// FIRST set labels per rule name (formatted for display).
83 pub first_set_labels: HashMap<String, String>,
84 /// Rules that can derive the empty string.
85 pub nullable_rules: HashSet<String>,
86 /// Rules that participate in a cycle, with their cycle path.
87 pub cyclic_rule_paths: HashMap<String, String>,
88 /// Import directives parsed from the document.
89 pub imports: Vec<ImportInfo>,
90 /// Recover directives parsed from the document.
91 pub recovers: Vec<RecoverInfo>,
92 /// No-collapse directives parsed from the document.
93 pub no_collapses: Vec<NoCollapseInfo>,
94 /// Pretty directives parsed from the document.
95 pub pretties: Vec<PrettyInfo>,
96}
97
98/// Diagnostic info extracted from the parser state (owned, no lifetimes).
99#[derive(Debug, Clone)]
100pub struct ParseDiagnostics {
101 /// Parser offset after parsing (how far it consumed).
102 pub offset: usize,
103 /// Furthest offset reached during parsing (for error reporting).
104 pub furthest_offset: usize,
105 /// If parsing panicked, the panic message.
106 pub panic_message: Option<String>,
107}
108
109/// Semantic token type indices matching our legend.
110pub mod token_types {
111 pub const RULE_DEFINITION: u32 = 0;
112 pub const RULE_REFERENCE: u32 = 1;
113 pub const STRING: u32 = 2;
114 pub const REGEXP: u32 = 3;
115 pub const KEYWORD: u32 = 5;
116}