Struct yash_syntax::syntax::Word
source · pub struct Word {
pub units: Vec<WordUnit>,
pub location: Location,
}
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: Location
Position of the word in the source code.
Implementations§
source§impl 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§
source§impl 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.
source§impl MaybeLiteral for Word
impl MaybeLiteral for Word
source§impl PartialEq<Word> for Word
impl PartialEq<Word> for Word
source§impl TryFrom<Word> for Assign
impl TryFrom<Word> for Assign
Fallible conversion from a word into an assignment.
source§fn 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
.