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

Function extract

codegraph-kernel/src/ccpp/mod.rs:351–437  ·  view source on GitHub ↗
(file_path: &str, source: &str, language: &str)

Source from the content-addressed store, hash-verified

349}
350
351pub fn extract(file_path: &str, source: &str, language: &str) -> Result<EmitOut, String> {
352 let variant = match language {
353 "c" => Variant::C,
354 "cpp" => Variant::Cpp,
355 other => return Err(format!("ccpp walker got language '{other}'")),
356 };
357 let grammar = crate::langs::grammar_for(language).ok_or("no c/cpp grammar")?;
358 let t0 = std::time::Instant::now();
359 let mut parser = Parser::new();
360 parser
361 .set_language(&grammar)
362 .map_err(|e| format!("set_language({language}) failed: {e}"))?;
363 let tree = parser
364 .parse(source, None)
365 .ok_or_else(|| "parser returned null tree".to_string())?;
366 // Measurement hatch (parity sweeps only — never set in production): skip
367 // the defer so the sweep can QUANTIFY how often UTF-8 vs UTF-16 error
368 // recovery actually diverges on this language's erroring files.
369 let no_defer = std::env::var("CODEGRAPH_KERNEL_CCPP_ERROR_EXTRACT").as_deref() == Ok("1");
370 if tree.root_node().has_error() && !no_defer {
371 return Err("defer: parse tree contains errors — wasm recovery is canonical".to_string());
372 }
373
374 let mut w = Walker {
375 src: source,
376 file_path,
377 variant,
378 line_starts: util::line_starts(source),
379 arena: Arena::default(),
380 tables: Tables::default(),
381 stack: Vec::new(),
382 nodes_meta: Vec::new(),
383 node_ids: Vec::new(),
384 namespace_prefix: Vec::new(),
385 local_fn_ptrs: HashMap::new(),
386 defined_fn_names: HashSet::new(),
387 imported_names: HashSet::new(),
388 fn_ref_cands: Vec::new(),
389 fs_values: HashMap::new(),
390 fs_value_counts: HashMap::new(),
391 value_scopes: Vec::new(),
392 };
393
394 let line_count = source.bytes().filter(|b| *b == b'\n').count() as u32 + 1;
395 let base_name = file_path.rsplit(['/', '\\']).next().unwrap_or(file_path);
396 let mut flags = BoolFlags::default();
397 flags.set(FLAG_IS_EXPORTED, false);
398 let file_id = w.arena.put(&ids::file_node_id(file_path));
399 let name_ref = w.arena.put(base_name);
400 let qn_ref = w.arena.put(file_path);
401 w.tables.push_node(&NodeRow {
402 kind: node_kind_index("file").unwrap(),
403 visibility: 0,
404 flags,
405 start_line: 1,
406 end_line: line_count,
407 start_column: 0,
408 end_column: 0,

Callers

nothing calls this directly

Calls 14

grammar_forFunction · 0.85
file_node_idFunction · 0.85
node_kind_indexFunction · 0.85
build_metaFunction · 0.85
putMethod · 0.80
push_nodeMethod · 0.80
lenMethod · 0.80
into_vecMethod · 0.80
unwrapMethod · 0.60
line_startsFunction · 0.50
setMethod · 0.45
visit_nodeMethod · 0.45

Tested by

no test coverage detected