Skip to content

Commit

Permalink
Added more tests. Added comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
cellomath committed Aug 27, 2024
1 parent 758b1e6 commit 72c84fd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
27 changes: 10 additions & 17 deletions src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1224,27 +1224,22 @@ pub fn parse_raw_string_literal(
match stream.get_next() {
Some('"') => pos.advance(),
Some(c) => return Err((LERR::UnexpectedInput(c.to_string()), start)),
None => {
return Err((LERR::UnterminatedString, start));
}
None => return Err((LERR::UnterminatedString, start))
}

let mut seen_hashes: Option<u8> = None;
// Match everything until the same number of '#'s are seen, prepended by a '"'

// Counts the number of '#' characters seen after a quotation mark.
// Becomes Some(0) after a quote is seen, but resets to None if a hash doesn't follow.
let mut seen_hashes: Option<u8> = None;
let mut result = SmartString::new_const();


loop {
let next_char = match stream.get_next() {
Some(ch) => {
pos.advance();
ch
}
None => {
pos.advance();
return Err((LERR::UnterminatedString, start));
}
Some(ch) => ch,
None => return Err((LERR::UnterminatedString, start))
};
pos.advance();

match (next_char, &mut seen_hashes) {
// Begin attempt to close string
Expand Down Expand Up @@ -1282,9 +1277,7 @@ pub fn parse_raw_string_literal(
seen_hashes = None;
}
// Normal new character seen
(c, None) => {
result.push(c);
}
(c, None) => result.push(c)
}

if next_char == '\n' {
Expand Down Expand Up @@ -1324,7 +1317,7 @@ pub fn parse_raw_string_literal(
/// |`` `hello``_{LF}{EOF}_ |`StringConstant("hello\n")` |``Some('`')`` |
/// |`` `hello ${`` |`InterpolatedString("hello ")`<br/>next token is `{`|`None` |
/// |`` } hello` `` |`StringConstant(" hello")` |`None` |
/// |`} hello`_{EOF}_ |`StringConstant(" hello")` |``Some('`')`` | |
/// |`} hello`_{EOF}_ |`StringConstant(" hello")` |``Some('`')`` |
///
/// This function does not throw a `LexError` for the following conditions:
///
Expand Down
24 changes: 22 additions & 2 deletions tests/string.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rhai::{Engine, EvalAltResult, ImmutableString, Scope, INT};
use rhai::{Engine, EvalAltResult, ImmutableString, LexError, ParseErrorType, Position, Scope, INT};

#[test]
fn test_string() {
Expand All @@ -18,8 +18,28 @@ fn test_string() {
assert_eq!(engine.eval::<String>(r#""Test string: \x58""#).unwrap(), "Test string: X");
assert_eq!(engine.eval::<String>(r#""\"hello\"""#).unwrap(), r#""hello""#);
assert_eq!(engine.eval::<String>(r#"r"Test""#).unwrap(), "Test");
assert_eq!(engine.eval::<String>(r##"r"Test string: \\u2764\nhello,\nworld!""##).unwrap(), r"Test string: \\u2764\nhello,\nworld!");
assert_eq!(engine.eval::<String>(r#"r"Test string: \\u2764\nhello,\nworld!""#).unwrap(), r#"Test string: \\u2764\nhello,\nworld!"#);
assert_eq!(engine.eval::<String>(r###"r##"Test string: r#"\\u2764\nhello,\\nworld!"#"##"###).unwrap(), r##"Test string: r#"\\u2764\nhello,\\nworld!"#"##);
assert_eq!(engine.eval::<String>(r###"r##"Test string: "## + "\u2764""###).unwrap(), "Test string: ❤");
let bad_result = *engine.eval::<String>(r###"r#"Test string: \"##"###).unwrap_err();
if let EvalAltResult::ErrorParsing(parse_error, pos) = bad_result {
assert_eq!(parse_error, ParseErrorType::UnknownOperator("#".to_string()));
assert_eq!(pos, Position::new(1, 19));
} else {
panic!("Wrong error type: {}", bad_result);
}
let bad_result = *engine
.eval::<String>(
r###"r##"Test string:
\"#"###,
)
.unwrap_err();
if let EvalAltResult::ErrorParsing(parse_error, pos) = bad_result {
assert_eq!(parse_error, ParseErrorType::BadInput(LexError::UnterminatedString));
assert_eq!(pos, Position::new(1, 1));
} else {
panic!("Wrong error type: {}", bad_result);
}

assert_eq!(engine.eval::<String>(r#""foo" + "bar""#).unwrap(), "foobar");

Expand Down

0 comments on commit 72c84fd

Please sign in to comment.