pub struct VariableReference(/* private fields */);Expand description
A reference to a variable in the makefile, e.g. $(FOO) or ${BAR}.
This wraps an EXPR syntax node whose first token is $ followed by ( or {.
Implementations§
Source§impl VariableReference
impl VariableReference
Sourcepub fn cast(syntax: SyntaxNode<Lang>) -> Option<Self>
pub fn cast(syntax: SyntaxNode<Lang>) -> Option<Self>
Try to cast a syntax node into a VariableReference.
Returns Some if the node is an EXPR whose first token is $ followed by
(, {, or an identifier (for single-character variables like $X).
Sourcepub fn syntax(&self) -> &SyntaxNode<Lang>
pub fn syntax(&self) -> &SyntaxNode<Lang>
Get the syntax node backing this variable reference.
Sourcepub fn name(&self) -> Option<String>
pub fn name(&self) -> Option<String>
Get the name of the referenced variable.
For simple references like $(FOO), returns "FOO".
For function calls like $(wildcard *.c), returns "wildcard".
Note: Variable references inside recipes are not parsed into the syntax tree (recipes are stored as raw text). This only finds references in variable values, prerequisites, and targets.
§Example
use makefile_lossless::Makefile;
let makefile: Makefile = "CFLAGS = $(BASE_FLAGS) -Wall\n".parse().unwrap();
let refs: Vec<_> = makefile.variable_references().collect();
assert_eq!(refs[0].name(), Some("BASE_FLAGS".to_string()));Sourcepub fn is_function_call(&self) -> bool
pub fn is_function_call(&self) -> bool
Check if this is a function call rather than a simple variable reference.
Returns true if the content after the function name contains whitespace
or commas, indicating arguments (e.g. $(subst a,b,text) vs $(CC)).
§Example
use makefile_lossless::Makefile;
let makefile: Makefile = "FILES = $(wildcard *.c)\n".parse().unwrap();
let refs: Vec<_> = makefile.variable_references().collect();
assert!(refs[0].is_function_call());Sourcepub fn argument_count(&self) -> usize
pub fn argument_count(&self) -> usize
Count the number of comma-separated arguments in a function call.
Returns 0 for simple variable references. For function calls, counts the commas at depth 0 (not inside nested parentheses) plus 1.
§Example
use makefile_lossless::Makefile;
let makefile: Makefile = "X = $(subst a,b,text)\n".parse().unwrap();
let refs: Vec<_> = makefile.variable_references().collect();
assert_eq!(refs[0].argument_count(), 3);Sourcepub fn argument_index_at_offset(&self, offset: usize) -> Option<usize>
pub fn argument_index_at_offset(&self, offset: usize) -> Option<usize>
Determine which argument (0-based) the given byte offset falls into.
Returns None if the offset is not inside this reference or if this
is not a function call.
§Example
use makefile_lossless::Makefile;
let makefile: Makefile = "X = $(subst a,b,text)\n".parse().unwrap();
let refs: Vec<_> = makefile.variable_references().collect();
// offset 12 is 'a' (first arg), offset 14 is 'b' (second arg), offset 16 is 't' (third arg)
assert_eq!(refs[0].argument_index_at_offset(12), Some(0));
assert_eq!(refs[0].argument_index_at_offset(14), Some(1));
assert_eq!(refs[0].argument_index_at_offset(16), Some(2));Sourcepub fn column(&self) -> usize
pub fn column(&self) -> usize
Get the column number (0-indexed, in bytes) where this reference starts.
Sourcepub fn line_col(&self) -> (usize, usize)
pub fn line_col(&self) -> (usize, usize)
Get both line and column (0-indexed) where this reference starts.
Sourcepub fn text_range(&self) -> TextRange
pub fn text_range(&self) -> TextRange
Get the text range of this reference in the source.
Trait Implementations§
Source§impl Clone for VariableReference
impl Clone for VariableReference
Source§fn clone(&self) -> VariableReference
fn clone(&self) -> VariableReference
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Display for VariableReference
impl Display for VariableReference
Source§impl Hash for VariableReference
impl Hash for VariableReference
Source§impl PartialEq for VariableReference
impl PartialEq for VariableReference
Source§fn eq(&self, other: &VariableReference) -> bool
fn eq(&self, other: &VariableReference) -> bool
self and other values to be equal, and is used by ==.