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

Function extract

codegraph-kernel/src/lua.rs:86–157  ·  view source on GitHub ↗
(file_path: &str, source: &str, language: &str)

Source from the content-addressed store, hash-verified

84}
85
86pub fn extract(file_path: &str, source: &str, language: &str) -> Result<EmitOut, String> {
87 let grammar = crate::langs::grammar_for(language).ok_or("no lua/luau grammar")?;
88 let t0 = std::time::Instant::now();
89 let mut parser = Parser::new();
90 parser
91 .set_language(&grammar)
92 .map_err(|e| format!("set_language({language}) failed: {e}"))?;
93 let tree = parser
94 .parse(source, None)
95 .ok_or_else(|| "parser returned null tree".to_string())?;
96 if tree.root_node().has_error() {
97 return Err("defer: parse tree contains errors — wasm recovery is canonical".to_string());
98 }
99
100 let mut w = Walker {
101 src: source,
102 file_path,
103 is_luau: language == "luau",
104 line_starts: util::line_starts(source),
105 arena: Arena::default(),
106 tables: Tables::default(),
107 stack: Vec::new(),
108 node_ids: Vec::new(),
109 defined_fn_names: HashSet::new(),
110 imported_names: HashSet::new(),
111 fn_ref_cands: Vec::new(),
112 };
113
114 // File node (tree-sitter.ts:508-521).
115 let line_count = source.bytes().filter(|b| *b == b'\n').count() as u32 + 1;
116 let base_name = file_path.rsplit(['/', '\\']).next().unwrap_or(file_path);
117 let mut flags = BoolFlags::default();
118 flags.set(FLAG_IS_EXPORTED, false);
119 let file_id = w.arena.put(&ids::file_node_id(file_path));
120 let name_ref = w.arena.put(base_name);
121 let qn_ref = w.arena.put(file_path);
122 w.tables.push_node(&NodeRow {
123 kind: node_kind_index("file").unwrap(),
124 visibility: 0,
125 flags,
126 start_line: 1,
127 end_line: line_count,
128 start_column: 0,
129 end_column: 0,
130 name: name_ref,
131 qualified_name: qn_ref,
132 id: file_id,
133 docstring: NONE_STR,
134 signature: NONE_STR,
135 decorators: NONE_STR,
136 type_parameters: NONE_STR,
137 return_type: NONE_STR,
138 extra_json: NONE_STR,
139 });
140 w.node_ids.push(ids::file_node_id(file_path));
141 w.stack.push(Scope { row: 0, kind: "file", name: base_name.to_string() });
142
143 // No packageTypes → no namespace node. Value-refs are language-gated off.

Callers

nothing calls this directly

Calls 13

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