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

Function extract

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

Source from the content-addressed store, hash-verified

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

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