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

Function extract

codegraph-kernel/src/swift.rs:236–310  ·  view source on GitHub ↗
(file_path: &str, source: &str)

Source from the content-addressed store, hash-verified

234}
235
236pub fn extract(file_path: &str, source: &str) -> Result<EmitOut, String> {
237 let grammar = crate::langs::grammar_for("swift").ok_or("no swift grammar")?;
238 let t0 = std::time::Instant::now();
239 let mut parser = Parser::new();
240 parser
241 .set_language(&grammar)
242 .map_err(|e| format!("set_language(swift) failed: {e}"))?;
243 let tree = parser
244 .parse(source, None)
245 .ok_or_else(|| "parser returned null tree".to_string())?;
246 if tree.root_node().has_error() {
247 return Err("defer: parse tree contains errors — wasm recovery is canonical".to_string());
248 }
249
250 let mut w = Walker {
251 src: source,
252 file_path,
253 line_starts: util::line_starts(source),
254 arena: Arena::default(),
255 node_id_allocator: ids::NodeIdAllocator::default(),
256 tables: Tables::default(),
257 stack: Vec::new(),
258 node_ids: Vec::new(),
259 defined_fn_names: HashSet::new(),
260 imported_names: HashSet::new(),
261 fn_ref_cands: Vec::new(),
262 fs_values: HashMap::new(),
263 fs_value_counts: HashMap::new(),
264 value_scopes: Vec::new(),
265 };
266
267 let line_count = source.bytes().filter(|b| *b == b'\n').count() as u32 + 1;
268 let base_name = file_path.rsplit(['/', '\\']).next().unwrap_or(file_path);
269 let mut flags = BoolFlags::default();
270 flags.set(FLAG_IS_EXPORTED, false);
271 let file_id = w.arena.put(&ids::file_node_id(file_path));
272 let name_ref = w.arena.put(base_name);
273 let qn_ref = w.arena.put(file_path);
274 w.tables.push_node(&NodeRow {
275 kind: node_kind_index("file").unwrap(),
276 visibility: 0,
277 flags,
278 start_line: 1,
279 end_line: line_count,
280 start_column: 0,
281 end_column: 0,
282 name: name_ref,
283 qualified_name: qn_ref,
284 id: file_id,
285 docstring: NONE_STR,
286 signature: NONE_STR,
287 decorators: NONE_STR,
288 type_parameters: NONE_STR,
289 return_type: NONE_STR,
290 extra_json: NONE_STR,
291 });
292 w.node_ids.push(ids::file_node_id(file_path));
293 w.stack.push(Scope { row: 0, kind: "file", name: base_name.to_string() });

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
parseMethod · 0.80
bytesMethod · 0.80
nextMethod · 0.80
push_nodeMethod · 0.80
pushMethod · 0.80
lenMethod · 0.80
into_vecMethod · 0.80
line_startsFunction · 0.70

Tested by

no test coverage detected