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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
#ifndef parse_tree_hpp_
#define parse_tree_hpp_ 1
#include <parse.h>
#include <string>
#include <boost/intrusive_ptr.hpp>
#include <boost/intrusive_ptr.hpp>
#include <boost/intrusive_ptr.hpp>
#include <list>
namespace foundry {
namespace parse {
struct node;
typedef boost::intrusive_ptr<node> node_ptr;
typedef node *node_weak_ptr;
struct node_const_visitor;
struct node_visitor;
struct component;
typedef boost::intrusive_ptr<component> component_ptr;
typedef component *component_weak_ptr;
struct string_literal;
typedef boost::intrusive_ptr<string_literal> string_literal_ptr;
typedef string_literal *string_literal_weak_ptr;
struct unresolved_symbol;
typedef boost::intrusive_ptr<unresolved_symbol> unresolved_symbol_ptr;
typedef unresolved_symbol *unresolved_symbol_weak_ptr;
struct terminal;
typedef boost::intrusive_ptr<terminal> terminal_ptr;
typedef terminal *terminal_weak_ptr;
struct nonterminal;
typedef boost::intrusive_ptr<nonterminal> nonterminal_ptr;
typedef nonterminal *nonterminal_weak_ptr;
struct regex;
typedef boost::intrusive_ptr<regex> regex_ptr;
typedef regex *regex_weak_ptr;
struct group;
typedef boost::intrusive_ptr<group> group_ptr;
typedef group *group_weak_ptr;
struct root;
typedef boost::intrusive_ptr<root> root_ptr;
typedef root *root_weak_ptr;
struct rule;
typedef boost::intrusive_ptr<rule> rule_ptr;
typedef rule *rule_weak_ptr;
struct alternative;
typedef boost::intrusive_ptr<alternative> alternative_ptr;
typedef alternative *alternative_weak_ptr;
}
}
namespace foundry {
namespace parse {
struct node {
node(void) throw() : refcount(0) { }
virtual ~node(void) throw() { }
virtual void apply(node_visitor &) = 0;
virtual void apply(node_const_visitor &) const = 0;
unsigned int refcount;
};
inline void intrusive_ptr_add_ref(node *n) { ++n->refcount; }
inline void intrusive_ptr_release(node *n) { if(!--n->refcount) delete n; }
class node_visitor
{
public:
virtual ~node_visitor(void) throw() { }
inline void descend(node &n) { n.apply(*this); }
template<typename T>
inline void descend(boost::intrusive_ptr<T> const &p) { if(p) descend(*p); }
template<typename T, typename Alloc>
inline void descend(std::list<T, Alloc> &l)
{
for(typename std::list<T, Alloc>::iterator i = l.begin(); i != l.end(); ++i)
descend(*i);
}
virtual void visit(string_literal &) = 0;
inline void descend(string_literal &n) { visit(n); }
inline void descend(boost::intrusive_ptr<string_literal> &p) { if(p) descend(*p); }
virtual void visit(unresolved_symbol &) = 0;
inline void descend(unresolved_symbol &n) { visit(n); }
inline void descend(boost::intrusive_ptr<unresolved_symbol> &p) { if(p) descend(*p); }
virtual void visit(terminal &) = 0;
inline void descend(terminal &n) { visit(n); }
inline void descend(boost::intrusive_ptr<terminal> &p) { if(p) descend(*p); }
virtual void visit(nonterminal &) = 0;
inline void descend(nonterminal &n) { visit(n); }
inline void descend(boost::intrusive_ptr<nonterminal> &p) { if(p) descend(*p); }
virtual void visit(regex &) = 0;
inline void descend(regex &n) { visit(n); }
inline void descend(boost::intrusive_ptr<regex> &p) { if(p) descend(*p); }
virtual void visit(group &) = 0;
inline void descend(group &n) { visit(n); }
inline void descend(boost::intrusive_ptr<group> &p) { if(p) descend(*p); }
virtual void visit(root &) = 0;
inline void descend(root &n) { visit(n); }
inline void descend(boost::intrusive_ptr<root> &p) { if(p) descend(*p); }
virtual void visit(rule &) = 0;
inline void descend(rule &n) { visit(n); }
inline void descend(boost::intrusive_ptr<rule> &p) { if(p) descend(*p); }
virtual void visit(alternative &) = 0;
inline void descend(alternative &n) { visit(n); }
inline void descend(boost::intrusive_ptr<alternative> &p) { if(p) descend(*p); }
};
class node_const_visitor
{
public:
virtual ~node_const_visitor(void) throw() { }
inline void descend(node const &n) { n.apply(*this); }
template<typename T>
inline void descend(boost::intrusive_ptr<T> const &p) { if(p) descend(*p); }
template<typename T, typename Alloc>
inline void descend(std::list<T, Alloc> const &l)
{
for(typename std::list<T, Alloc>::const_iterator i = l.begin(); i != l.end(); ++i)
descend(*i);
}
virtual void visit(string_literal const &) = 0;
inline void descend(string_literal const &n) { visit(n); }
inline void descend(boost::intrusive_ptr<string_literal> const &p) { if(p) descend(*p); }
virtual void visit(unresolved_symbol const &) = 0;
inline void descend(unresolved_symbol const &n) { visit(n); }
inline void descend(boost::intrusive_ptr<unresolved_symbol> const &p) { if(p) descend(*p); }
virtual void visit(terminal const &) = 0;
inline void descend(terminal const &n) { visit(n); }
inline void descend(boost::intrusive_ptr<terminal> const &p) { if(p) descend(*p); }
virtual void visit(nonterminal const &) = 0;
inline void descend(nonterminal const &n) { visit(n); }
inline void descend(boost::intrusive_ptr<nonterminal> const &p) { if(p) descend(*p); }
virtual void visit(regex const &) = 0;
inline void descend(regex const &n) { visit(n); }
inline void descend(boost::intrusive_ptr<regex> const &p) { if(p) descend(*p); }
virtual void visit(group const &) = 0;
inline void descend(group const &n) { visit(n); }
inline void descend(boost::intrusive_ptr<group> const &p) { if(p) descend(*p); }
virtual void visit(root const &) = 0;
inline void descend(root const &n) { visit(n); }
inline void descend(boost::intrusive_ptr<root> const &p) { if(p) descend(*p); }
virtual void visit(rule const &) = 0;
inline void descend(rule const &n) { visit(n); }
inline void descend(boost::intrusive_ptr<rule> const &p) { if(p) descend(*p); }
virtual void visit(alternative const &) = 0;
inline void descend(alternative const &n) { visit(n); }
inline void descend(boost::intrusive_ptr<alternative> const &p) { if(p) descend(*p); }
};
struct component : node {
component(void) throw() { }
virtual ~component(void) throw() { }
using node::apply;
std::string name;
};
struct string_literal : component
{
string_literal() throw() { }
virtual ~string_literal(void) throw() { }
virtual void apply(node_visitor &);
virtual void apply(node_const_visitor &) const;
std::string text;
};
struct unresolved_symbol : component
{
unresolved_symbol() throw() { }
virtual ~unresolved_symbol(void) throw() { }
virtual void apply(node_visitor &);
virtual void apply(node_const_visitor &) const;
};
struct terminal : component
{
terminal() throw() { }
virtual ~terminal(void) throw() { }
virtual void apply(node_visitor &);
virtual void apply(node_const_visitor &) const;
};
struct nonterminal : component
{
nonterminal() throw() { }
virtual ~nonterminal(void) throw() { }
virtual void apply(node_visitor &);
virtual void apply(node_const_visitor &) const;
boost::intrusive_ptr< ::foundry::parse::rule> rule;
};
struct regex : component
{
regex() throw() { }
virtual ~regex(void) throw() { }
virtual void apply(node_visitor &);
virtual void apply(node_const_visitor &) const;
std::string text;
};
struct group : component
{
group() throw() { }
virtual ~group(void) throw() { }
virtual void apply(node_visitor &);
virtual void apply(node_const_visitor &) const;
std::list<boost::intrusive_ptr< ::foundry::parse::component> > components;
repetition rep;
};
struct root : node
{
root() throw() { }
virtual ~root(void) throw() { }
virtual void apply(node_visitor &);
virtual void apply(node_const_visitor &) const;
std::string ns;
std::list<boost::intrusive_ptr< ::foundry::parse::rule> > rules;
std::list<boost::intrusive_ptr< ::foundry::parse::string_literal> > literals;
std::list<boost::intrusive_ptr< ::foundry::parse::regex> > regexes;
};
struct rule : node
{
rule() throw() { }
virtual ~rule(void) throw() { }
virtual void apply(node_visitor &);
virtual void apply(node_const_visitor &) const;
std::string name;
std::list<boost::intrusive_ptr< ::foundry::parse::alternative> > alternatives;
};
struct alternative : node
{
alternative() throw() { }
virtual ~alternative(void) throw() { }
virtual void apply(node_visitor &);
virtual void apply(node_const_visitor &) const;
std::string name;
boost::intrusive_ptr< ::foundry::parse::group> group;
};
}
}
#endif
|