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

Function extract

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

Source from the content-addressed store, hash-verified

74}
75
76pub fn extract(file_path: &str, source: &str) -> Result<EmitOut, String> {
77 let grammar = crate::langs::grammar_for("r").ok_or("no r grammar")?;
78 let t0 = std::time::Instant::now();
79 let mut parser = Parser::new();
80 parser
81 .set_language(&grammar)
82 .map_err(|e| format!("set_language(r) failed: {e}"))?;
83 let tree = parser
84 .parse(source, None)
85 .ok_or_else(|| "parser returned null tree".to_string())?;
86 if tree.root_node().has_error() {
87 return Err("defer: parse tree contains errors — wasm recovery is canonical".to_string());
88 }
89
90 let mut w = Walker {
91 src: source,
92 file_path,
93 line_starts: util::line_starts(source),
94 arena: Arena::default(),
95 tables: Tables::default(),
96 stack: Vec::new(),
97 };
98
99 // File node (tree-sitter.ts:508-521) — the only node with isExported set.
100 let line_count = source.bytes().filter(|b| *b == b'\n').count() as u32 + 1;
101 let base_name = file_path.rsplit(['/', '\\']).next().unwrap_or(file_path);
102 let mut flags = BoolFlags::default();
103 flags.set(FLAG_IS_EXPORTED, false);
104 let file_id = w.arena.put(&ids::file_node_id(file_path));
105 let name_ref = w.arena.put(base_name);
106 let qn_ref = w.arena.put(file_path);
107 w.tables.push_node(&NodeRow {
108 kind: node_kind_index("file").unwrap(),
109 visibility: 0,
110 flags,
111 start_line: 1,
112 end_line: line_count,
113 start_column: 0,
114 end_column: 0,
115 name: name_ref,
116 qualified_name: qn_ref,
117 id: file_id,
118 docstring: NONE_STR,
119 signature: NONE_STR,
120 decorators: NONE_STR,
121 type_parameters: NONE_STR,
122 return_type: NONE_STR,
123 extra_json: NONE_STR,
124 });
125 w.stack.push(Scope { row: 0, kind: "file", name: base_name.to_string() });
126
127 // extractFilePackage returns null (no packageTypes); both end-of-file
128 // flushes are no-ops for R (no fnRefSpec, VALUE_REF_LANGS gate).
129 w.visit(tree.root_node());
130 w.stack.pop();
131
132 let duration_ms = t0.elapsed().as_secs_f64() * 1000.0;
133 let meta = build_meta(&w.tables, w.arena.len(), NONE_STR, duration_ms);

Callers

nothing calls this directly

Calls 12

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
line_startsFunction · 0.70
unwrapMethod · 0.60
setMethod · 0.45
visitMethod · 0.45

Tested by

no test coverage detected