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

Function macro_capture_value

codegraph-kernel/src/rustlang.rs:2052–2094  ·  view source on GitHub ↗

captureValue: `tokens[from..to)` as a function value — an identifier alone, or a path alone whose last segment is written like a function's and none of whose segments is generic, placed at that last segment.

(tokens: &[Node<'t>], from: usize, to: usize, src: &'t str, out: &mut Vec<MacroRef<'t>>)

Source from the content-addressed store, hash-verified

2050/// alone, or a path alone whose last segment is written like a function's
2051/// and none of whose segments is generic, placed at that last segment.
2052fn macro_capture_value<'t>(tokens: &[Node<'t>], from: usize, to: usize, src: &'t str, out: &mut Vec<MacroRef<'t>>) {
2053 if to <= from {
2054 return;
2055 }
2056 let n = to - from;
2057 if n == 1 {
2058 let t = tokens[from];
2059 if !macro_is_name(Some(&t), src) {
2060 return;
2061 }
2062 let name = macro_tok_text(t, src);
2063 if !name.is_empty() && !is_stoplisted(name) {
2064 out.push(MacroRef { name: name.to_string(), kind: "function_ref", at: t });
2065 }
2066 return;
2067 }
2068 if n < 3 || n % 2 == 0 {
2069 return;
2070 }
2071 let mut text = String::new();
2072 for i in from..to {
2073 let t = tokens[i];
2074 if (i - from) % 2 == 1 {
2075 if t.kind() != "::" {
2076 return;
2077 }
2078 text.push_str("::");
2079 } else {
2080 let ok = if i == to - 1 { macro_is_name(Some(&t), src) } else { macro_is_segment(Some(&t), src) };
2081 if !ok {
2082 return;
2083 }
2084 text.push_str(macro_tok_text(t, src));
2085 }
2086 }
2087 let last = tokens[to - 1];
2088 let last_text = macro_tok_text(last, src);
2089 let bare = last_text.strip_prefix("r#").unwrap_or(last_text);
2090 if !bare.starts_with(|c: char| c.is_ascii_lowercase() || c == '_') || !is_path_value_text(&text) || is_stoplisted(&text) {
2091 return;
2092 }
2093 out.push(MacroRef { name: text, kind: "function_ref", at: last });
2094}
2095
2096/// Fragments rustc itself parses as Rust code where a macro is called (CODE_FRAGMENTS).
2097const MACRO_CODE_FRAGMENTS: &[&str] = &["expr", "expr_2021", "literal", "block", "stmt"];

Callers 2

macro_capture_valuesFunction · 0.85

Calls 6

macro_is_nameFunction · 0.85
macro_tok_textFunction · 0.85
macro_is_segmentFunction · 0.85
is_path_value_textFunction · 0.85
pushMethod · 0.80
is_stoplistedFunction · 0.70

Tested by

no test coverage detected