mCRL2
Loading...
Searching...
No Matches
dparser.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//
11
12#ifndef MCRL2_CORE_DPARSER_H
13#define MCRL2_CORE_DPARSER_H
14
15#include <cstddef>
16#include <sstream>
17#include <string>
18
19// prototypes
20struct D_ParseNode;
21struct D_ParserTables;
22struct D_Parser;
23struct d_loc_t;
24typedef void (*D_SyntaxErrorFn)(struct D_Parser *);
25typedef struct D_ParseNode *(*D_AmbiguityFn)(struct D_Parser *,
26 int n, struct D_ParseNode **v);
27
28
29
30namespace mcrl2 {
31
32namespace core {
33
34namespace detail
35{
36
37std::string add_context(const d_loc_t* loc, const std::string& message);
38
39template <class T> // note, T is only a dummy
41{
42 static std::size_t value;
43 static std::size_t max_value;
44};
45
46template <class T>
48
49template <class T>
51
52inline
54{
56}
57
58inline
60{
62}
63
64inline
66{
68}
69
70inline
72{
74}
75
76inline
78{
80}
81
82} // namespace detail
83
86{
87 D_ParseNode* node;
88 D_Parser* parser = nullptr;
89
90 explicit parse_node(D_ParseNode* n, D_Parser* parser_ = nullptr)
91 : node(n), parser(parser_)
92 {}
93
94 int symbol() const;
95 int child_count() const;
96
97 // 0 <= i < child_count()
98 parse_node child(int i) const;
100 std::string string() const;
101 std::string tree() const;
102 int column() const;
103 int line() const;
104 std::string pathname() const;
105 std::string add_context(const std::string& message) const;
106
107 explicit operator bool() const
108 {
109 return node != nullptr;
110 }
111
112 ~parse_node();
113};
114
117{
118 D_ParserTables& m_table;
119
120 explicit parser_table(D_ParserTables& table)
121 : m_table(table)
122 { }
123
124 // Prints a tree of
125 std::string tree(const core::parse_node& node) const;
126
127 // Returns the number of symbols in the table
128 unsigned int symbol_count() const;
129
130 // Returns the name of the i-th symbol
131 std::string symbol_name(unsigned int i) const;
132
133 std::string symbol_name(const parse_node& node) const;
134
135 // Returns the 'start symbol' of the i-th symbol
136 int start_symbol(unsigned int i) const;
137
138 // Returns true if the i-th symbol is of type D_SYMBOL_NTERM
139 bool is_term_symbol(unsigned int i) const;
140
141 unsigned int start_symbol_index(const std::string& name) const;
142
143 void print() const;
144};
145
147struct parser
148{
150 D_Parser* m_parser;
152
153 explicit parser(D_ParserTables& tables, D_AmbiguityFn ambiguity_fn = nullptr, D_SyntaxErrorFn syntax_error_fn = nullptr, std::size_t max_error_message_count = 1);
154
155 ~parser();
156
157 const parser_table& symbol_table() const;
158
159 unsigned int start_symbol_index(const std::string& name) const;
160
163 parse_node parse(const std::string& text, unsigned int start_symbol_index = 0, bool partial_parses = false);
164
165 void print_symbol_table() const;
166
167 std::string indent(unsigned int count) const;
168
169 std::string truncate(const std::string& s, unsigned int max_size = 20) const;
170
171 void print_tree(const parse_node& node, unsigned int level = 0) const;
172
173 void destroy_parse_node(const parse_node& node);
174
175 void custom_parse_error(const std::string& message) const;
176
178 void announce(D_ParseNode& node_ref);
179
180 void print_node(std::ostream& out, const parse_node& node) const
181 {
182 out << "symbol = " << symbol_table().symbol_name(node) << std::endl
183 << "string = " << node.string() << std::endl
184 << "child_count = " << node.child_count();
185 for (int i = 0; i < node.child_count(); i++)
186 {
187 out << std::endl
188 << "child " << i << " = " << symbol_table().symbol_name(node.child(i))
189 << " " << node.child(i).string();
190 }
191 }
192
193 std::string print_node(const parse_node& node) const
194 {
195 std::ostringstream out;
196 print_node(out, node);
197 return out.str();
198 }
199};
200
201} // namespace core
202
203} // namespace mcrl2
204
205#endif // MCRL2_CORE_DPARSER_H
struct D_ParseNode *(* D_AmbiguityFn)(struct D_Parser *, int n, struct D_ParseNode **v)
Definition dparser.h:25
void(* D_SyntaxErrorFn)(struct D_Parser *)
Definition dparser.h:24
std::string add_context(const d_loc_t *loc, const std::string &message)
Definition dparser.cpp:277
void increment_dparser_error_message_count()
Definition dparser.h:59
void set_dparser_max_error_message_count(std::size_t n)
Definition dparser.h:77
void reset_dparser_error_message_count()
Definition dparser.h:53
std::size_t get_dparser_max_error_message_count()
Definition dparser.h:71
std::size_t get_dparser_error_message_count()
Definition dparser.h:65
A class that takes a linear process specification and checks all tau-summands of that LPS for conflue...
Definition indexed_set.h:72
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
D_ParseNode * node
Definition dparser.h:87
std::string pathname() const
Definition dparser.cpp:80
parse_node find_in_tree(int symbol) const
Definition dparser.cpp:48
std::string tree() const
Definition dparser.cpp:58
std::string add_context(const std::string &message) const
Definition dparser.cpp:27
parse_node(D_ParseNode *n, D_Parser *parser_=nullptr)
Definition dparser.h:90
std::string string() const
Definition dparser.cpp:53
Wrapper for D_ParserTables.
Definition dparser.h:117
D_ParserTables & m_table
Definition dparser.h:118
std::string symbol_name(unsigned int i) const
Definition dparser.cpp:115
std::string tree(const core::parse_node &node) const
Definition dparser.cpp:94
bool is_term_symbol(unsigned int i) const
Definition dparser.cpp:144
int start_symbol(unsigned int i) const
Definition dparser.cpp:138
unsigned int start_symbol_index(const std::string &name) const
Definition dparser.cpp:149
parser_table(D_ParserTables &table)
Definition dparser.h:120
unsigned int symbol_count() const
Definition dparser.cpp:109
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
void announce(D_ParseNode &node_ref)
Callback function for nodes in the parse tree.
Definition dparser.cpp:269
std::string print_node(const parse_node &node) const
Definition dparser.h:193
void custom_parse_error(const std::string &message) const
Definition dparser.cpp:516
void print_symbol_table() const
Definition dparser.cpp:225
D_Parser * m_parser
Definition dparser.h:150
std::size_t m_max_error_message_count
Definition dparser.h:151
std::string indent(unsigned int count) const
Definition dparser.cpp:230
std::string truncate(const std::string &s, unsigned int max_size=20) const
Definition dparser.cpp:235
parser_table m_table
Definition dparser.h:149
void print_tree(const parse_node &node, unsigned int level=0) const
Definition dparser.cpp:249
unsigned int start_symbol_index(const std::string &name) const
Definition dparser.cpp:204
void destroy_parse_node(const parse_node &node)
Definition dparser.cpp:263