mCRL2
Loading...
Searching...
No Matches
parse.h
Go to the documentation of this file.
1// Author(s): Wieger Wesselink
2// Copyright: see the accompanying file COPYING or copy at
3// https://github.com/mCRL2org/mCRL2/blob/master/COPYING
4//
5// Distributed under the Boost Software License, Version 1.0.
6// (See accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt)
8//
12
13#ifndef MCRL2_CORE_PARSE_H
14#define MCRL2_CORE_PARSE_H
15
19
20struct D_ParserTables; // prototype
21
22extern "C"
23{
24 extern D_ParserTables parser_tables_mcrl2;
25 extern D_ParserTables parser_tables_fsm;
26 extern D_ParserTables parser_tables_dot;
27}
28
29namespace mcrl2
30{
31namespace core
32{
33
35{
36protected:
37 static std::string get_error_message(const parse_node& node, const std::string& user_message)
38 {
39 try
40 {
41 return node.add_context(user_message);
42 }
43 catch (...)
44 {
45 return user_message;
46 }
47 }
48
49 parse_node_exception(const std::string& message)
50 : mcrl2::runtime_error(message)
51 { }
52
53public:
54 parse_node_exception(const parse_node& node, const std::string& message)
55 : mcrl2::runtime_error(get_error_message(node, message))
56 { }
57};
58
60{
61private:
62 static std::string get_error_message(const parser& p, const parse_node& node)
63 {
64 std::string inherited = parse_node_exception::get_error_message(node, "unexpected parse node!");
65 try
66 {
67 std::ostringstream out;
68 out << inherited << std::endl;
69 p.print_node(out, node);
70 return out.str();
71 }
72 catch (...)
73 {
74 return inherited;
75 }
76 }
77public:
80 { }
81};
82
84{
86
87 parser_actions(const parser& parser_)
88 : m_parser(parser_)
89 {}
90
91 // starts a traversal in node, and calls the function f to each subnode of the given type
92 template <typename Function>
93 void traverse(const parse_node& node, const Function& f) const
94 {
95 if (!node)
96 {
97 return;
98 }
99 if (!f(node))
100 {
101 for (int i = 0; i < node.child_count(); i++)
102 {
103 traverse(node.child(i), f);
104 }
105 }
106 }
107
108 // callback function that applies a function to nodes of a given type
109 template <typename Function>
110 struct visitor
111 {
113 const std::string& type;
114 const Function& f;
115
116 visitor(const parser_table& table_, const std::string& type_, const Function& f_)
117 : table(table_),
118 type(type_),
119 f(f_)
120 {}
121
122 bool operator()(const parse_node& node) const
123 {
124 if (table.symbol_name(node) == type)
125 {
126 f(node);
127 return true;
128 }
129 return false;
130 }
131 };
132
133 template <typename Function>
134 visitor<Function> make_visitor(const parser_table& table, const std::string& type, const Function& f) const
135 {
136 return visitor<Function>(table, type, f);
137 }
138
139 // callback function that applies a function to a node, and adds the result to a container
140 template <typename Container, typename Function>
142 {
144 const std::string& type;
145 Container& container;
146 const Function& f;
147
148 collector(const parser_table& table_, const std::string& type_, Container& container_, const Function& f_)
149 : table(table_),
150 type(type_),
151 container(container_),
152 f(f_)
153 {}
154
155 bool operator()(const parse_node& node) const
156 {
157 if (table.symbol_name(node) == type)
158 {
159 container.push_back(f(node));
160 return true;
161 }
162 return false;
163 }
164 };
165
166 template <typename Container, typename Function>
167 collector<Container, Function> make_collector(const parser_table& table, const std::string& type, Container& container, const Function& f) const
168 {
169 return collector<Container, Function>(table, type, container, f);
170 }
171
172 // callback function that applies a function to a node, and adds the result to a set container
173 template <typename SetContainer, typename Function>
175 {
177 const std::string& type;
178 SetContainer& container;
179 const Function& f;
180
181 set_collector(const parser_table& table_, const std::string& type_, SetContainer& container_, const Function& f_)
182 : table(table_),
183 type(type_),
184 container(container_),
185 f(f_)
186 {}
187
188 bool operator()(const parse_node& node) const
189 {
190 if (table.symbol_name(node) == type)
191 {
192 container.insert(f(node));
193 return true;
194 }
195 return false;
196 }
197 };
198
199 template <typename SetContainer, typename Function>
200 set_collector<SetContainer, Function> make_set_collector(const parser_table& table, const std::string& type, SetContainer& container, const Function& f) const
201 {
202 return set_collector<SetContainer, Function>(table, type, container, f);
203 }
204
205 std::string symbol_name(const parse_node& node) const
206 {
207 return m_parser.symbol_table().symbol_name(node.symbol());
208 }
209};
210
212{
214 : parser_actions(parser_)
215 {}
216
217 template <typename T, typename Function>
218 atermpp::term_list<T> parse_list(const parse_node& node, const std::string& type, const Function& f) const
219 {
220 std::vector<T> result;
221 traverse(node, make_collector(m_parser.symbol_table(), type, result, f));
222 return atermpp::term_list<T>(result.begin(), result.end());
223 }
224
225 template <typename T, typename Function>
226 std::vector<T> parse_vector(const parse_node& node, const std::string& type, const Function& f) const
227 {
228 std::vector<T> result;
229 traverse(node, make_collector(m_parser.symbol_table(), type, result, f));
230 return result;
231 }
232
234 {
235 return core::identifier_string(node.string());
236 }
237
239 {
240 return core::identifier_string(node.string());
241 }
242
244 {
245 return parse_list<core::identifier_string>(node, "Id", [&](const core::parse_node& node) { return parse_Id(node); });
246 }
247};
248
249template <typename T>
250void print_aterm(const T&)
251{
252}
253
254template<>
255inline
257{
258 std::clog << "aterm: " << x << std::endl;
259}
260
262inline
263identifier_string parse_identifier(const std::string& text)
264{
266 unsigned int start_symbol_index = p.start_symbol_index("Id");
267 bool partial_parses = false;
268 core::parse_node node = p.parse(text, start_symbol_index, partial_parses);
270 return result;
271}
272
273inline
274bool is_user_identifier(std::string const& s)
275{
276 try
277 {
279 }
280 catch (...)
281 {
282 return false;
283 }
284 return true;
285}
286
287} // namespace core
288} // namespace mcrl2
289
290#endif // MCRL2_CORE_PARSE_H
Term containing a string.
A list of aterm objects.
Definition aterm_list.h:24
parse_node_exception(const std::string &message)
Definition parse.h:49
parse_node_exception(const parse_node &node, const std::string &message)
Definition parse.h:54
static std::string get_error_message(const parse_node &node, const std::string &user_message)
Definition parse.h:37
parse_node_unexpected_exception(const parser &p, const parse_node &node)
Definition parse.h:78
static std::string get_error_message(const parser &p, const parse_node &node)
Definition parse.h:62
Standard exception class for reporting runtime errors.
Definition exception.h:27
D_ParserTables parser_tables_fsm
D_ParserTables parser_tables_dot
D_ParserTables parser_tables_mcrl2
add your file description here.
aterm representations of identifier strings.
void syntax_error_fn(struct D_Parser *ap)
Custom syntax error function that prints both the line number and the column.
Definition dparser.cpp:463
struct D_ParseNode * ambiguity_fn(struct D_Parser *, int, struct D_ParseNode **)
Function for resolving ambiguities in the '_ -> _ <> _' operator for process expressions.
Definition dparser.cpp:332
void print_aterm(const T &)
Definition parse.h:250
atermpp::aterm_string identifier_string
String type of the LPS library. Identifier strings are represented internally as ATerms.
identifier_string parse_identifier(const std::string &text)
Parse an identifier.
Definition parse.h:263
bool is_user_identifier(std::string const &s)
Definition parse.h:274
A class that takes a linear process specification and checks all tau-summands of that LPS for conflue...
Definition indexed_set.h:72
core::identifier_string parse_Id(const parse_node &node) const
Definition parse.h:233
atermpp::term_list< T > parse_list(const parse_node &node, const std::string &type, const Function &f) const
Definition parse.h:218
default_parser_actions(const parser &parser_)
Definition parse.h:213
std::vector< T > parse_vector(const parse_node &node, const std::string &type, const Function &f) const
Definition parse.h:226
core::identifier_string_list parse_IdList(const parse_node &node) const
Definition parse.h:243
core::identifier_string parse_Number(const parse_node &node) const
Definition parse.h:238
Wrapper for D_ParseNode.
Definition dparser.h:86
parse_node child(int i) const
Definition dparser.cpp:43
int child_count() const
Definition dparser.cpp:37
std::string add_context(const std::string &message) const
Definition dparser.cpp:27
std::string string() const
Definition dparser.cpp:53
bool operator()(const parse_node &node) const
Definition parse.h:155
collector(const parser_table &table_, const std::string &type_, Container &container_, const Function &f_)
Definition parse.h:148
set_collector(const parser_table &table_, const std::string &type_, SetContainer &container_, const Function &f_)
Definition parse.h:181
bool operator()(const parse_node &node) const
Definition parse.h:188
visitor(const parser_table &table_, const std::string &type_, const Function &f_)
Definition parse.h:116
const parser_table & table
Definition parse.h:112
bool operator()(const parse_node &node) const
Definition parse.h:122
std::string symbol_name(const parse_node &node) const
Definition parse.h:205
parser_actions(const parser &parser_)
Definition parse.h:87
void traverse(const parse_node &node, const Function &f) const
Definition parse.h:93
const parser & m_parser
Definition parse.h:85
set_collector< SetContainer, Function > make_set_collector(const parser_table &table, const std::string &type, SetContainer &container, const Function &f) const
Definition parse.h:200
visitor< Function > make_visitor(const parser_table &table, const std::string &type, const Function &f) const
Definition parse.h:134
collector< Container, Function > make_collector(const parser_table &table, const std::string &type, Container &container, const Function &f) const
Definition parse.h:167
Wrapper for D_ParserTables.
Definition dparser.h:117
std::string symbol_name(unsigned int i) const
Definition dparser.cpp:115
Wrapper for D_Parser and its corresponding D_ParserTables.
Definition dparser.h:148
void print_node(std::ostream &out, const parse_node &node) const
Definition dparser.h:180
parse_node parse(const std::string &text, unsigned int start_symbol_index=0, bool partial_parses=false)
Parses a string. N.B. The user is responsible for destruction of the returned value by calling destro...
Definition dparser.cpp:209
const parser_table & symbol_table() const
Definition dparser.cpp:199
unsigned int start_symbol_index(const std::string &name) const
Definition dparser.cpp:204