Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement raw string literals #3556

Merged
merged 10 commits into from
Nov 28, 2023
16 changes: 9 additions & 7 deletions compiler/noirc_frontend/src/lexer/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
position: Position,
done: bool,
skip_comments: bool,
skip_whitespaces: bool,

Check warning on line 19 in compiler/noirc_frontend/src/lexer/lexer.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (whitespaces)
}

pub type SpannedTokenResult = Result<SpannedToken, LexerErrorKind>;
Expand All @@ -43,7 +43,7 @@
position: 0,
done: false,
skip_comments: true,
skip_whitespaces: true,

Check warning on line 46 in compiler/noirc_frontend/src/lexer/lexer.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (whitespaces)
}
}

Expand All @@ -52,8 +52,8 @@
self
}

pub fn skip_whitespaces(mut self, flag: bool) -> Self {

Check warning on line 55 in compiler/noirc_frontend/src/lexer/lexer.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (whitespaces)
self.skip_whitespaces = flag;

Check warning on line 56 in compiler/noirc_frontend/src/lexer/lexer.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (whitespaces)
self
}

Expand Down Expand Up @@ -96,7 +96,7 @@
match self.next_char() {
Some(x) if x.is_whitespace() => {
let spanned = self.eat_whitespace(x);
if self.skip_whitespaces {

Check warning on line 99 in compiler/noirc_frontend/src/lexer/lexer.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (whitespaces)
self.next_token()
} else {
Ok(spanned)
Expand Down Expand Up @@ -465,13 +465,15 @@
// Problem: we commit to eating raw strings once we see one or two characters.
// This is unclean, but likely ok in all practical cases, and works with existing
// `Lexer` methods.
let peek2 = self.peek2_char();
if (self.peek_char_is('#') && (peek2 == Some('#') || peek2 == Some('"')))
|| self.peek_char_is('"')
{
self.eat_raw_string()
} else {
self.eat_alpha_numeric('r')
let peek1 = self.peek_char().unwrap_or('X');
let peek2 = self.peek2_char().unwrap_or('X');
match (peek1, peek2) {
('#', '#') | ('#', '"') | ('"', _) => {
self.eat_raw_string()
}
_ => {
self.eat_alpha_numeric('r')
}
}
}

Expand Down
46 changes: 44 additions & 2 deletions compiler/noirc_frontend/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2552,7 +2552,7 @@
}

#[test]
fn parse_raw_string() {
fn parse_raw_string_expr() {
let cases = vec![
Case { source: r##" r"foo" "##, expect: r##"r"foo""##, errors: 0 },
Case { source: r##" r#"foo"# "##, expect: r##"r#"foo"#"##, errors: 0 },
Expand All @@ -2561,12 +2561,54 @@
Case { source: r##" r#"\"# "##, expect: r##"r#"\"#"##, errors: 0 },
Case { source: r##" r#"\\"# "##, expect: r##"r#"\\"#"##, errors: 0 },
Case { source: r##" r#"\\\"# "##, expect: r##"r#"\\\"#"##, errors: 0 },
// escape sequence
Case { source: r##" r#"\t\n\\t\\n\\\t\\\n\\\\"# "##, expect: r##"r#"\t\n\\t\\n\\\t\\\n\\\\"#"##, errors: 0 },
Case { source: r##" r#"\\\\\\\\"# "##, expect: r##"r#"\\\\\\\\"#"##, errors: 0 },
// mismatch - errors:
Case { source: r###" r#"foo"## "###, expect: r###"r#"foo"#"###, errors: 1 },
Case { source: r###" r##"foo"# "###, expect: "(none)", errors: 2 },
// mismatch: short:
Case { source: r###" r"foo"# "###, expect: r###"r"foo""###, errors: 1 },
Case { source: r###" r#"foo" "###, expect: "(none)", errors: 2 },
// empty string
Case { source: r####"r"""####, expect: r####"r"""####, errors: 0 },
Case { source: r####"r###""###"####, expect: r####"r###""###"####, errors: 0 },
// miscelanneous

Check warning on line 2576 in compiler/noirc_frontend/src/parser/parser.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (miscelanneous)
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved
Case { source: r###" r#\"foo\"# "###, expect: "plain::r", errors: 2 },
Case { source: r###" r\"foo\" "###, expect: "plain::r", errors: 1 },
Case { source: r###" r##"foo"# "###, expect: "(none)", errors: 2 },
// missing 'r' letter
Case { source: r###" ##"foo"# "###, expect: r#""foo""#, errors: 2 },
Case { source: r###" #"foo" "###, expect: "plain::foo", errors: 2 },
// whitespace
Case { source: r###" r #"foo"# "###, expect: "plain::r", errors: 2 },
Case { source: r###" r# "foo"# "###, expect: "plain::r", errors: 3 },
Case { source: r###" r#"foo" # "###, expect: "(none)", errors: 2 },
// after identifier
Case { source: r###" bar#"foo"# "###, expect: "plain::bar", errors: 2 },
];

check_cases_with_errors(&cases[..], expression());
check_cases_with_errors(&cases[..], literal());
}

#[test]
fn parse_raw_string_lit() {
let lit_cases = vec![
Case { source: r##" r"foo" "##, expect: r##"r"foo""##, errors: 0 },
Case { source: r##" r#"foo"# "##, expect: r##"r#"foo"#"##, errors: 0 },
// backslash
Case { source: r##" r"\\" "##, expect: r##"r"\\""##, errors: 0 },
Case { source: r##" r#"\"# "##, expect: r##"r#"\"#"##, errors: 0 },
Case { source: r##" r#"\\"# "##, expect: r##"r#"\\"#"##, errors: 0 },
Case { source: r##" r#"\\\"# "##, expect: r##"r#"\\\"#"##, errors: 0 },
// escape sequence
Case { source: r##" r#"\t\n\\t\\n\\\t\\\n\\\\"# "##, expect: r##"r#"\t\n\\t\\n\\\t\\\n\\\\"#"##, errors: 0 },
Case { source: r##" r#"\\\\\\\\"# "##, expect: r##"r#"\\\\\\\\"#"##, errors: 0 },
// mismatch - errors:
Case { source: r###" r#"foo"## "###, expect: r###"r#"foo"#"###, errors: 1 },
Case { source: r###" r##"foo"# "###, expect: "(none)", errors: 2 },
];

check_cases_with_errors(&lit_cases[..], literal());
}
}
Loading