Debian Codegraph @

production/production.h

raw
1 #ifndef _INCLUDED_PRODUCTION_
2 #define _INCLUDED_PRODUCTION_
3
4 #include <vector>
5 #include <string>
6 #include <iosfwd>
7 #include <algorithm>
8
9 #include "../block/block.h"
10 #include "../symbol/symbol.h"
11 #include "../terminal/terminal.h"
12
13 // NOTE: To obtain all productions of a certain Non-Terminal, use
14 // NonTerminal's `productions()' member
15
16 // A Production is a vector of symbols. Its elements specify the RHS elements
17 // of the producion LHS -> RHS. Several of the vector's public members are
18 // available, see the public: section.
19
20 class Production: private std::vector<Symbol *>
21 {
22 using Inherit = std::vector<Symbol *>;
23
24 friend std::ostream &operator<<(std::ostream &out,
25 Production const *production);
26
27 Terminal const *d_precedence; // 0 or a pointer to some terminal
28 // defining this production's
29 // precedence (through %prec)
30
31 Block d_action; // action associated with this
32 // production.
33
34 Symbol const *d_nonTerminal; // pointer to the lhs nonterminal of
35 // this production
36
37 size_t d_nr; // production order number
38 // over all productions,
39 // starts at 1
40
41 mutable bool d_used; // true once this production has been
42 // used.
43
44 size_t d_lineNr; // line in the grammar file where the
45 // production is defined.
46 size_t d_nameIdx; // index in s_filename of the name of
47 // the file in which this production
48 // is defined.
49
50 static size_t s_nr; // incremented at each new production
51
52 static bool s_unused; // prevents multiple 'unused
53 // production rules' warnings
54
55 static Production const *s_startProduction; // a pointer to the start
56 // production rule.
57
58 static std::vector<std::string> s_fileName;
59
60 public:
61 using Inherit::size;
62 using Inherit::begin;
63 using Inherit::end;
64 using Inherit::rbegin;
65 using Inherit::rend;
66 using Inherit::push_back;
67
68 using Vector = std::vector<Production *>;
69 using ConstVector = std::vector<Production const*>;
70 using ConstIter = ConstVector::const_iterator;
71
72 Production(Symbol const *nonTerminal, size_t lineNr);
73
74 Block const &action() const;
75 void setBlockLineNr(size_t lineNr);
76
77 Symbol const &operator[](size_t idx) const; // same as rhs(idx)
78 Symbol const *rhs(size_t idx) const; // idx-ed symbol in the rhs
79 Symbol const *lhs() const;
80 Terminal const *precedence() const;
81 bool hasAction() const;
82 bool isEmpty() const;
83 size_t nr() const;
84 void used() const; // d_used is mutable
85
86 void setAction(Block const &block);
87 void setPrecedence(Terminal const *terminal);
88
89 std::string const &fileName() const;
90 size_t lineNr() const;
91
92 static Production const *start();
93 static void insertAction(Production const *prod, std::ostream &out,
94 bool lineDirectives, size_t indent);
95
96 static void setStart(Production const *production);
97 static void termToNonterm(Production *pPtr, Symbol *terminal,
98 Symbol *nonTerminal);
99
100 static void unused(Production const *production);
101 static bool notUsed();
102
103 static void storeFilename(std::string const &filename);
104
105 void setLineNr(size_t lineNr);
106
107 private:
108 Symbol *vectorIdx(size_t idx) const;
109 std::ostream &standard(std::ostream &out) const;
110 };
111
112 #include "production.f"
113
114 #endif
115