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

Function extract

codegraph-kernel/src/lua.rs:87–159  ·  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 = parser
95 .parse(source, None)
96 .ok_or_else(|| "parser returned null tree".to_string())?;
97 if tree.root_node().has_error() {
98 return Err("defer: parse tree contains errors — wasm recovery is canonical".to_string());
99 }
100
101 let mut w = Walker {
102 src: source,
103 file_path,
104 is_luau: language == "luau",
105 line_starts: util::line_starts(source),
106 arena: Arena::default(),
107 node_id_allocator: ids::NodeIdAllocator::default(),
108 tables: Tables::default(),
109 stack: Vec::new(),
110 node_ids: Vec::new(),
111 defined_fn_names: HashSet::new(),
112 imported_names: HashSet::new(),
113 fn_ref_cands: Vec::new(),
114 };
115
116 // File node (tree-sitter.ts:508-521).
117 let line_count = source.bytes().filter(|b| *b == b'\n').count() as u32 + 1;
118 let base_name = file_path.rsplit(['/', '\\']).next().unwrap_or(file_path);
119 let mut flags = BoolFlags::default();
120 flags.set(FLAG_IS_EXPORTED, false);
121 let file_id = w.arena.put(&ids::file_node_id(file_path));
122 let name_ref = w.arena.put(base_name);
123 let qn_ref = w.arena.put(file_path);
124 w.tables.push_node(&NodeRow {
125 kind: node_kind_index("file").unwrap(),
126 visibility: 0,
127 flags,
128 start_line: 1,
129 end_line: line_count,
130 start_column: 0,
131 end_column: 0,
132 name: name_ref,
133 qualified_name: qn_ref,
134 id: file_id,
135 docstring: NONE_STR,
136 signature: NONE_STR,
137 decorators: NONE_STR,
138 type_parameters: NONE_STR,
139 return_type: NONE_STR,
140 extra_json: NONE_STR,
141 });
142 w.node_ids.push(ids::file_node_id(file_path));
143 w.stack.push(Scope { row: 0, kind: "file", name: base_name.to_string() });
144

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