Skip to content

Commit

Permalink
Define dollar-single-quote in shell syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
magicant committed Nov 24, 2024
1 parent 83b7e81 commit 568f110
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions yash-semantics/src/expansion/initial/word.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ impl Expand for WordUnit {
double_quote(&mut phrase);
Ok(phrase)
}
DollarSingleQuote(_) => todo!(),
Tilde(name) => Ok(super::tilde::expand(name, env.inner).into()),
}
}
Expand Down
61 changes: 61 additions & 0 deletions yash-syntax/src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,65 @@ pub use TextUnit::*;
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Text(pub Vec<TextUnit>);

/// Element of an [`EscapedString`].
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum EscapeUnit {
/// Literal single character
Literal(char),
/// Backslash-escaped double-quote character (`\"`)
DoubleQuote,
/// Backslash-escaped single-quote character (`\'`)
SingleQuote,
/// Backslash-escaped backslash character (`\\`)
Backslash,
/// Backslash-escaped question mark character (`\?`)
Question,
/// Backslash notation for the bell character (`\a`, ASCII 7)
Alert,
/// Backslash notation for the backspace character (`\b`, ASCII 8)
Backspace,
/// Backslash notation for the escape character (`\e`, ASCII 27)
Escape,
/// Backslash notation for the form feed character (`\f`, ASCII 12)
FormFeed,
/// Backslash notation for the newline character (`\n`, ASCII 10)
Newline,
/// Backslash notation for the carriage return character (`\r`, ASCII 13)
CarriageReturn,
/// Backslash notation for the horizontal tab character (`\t`, ASCII 9)
Tab,
/// Backslash notation for the vertical tab character (`\v`, ASCII 11)
VerticalTab,
/// Control character notation (`\c...`)
///
/// The associated value is the control character represented by the
/// following character in the input.
Control(u8),
/// Single-byte octal notation (`\OOO`)
///
/// The associated value is the byte represented by the three octal digits
/// following the backslash.
Octal(u8),
/// Single-byte hexadecimal notation (`\xHH`)
///
/// The associated value is the byte represented by the two hexadecimal
/// digits following the `x`.
Hex(u8),
/// Unicode notation (`\uHHHH` or `\UHHHHHHHH`)
///
/// The associated value is the Unicode scalar value represented by the four
/// or eight hexadecimal digits following the `u` or `U`.
Unicode(char),
}

/// String that may contain some escapes
///
/// An escaped string is a sequence of [escape unit](EscapeUnit)s, which may
/// contain some kinds of escapes. This type is used for the value of a
/// [dollar-single-quoted string](WordUnit::DollarSingleQuote).
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct EscapedString(pub Vec<EscapeUnit>);

/// Element of a [Word], i.e., text with quotes and tilde expansion
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum WordUnit {
Expand All @@ -350,6 +409,8 @@ pub enum WordUnit {
SingleQuote(String),
/// Text surrounded with a pair of double quotations
DoubleQuote(Text),
/// String surrounded with a pair of single quotations and preceded by a dollar sign
DollarSingleQuote(EscapedString),
/// Tilde expansion
///
/// The `String` value does not contain the initial tilde.
Expand Down
1 change: 1 addition & 0 deletions yash-syntax/src/syntax/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ impl Unquote for WordUnit {
Ok(true)
}
DoubleQuote(inner) => inner.write_unquoted(w),
DollarSingleQuote(_) => todo!(),
Tilde(s) => {
write!(w, "~{s}")?;
Ok(false)
Expand Down
1 change: 1 addition & 0 deletions yash-syntax/src/syntax/impl_display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ impl fmt::Display for WordUnit {
Unquoted(dq) => dq.fmt(f),
SingleQuote(s) => write!(f, "'{s}'"),
DoubleQuote(content) => write!(f, "\"{content}\""),
DollarSingleQuote(_) => todo!(),
Tilde(s) => write!(f, "~{s}"),
}
}
Expand Down

0 comments on commit 568f110

Please sign in to comment.