1use std::collections::HashMap;
8
9use crate::syntax::ast::{
10 AssignOp, ChoiceOpt, Expr, IdentRef, LabelDecl, LabelRef, Module, Param, PathElem, Pattern,
11 Pos, Stmt, StrPart, TyName, TypeDeclKind,
12};
13use crate::syntax::parser::{IdentMap, parse_with, scan_identities};
14
15#[derive(Clone, Copy, Debug, PartialEq, Eq)]
17pub enum SymbolKind {
18 Label,
20 Fn,
22 Var,
24 Identity,
26 Struct,
28 Enum,
30 Variant,
32 Field,
34 Param,
36 Local,
38}
39
40#[derive(Clone, Copy, Debug, PartialEq, Eq)]
42pub struct Span {
43 pub line: u32,
45 pub col: u32,
47 pub len: u32,
49}
50
51impl Span {
52 pub fn contains(&self, line: u32, col: u32) -> bool {
54 self.line == line && col >= self.col && col <= self.col + self.len
55 }
56}
57
58#[derive(Clone, Debug, PartialEq, Eq)]
60pub struct Symbol {
61 pub kind: SymbolKind,
63 pub name: String,
65 pub module: String,
67 pub span: Span,
69 pub detail: String,
71 pub container: Option<String>,
73}
74
75#[derive(Clone, Debug, PartialEq, Eq)]
77pub struct Reference {
78 pub module: String,
80 pub span: Span,
82 pub target: usize,
84}
85
86#[derive(Clone, Debug, Default, PartialEq, Eq)]
88pub struct Analysis {
89 pub symbols: Vec<Symbol>,
91 pub references: Vec<Reference>,
93}
94
95impl Analysis {
96 pub fn symbol_at(&self, module: &str, line: u32, col: u32) -> Option<usize> {
98 let by_ref = self
99 .references
100 .iter()
101 .find(|r| r.module == module && r.span.contains(line, col))
102 .map(|r| r.target);
103 by_ref.or_else(|| {
104 self.symbols
105 .iter()
106 .position(|s| s.module == module && s.span.contains(line, col))
107 })
108 }
109
110 pub fn references_to(&self, symbol: usize) -> impl Iterator<Item = &Reference> {
112 self.references.iter().filter(move |r| r.target == symbol)
113 }
114}
115
116pub fn analyze(modules: &[(&str, &str)]) -> Analysis {
118 let mut idents = IdentMap::new();
119 for (name, source) in modules {
120 idents
121 .entry((*name).to_string())
122 .or_insert_with(|| scan_identities(source));
123 }
124 let mut out = Analysis::default();
125 let mut parsed: Vec<(&str, Module, Vec<&str>, Tops)> = Vec::new();
126 for (name, source) in modules {
127 let (ast, _) = parse_with(source, name, &idents);
128 let lines = source_lines(source);
129 let tops = declare(&mut out, name, &ast, &lines);
130 parsed.push((name, ast, lines, tops));
131 }
132 let all: HashMap<&str, &Tops> = parsed.iter().map(|(n, _, _, t)| (*n, t)).collect();
133 for (name, ast, lines, tops) in &parsed {
134 let imports: HashMap<&str, &str> = ast
135 .imports
136 .iter()
137 .map(|i| (i.local.as_str(), i.path.as_str()))
138 .collect();
139 let mut w = Walker {
140 out: &mut out,
141 module: name,
142 lines,
143 tops,
144 all: &all,
145 imports,
146 scopes: Vec::new(),
147 current: String::new(),
148 };
149 w.module(ast);
150 }
151 out
152}
153
154pub fn is_name_char(c: char) -> bool {
156 c.is_alphanumeric() || c == '_'
157}
158
159mod declare;
160#[cfg(test)]
161mod tests;
162mod walk;
163
164use declare::*;
165use walk::*;