aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/lex/rust-lex.cc
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2022-11-28 11:45:44 +0000
committerGitHub <noreply@github.com>2022-11-28 11:45:44 +0000
commit71b8beb150d96548a2ebfb8ef964d14225e300f0 (patch)
treee873bba623a4d69fbccd12272f8400131406f944 /gcc/rust/lex/rust-lex.cc
parent9ef8144e3994dea423011c0c248a0ea71c73cf25 (diff)
parentb43c5d4fd82c220419f9234588fed8131d416fff (diff)
downloadgcc-71b8beb150d96548a2ebfb8ef964d14225e300f0.zip
gcc-71b8beb150d96548a2ebfb8ef964d14225e300f0.tar.gz
gcc-71b8beb150d96548a2ebfb8ef964d14225e300f0.tar.bz2
Merge #1651
1651: Improve dump lex r=CohenArthur a=tamaroning Fixes #1650 Co-authored-by: Raiki Tamura <tamaron1203@gmail.com>
Diffstat (limited to 'gcc/rust/lex/rust-lex.cc')
-rw-r--r--gcc/rust/lex/rust-lex.cc47
1 files changed, 44 insertions, 3 deletions
diff --git a/gcc/rust/lex/rust-lex.cc b/gcc/rust/lex/rust-lex.cc
index 692af5d..ea17ecc 100644
--- a/gcc/rust/lex/rust-lex.cc
+++ b/gcc/rust/lex/rust-lex.cc
@@ -118,13 +118,15 @@ is_non_decimal_int_literal_separator (char character)
Lexer::Lexer (const std::string &input)
: input (RAIIFile::create_error ()), current_line (1), current_column (1),
- line_map (nullptr), raw_input_source (new BufferInputSource (input, 0)),
+ line_map (nullptr), dump_lex_out (Optional<std::ofstream &>::none ()),
+ raw_input_source (new BufferInputSource (input, 0)),
input_queue{*raw_input_source}, token_queue (TokenSource (this))
{}
-Lexer::Lexer (const char *filename, RAIIFile file_input, Linemap *linemap)
+Lexer::Lexer (const char *filename, RAIIFile file_input, Linemap *linemap,
+ Optional<std::ofstream &> dump_lex_opt)
: input (std::move (file_input)), current_line (1), current_column (1),
- line_map (linemap),
+ line_map (linemap), dump_lex_out (dump_lex_opt),
raw_input_source (new FileInputSource (input.get_raw ())),
input_queue{*raw_input_source}, token_queue (TokenSource (this))
{
@@ -187,6 +189,45 @@ Lexer::skip_input ()
}
void
+Lexer::skip_token (int n)
+{
+ // dump tokens if dump-lex option is enabled
+ if (dump_lex_out.is_some ())
+ dump_and_skip (n);
+ else
+ token_queue.skip (n);
+}
+
+void
+Lexer::dump_and_skip (int n)
+{
+ std::ofstream &out = dump_lex_out.get ();
+ bool found_eof = false;
+ const_TokenPtr tok;
+ for (int i = 0; i < n + 1; i++)
+ {
+ if (!found_eof)
+ {
+ tok = peek_token ();
+ found_eof |= tok->get_id () == Rust::END_OF_FILE;
+
+ Location loc = tok->get_locus ();
+
+ out << "<id=";
+ out << tok->token_id_to_str ();
+ out << (tok->has_str () ? (std::string (", text=") + tok->get_str ()
+ + std::string (", typehint=")
+ + std::string (tok->get_type_hint_str ()))
+ : "")
+ << " ";
+ out << get_line_map ()->to_string (loc) << " ";
+ }
+
+ token_queue.skip (0);
+ }
+}
+
+void
Lexer::replace_current_token (TokenPtr replacement)
{
token_queue.replace_current_value (replacement);