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

Function extract

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

Source from the content-addressed store, hash-verified

85}
86
87pub fn extract(file_path: &str, source: &str, language: &str) -> Result<EmitOut, String> {
88 let grammar = crate::langs::grammar_for(language).ok_or("no lua/luau grammar")?;
89 let t0 = std::time::Instant::now();
90 let mut parser = Parser::new();
91 parser
92 .set_language(&grammar)
93 .map_err(|e| format!("set_language({language}) failed: {e}"))?;
94 let tree = crate::parse_budget::parse(&mut parser, source)?;
95 if tree.root_node().has_error() {
96 return Err("defer: parse tree contains errors — wasm recovery is canonical".to_string());
97 }
98
99 let mut w = Walker {
100 src: source,
101 file_path,
102 is_luau: language == "luau",
103 line_starts: util::line_starts(source),
104 arena: Arena::default(),
105 node_id_allocator: ids::NodeIdAllocator::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.
144 w.visit(tree.root_node());

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
parseFunction · 0.70
line_startsFunction · 0.70

Tested by

no test coverage detected