-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathport_parser.cpp
210 lines (186 loc) · 7.79 KB
/
port_parser.cpp
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
199
200
201
202
203
204
205
206
207
208
209
210
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_object.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/io.hpp>
#include <vector>
#include <iostream>
#include <string>
#include <complex>
namespace client
{
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
///////////////////////////////////////////////////////////////////////////
// Our port_sig struct
///////////////////////////////////////////////////////////////////////////
//[tutorial_port_sig
struct port_sig
{
std::string sig_name;
std::string dir;
};
//]
}
// We need to tell fusion about our port_sig struct
// to make it a first-class fusion citizen. This has to
// be in global scope.
BOOST_FUSION_ADAPT_STRUCT(
client::port_sig,
(std::string, sig_name)
(std::string, dir)
//(VHDL_Parser::direction, dir)
)
namespace client
{
///////////////////////////////////////////////////////////////////////////////
// Our port_sig parser
///////////////////////////////////////////////////////////////////////////////
//[tutorial_port_sig
template <typename Iterator>
struct port_parser : qi::grammar<Iterator, std::vector<port_sig>(), ascii::space_type>
{
port_parser() : port_parser::base_type(start)
{
using qi::int_;
using qi::lit;
using qi::double_;
using qi::lexeme;
using ascii::char_;
using ascii::no_case;
quoted_string %= lexeme['"' >> +(char_ - '"') >> '"'];
start %= lit("port")
>> '('
>> p_sig >> *(lit(';') >> p_sig)
>> ')' >> ';';
p_sig %= ident >> ':' >> direction_spec ;
ident %= ((char_("a-zA-Z_") >> *char_("a-zA-Z_0-9")) - keyword );
direction_spec %= (in_kwd | out_kwd | inout_kwd );
/* //more keywords for later
dot = char_('.');
colon = char_(':');
semicolon = char_(';');
comma = char_(',');
single_quote = char_('\'');
double_quote = char_('"');
lparen = char_('(');
comment_delim = lexeme["--"];
sig_assign = lexeme["<="];
eq_gt = lexeme["=>"];
var_assign = lexeme[":="];
x_char = no_case[char_('x')];
entity_kwd = no_case[ lexeme["entity"] ];
architecture_kwd = no_case[ lexeme["architecture"] ];
package_kwd = no_case[ lexeme["package"] ];
port_kwd = no_case[ lexeme["port"] ];
map_kwd = no_case[ lexeme["map"] ];
body_kwd = no_case[ lexeme["body"] ];
generic_kwd = no_case[ lexeme["generic"] ];
signal_kwd = no_case[ lexeme["signal"] ];
is_kwd = no_case[ lexeme["is"] ];
to_kwd = no_case[ lexeme["to"] ];
if_kwd = no_case[ lexeme["if"] ];
of_kwd = no_case[ lexeme["of"] ];
*/
in_kwd = no_case[ lexeme["in"] ];
out_kwd = no_case[ lexeme["out"] ];
inout_kwd = no_case[ lexeme["inout"]];
/*
for_kwd = no_case[ lexeme["for"] ];
downto_kwd = no_case[ lexeme["downto"] ];
case_kwd = no_case[ lexeme["case"] ];
loop_kwd = no_case[ lexeme["loop"] ];
component_kwd = no_case[ lexeme["component"] ];
configuration_kwd = no_case[ lexeme["configuration"] ];
library_kwd = no_case[ lexeme["library"] ];
process_kwd = no_case[ lexeme["process"] ];
use_kwd = no_case[ lexeme["use"] ];
begin_kwd = no_case[ lexeme["begin"]] ;
end_kwd = no_case[ lexeme["end"] ];
type_kwd = no_case[ lexeme["type"]];
when_kwd = no_case[ lexeme["when"]];
else_kwd = no_case[ lexeme["else"]];
not_kwd = no_case[ lexeme["not"]];
and_kwd = no_case[ lexeme["and"]];
attribute_kwd = no_case[ lexeme["attribute"]];
// are pragams are case-sensitive?
pragma = no_case[ lexeme["pragma"]];
synopsys = no_case[ lexeme["synopsys"]];
synthesis = no_case[ lexeme["synthesis"]];
translate_off = no_case[ lexeme["translate_off"]];
translate_on = no_case[ lexeme["translate_on"]];
*/
/*
keyword = (
entity_kwd | architecture_kwd | package_kwd |
port_kwd | map_kwd | body_kwd | generic_kwd |
signal_kwd | is_kwd | to_kwd | if_kwd |
of_kwd | in_kwd | out_kwd | inout_kwd | for_kwd |
downto_kwd | case_kwd | loop_kwd | component_kwd |
library_kwd | process_kwd | use_kwd | begin_kwd |
end_kwd | type_kwd | when_kwd | else_kwd | not_kwd |
and_kwd | attribute_kwd | configuration_kwd
);
*/
keyword = ( in_kwd | out_kwd | inout_kwd);
}
qi::rule<Iterator, std::string(), ascii::space_type> direction_spec;
qi::rule<Iterator, std::string(), ascii::space_type> ident;
qi::rule<Iterator, std::string(), ascii::space_type> quoted_string;
qi::rule<Iterator, port_sig(), ascii::space_type> p_sig;
qi::rule<Iterator, std::vector<port_sig>(), ascii::space_type> start;
qi::rule<Iterator, std::string(), ascii::space_type> in_kwd;
qi::rule<Iterator, std::string(), ascii::space_type> out_kwd;
qi::rule<Iterator, std::string(), ascii::space_type> inout_kwd;
qi::rule<Iterator, std::string(), ascii::space_type> keyword;
};
//]
}
////////////////////////////////////////////////////////////////////////////
// Main program
////////////////////////////////////////////////////////////////////////////
int
main()
{
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "\t\tA port_sig parser for Spirit...\n\n";
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout
<< "Give me a port of the form :"
<< "port(sig_name: direction ) \n";
std::cout << "Type [q or Q] to quit\n\n";
using boost::spirit::ascii::space;
typedef std::string::const_iterator iterator_type;
typedef client::port_parser<iterator_type> port_parser;
port_parser g; // Our grammar
std::string str;
while (getline(std::cin, str))
{
if (str.empty() || str[0] == 'q' || str[0] == 'Q')
break;
std::vector<client::port_sig> emps;
std::string::const_iterator iter = str.begin();
std::string::const_iterator end = str.end();
bool r = phrase_parse(iter, end, g, space, emps);
if (r && iter == end)
{
std::cout << boost::fusion::tuple_open('[');
std::cout << boost::fusion::tuple_close(']');
std::cout << boost::fusion::tuple_delimiter(", ");
std::cout << "-------------------------\n";
std::cout << "Parsing succeeded\n";
//std::cout << "got: " << boost::fusion::as_vector(emp) << std::endl;
std::cout << "\n-------------------------\n";
}
else
{
std::cout << "-------------------------\n";
std::cout << "Parsing failed\n";
std::cout << "-------------------------\n";
}
}
std::cout << "Bye... :-) \n\n";
return 0;
}