Support back-tick at EOL for literal strings.
This commit is contained in:
parent
d756b7bac6
commit
e36e490a30
@ -715,13 +715,13 @@ fn parse_map_literal(
|
|||||||
return Err(PERR::Reserved(s).into_err(pos));
|
return Err(PERR::Reserved(s).into_err(pos));
|
||||||
}
|
}
|
||||||
(Token::LexError(err), pos) => return Err(err.into_err(pos)),
|
(Token::LexError(err), pos) => return Err(err.into_err(pos)),
|
||||||
(_, pos) if map.is_empty() => {
|
(Token::EOF, pos) => {
|
||||||
return Err(
|
return Err(
|
||||||
PERR::MissingToken(Token::RightBrace.into(), MISSING_RBRACE.into())
|
PERR::MissingToken(Token::RightBrace.into(), MISSING_RBRACE.into())
|
||||||
.into_err(pos),
|
.into_err(pos),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
(Token::EOF, pos) => {
|
(_, pos) if map.is_empty() => {
|
||||||
return Err(
|
return Err(
|
||||||
PERR::MissingToken(Token::RightBrace.into(), MISSING_RBRACE.into())
|
PERR::MissingToken(Token::RightBrace.into(), MISSING_RBRACE.into())
|
||||||
.into_err(pos),
|
.into_err(pos),
|
||||||
|
31
src/token.rs
31
src/token.rs
@ -1294,12 +1294,33 @@ fn get_next_token_inner(
|
|||||||
return get_identifier(stream, pos, start_pos, c);
|
return get_identifier(stream, pos, start_pos, c);
|
||||||
}
|
}
|
||||||
|
|
||||||
// " or ` - string literal
|
// " - string literal
|
||||||
('"', _) | ('`', _) => {
|
('"', _) => {
|
||||||
let multi_line = c == '`';
|
return parse_string_literal(stream, state, pos, c, true, false).map_or_else(
|
||||||
|
|err| Some((Token::LexError(err.0), err.1)),
|
||||||
|
|out| Some((Token::StringConstant(out), start_pos)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
// ` - string literal
|
||||||
|
('`', _) => {
|
||||||
|
// Start from the next line if ` at the end of line
|
||||||
|
match stream.peek_next() {
|
||||||
|
// `\r - start from next line
|
||||||
|
Some('\r') => {
|
||||||
|
eat_next(stream, pos);
|
||||||
|
// `\r\n
|
||||||
|
if stream.peek_next().map(|ch| ch == '\n').unwrap_or(false) {
|
||||||
|
eat_next(stream, pos);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// `\n - start from next line
|
||||||
|
Some('\n') => {
|
||||||
|
eat_next(stream, pos);
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
|
||||||
return parse_string_literal(stream, state, pos, c, !multi_line, multi_line)
|
return parse_string_literal(stream, state, pos, c, false, true).map_or_else(
|
||||||
.map_or_else(
|
|
||||||
|err| Some((Token::LexError(err.0), err.1)),
|
|err| Some((Token::LexError(err.0), err.1)),
|
||||||
|out| Some((Token::StringConstant(out), start_pos)),
|
|out| Some((Token::StringConstant(out), start_pos)),
|
||||||
);
|
);
|
||||||
|
@ -8,6 +8,10 @@ fn test_string() -> Result<(), Box<EvalAltResult>> {
|
|||||||
engine.eval::<String>(r#""Test string: \u2764""#)?,
|
engine.eval::<String>(r#""Test string: \u2764""#)?,
|
||||||
"Test string: ❤"
|
"Test string: ❤"
|
||||||
);
|
);
|
||||||
|
assert_eq!(
|
||||||
|
engine.eval::<String>("\"Test\rstring: \\u2764\"")?,
|
||||||
|
"Test\rstring: ❤"
|
||||||
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
engine.eval::<String>(" \"Test string: \\u2764\\\n hello, world!\"")?,
|
engine.eval::<String>(" \"Test string: \\u2764\\\n hello, world!\"")?,
|
||||||
"Test string: ❤ hello, world!"
|
"Test string: ❤ hello, world!"
|
||||||
@ -16,6 +20,14 @@ fn test_string() -> Result<(), Box<EvalAltResult>> {
|
|||||||
engine.eval::<String>(" `Test string: \\u2764\nhello,\\nworld!`")?,
|
engine.eval::<String>(" `Test string: \\u2764\nhello,\\nworld!`")?,
|
||||||
"Test string: \\u2764\nhello,\\nworld!"
|
"Test string: \\u2764\nhello,\\nworld!"
|
||||||
);
|
);
|
||||||
|
assert_eq!(
|
||||||
|
engine.eval::<String>(" `\nTest string: \\u2764\nhello,\\nworld!`")?,
|
||||||
|
"Test string: \\u2764\nhello,\\nworld!"
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
engine.eval::<String>(" `\r\nTest string: \\u2764\nhello,\\nworld!`")?,
|
||||||
|
"Test string: \\u2764\nhello,\\nworld!"
|
||||||
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
engine.eval::<String>(r#""Test string: \x58""#)?,
|
engine.eval::<String>(r#""Test string: \x58""#)?,
|
||||||
"Test string: X"
|
"Test string: X"
|
||||||
|
Loading…
Reference in New Issue
Block a user