Merge pull request #75 from balaskoa/master
[deliverable/titan.core.git] / langviz / Symbol.hh
1 /******************************************************************************
2 * Copyright (c) 2000-2016 Ericsson Telecom AB
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Balasko, Jeno
10 * Forstner, Matyas
11 *
12 ******************************************************************************/
13 #ifndef _langviz_Symbol_HH
14 #define _langviz_Symbol_HH
15
16 #include "Node.hh"
17 #include "../compiler2/string.hh"
18 #include "../compiler2/vector.hh"
19 #include "../compiler2/map.hh"
20
21 class Symbol;
22 class Grammar;
23
24 /**
25 * Unique set of symbols.
26 */
27 class SymbolSet : public Node {
28 protected:
29 map<Symbol*, void> ss;
30
31 SymbolSet(const SymbolSet& p);
32 public:
33 SymbolSet() {}
34 virtual ~SymbolSet();
35 virtual SymbolSet* clone() const {return new SymbolSet(*this);}
36 void add_s(Symbol *p_s);
37 void add_ss(const SymbolSet& p_ss);
38 void remove_s(Symbol *p_s);
39 void remove_ss(const SymbolSet& p_ss);
40 void remove_all();
41 size_t get_nof_ss() const {return ss.size();}
42 Symbol* get_s_byIndex(size_t p_i) const {return ss.get_nth_key(p_i);}
43 };
44
45 /**
46 * Terminal symbols (tokens) and nonterminal symbols.
47 */
48 class Symbol : public Node {
49 protected:
50 string id;
51 string id_dot; /**< this id is used in dot file */
52 SymbolSet refd_by; /**< this symbol is referenced directly by these
53 symbols */
54 SymbolSet refs; /**< these symbols are referenced directly by this
55 symbol */
56 SymbolSet refd_from; /**< this symbol is referenced directly or
57 indirectly from these symbols */
58 SymbolSet refs_weight; /**< these NONTERMINAL symbols are referenced
59 directly or indirectly by this symbol;
60 the dist of these symbols are greater
61 than dist of this */
62 bool is_terminal; /**< true if is terminal symbol */
63 bool is_recursive;
64 bool can_be_empty;
65 int dist; /**< shortest distance from the start symbol; -1:
66 unreachable */
67 int weight; /**< 1 + number of nonterminal symbols that can be
68 reached from this one, and have greater dist than
69 this */
70
71 Symbol(const Symbol& p);
72 public:
73 Symbol(const string& p_id);
74 virtual ~Symbol() {}
75 virtual Symbol* clone() const {return new Symbol(*this);}
76 bool get_is_terminal() {return is_terminal;}
77 void set_is_terminal() {is_terminal=true;}
78 const string& get_id() const {return id;}
79 const string& get_id_dot();
80 void add_refd_by(Symbol* p_symbol) {refd_by.add_s(p_symbol);}
81 void add_refs(Symbol* p_symbol) {refs.add_s(p_symbol);}
82 const SymbolSet& get_refd_by() {return refd_by;}
83 const SymbolSet& get_refs() {return refs;}
84 void set_is_recursive() {is_recursive=true;}
85 void set_can_be_empty() {can_be_empty=true;}
86 bool get_can_be_empty() {return can_be_empty;}
87 void set_dist(int p_i) {if(dist==-1) dist=p_i;}
88 int get_dist() {return dist;}
89 int get_weight();
90 };
91
92 /**
93 * Ordered list of symbols.
94 */
95 class Symbols : public Node {
96 protected:
97 vector<Symbol> ss; /**< symbols */
98
99 Symbols(const Symbols& p);
100 public:
101 Symbols() {}
102 virtual ~Symbols();
103 virtual Symbols* clone() const {return new Symbols(*this);}
104 void add_s(Symbol *p_s);
105 size_t get_nof_ss() const {return ss.size();}
106 const Symbol* get_s_byIndex(size_t p_i) const {return ss[p_i];}
107 Symbol*& get_s_byIndex(size_t p_i) {return ss[p_i];}
108 void replace_aliases(Grammar *grammar);
109 };
110
111 /**
112 * Unique map of symbols.
113 */
114 class SymbolMap : public Node {
115 protected:
116 map<string, Symbol> ss;
117
118 SymbolMap(const SymbolMap& p);
119 public:
120 SymbolMap() {}
121 virtual ~SymbolMap();
122 virtual SymbolMap* clone() const {return new SymbolMap(*this);}
123 void add_s(Symbol *p_s);
124 size_t get_nof_ss() const {return ss.size();}
125 const Symbol* get_s_byIndex(size_t p_i) const {return ss.get_nth_elem(p_i);}
126 Symbol*& get_s_byIndex(size_t p_i) {return ss.get_nth_elem(p_i);}
127 bool has_s_withId(const string& p_id) const {return ss.has_key(p_id);}
128 const Symbol* get_s_byId(const string& p_id) const {return ss[p_id];}
129 Symbol*& get_s_byId(const string& p_id) {return ss[p_id];}
130 /** Each Symbol instance is owned by the Grammar's SymbolMap. The
131 * only place from where you should destruct them. */
132 void destruct_ss();
133 /** Used by Grammar after replacing aliases */
134 void destruct_symbol(const string& p_id);
135 };
136
137 #endif // _langviz_Symbol_HH
This page took 0.032851 seconds and 5 git commands to generate.