Skip to content

Commit 3415c94

Browse files
authored
Merge pull request #2275 from hzeller/feature-20241003-print-raw-tokens
Give newline token symbolic name; print raw tokens c-escaped.
2 parents 032887d + 30fbee8 commit 3415c94

File tree

4 files changed

+10
-9
lines changed

4 files changed

+10
-9
lines changed

common/text/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ cc_library(
2626
"//common/util:iterator-range",
2727
"//common/util:logging",
2828
"//common/util:range",
29+
"@com_google_absl//absl/strings",
2930
"@com_google_absl//absl/strings:string_view",
3031
],
3132
)

common/text/token_info.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <string>
2121
#include <vector>
2222

23+
#include "absl/strings/escaping.h"
2324
#include "absl/strings/string_view.h"
2425
#include "common/strings/rebase.h"
2526
#include "common/text/constants.h"
@@ -53,14 +54,15 @@ std::ostream &TokenInfo::ToStream(std::ostream &output_stream,
5354
output_stream << "(#";
5455
context.token_enum_translator(output_stream, token_enum_);
5556
output_stream << " @" << left(context.base) << '-' << right(context.base)
56-
<< ": \"" << text_ << "\")";
57+
<< ": \"" << absl::CEscape(text_) << "\")";
5758
const auto dist = std::distance(context.base.end(), text_.end());
5859
CHECK(IsSubRange(text_, context.base)) << "text.end() is off by " << dist;
5960
return output_stream;
6061
}
6162

6263
std::ostream &TokenInfo::ToStream(std::ostream &output_stream) const {
63-
return output_stream << "(#" << token_enum_ << ": \"" << text_ << "\")";
64+
return output_stream << "(#" << token_enum_ << ": \"" << absl::CEscape(text_)
65+
<< "\")";
6466
}
6567

6668
std::string TokenInfo::ToString(const Context &context) const {

verilog/parser/verilog.y

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ is not locally defined, so the grammar here uses only generic identifiers.
667667
%token TK_COMMENT_BLOCK "/&lowast;comment&lowast;/"
668668
%token TK_EOL_COMMENT "// end of line comment"
669669
%token TK_SPACE "<<space>>" /* includes tabs */
670-
%token TK_NEWLINE "<<\\n>>"
670+
%token TK_NEWLINE "<<newline>>"
671671
%token TK_LINE_CONT "<<\\line-cont>>"
672672
%token TK_ATTRIBUTE "(*attribute*)"
673673

@@ -3518,12 +3518,12 @@ instantiation_base
35183518
{ $$ = MakeInstantiationBase($1, $2); }
35193519
/*
35203520
* TODO: support mixed anonymous declarations
3521-
*
3521+
*
35223522
* This production rule was commented out because it caused
35233523
* verible-verilog-syntax to crash for some inputs. It may be necessary to
35243524
* re-enable it in the future to support declarations that mix anonymous and
35253525
* named instances.
3526-
*
3526+
*
35273527
* For more details, see https://github.com/chipsalliance/verible/issues/2181
35283528
*/
35293529
// | reference call_base ',' gate_instance_or_register_variable_list

verilog/tools/syntax/verilog_syntax_test.sh

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,11 +307,9 @@ All lexed tokens:
307307
(#"<<space>>" @6-7: " ")
308308
(#SymbolIdentifier @7-9: "mm")
309309
(#';' @9-10: ";")
310-
(#"<<\\\\n>>" @10-11: "
311-
")
310+
(#"<<newline>>" @10-11: "\\n")
312311
(#"endmodule" @11-20: "endmodule")
313-
(#"<<\\\\n>>" @20-21: "
314-
")
312+
(#"<<newline>>" @20-21: "\\n")
315313
(#"end of file" @21-21: "")
316314
EOF
317315

0 commit comments

Comments
 (0)