2021-04-04 16:41:02 -07:00
|
|
|
use crate::common::*;
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Clone, Copy, Ord, PartialOrd, Eq)]
|
2021-04-05 21:28:37 -07:00
|
|
|
pub(crate) struct StringKind {
|
|
|
|
delimiter: StringDelimiter,
|
2021-09-16 06:44:40 -07:00
|
|
|
indented: bool,
|
2021-04-05 21:28:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Clone, Copy, Ord, PartialOrd, Eq)]
|
|
|
|
enum StringDelimiter {
|
2021-04-04 16:41:02 -07:00
|
|
|
Backtick,
|
2021-04-05 21:28:37 -07:00
|
|
|
QuoteDouble,
|
|
|
|
QuoteSingle,
|
2021-04-04 16:41:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl StringKind {
|
2021-04-05 21:28:37 -07:00
|
|
|
// Indented values must come before un-indented values, or else
|
|
|
|
// `Self::from_token_start` will incorrectly return indented = false
|
|
|
|
// for indented strings.
|
|
|
|
const ALL: &'static [Self] = &[
|
|
|
|
Self::new(StringDelimiter::Backtick, true),
|
|
|
|
Self::new(StringDelimiter::Backtick, false),
|
|
|
|
Self::new(StringDelimiter::QuoteDouble, true),
|
|
|
|
Self::new(StringDelimiter::QuoteDouble, false),
|
|
|
|
Self::new(StringDelimiter::QuoteSingle, true),
|
|
|
|
Self::new(StringDelimiter::QuoteSingle, false),
|
|
|
|
];
|
|
|
|
|
|
|
|
const fn new(delimiter: StringDelimiter, indented: bool) -> Self {
|
|
|
|
Self {
|
|
|
|
delimiter,
|
|
|
|
indented,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn delimiter(self) -> &'static str {
|
|
|
|
match (self.delimiter, self.indented) {
|
|
|
|
(StringDelimiter::Backtick, false) => "`",
|
|
|
|
(StringDelimiter::Backtick, true) => "```",
|
|
|
|
(StringDelimiter::QuoteDouble, false) => "\"",
|
|
|
|
(StringDelimiter::QuoteDouble, true) => "\"\"\"",
|
|
|
|
(StringDelimiter::QuoteSingle, false) => "'",
|
|
|
|
(StringDelimiter::QuoteSingle, true) => "'''",
|
2021-04-04 16:41:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-05 21:28:37 -07:00
|
|
|
pub(crate) fn delimiter_len(self) -> usize {
|
|
|
|
self.delimiter().len()
|
|
|
|
}
|
|
|
|
|
2021-04-04 16:41:02 -07:00
|
|
|
pub(crate) fn token_kind(self) -> TokenKind {
|
2021-04-05 21:28:37 -07:00
|
|
|
match self.delimiter {
|
|
|
|
StringDelimiter::QuoteDouble | StringDelimiter::QuoteSingle => TokenKind::StringToken,
|
|
|
|
StringDelimiter::Backtick => TokenKind::Backtick,
|
|
|
|
}
|
2021-04-04 16:41:02 -07:00
|
|
|
}
|
|
|
|
|
2021-07-26 01:26:06 -07:00
|
|
|
pub(crate) fn unterminated_error_kind(self) -> CompileErrorKind<'static> {
|
2021-04-05 21:28:37 -07:00
|
|
|
match self.delimiter {
|
2021-09-16 06:44:40 -07:00
|
|
|
StringDelimiter::QuoteDouble | StringDelimiter::QuoteSingle => {
|
|
|
|
CompileErrorKind::UnterminatedString
|
|
|
|
}
|
2021-07-26 01:26:06 -07:00
|
|
|
StringDelimiter::Backtick => CompileErrorKind::UnterminatedBacktick,
|
2021-04-05 21:28:37 -07:00
|
|
|
}
|
2021-04-04 16:41:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn processes_escape_sequences(self) -> bool {
|
2021-04-05 21:28:37 -07:00
|
|
|
match self.delimiter {
|
|
|
|
StringDelimiter::QuoteDouble => true,
|
|
|
|
StringDelimiter::Backtick | StringDelimiter::QuoteSingle => false,
|
2021-04-04 16:41:02 -07:00
|
|
|
}
|
|
|
|
}
|
2021-04-05 21:28:37 -07:00
|
|
|
|
|
|
|
pub(crate) fn indented(self) -> bool {
|
|
|
|
self.indented
|
|
|
|
}
|
|
|
|
|
2021-07-26 01:26:06 -07:00
|
|
|
pub(crate) fn from_string_or_backtick(token: Token) -> CompileResult<Self> {
|
2021-04-05 21:28:37 -07:00
|
|
|
Self::from_token_start(token.lexeme()).ok_or_else(|| {
|
2021-07-26 01:26:06 -07:00
|
|
|
token.error(CompileErrorKind::Internal {
|
2021-04-05 21:28:37 -07:00
|
|
|
message: "StringKind::from_token: Expected String or Backtick".to_owned(),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn from_token_start(token_start: &str) -> Option<Self> {
|
|
|
|
for &kind in Self::ALL {
|
|
|
|
if token_start.starts_with(kind.delimiter()) {
|
|
|
|
return Some(kind);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
2021-04-04 16:41:02 -07:00
|
|
|
}
|