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

Function extract

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

Source from the content-addressed store, hash-verified

399}
400
401pub fn extract(file_path: &str, source: &str, language: &str) -> Result<EmitOut, String> {
402 let variant = match language {
403 "c" => Variant::C,
404 "cpp" => Variant::Cpp,
405 other => return Err(format!("ccpp walker got language '{other}'")),
406 };
407 let grammar = crate::langs::grammar_for(language).ok_or("no c/cpp grammar")?;
408 let t0 = std::time::Instant::now();
409 let mut parser = Parser::new();
410 parser
411 .set_language(&grammar)
412 .map_err(|e| format!("set_language({language}) failed: {e}"))?;
413 let tree = crate::parse_budget::parse(&mut parser, source)?;
414 // Measurement hatch (parity sweeps only — never set in production): skip
415 // the defer so the sweep can QUANTIFY how often UTF-8 vs UTF-16 error
416 // recovery actually diverges on this language's erroring files.
417 let no_defer = std::env::var("CODEGRAPH_KERNEL_CCPP_ERROR_EXTRACT").as_deref() == Ok("1");
418 if tree.root_node().has_error() && !no_defer {
419 return Err("defer: parse tree contains errors — wasm recovery is canonical".to_string());
420 }
421
422 let brace_scopes = if variant == Variant::Cpp && tree.root_node().has_error() {
423 brace_scopes::scan(source)
424 } else {
425 None
426 };
427 let walk_declared_types =
428 variant == Variant::C || !tree.root_node().has_error() || brace_scopes.is_some();
429 let mut w = Walker {
430 src: source,
431 file_path,
432 variant,
433 line_starts: util::line_starts(source),
434 arena: Arena::default(),
435 node_id_allocator: ids::NodeIdAllocator::default(),
436 tables: Tables::default(),
437 stack: Vec::new(),
438 nodes_meta: Vec::new(),
439 node_ids: Vec::new(),
440 namespace_prefix: Vec::new(),
441 local_fn_ptrs: HashMap::new(),
442 defined_fn_names: HashSet::new(),
443 imported_names: HashSet::new(),
444 fn_ref_cands: Vec::new(),
445 fs_values: HashMap::new(),
446 fs_value_counts: HashMap::new(),
447 value_scopes: Vec::new(),
448 brace_scopes,
449 class_scopes: NestedIntervals::new(),
450 class_scope_rows: HashSet::new(),
451 walk_declared_types,
452 };
453
454 let line_count = source.bytes().filter(|b| *b == b'\n').count() as u32 + 1;
455 let base_name = file_path.rsplit(['/', '\\']).next().unwrap_or(file_path);
456 let mut flags = BoolFlags::default();
457 flags.set(FLAG_IS_EXPORTED, false);
458 let file_id = w.arena.put(&ids::file_node_id(file_path));

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
scanFunction · 0.70
putMethod · 0.65

Tested by

no test coverage detected