MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / extract

Function extract

codegraph-kernel/src/rustlang.rs:141–220  ·  view source on GitHub ↗
(file_path: &str, source: &str)

Source from the content-addressed store, hash-verified

139}
140
141pub fn extract(file_path: &str, source: &str) -> Result<EmitOut, String> {
142 let grammar = crate::langs::grammar_for("rust").ok_or("no rust grammar")?;
143 let t0 = std::time::Instant::now();
144 let mut parser = Parser::new();
145 parser
146 .set_language(&grammar)
147 .map_err(|e| format!("set_language(rust) failed: {e}"))?;
148 let tree = crate::parse_budget::parse(&mut parser, source)?;
149 if tree.root_node().has_error() {
150 return Err("defer: parse tree contains errors — wasm recovery is canonical".to_string());
151 }
152
153 let mut w = Walker {
154 src: source,
155 file_path,
156 line_starts: util::line_starts(source),
157 arena: Arena::default(),
158 node_id_allocator: ids::NodeIdAllocator::default(),
159 tables: Tables::default(),
160 stack: Vec::new(),
161 nodes_meta: Vec::new(),
162 node_ids: Vec::new(),
163 defined_fn_names: HashSet::new(),
164 imported_names: HashSet::new(),
165 fn_ref_cands: Vec::new(),
166 macro_fn_ref_cands: Vec::new(),
167 declared_macros: if source.contains("macro_rules!") {
168 rust_declared_expression_macros(tree.root_node(), source)
169 } else {
170 HashSet::new()
171 },
172 fs_values: HashMap::new(),
173 fs_value_counts: HashMap::new(),
174 value_scopes: Vec::new(),
175 };
176
177 let line_count = source.bytes().filter(|b| *b == b'\n').count() as u32 + 1;
178 let base_name = file_path.rsplit(['/', '\\']).next().unwrap_or(file_path);
179 let mut flags = BoolFlags::default();
180 flags.set(FLAG_IS_EXPORTED, false);
181 let file_id = w.arena.put(&ids::file_node_id(file_path));
182 let name_ref = w.arena.put(base_name);
183 let qn_ref = w.arena.put(file_path);
184 w.tables.push_node(&NodeRow {
185 kind: node_kind_index("file").unwrap(),
186 visibility: 0,
187 flags,
188 start_line: 1,
189 end_line: line_count,
190 start_column: 0,
191 end_column: 0,
192 name: name_ref,
193 qualified_name: qn_ref,
194 id: file_id,
195 docstring: NONE_STR,
196 signature: NONE_STR,
197 decorators: NONE_STR,
198 type_parameters: NONE_STR,

Callers

nothing calls this directly

Calls 15

grammar_forFunction · 0.85
file_node_idFunction · 0.85
node_kind_indexFunction · 0.85
build_metaFunction · 0.85
bytesMethod · 0.80
nextMethod · 0.80
push_nodeMethod · 0.80
pushMethod · 0.80
lenMethod · 0.80
into_vecMethod · 0.80
parseFunction · 0.70

Tested by

no test coverage detected