forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verilog_token.cc
68 lines (56 loc) · 2.12 KB
/
verilog_token.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright 2017-2020 The Verible Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "verilog/parser/verilog_token.h"
#include <string>
#include "absl/strings/string_view.h"
#include "verilog/parser/verilog_parser.h"
namespace verilog {
// Strings returned by this function are used as token tags
// in verible-verilog-syntax' JSON output. Changing them might
// break third-party code.
absl::string_view TokenTypeToString(size_t tokentype) {
switch (tokentype) {
// Returns stringified symbol name
#define CASE_STRINGIFY(val) \
case verilog_tokentype::val: \
return #val;
// Tokens with verbose or unusual aliases
CASE_STRINGIFY(TK_COMMENT_BLOCK)
CASE_STRINGIFY(TK_EOL_COMMENT)
CASE_STRINGIFY(TK_SPACE)
CASE_STRINGIFY(TK_NEWLINE)
CASE_STRINGIFY(TK_LINE_CONT)
CASE_STRINGIFY(TK_ATTRIBUTE)
CASE_STRINGIFY(TK_FILEPATH)
CASE_STRINGIFY(PP_define_body)
CASE_STRINGIFY(PP_default_text)
#undef CASE_STRINGIFY
// The string returned by verilog_symbol_name() for single quote character
// ("'\\''") contains backslash. This is the only such case, so generic
// unescaping code in `default` section below would be superfluous.
case '\'':
return "'";
// Returns token type name or its alias (if available) as used in verilog.y
default: {
absl::string_view symbol_name(verilog_symbol_name(tokentype));
if (symbol_name.size() >= 2 &&
(symbol_name[0] == '"' || symbol_name[0] == '\'')) {
// Strip quotes
return symbol_name.substr(1, symbol_name.size() - 2);
}
return symbol_name;
}
}
}
} // namespace verilog