use tabnas::{Context, ImperativeLexMatcher, Lexer, Options, Rule, Token, Value, TIN_ST};
const UNKNOWN_ESCAPE: char = '\u{1b}';
pub(crate) fn make_toml_string_matcher(_options: &Options) -> Option<ImperativeLexMatcher> {
Some(std::sync::Arc::new(toml_string_matcher))
}
fn escape_char(escape: char) -> Option<char> {
match escape {
'b' => Some('\u{8}'),
't' => Some('\t'),
'n' => Some('\n'),
'f' => Some('\u{c}'),
'r' => Some('\r'),
'"' => Some('"'),
'\\' => Some('\\'),
_ => None,
}
}
fn is_control_other_than_tab(character: char) -> bool {
(character < '\u{20}' && character != '\t') || character == '\u{7f}'
}
fn is_hexadecimal_lax(character: char) -> bool {
character.is_ascii_alphanumeric()
}
fn hex_prefix(text: &str) -> Option<u32> {
let mut value: u64 = 0;
let mut seen = false;
for character in text.chars() {
let Some(digit) = character.to_digit(16) else {
break;
};
seen = true;
value = (value * 16 + u64::from(digit)).min(u64::from(u32::MAX));
}
#[allow(clippy::cast_possible_truncation)]
seen.then_some(value as u32)
}
fn two_hex(text: &str) -> Option<u32> {
let digits: Vec<char> = text.chars().take(2).collect();
(2 == digits.len() && digits.iter().all(char::is_ascii_hexdigit))
.then(|| u32::from_str_radix(&digits.iter().collect::<String>(), 16).ok())
.flatten()
}
#[allow(clippy::too_many_lines)]
fn toml_string_matcher(
lexer: &mut Lexer<'_>,
_rule: &mut Rule,
_context: &mut Context,
) -> Option<Token> {
let point = lexer.point();
let source: Vec<char> = lexer.remaining().chars().collect();
let length = source.len();
if 0 == length {
return None;
}
let delimiter = source[0];
if '\'' != delimiter && '"' != delimiter {
return None;
}
let origin = point.site.pos;
let span = |start: usize, end: usize| (origin + start, origin + end);
let begin = 0_usize;
let mut index = 0_usize;
let mut is_multiline = false;
if Some(&delimiter) == source.get(1) {
if Some(&delimiter) != source.get(2) {
let text: String = source[..2].iter().collect();
lexer.advance_chars(2);
return Some(lexer.token(
"#ST",
TIN_ST,
Value::String(String::new()),
text,
end(lexer),
));
}
index += 2;
is_multiline = true;
}
if is_multiline && Some(&'\n') == source.get(index + 1) {
index += 1;
}
let mut value = String::new();
let mut closed = false;
while index < length {
index += 1;
let Some(character) = source.get(index).copied() else {
let (start, end) = span(begin, index);
return Some(lexer.bad_span("unterminated_string", start, end));
};
if '\n' == character {
if !is_multiline {
let (start, end) = span(index, index + 1);
return Some(lexer.bad_span("unprintable", start, end));
}
value.push('\n');
continue;
}
if delimiter == character {
if is_multiline {
if Some(&delimiter) != source.get(index + 1) {
value.push(delimiter);
continue;
}
if Some(&delimiter) != source.get(index + 2) {
value.push(delimiter);
value.push(delimiter);
index += 1;
continue;
}
index += 2;
if Some(&delimiter) == source.get(index + 1) {
value.push(delimiter);
index += 1;
}
if Some(&delimiter) == source.get(index + 1) {
value.push(delimiter);
index += 1;
}
}
index += 1;
closed = true;
break;
}
if is_control_other_than_tab(character) {
let (start, end) = span(index, index + 1);
return Some(lexer.bad_span("unprintable", start, end));
}
if '\'' == delimiter {
value.push(character);
continue;
}
if '\\' != character {
value.push(character);
continue;
}
index += 1;
let escape = source.get(index).copied();
if let Some(replacement) = escape.and_then(escape_char) {
value.push(replacement);
continue;
}
match escape {
Some('x') => {
index += 1;
let rest: String = source[index.min(length)..].iter().collect();
let Some(code) = two_hex(&rest) else {
let (start, end) = span(index.saturating_sub(2), index + 2);
return Some(lexer.bad_span("invalid_ascii", start, end));
};
value.push(char::from_u32(code).unwrap_or(char::REPLACEMENT_CHARACTER));
index += 1;
}
Some(marker @ ('u' | 'U')) => {
let begin_unicode = index;
let size = if 'u' == marker { 4 } else { 8 };
let mut digits = String::new();
for _ in 0..size {
index += 1;
match source.get(index).copied() {
Some(digit) if is_hexadecimal_lax(digit) => digits.push(digit),
_ => {
let (start, end) = span(begin_unicode, index);
return Some(lexer.bad_span("invalid_unicode", start, end));
}
}
}
let code = hex_prefix(&digits);
let Some(code) = code.filter(|code| *code <= 0x0010_FFFF) else {
let (start, end) = span(begin_unicode, index);
return Some(lexer.bad_span("invalid_unicode", start, end));
};
value.push(char::from_u32(code).unwrap_or(char::REPLACEMENT_CHARACTER));
}
Some(' ' | '\t' | '\n' | '\r') if is_multiline => {
loop {
match source.get(index + 1).copied() {
Some(' ' | '\t' | '\n') => index += 1,
Some('\r') if Some(&'\n') == source.get(index + 2) => index += 2,
_ => break,
}
}
}
_ => value.push(UNKNOWN_ESCAPE),
}
}
if !closed {
let (start, end) = span(begin, index);
return Some(lexer.bad_span("unterminated_string", start, end));
}
let text: String = source[begin..index.min(length)].iter().collect();
lexer.advance_chars(index.min(length));
Some(lexer.token("#ST", TIN_ST, Value::String(value), text, end(lexer)))
}
fn end(lexer: &Lexer<'_>) -> tabnas::Point {
lexer.point()
}