Skip to content

Commit 4b62b9e

Browse files
authored
Merge pull request #873 from diffblue/vlindex-attributes
vlindex: parse attributes
2 parents c8b1a63 + ce57d28 commit 4b62b9e

File tree

2 files changed

+82
-26
lines changed

2 files changed

+82
-26
lines changed

src/vlindex/vlindex_parser.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ void verilog_indexer_parsert::rModule(
114114

115115
void verilog_indexer_parsert::rItem()
116116
{
117+
// Attribute before item?
118+
rAttribute();
119+
117120
auto &token = peek();
118121

119122
if(token == TOK_MODULE)
@@ -603,6 +606,39 @@ void verilog_indexer_parsert::rModport()
603606
skip_until(';');
604607
}
605608

609+
void verilog_indexer_parsert::rAttribute()
610+
{
611+
// (* attr_name = constant, ... *)
612+
if(peek() == TOK_PARENASTERIC)
613+
{
614+
while(true)
615+
{
616+
rAttrSpec();
617+
if(peek() != ',')
618+
break;
619+
next_token(); // eat ,
620+
}
621+
622+
if(peek() == TOK_ASTERICPAREN)
623+
next_token(); // eat *)
624+
}
625+
}
626+
627+
void verilog_indexer_parsert::rAttrSpec()
628+
{
629+
// attr_name = constant
630+
auto name = next_token();
631+
if(!name.is_identifier())
632+
return; // give up
633+
634+
auto eq = next_token();
635+
if(eq != '=')
636+
return; // give up
637+
638+
rExpression([](const tokent &token)
639+
{ return token == ',' || token == TOK_ASTERICPAREN; });
640+
}
641+
606642
void verilog_indexer_parsert::rCell()
607643
{
608644
next_token(); // cell
@@ -695,6 +731,23 @@ void verilog_indexer_parsert::rParenExpression()
695731
}
696732
}
697733

734+
void verilog_indexer_parsert::rExpression(
735+
std::function<bool(const tokent &)> stopping_condition)
736+
{
737+
std::size_t count = 0;
738+
739+
while(count != 0 || !stopping_condition(peek()))
740+
{
741+
auto token = next_token();
742+
if(token.is_eof())
743+
return;
744+
else if(token == '(')
745+
count++;
746+
else if(token == ')')
747+
count--;
748+
}
749+
}
750+
698751
void verilog_indexer_parsert::rDeclaration()
699752
{
700753
if(peek() == TOK_PROTECTED)

src/vlindex/vlindex_parser.h

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,32 @@ class verilog_indexer_parsert
3838
verilog_indexert &indexer;
3939
irep_idt current_module;
4040

41+
struct tokent
42+
{
43+
int kind;
44+
std::string text;
45+
bool is_eof() const
46+
{
47+
return kind == 0; // EOF, flex magic number
48+
}
49+
bool is_identifier() const
50+
{
51+
return kind == TOK_NON_TYPE_IDENTIFIER || kind == TOK_TYPE_IDENTIFIER;
52+
}
53+
bool is_system_identifier() const
54+
{
55+
return kind == TOK_SYSIDENT;
56+
}
57+
bool operator==(int other) const
58+
{
59+
return kind == other;
60+
}
61+
bool operator!=(int other) const
62+
{
63+
return kind != other;
64+
}
65+
};
66+
4167
// modules, classes, primitives, packages, interfaces, configurations
4268
void rModule(verilog_indexert::idt::kindt, int end_token);
4369
void rImport();
@@ -46,6 +72,8 @@ class verilog_indexer_parsert
4672
void rItem();
4773
void rBind();
4874

75+
void rAttribute();
76+
void rAttrSpec();
4977
void rCell();
5078
void rDesign();
5179
void rInterconnect();
@@ -69,6 +97,7 @@ class verilog_indexer_parsert
6997
void rCaseLabel();
7098
void rUniquePriority();
7199
void rParenExpression(); // (expression)
100+
void rExpression(std::function<bool(const tokent &)>); // expression
72101
void rDeclaration(); // var, reg, wire, input, typedef, defparam ...
73102
void rType();
74103
void rTypeOpt();
@@ -92,32 +121,6 @@ class verilog_indexer_parsert
92121
void rSpecify();
93122
void skip_until(int token);
94123

95-
struct tokent
96-
{
97-
int kind;
98-
std::string text;
99-
bool is_eof() const
100-
{
101-
return kind == 0; // EOF, flex magic number
102-
}
103-
bool is_identifier() const
104-
{
105-
return kind == TOK_NON_TYPE_IDENTIFIER || kind == TOK_TYPE_IDENTIFIER;
106-
}
107-
bool is_system_identifier() const
108-
{
109-
return kind == TOK_SYSIDENT;
110-
}
111-
bool operator==(int other) const
112-
{
113-
return kind == other;
114-
}
115-
bool operator!=(int other) const
116-
{
117-
return kind != other;
118-
}
119-
};
120-
121124
static bool may_be_type(const tokent &);
122125

123126
// consume a token, returned as rvalue

0 commit comments

Comments
 (0)