Debian Codegraph @

documentation/manual/algorithm/demos/Parserbase.h

raw
1 #ifndef ParserBase_h_included
2 #define ParserBase_h_included
3
4 #include <vector>
5 #include <iostream>
6
7 // $insert debugincludes
8 #include <iostream>
9 #include <sstream>
10 #include <string>
11 #include <map>
12 #include <iomanip>
13
14 namespace // anonymous
15 {
16 struct PI_;
17 }
18
19
20 class ParserBase
21 {
22 public:
23 // $insert tokens
24
25 // Symbolic tokens:
26 enum Tokens_
27 {
28 ID = 257,
29 UNARY,
30 };
31
32 // $insert STYPE
33 using STYPE_ = int;
34
35 private:
36 int d_stackIdx_;
37 std::vector<size_t> d_stateStack_;
38 std::vector<STYPE_> d_valueStack_;
39
40 protected:
41 enum Return_
42 {
43 PARSE_ACCEPT_ = 0, // values used as parse()'s return values
44 PARSE_ABORT_ = 1
45 };
46 enum ErrorRecovery_
47 {
48 DEFAULT_RECOVERY_MODE_,
49 UNEXPECTED_TOKEN_,
50 };
51 bool d_debug_;
52 size_t d_nErrors_;
53 size_t d_requiredTokens_;
54 size_t d_acceptedTokens_;
55 int d_token_;
56 int d_nextToken_;
57 size_t d_state_;
58 STYPE_ *d_vsp_;
59 STYPE_ d_val_;
60 STYPE_ d_nextVal_;
61
62 ParserBase();
63
64 // $insert debugdecl
65 static std::ostringstream s_out_;
66
67 std::string symbol_(int value) const;
68 std::string stype_(char const *pre, STYPE_ const &semVal,
69 char const *post = "") const;
70 static std::ostream &dflush_(std::ostream &out);
71 void ABORT() const;
72 void ACCEPT() const;
73 void ERROR() const;
74 void clearin();
75 bool debug() const;
76 void pop_(size_t count = 1);
77 void push_(size_t nextState);
78 void popToken_();
79 void pushToken_(int token);
80 void reduce_(PI_ const &productionInfo);
81 void errorVerbose_();
82 size_t top_() const;
83
84 public:
85 void setDebug(bool mode);
86 };
87
88 inline bool ParserBase::debug() const
89 {
90 return d_debug_;
91 }
92
93 inline void ParserBase::setDebug(bool mode)
94 {
95 d_debug_ = mode;
96 }
97
98 inline void ParserBase::ABORT() const
99 {
100 // $insert debug
101 if (d_debug_)
102 s_out_ << "ABORT(): Parsing unsuccessful" << "\n" << dflush_;
103 throw PARSE_ABORT_;
104 }
105
106 inline void ParserBase::ACCEPT() const
107 {
108 // $insert debug
109 if (d_debug_)
110 s_out_ << "ACCEPT(): Parsing successful" << "\n" << dflush_;
111 throw PARSE_ACCEPT_;
112 }
113
114 inline void ParserBase::ERROR() const
115 {
116 // $insert debug
117 if (d_debug_)
118 s_out_ << "ERROR(): Forced error condition" << "\n" << dflush_;
119 throw UNEXPECTED_TOKEN_;
120 }
121
122
123 // As a convenience, when including ParserBase.h its symbols are available as
124 // symbols in the class Parser, too.
125 #define Parser ParserBase
126
127
128 #endif
129
130