forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverilog_token_classifications.cc
198 lines (183 loc) · 5.62 KB
/
verilog_token_classifications.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// 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_classifications.h"
#include "verilog/parser/verilog_token_enum.h"
namespace verilog {
bool IsWhitespace(verilog_tokentype token_type) {
return (token_type == verilog_tokentype::TK_SPACE ||
token_type == verilog_tokentype::TK_NEWLINE);
}
bool IsComment(verilog_tokentype token_type) {
return (token_type == verilog_tokentype::TK_COMMENT_BLOCK ||
token_type == verilog_tokentype::TK_EOL_COMMENT);
}
bool IsUnaryOperator(verilog_tokentype token_type) {
switch (static_cast<int>(token_type)) {
// See verilog/parser/verilog.y
// TODO(fangism): find a way to generate this function automatically
// from the yacc file, perhaps with extra annotations or metadata.
case '+':
case '-':
case '~':
case '&':
case '!':
case '|':
case '^':
case TK_NAND:
case TK_NOR:
case TK_NXOR:
case TK_INCR:
case TK_DECR:
return true;
default:
return false;
}
}
bool IsAssociativeOperator(verilog_tokentype op) {
switch (static_cast<int>(op)) {
case verilog_tokentype::TK_or:
case verilog_tokentype::TK_and:
case verilog_tokentype::TK_LAND:
case verilog_tokentype::TK_LOR:
case verilog_tokentype::TK_NXOR:
case '+':
case '*':
case '^':
case '|':
case '&':
return true;
default:
return false;
}
}
bool IsTernaryOperator(verilog_tokentype token_type) {
switch (static_cast<int>(token_type)) {
case '?':
case ':':
return true;
default:
return false;
}
}
bool IsPreprocessorControlFlow(verilog_tokentype token_type) {
switch (token_type) {
case verilog_tokentype::PP_ifdef:
case verilog_tokentype::PP_ifndef:
case verilog_tokentype::PP_elsif:
case verilog_tokentype::PP_else:
case verilog_tokentype::PP_endif:
return true;
default:
return false;
}
}
bool IsPreprocessorKeyword(verilog_tokentype token_type) {
switch (token_type) {
case verilog_tokentype::PP_include:
case verilog_tokentype::PP_define:
case verilog_tokentype::PP_ifdef:
case verilog_tokentype::PP_ifndef:
case verilog_tokentype::PP_else:
case verilog_tokentype::PP_elsif:
case verilog_tokentype::PP_endif:
case verilog_tokentype::PP_undef:
return true;
default:
return false;
}
}
bool IsPreprocessorControlToken(verilog_tokentype token_type) {
switch (token_type) {
case verilog_tokentype::PP_Identifier:
case verilog_tokentype::PP_include:
case verilog_tokentype::PP_define:
case verilog_tokentype::PP_define_body:
case verilog_tokentype::PP_ifdef:
case verilog_tokentype::PP_ifndef:
case verilog_tokentype::PP_else:
case verilog_tokentype::PP_elsif:
case verilog_tokentype::PP_endif:
case verilog_tokentype::PP_undef:
case verilog_tokentype::PP_default_text:
// Excludes macro call tokens.
return true;
default:
return false;
}
}
bool IsEndKeyword(verilog_tokentype token_type) {
switch (token_type) {
case verilog_tokentype::TK_end:
case verilog_tokentype::TK_endcase:
case verilog_tokentype::TK_endgroup:
case verilog_tokentype::TK_endpackage:
case verilog_tokentype::TK_endgenerate:
case verilog_tokentype::TK_endinterface:
case verilog_tokentype::TK_endfunction:
case verilog_tokentype::TK_endtask:
case verilog_tokentype::TK_endproperty:
case verilog_tokentype::TK_endclocking:
case verilog_tokentype::TK_endclass:
case verilog_tokentype::TK_endmodule:
// TODO(fangism): join and join* keywords?
return true;
default:
return false;
}
}
bool IsUnlexed(verilog_tokentype token_type) {
switch (token_type) {
case verilog_tokentype::MacroArg:
case verilog_tokentype::PP_define_body:
return true;
default:
return false;
}
}
bool IsIdentifierLike(verilog_tokentype token_type) {
switch (token_type) {
case verilog_tokentype::SymbolIdentifier:
case verilog_tokentype::PP_Identifier:
case verilog_tokentype::MacroIdentifier:
case verilog_tokentype::MacroIdItem:
case verilog_tokentype::MacroCallId:
case verilog_tokentype::SystemTFIdentifier:
case verilog_tokentype::EscapedIdentifier:
// specify block built-in functions
case verilog_tokentype::TK_Srecrem:
case verilog_tokentype::TK_Ssetuphold:
case verilog_tokentype::TK_Speriod:
case verilog_tokentype::TK_Shold:
case verilog_tokentype::TK_Srecovery:
case verilog_tokentype::TK_Sremoval:
case verilog_tokentype::TK_Ssetup:
case verilog_tokentype::TK_Sskew:
case verilog_tokentype::TK_Stimeskew:
case verilog_tokentype::TK_Swidth:
// KeywordIdentifier tokens
case verilog_tokentype::TK_access:
case verilog_tokentype::TK_exclude:
case verilog_tokentype::TK_flow:
case verilog_tokentype::TK_from:
case verilog_tokentype::TK_discrete:
case verilog_tokentype::TK_sample:
case verilog_tokentype::TK_infinite:
case verilog_tokentype::TK_continuous:
return true;
default:
break;
}
return false;
}
} // namespace verilog