Awali
Another Weighted Automata library
visitor.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2022 Sylvain Lombardy, Victor Marsault, Jacques Sakarovitch
3 //
4 // Awali is a free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef COMMON_JSON_VISITOR_HH
18 #define COMMON_JSON_VISITOR_HH
19 
20 #include <cstddef>
21 
23 #include <awali/common/json_ast.hh>
24 
25 namespace awali {
26 namespace json {
27 
28 enum class visit_t {
29  IGNORE_UNVISITED = -1,
30  CONTINUE = 0,
33 };
34 
71 class visitor_t {
72 private:
73  visit_t _stop;
74  json::path_t _path_from_root;
75  template<typename L> inline void visit_leaf(L* node)
76  { enter(node); if (!is_stopped()) leaf(node->value); leave(node);}
77 
78 public:
79  inline void stop(visit_t b = visit_t::IGNORE_UNVISITED) {_stop=b;}
80  inline void step_stop() {
81  switch(_stop) {
83  _stop = visit_t::CONTINUE;
84  break;
87  break;
88  default:{}
89  }
90  }
91  inline bool is_stopped() {return visit_t::CONTINUE != _stop;}
92  inline visit_t stop_status() {return _stop;}
93  inline json::path_t const& path() {return _path_from_root; }
94  inline unsigned depth() {return _path_from_root.size(); }
95 
96 protected:
97  visitor_t() : _stop(visit_t::CONTINUE), _path_from_root() {}
98 
99  /* Part of these functions are supposed to be overriden. */
100 
104  virtual void enter(node_t const*) {}
105 
106 
111  virtual void enter(bool_t const*) {}
112 
113 
117  virtual void enter(object_t const*) {}
118 
122  virtual void enter(array_t const*) {}
123 
128  virtual void enter(int_t const*) {}
129 
134  virtual void enter(string_t const*) {}
135 
140  virtual void enter(float_t const*) {}
141 
146  virtual void enter(null_t const*) {}
147 
148 
152  virtual void leaf(int) {}
153 
157  virtual void leaf(std::string const&) {}
158 
162  virtual void leaf(double) {}
163 
167  virtual void leaf(bool) {}
168 
172  virtual void leaf(std::nullptr_t) {}
173 
174 
176  virtual void before_child(object_t const*, std::string const&,
177  node_t const*) {}
178 
179  virtual void after_child(object_t const*, std::string const&,
180  node_t const*) {}
181 
182 
183  virtual void before_child(array_t const*, unsigned,
184  node_t const*) {}
185 
186  virtual void after_child(array_t const*, unsigned,
187  node_t const*) {}
188 
190  virtual void before_child(node_t const*, uint_or_string_t const&,
191  node_t const*) {}
192 
194  virtual void after_child(node_t const*, uint_or_string_t const&,
195  node_t const*) {}
196 
197  /* These functions are called in between */
198 
203  virtual void between_children(node_t const*) {}
204 
209  virtual void between_children(object_t const*) {}
210 
215  virtual void between_children(array_t const*) {}
216 
218  virtual void leave(node_t const*) {}
219 
220  virtual void leave(bool_t const*) {}
221  virtual void leave(object_t const*) {}
222  virtual void leave(array_t const*) {}
223  virtual void leave(int_t const*) {}
224  virtual void leave(string_t const*) {}
225  virtual void leave(float_t const*) {}
226  virtual void leave(null_t const*) {}
227 
228  /* These functions are usually not overriden. */
229  inline virtual void visit(bool_t const* node) {visit_leaf(node);}
230  inline virtual void visit(int_t const* node) {visit_leaf(node);}
231  inline virtual void visit(string_t const* node) {visit_leaf(node);}
232  inline virtual void visit(float_t const* node) {visit_leaf(node);}
233  inline virtual void visit(null_t const* node)
234  {enter(node); if (!is_stopped()) leaf(nullptr); leave(node);}
235 
236  virtual void visit(object_t const* node);
237  virtual void visit(array_t const* node);
238  virtual void visit(node_t const* node);
239 
240 public:
241  inline virtual void run(json_ast_t tree) { run (&(*tree)); }
242  inline virtual void run(node_t const* node) { visit(node); }
243 
244  virtual ~visitor_t() = default;
245 };
246 
247 
248 } // end of namespace json
249 } // end of namespace awali
250 #endif
Definition: node.hh:431
Definition: node.hh:555
Definition: node.hh:508
Definition: node.hh:488
Definition: node.hh:193
Definition: node.hh:578
Definition: node.hh:367
Definition: node.hh:529
Definition: node.hh:48
Abstract class that helps implement a visitor of json tree or subtree.
Definition: visitor.hh:71
bool is_stopped()
Definition: visitor.hh:91
visit_t stop_status()
Definition: visitor.hh:92
virtual void visit(object_t const *node)
virtual void leave(bool_t const *)
Definition: visitor.hh:220
virtual void leave(null_t const *)
Definition: visitor.hh:226
virtual void enter(int_t const *)
Function called when starting visiting a bool_t, just after the kind-independent (node_t const*),...
Definition: visitor.hh:128
virtual ~visitor_t()=default
virtual void run(json_ast_t tree)
Definition: visitor.hh:241
virtual void enter(string_t const *)
Function called when starting visiting a string_t, just after the kind-independent (node_t const*),...
Definition: visitor.hh:134
virtual void enter(bool_t const *)
Function called when starting visiting a bool_t, just after the kind-independent (node_t const*),...
Definition: visitor.hh:111
virtual void enter(array_t const *)
Function called when starting visiting an array_t, just after the kind-independent (node_t const*).
Definition: visitor.hh:122
unsigned depth()
Definition: visitor.hh:94
virtual void enter(null_t const *)
Function called when starting visiting a null_t, just after the kind-independent (node_t const*),...
Definition: visitor.hh:146
virtual void visit(int_t const *node)
Definition: visitor.hh:230
virtual void between_children(array_t const *)
Function called between visiting two children, after the function with the same name and argument typ...
Definition: visitor.hh:215
virtual void between_children(node_t const *)
Function called between visiting two children, before the function with the same name and specialized...
Definition: visitor.hh:203
void stop(visit_t b=visit_t::IGNORE_UNVISITED)
Definition: visitor.hh:79
virtual void visit(bool_t const *node)
Definition: visitor.hh:229
virtual void leaf(double)
Function called when visiting a float_t, after (float_t const*) but before leave(float_t const*).
Definition: visitor.hh:162
virtual void leave(object_t const *)
Definition: visitor.hh:221
virtual void before_child(object_t const *, std::string const &, node_t const *)
Function called just before and just after visiting a child node.
Definition: visitor.hh:176
virtual void leave(int_t const *)
Definition: visitor.hh:223
visitor_t()
Definition: visitor.hh:97
virtual void leave(array_t const *)
Definition: visitor.hh:222
void step_stop()
Definition: visitor.hh:80
virtual void run(node_t const *node)
Definition: visitor.hh:242
virtual void leaf(std::string const &)
Function called when visiting a string_t, after (string_t const*) but before leave(string_t const*).
Definition: visitor.hh:157
virtual void after_child(object_t const *, std::string const &, node_t const *)
Definition: visitor.hh:179
json::path_t const & path()
Definition: visitor.hh:93
virtual void leave(float_t const *)
Definition: visitor.hh:225
virtual void visit(node_t const *node)
virtual void enter(object_t const *)
Function called when starting visiting an object_t, just after the kind-independent (node_t const*).
Definition: visitor.hh:117
virtual void visit(null_t const *node)
Definition: visitor.hh:233
virtual void leaf(std::nullptr_t)
Function called when visiting a null_t, after (null_t const*) but before leave(null_t const*).
Definition: visitor.hh:172
virtual void leave(node_t const *)
This function is called for every node type.
Definition: visitor.hh:218
virtual void after_child(array_t const *, unsigned, node_t const *)
Definition: visitor.hh:186
virtual void between_children(object_t const *)
Function called between visiting two children, after the function with the same name and argument typ...
Definition: visitor.hh:209
virtual void before_child(array_t const *, unsigned, node_t const *)
Definition: visitor.hh:183
virtual void leave(string_t const *)
Definition: visitor.hh:224
virtual void leaf(bool)
Function called when visiting a bool_t, after (bool_t const*) but before leave(bool_t const*).
Definition: visitor.hh:167
virtual void visit(array_t const *node)
virtual void leaf(int)
Function called when visiting a int_t, after (int_t const*) but before leave(int_t const*).
Definition: visitor.hh:152
virtual void enter(float_t const *)
Function called when starting visiting a float_t, just after the kind-independent (node_t const*),...
Definition: visitor.hh:140
virtual void visit(string_t const *node)
Definition: visitor.hh:231
virtual void before_child(node_t const *, uint_or_string_t const &, node_t const *)
Function called just before the specialized version with the same name.
Definition: visitor.hh:190
virtual void enter(node_t const *)
Very first function called when visiting a node.
Definition: visitor.hh:104
virtual void after_child(node_t const *, uint_or_string_t const &, node_t const *)
Function called just after the specialized version with the same name.
Definition: visitor.hh:194
virtual void visit(float_t const *node)
Definition: visitor.hh:232
std::ostream & json(automaton_t aut, std::ostream &out)
std::vector< uint_or_string_t > path_t
Definition: node.hh:85
visit_t
Definition: visitor.hh:28
@ IGNORE_UNVISITED_CHILDREN_AND_SIBLINGS
Main namespace of Awali.
Definition: ato.hh:22
std::shared_ptr< json::object_t > json_ast_t
Definition: json_ast.hh:27