forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verilog_excerpt_parse.cc
142 lines (125 loc) · 5.93 KB
/
verilog_excerpt_parse.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
// 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/analysis/verilog_excerpt_parse.h"
#include <functional>
#include <map>
#include <memory>
#include <string>
#include "absl/memory/memory.h"
#include "absl/status/status.h"
#include "absl/strings/ascii.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "common/text/text_structure.h"
#include "common/util/container_util.h"
#include "common/util/logging.h"
#include "verilog/analysis/verilog_analyzer.h"
namespace verilog {
using verible::container::FindOrNull;
// Function template to create any mini-parser for Verilog.
// 'prolog' and 'epilog' are text that wrap around the 'text' argument to
// form a whole Verilog source.
// The returned analyzer's text structure will discard parsed information
// about the prolog and epilog, leaving only the substructure of interest.
static std::unique_ptr<VerilogAnalyzer> AnalyzeVerilogConstruct(
absl::string_view prolog, absl::string_view text, absl::string_view epilog,
absl::string_view filename) {
VLOG(2) << __FUNCTION__;
CHECK(epilog.empty() || absl::ascii_isspace(epilog[0]))
<< "epilog text must begin with a whitespace to prevent unintentional "
"token-joining and escaped-identifier extension.";
const std::string analyze_text = absl::StrCat(prolog, text, epilog);
// Disable parser directive comments because a specific parser
// is already being selected.
auto analyzer_ptr =
absl::make_unique<VerilogAnalyzer>(analyze_text, filename);
if (!ABSL_DIE_IF_NULL(analyzer_ptr)->Analyze().ok()) {
VLOG(2) << __FUNCTION__ << ": Analyze() failed. code:\n" << analyze_text;
// Continue to processes, even if there's an error, so that token
// string_views can be properly rebased.
// There may or may not be a formed syntax tree.
}
// Trim off prolog and epilog from internal text structure to make it look
// as if only text were analyzed.
analyzer_ptr->MutableData().FocusOnSubtreeSpanningSubstring(prolog.length(),
text.length());
// TODO(b/129905498): Strengthen check on the resulting syntax tree root,
// by qualifying it against an expected nonterminal node type(s).
VLOG(2) << "end of " << __FUNCTION__;
return analyzer_ptr; // Let caller check analyzer_ptr's status.
}
std::unique_ptr<VerilogAnalyzer> AnalyzeVerilogPropertySpec(
absl::string_view text, absl::string_view filename) {
return AnalyzeVerilogConstruct("module foo;\nproperty p;\n", text,
"\nendproperty;\nendmodule;\n", filename);
}
std::unique_ptr<VerilogAnalyzer> AnalyzeVerilogStatements(
absl::string_view text, absl::string_view filename) {
return AnalyzeVerilogConstruct("function foo();\n", text, "\nendfunction\n",
filename);
}
std::unique_ptr<VerilogAnalyzer> AnalyzeVerilogExpression(
absl::string_view text, absl::string_view filename) {
return AnalyzeVerilogConstruct("module foo;\nif (", text,
" ) $error;\nendmodule\n", filename);
// $error in this context is an elaboration system task
// The space before the ) is critical to accommodate escaped identifiers.
// Without the space, lexing an escaped identifier would consume part
// of the epilog text.
}
std::unique_ptr<VerilogAnalyzer> AnalyzeVerilogModuleBody(
absl::string_view text, absl::string_view filename) {
return AnalyzeVerilogConstruct("module foo;\n", text, "\nendmodule\n",
filename);
}
std::unique_ptr<VerilogAnalyzer> AnalyzeVerilogClassBody(
absl::string_view text, absl::string_view filename) {
return AnalyzeVerilogConstruct("class foo;\n", text, "\nendclass\n",
filename);
}
std::unique_ptr<VerilogAnalyzer> AnalyzeVerilogPackageBody(
absl::string_view text, absl::string_view filename) {
return AnalyzeVerilogConstruct("package foo;\n", text, "\nendpackage\n",
filename);
}
std::unique_ptr<VerilogAnalyzer> AnalyzeVerilogLibraryMap(
absl::string_view text, absl::string_view filename) {
// The prolog/epilog strings come from verilog.lex as token enums:
// PD_LIBRARY_SYNTAX_BEGIN and PD_LIBRARY_SYNTAX_END.
// These are used in verilog.y to enclose the complete library_description
// grammar rule.
return AnalyzeVerilogConstruct(
"`____verible_verilog_library_begin____\n", text,
"\n`____verible_verilog_library_end____\n", filename);
}
std::unique_ptr<VerilogAnalyzer> AnalyzeVerilogWithMode(
absl::string_view text, absl::string_view filename,
absl::string_view mode) {
static const auto* func_map =
new std::map<absl::string_view,
std::function<std::unique_ptr<VerilogAnalyzer>(
absl::string_view, absl::string_view)>>{
{"parse-as-statements", &AnalyzeVerilogStatements},
{"parse-as-expression", &AnalyzeVerilogExpression},
{"parse-as-module-body", &AnalyzeVerilogModuleBody},
{"parse-as-class-body", &AnalyzeVerilogClassBody},
{"parse-as-package-body", &AnalyzeVerilogPackageBody},
{"parse-as-property-spec", &AnalyzeVerilogPropertySpec},
{"parse-as-library-map", &AnalyzeVerilogLibraryMap},
};
auto func_ptr = FindOrNull(*func_map, mode);
if (func_ptr == nullptr) return nullptr;
return (*func_ptr)(text, filename);
}
} // namespace verilog