1
2
3
4
5
6 #ifndef SNG2HTML_SNG2HTML_PARSER_INCLUDED
7 #define SNG2HTML_SNG2HTML_PARSER_INCLUDED
8 #include <sng2html/sng2html/CharSet.hpp>
9 #include <soulng/lexer/Token.hpp>
10 #include <soulng/cppcode/Expression.hpp>
11 #include <soulng/cppcode/Statement.hpp>
12 #include <soulng/cppcode/Type.hpp>
13 #include <string>
14 #include <memory>
15 #include <map>
16
17 namespace sng2html { namespace sng2html {
18
19 class Visitor;
20
21 class Parser
22 {
23 public:
24 Parser(const std::u32string& name_);
25 virtual ~Parser();
26 virtual Parser* Clone() const;
27 virtual void Accept(Visitor& visitor) = 0;
28 virtual bool IsNonterminal() const { return false; }
29 virtual bool IsAction() const { return false; }
30 virtual bool IsTokenSwitch() const { return false; }
31 virtual bool IsActionToken() const { return false; }
32 virtual bool IsToken() const { return false; }
33 virtual bool IsGrammar() const { return false; }
34 virtual bool IsEmptyOrEmptyActionParser() const { return false; }
35 virtual bool IsPostfix() const { return false; }
36 bool ParentIsList() const { return parentIsList; }
37 virtual bool IsList() const { return false; }
38 const std::u32string& Name() const { return name; }
39 Parser* Parent() const { return parent; }
40 void SetParent(Parser* parent_) { parent = parent_; }
41 void SetParentIsList() { parentIsList = true; }
42 private:
43 Parser* parent;
44 std::u32string name;
45 bool parentIsList;
46 };
47
48 class UnaryParser : public Parser
49 {
50 public:
51 UnaryParser(const std::u32string& name_, Parser* child_);
52 Parser* Child() const { return child.get(); }
53 private:
54 std::unique_ptr<Parser> child;
55 };
56
57 class BinaryParser : public Parser
58 {
59 public:
60 BinaryParser(const std::u32string& name_, Parser* left_, Parser* right_);
61 Parser* Left() const { return left.get(); }
62 Parser* Right() const { return right.get(); }
63 private:
64 std::unique_ptr<Parser> left;
65 std::unique_ptr<Parser> right;
66 };
67
68 class EmptyParser : public Parser
69 {
70 public:
71 EmptyParser();
72 Parser* Clone() const override;
73 void Accept(Visitor& visitor) override;
74 bool IsEmptyOrEmptyActionParser() const override { return true; }
75 };
76
77 class TokenParser : public Parser
78 {
79 public:
80 TokenParser(const std::u32string& tokenName_);
81 Parser* Clone() const override;
82 void Accept(Visitor& visitor) override;
83 bool IsToken() const override { return true; }
84 const std::u32string& TokenName() const { return tokenName; }
85 private:
86 std::u32string tokenName;
87 };
88
89 class CharParser : public Parser
90 {
91 public:
92 CharParser(char32_t chr_);
93 Parser* Clone() const override;
94 void Accept(Visitor& visitor) override;
95 char32_t Chr() const { return chr; }
96 private:
97 char32_t chr;
98 };
99
100 class StringParser : public Parser
101 {
102 public:
103 StringParser(const std::u32string& str_);
104 Parser* Clone() const override;
105 void Accept(Visitor& visitor) override;
106 const std::u32string& Str() const { return str; }
107 void SetArrayName(const std::string& arrayName_);
108 const std::string& ArrayName() const { return arrayName; }
109 private:
110 std::u32string str;
111 std::string arrayName;
112 };
113
114 class CharSetParser : public Parser
115 {
116 public:
117 CharSetParser(const CharSet& set_);
118 Parser* Clone() const override;
119 void Accept(Visitor& visitor) override;
120 const CharSet& Set() const { return set; }
121 void SetArrayName(const std::string& arrayName_);
122 const std::string& ArrayName() const { return arrayName; }
123 private:
124 CharSet set;
125 std::string arrayName;
126 };
127
128 class OptionalParser : public UnaryParser
129 {
130 public:
131 OptionalParser(Parser* child_);
132 Parser* Clone() const override;
133 void Accept(Visitor& visitor) override;
134 bool IsPostfix() const override { return true; }
135 };
136
137 class KleeneParser : public UnaryParser
138 {
139 public:
140 KleeneParser(Parser* child_);
141 Parser* Clone() const override;
142 void Accept(Visitor& visitor) override;
143 bool IsPostfix() const override { return true; }
144 };
145
146 class PositiveParser : public UnaryParser
147 {
148 public:
149 PositiveParser(Parser* child_);
150 Parser* Clone() const override;
151 void Accept(Visitor& visitor) override;
152 bool IsPostfix() const override { return true; }
153 };
154
155 class ExpectationParser : public UnaryParser
156 {
157 public:
158 ExpectationParser(Parser* child_);
159 Parser* Clone() const override;
160 void Accept(Visitor& visitor) override;
161 };
162
163 class GroupingParser : public UnaryParser
164 {
165 public:
166 GroupingParser(Parser* child_);
167 Parser* Clone() const override;
168 void Accept(Visitor& visitor) override;
169 bool IsAction() const override { return Child()->IsAction(); }
170 };
171
172 class SequenceParser : public BinaryParser
173 {
174 public:
175 SequenceParser(Parser* left_, Parser* right_);
176 Parser* Clone() const override;
177 void Accept(Visitor& visitor) override;
178 bool IsAction() const override;
179 };
180
181 class AlternativeParser : public BinaryParser
182 {
183 public:
184 AlternativeParser(Parser* left_, Parser* right_);
185 bool IsTokenSwitch() const override { return Left()->IsActionToken() && Right()->IsActionToken() || Left()->IsTokenSwitch() && Right()->IsActionToken(); }
186 Parser* Clone() const override;
187 void Accept(Visitor& visitor) override;
188 };
189
190 class DifferenceParser : public BinaryParser
191 {
192 public:
193 DifferenceParser(Parser* left_, Parser* right_);
194 Parser* Clone() const override;
195 void Accept(Visitor& visitor) override;
196 };
197
198 class ListParser : public UnaryParser
199 {
200 public:
201 ListParser(Parser* left_, Parser* right_);
202 Parser* Clone() const override;
203 void Accept(Visitor& visitor) override;
204 Parser* Left() const { return left; }
205 Parser* Right() const { return right; }
206 private:
207 Parser* left;
208 Parser* right;
209 };
210
211 class ActionParser : public UnaryParser
212 {
213 public:
214 ActionParser(soulng::cppcode::CompoundStatement* successCode_, soulng::cppcode::CompoundStatement* failCode_, Parser* child_);
215 ~ActionParser();
216 Parser* Clone() const override;
217 void Accept(Visitor& visitor) override;
218 bool IsAction() const override { return true; }
219 bool IsActionToken() const override;
220 bool IsEmptyOrEmptyActionParser() const override { return Child()->IsEmptyOrEmptyActionParser(); }
221 soulng::cppcode::CompoundStatement* SuccessCode() const { return successCode; }
222 soulng::cppcode::CompoundStatement* FailCode() const { return failCode; }
223 private:
224 soulng::cppcode::CompoundStatement* successCode;
225 std::unique_ptr<soulng::cppcode::CompoundStatement> ownedSuccessCode;
226 soulng::cppcode::CompoundStatement* failCode;
227 std::unique_ptr<soulng::cppcode::CompoundStatement> ownedFailCode;
228 };
229
230 struct Parameter
231 {
232 Parameter(soulng::cppcode::TypeId* type_, const std::u32string& name_);
233 Parameter(soulng::cppcode::TypeId* type_, const std::u32string& name_, bool variable_);
234 virtual ~Parameter();
235 std::unique_ptr<soulng::cppcode::TypeId> type;
236 std::u32string name;
237 bool variable;
238 };
239
240 struct Variable : Parameter
241 {
242 Variable(soulng::cppcode::TypeId* type_, const std::u32string& name_);
243 };
244
245 class RuleParser;
246
247 class NonterminalParser : public Parser
248 {
249 public:
250 NonterminalParser(const std::u32string& name_, const std::u32string& ruleName_);
251 Parser* Clone() const override;
252 void Accept(Visitor& visitor) override;
253 bool IsNonterminal() const override { return true; }
254 const std::u32string& RuleName() const { return ruleName; }
255 void SetRule(RuleParser* rule) { this->rule = rule; }
256 RuleParser* Rule() const { return rule; }
257 void SetArguments(soulng::cppcode::ExpressionList* args_);
258 const std::std::vector<soulng::cppcode::CppObject*>&Arguments() const{returnargs;}
259 private:
260 std::u32string ruleName;
261 RuleParser* rule;
262 std::vector<soulng::cppcode::CppObject*> args;
263 std::vector<std::std::unique_ptr<soulng::cppcode::CppObject>>ownedArgs;
264 };
265
266 class GrammarParser;
267
268 class RuleParser : public Parser
269 {
270 public:
271 RuleParser(const std::u32string& name_);
272 GrammarParser* GetGrammar() const;
273 void Accept(Visitor& visitor) override;
274 void SetDefinition(Parser* definition_);
275 Parser* Definition() { return definition.get(); }
276 void AddParamOrVariable(Parameter* paramOrVariable);
277 const std::std::vector<std::std::unique_ptr<Parameter>>&Parameters() const{returnparameters;}
278 const std::std::vector<std::std::unique_ptr<Variable>>&Variables() const{returnvariables;}
279 void SetReturnType(soulng::cppcode::TypeId* returnType_);
280 soulng::cppcode::TypeId* ReturnType() const { return returnType.get(); }
281 void AddNonterminal(NonterminalParser* nonterminal);
282 const std::std::vector<NonterminalParser*>&Nonterminals() const{returnnonterminals;}
283 const std::u32string& Info() const { return info; }
284 void SetInfo(const std::u32string& info_);
285 void SetHasReturn() { hasReturn = true; }
286 bool HasReturn() const { return hasReturn; }
287 private:
288 std::unique_ptr<Parser> definition;
289 std::vector<std::std::unique_ptr<Parameter>>parameters;
290 std::vector<std::std::unique_ptr<Variable>>variables;
291 std::unique_ptr<soulng::cppcode::TypeId> returnType;
292 std::vector<NonterminalParser*> nonterminals;
293 std::u32string info;
294 bool hasReturn;
295 };
296
297 class GrammarParser : public Parser
298 {
299 public:
300 GrammarParser(const std::u32string& name_, const std::u32string& api_);
301 bool IsGrammar() const override { return true; }
302 void Accept(Visitor& visitor) override;
303 const std::u32string& Api() const { return api; }
304 void SetMain() { main = true; }
305 bool Main() const { return main; }
306 void AddUsing(const std::u32string& using_);
307 const std::std::vector<std::u32string>&Usings() const{returnusings;}
308 void SetLexer(const std::u32string& lexer_) { lexer = lexer_; }
309 const std::u32string& Lexer() const { return lexer; }
310 void AddRule(RuleParser* rule);
311 void MapRule(RuleParser* rule);
312 RuleParser* GetRule(const std::u32string& ruleName) const;
313 const std::std::vector<std::std::unique_ptr<RuleParser>>&Rules() const{returnrules;}
314 void AddRuleInfo(const std::u32string& ruleName, const std::u32string& ruleInfo);
315 const std::std::vector<std::std::pair<std::u32string, std::u32string>>&RuleInfos() const{returnruleInfos;}
316 private:
317 std::u32string api;
318 bool main;
319 std::vector<std::u32string> usings;
320 std::u32string lexer;
321 std::vector<std::std::unique_ptr<RuleParser>>rules;
322 std::map<std::u32string, RuleParser*> ruleMap;
323 std::vector<std::std::pair<std::u32string, std::u32string>>ruleInfos;
324 };
325
326 } }
327
328 #endif // SNG2HTML_SNG2HTML_PARSER_INCLUDED