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

Function extract

codegraph-kernel/src/swift.rs:166–239  ·  view source on GitHub ↗
(file_path: &str, source: &str)

Source from the content-addressed store, hash-verified

164}
165
166pub fn extract(file_path: &str, source: &str) -> Result<EmitOut, String> {
167 let grammar = crate::langs::grammar_for("swift").ok_or("no swift grammar")?;
168 let t0 = std::time::Instant::now();
169 let mut parser = Parser::new();
170 parser
171 .set_language(&grammar)
172 .map_err(|e| format!("set_language(swift) failed: {e}"))?;
173 let tree = parser
174 .parse(source, None)
175 .ok_or_else(|| "parser returned null tree".to_string())?;
176 if tree.root_node().has_error() {
177 return Err("defer: parse tree contains errors — wasm recovery is canonical".to_string());
178 }
179
180 let mut w = Walker {
181 src: source,
182 file_path,
183 line_starts: util::line_starts(source),
184 arena: Arena::default(),
185 tables: Tables::default(),
186 stack: Vec::new(),
187 node_ids: Vec::new(),
188 defined_fn_names: HashSet::new(),
189 imported_names: HashSet::new(),
190 fn_ref_cands: Vec::new(),
191 fs_values: HashMap::new(),
192 fs_value_counts: HashMap::new(),
193 value_scopes: Vec::new(),
194 };
195
196 let line_count = source.bytes().filter(|b| *b == b'\n').count() as u32 + 1;
197 let base_name = file_path.rsplit(['/', '\\']).next().unwrap_or(file_path);
198 let mut flags = BoolFlags::default();
199 flags.set(FLAG_IS_EXPORTED, false);
200 let file_id = w.arena.put(&ids::file_node_id(file_path));
201 let name_ref = w.arena.put(base_name);
202 let qn_ref = w.arena.put(file_path);
203 w.tables.push_node(&NodeRow {
204 kind: node_kind_index("file").unwrap(),
205 visibility: 0,
206 flags,
207 start_line: 1,
208 end_line: line_count,
209 start_column: 0,
210 end_column: 0,
211 name: name_ref,
212 qualified_name: qn_ref,
213 id: file_id,
214 docstring: NONE_STR,
215 signature: NONE_STR,
216 decorators: NONE_STR,
217 type_parameters: NONE_STR,
218 return_type: NONE_STR,
219 extra_json: NONE_STR,
220 });
221 w.node_ids.push(ids::file_node_id(file_path));
222 w.stack.push(Scope { row: 0, kind: "file", name: base_name.to_string() });
223

Callers

nothing calls this directly

Calls 14

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
visit_nodeMethod · 0.45

Tested by

no test coverage detected