Struct yash_syntax::syntax::Word
source · [−]Expand description
Token that may involve expansions and quotes.
A word is a sequence of word units. It depends on context whether an empty word is valid or not. It is your responsibility to ensure a word is non-empty in a context where it cannot.
The difference between words and texts is that only words can contain
single- and double-quotes and tilde expansions. Compare WordUnit and TextUnit.
Fields
units: Vec<WordUnit>Word units that constitute the word.
location: LocationPosition of the word in the source code.
Implementations
sourceimpl Word
impl Word
sourcepub fn parse_tilde_front(&mut self)
pub fn parse_tilde_front(&mut self)
Parses a tilde expansion at the beginning of the word.
This function checks if self.units begins with an unquoted tilde
character, i.e., WordUnit::Unquoted(TextUnit::Literal('~')). If so, the
word unit is replaced with a WordUnit::Tilde value. Other unquoted
characters that follow the tilde are together replaced to produce the
value of the WordUnit::Tilde.
let mut word = Word::from_str("~").unwrap();
word.parse_tilde_front();
assert_eq!(word.units, [Tilde("".to_string())]);let mut word = Word::from_str("~foo").unwrap();
word.parse_tilde_front();
assert_eq!(word.units, [Tilde("foo".to_string())]);If there is no leading tilde, self.units will have the same content
when this function returns.
let mut word = Word::from_str("X").unwrap();
assert_eq!(word.units, [Unquoted(Literal('X'))]);
word.parse_tilde_front();
assert_eq!(word.units, [Unquoted(Literal('X'))]);This function parses a literal word units only, which differs from the
strictly POSIX-conforming behavior. For example, POSIX requires the word
~$() to be regarded as a tilde expansion, but this function does not
convert it to WordUnit::Tilde("$()".to_string()).
The tilde expansion are delimited by an unquoted slash or colon. This is also not strictly POSIX-conforming since POSIX allows colons to be included in the tilde expansion.
This function only parses a tilde expansion at the beginning of the word.
If the word is a colon-separated list of paths, you might want to use
parse_tilde_everywhere instead.
sourcepub fn parse_tilde_everywhere(&mut self)
pub fn parse_tilde_everywhere(&mut self)
Parses tilde expansions in the word.
This function works the same as
parse_tilde_front except that it parses
tilde expansions not only at the beginning of the word but also after
each unquoted colon.
let mut word = Word::from_str("~:~a/b:~c").unwrap();
word.parse_tilde_everywhere();
assert_eq!(
word.units,
[
Tilde("".to_string()),
Unquoted(Literal(':')),
Tilde("a".to_string()),
Unquoted(Literal('/')),
Unquoted(Literal('b')),
Unquoted(Literal(':')),
Tilde("c".to_string()),
]
);Trait Implementations
sourceimpl FromStr for Word
impl FromStr for Word
Converts a string to a word.
This implementation does not parse any tilde expansions in the word.
To parse them, you need to call Word::parse_tilde_front or
Word::parse_tilde_everywhere on the resultant word.
sourceimpl MaybeLiteral for Word
impl MaybeLiteral for Word
sourcefn extend_if_literal<T: Extend<char>>(&self, result: T) -> Result<T, T>
fn extend_if_literal<T: Extend<char>>(&self, result: T) -> Result<T, T>
sourcefn to_string_if_literal(&self) -> Option<String>
fn to_string_if_literal(&self) -> Option<String>
self is literal and, if so, converts to a string.sourceimpl TryFrom<Word> for Assign
impl TryFrom<Word> for Assign
Fallible conversion from a word into an assignment.
sourcefn try_from(word: Word) -> Result<Assign, Word>
fn try_from(word: Word) -> Result<Assign, Word>
Converts a word into an assignment.
For a successful conversion, the word must be of the form name=value,
where name is a non-empty literal word,
= is an unquoted equal sign, and value is a word. If the input word
does not match this syntax, it is returned intact in Err.