From 46e248fb7fe7af696f5bd331f1b981cfe2ffca76 Mon Sep 17 00:00:00 2001 From: liushuyu Date: Tue, 7 Jun 2022 00:57:43 -0600 Subject: rust/lex: skip broken string expression ... ... when the matching quote is not found on the same line this could unstuck the string parser when the parser could not advance the parsing position --- gcc/rust/lex/rust-lex.cc | 32 ++++++++++++++++++++++++++++++++ gcc/rust/lex/rust-lex.h | 1 + 2 files changed, 33 insertions(+) (limited to 'gcc/rust/lex') diff --git a/gcc/rust/lex/rust-lex.cc b/gcc/rust/lex/rust-lex.cc index ea09ac2..023b676 100644 --- a/gcc/rust/lex/rust-lex.cc +++ b/gcc/rust/lex/rust-lex.cc @@ -1856,6 +1856,35 @@ Lexer::parse_raw_identifier (Location loc) } } +// skip broken string input (unterminated strings) +void +Lexer::skip_broken_string_input (int current_char) +{ + while (current_char != '"' && current_char != EOF) + { + if (current_char == '\n') + { + current_line++; + current_column = 1; + } + else + { + current_column++; + } + skip_input (); + current_char = peek_input (); + } + if (current_char == '"') + { + current_column++; + + skip_input (); + current_char = peek_input (); + } + rust_debug ("skipped to %d:%d due to bad quotes", current_line, + current_column); +} + // Parses a unicode string. TokenPtr Lexer::parse_string (Location loc) @@ -1903,6 +1932,9 @@ Lexer::parse_string (Location loc) if (current_char32.value == '\n') { rust_error_at (get_current_location (), "unended string literal"); + // by this point, the parser will stuck at this position due to + // undetermined string termination. we now need to unstuck the parser + skip_broken_string_input (current_char32.value); } else if (current_char32.value == '"') { diff --git a/gcc/rust/lex/rust-lex.h b/gcc/rust/lex/rust-lex.h index e01d86d..429b9e1 100644 --- a/gcc/rust/lex/rust-lex.h +++ b/gcc/rust/lex/rust-lex.h @@ -114,6 +114,7 @@ private: Codepoint peek_codepoint_input (); Codepoint test_peek_codepoint_input (int n); void skip_codepoint_input (); + void skip_broken_string_input (int current_char); TokenPtr parse_byte_char (Location loc); TokenPtr parse_byte_string (Location loc); -- cgit v1.1