Skip to main content

VariableReference

Struct VariableReference 

Source
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

Source

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).

Source

pub fn syntax(&self) -> &SyntaxNode<Lang>

Get the syntax node backing this variable reference.

Source

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()));
Source

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());
Source

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);
Source

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));
Source

pub fn line(&self) -> usize

Get the line number (0-indexed) where this reference starts.

Source

pub fn column(&self) -> usize

Get the column number (0-indexed, in bytes) where this reference starts.

Source

pub fn line_col(&self) -> (usize, usize)

Get both line and column (0-indexed) where this reference starts.

Source

pub fn text_range(&self) -> TextRange

Get the text range of this reference in the source.

Trait Implementations§

Source§

impl Clone for VariableReference

Source§

fn clone(&self) -> VariableReference

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Display for VariableReference

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Hash for VariableReference

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for VariableReference

Source§

fn eq(&self, other: &VariableReference) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for VariableReference

Source§

impl StructuralPartialEq for VariableReference

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.