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

Function extract

codegraph-kernel/src/go.rs:104–179  ·  view source on GitHub ↗
(file_path: &str, source: &str)

Source from the content-addressed store, hash-verified

102}
103
104pub fn extract(file_path: &str, source: &str) -> Result<EmitOut, String> {
105 let grammar = crate::langs::grammar_for("go").ok_or("no go grammar")?;
106 let t0 = std::time::Instant::now();
107 let mut parser = Parser::new();
108 parser
109 .set_language(&grammar)
110 .map_err(|e| format!("set_language(go) failed: {e}"))?;
111 let tree = parser
112 .parse(source, None)
113 .ok_or_else(|| "parser returned null tree".to_string())?;
114 if tree.root_node().has_error() {
115 return Err("defer: parse tree contains errors — wasm recovery is canonical".to_string());
116 }
117
118 let mut w = Walker {
119 src: source,
120 file_path,
121 line_starts: util::line_starts(source),
122 arena: Arena::default(),
123 node_id_allocator: ids::NodeIdAllocator::default(),
124 tables: Tables::default(),
125 stack: Vec::new(),
126 nodes_meta: Vec::new(),
127 node_ids: Vec::new(),
128 defined_fn_names: HashSet::new(),
129 imported_names: HashSet::new(),
130 fn_ref_cands: Vec::new(),
131 fs_values: HashMap::new(),
132 fs_value_counts: HashMap::new(),
133 value_scopes: Vec::new(),
134 };
135
136 let line_count = source.bytes().filter(|b| *b == b'\n').count() as u32 + 1;
137 let base_name = file_path.rsplit(['/', '\\']).next().unwrap_or(file_path);
138 let mut flags = BoolFlags::default();
139 flags.set(FLAG_IS_EXPORTED, false);
140 let file_id = w.arena.put(&ids::file_node_id(file_path));
141 let name_ref = w.arena.put(base_name);
142 let qn_ref = w.arena.put(file_path);
143 w.tables.push_node(&NodeRow {
144 kind: node_kind_index("file").unwrap(),
145 visibility: 0,
146 flags,
147 start_line: 1,
148 end_line: line_count,
149 start_column: 0,
150 end_column: 0,
151 name: name_ref,
152 qualified_name: qn_ref,
153 id: file_id,
154 docstring: NONE_STR,
155 signature: NONE_STR,
156 decorators: NONE_STR,
157 type_parameters: NONE_STR,
158 return_type: NONE_STR,
159 extra_json: NONE_STR,
160 });
161 w.nodes_meta.push(NodeMeta { kind: "file", name: base_name.to_string() });

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