1 using System;
2 using System.Collections;
3
4
5
6
7
8 namespace soulcm.scmpg
9 {
10 public abstract class Parser
11 {
12 public Parser(const ustring& name_) :
13 name(name_), parent(null)
14 {
15 }
16 suppress Parser(const Parser&);
17 suppress Parser(Parser&&);
18 suppress void operator=(const Parser&);
19 suppress void operator=(Parser&&);
20 public virtual ~Parser()
21 {
22 }
23 public const ustring& Name() const
24 {
25 return name;
26 }
27 public Parser* Parent() const
28 {
29 return parent;
30 }
31 public void SetParent(Parser* parent_)
32 {
33 parent = parent_;
34 }
35 public abstract void Accept(Visitor& visitor);
36 public virtual bool IsNonterminal() const
37 {
38 return false;
39 }
40 public virtual bool IsAction() const
41 {
42 return false;
43 }
44 public virtual bool IsTokenSwitch() const
45 {
46 return false;
47 }
48 public virtual bool IsActionToken() const
49 {
50 return false;
51 }
52 public virtual bool IsToken() const
53 {
54 return false;
55 }
56 public virtual nothrow bool IsNothrow() const
57 {
58 if (parent != null)
59 {
60 return parent->IsNothrow();
61 }
62 return false;
63 }
64 public virtual nothrow bool IsState() const
65 {
66 if (parent != null)
67 {
68 return parent->IsState();
69 }
70 return false;
71 }
72 public virtual Parser* Clone() const
73 {
74 throw Exception("this class does not support clone operation");
75 }
76 private Parser* parent;
77 private ustring name;
78 }
79 public abstract class UnaryParser : Parser
80 {
81 public UnaryParser(const ustring& name_, Parser* child_) :
82 base(name_), child(child_)
83 {
84 child->SetParent(this);
85 }
86 public Parser* Child() const
87 {
88 return child.Get();
89 }
90 private UniquePtr<Parser> child;
91 }
92 public abstract class BinaryParser : Parser
93 {
94 public BinaryParser(const ustring& name_, Parser* left_, Parser* right_) :
95 base(name_), left(left_), right(right_)
96 {
97 left->SetParent(this);
98 right->SetParent(this);
99 }
100 public Parser* Left() const
101 {
102 return left.Get();
103 }
104 public Parser* Right() const
105 {
106 return right.Get();
107 }
108 private UniquePtr<Parser> left;
109 private UniquePtr<Parser> right;
110 }
111 public class EmptyParser : Parser
112 {
113 public EmptyParser() :
114 base(u"empty")
115 {
116 }
117 public override Parser* Clone() const
118 {
119 return new EmptyParser();
120 }
121 public override void Accept(Visitor& visitor)
122 {
123 visitor.Visit(*this);
124 }
125 }
126 public class AnyParser : Parser
127 {
128 public AnyParser() :
129 base(u"any")
130 {
131 }
132 public override Parser* Clone() const
133 {
134 return new AnyParser();
135 }
136 public override void Accept(Visitor& visitor)
137 {
138 visitor.Visit(*this);
139 }
140 }
141 public class TokenParser : Parser
142 {
143 public TokenParser(const ustring& tokenName_) :
144 base(tokenName_), tokenName(tokenName_)
145 {
146 }
147 public const ustring& TokenName() const
148 {
149 return tokenName;
150 }
151 public override bool IsToken() const
152 {
153 return true;
154 }
155 public override Parser* Clone() const
156 {
157 return new TokenParser(tokenName);
158 }
159 public override void Accept(Visitor& visitor)
160 {
161 visitor.Visit(*this);
162 }
163 private ustring tokenName;
164 }
165 public class CharParser : Parser
166 {
167 public CharParser(uchar chr_) :
168 base(u"char"), chr(chr_)
169 {
170 }
171 public uchar Chr() const
172 {
173 return chr;
174 }
175 public override Parser* Clone() const
176 {
177 return new CharParser(chr);
178 }
179 public override void Accept(Visitor& visitor)
180 {
181 visitor.Visit(*this);
182 }
183 private uchar chr;
184 }
185 public class StringParser : Parser
186 {
187 public StringParser(const ustring& str_) :
188 base(str_), str(str_)
189 {
190 }
191 public const ustring& Str() const
192 {
193 return str;
194 }
195 public const string& ArrayName() const
196 {
197 return arrayName;
198 }
199 public void SetArrayName(const string& arrayName_)
200 {
201 arrayName = arrayName_;
202 }
203 public override Parser* Clone() const
204 {
205 return new StringParser(str);
206 }
207 public override void Accept(Visitor& visitor)
208 {
209 visitor.Visit(*this);
210 }
211 private ustring str;
212 private string arrayName;
213 }
214 public class CharSetParser : Parser
215 {
216 public CharSetParser(const CharSet& set_) :
217 base(u"charset"), set(set_)
218 {
219 }
220 public const CharSet& Set() const
221 {
222 return set;
223 }
224 public const string& ArrayName() const
225 {
226 return arrayName;
227 }
228 public void SetArrayName(const string& arrayName_)
229 {
230 arrayName = arrayName_;
231 }
232 public override Parser* Clone() const
233 {
234 return new CharSetParser(set);
235 }
236 public override void Accept(Visitor& visitor)
237 {
238 visitor.Visit(*this);
239 }
240 private CharSet set;
241 private string arrayName;
242 }
243 public class OptionalParser : UnaryParser
244 {
245 public OptionalParser(Parser* child_) :
246 base(u"optional", child_)
247 {
248 }
249 public override Parser* Clone() const
250 {
251 return new OptionalParser(Child()->Clone());
252 }
253 public override void Accept(Visitor& visitor)
254 {
255 visitor.Visit(*this);
256 }
257 }
258 public class KleeneParser : UnaryParser
259 {
260 public KleeneParser(Parser* child_) :
261 base(u"kleene", child_)
262 {
263 }
264 public override Parser* Clone() const
265 {
266 return new KleeneParser(Child()->Clone());
267 }
268 public override void Accept(Visitor& visitor)
269 {
270 visitor.Visit(*this);
271 }
272 }
273 public class PositiveParser : UnaryParser
274 {
275 public PositiveParser(Parser* child_) :
276 base(u"positive", child_)
277 {
278 }
279 public override Parser* Clone() const
280 {
281 return new PositiveParser(Child()->Clone());
282 }
283 public override void Accept(Visitor& visitor)
284 {
285 visitor.Visit(*this);
286 }
287 }
288 public class ExpectationParser : UnaryParser
289 {
290 public ExpectationParser(Parser* child_) :
291 base(u"expectation", child_)
292 {
293 }
294 public override Parser* Clone() const
295 {
296 return new ExpectationParser(Child()->Clone());
297 }
298 public override void Accept(Visitor& visitor)
299 {
300 visitor.Visit(*this);
301 }
302 }
303 public class GroupingParser : UnaryParser
304 {
305 public GroupingParser(Parser* child_) :
306 base(u"grouping", child_)
307 {
308 }
309 public override bool IsAction() const
310 {
311 return Child()->IsAction();
312 }
313 public override Parser* Clone() const
314 {
315 return new GroupingParser(Child()->Clone());
316 }
317 public override void Accept(Visitor& visitor)
318 {
319 visitor.Visit(*this);
320 }
321 }
322 public class SequenceParser : BinaryParser
323 {
324 public SequenceParser(Parser* left_, Parser* right_) :
325 base(u"sequence", left_, right_)
326 {
327 }
328 public override Parser* Clone() const
329 {
330 return new SequenceParser(Left()->Clone(), Right()->Clone());
331 }
332 public override void Accept(Visitor& visitor)
333 {
334 visitor.Visit(*this);
335 }
336 }
337 public class AlternativeParser : BinaryParser
338 {
339 public AlternativeParser(Parser* left_, Parser* right_) :
340 base(u"alternative", left_, right_)
341 {
342 }
343 public override bool IsTokenSwitch() const
344 {
345 return Left()->IsActionToken() && Right()->IsActionToken() || Left()->IsTokenSwitch() && Right()->IsActionToken();
346 }
347 public override Parser* Clone() const
348 {
349 return new AlternativeParser(Left()->Clone(), Right()->Clone());
350 }
351 public override void Accept(Visitor& visitor)
352 {
353 visitor.Visit(*this);
354 }
355 }
356 public class DifferenceParser : BinaryParser
357 {
358 public DifferenceParser(Parser* left_, Parser* right_) :
359 base(u"difference", left_, right_)
360 {
361 }
362 public override Parser* Clone() const
363 {
364 return new DifferenceParser(Left()->Clone(), Right()->Clone());
365 }
366 public override void Accept(Visitor& visitor)
367 {
368 visitor.Visit(*this);
369 }
370 }
371 public class ListParser : UnaryParser
372 {
373 public ListParser(Parser* left_, Parser* right_) :
374 base(u"list", new SequenceParser(left_, new KleeneParser(new SequenceParser(right_, left_->Clone())))), left(left_), right(right_)
375 {
376 }
377 public Parser* Left() const
378 {
379 return left;
380 }
381 public Parser* Right() const
382 {
383 return right;
384 }
385 public override Parser* Clone() const
386 {
387 return new ListParser(left->Clone(), right->Clone());
388 }
389 public override void Accept(Visitor& visitor)
390 {
391 visitor.Visit(*this);
392 }
393 private Parser* left;
394 private Parser* right;
395 }
396 public class ActionParser : UnaryParser
397 {
398 public ActionParser(Cm.Ast.CompoundStatementNode* successCode_, Cm.Ast.CompoundStatementNode* failCode_, Parser* child_) :
399 base(u"action", child_), successCode(successCode_), failCode(failCode_)
400 {
401 }
402 public Cm.Ast.CompoundStatementNode* SuccessCode() const
403 {
404 return successCode.Get();
405 }
406 public Cm.Ast.CompoundStatementNode* FailCode() const
407 {
408 return failCode.Get();
409 }
410 public override bool IsAction() const
411 {
412 return true;
413 }
414 public override Parser* Clone() const
415 {
416 Cm.Ast.CloneContext cloneContext;
417 Cm.Ast.CompoundStatementNode* failCode = null;
418 if ((failCode != null))
419 {
420 failCode = cast<Cm.Ast.CompoundStatementNode*>(failCode->Clone(cloneContext));
421 }
422 return new ActionParser(cast<Cm.Ast.CompoundStatementNode*>(successCode->Clone(cloneContext)), failCode, Child()->Clone());
423 }
424 public override void Accept(Visitor& visitor)
425 {
426 visitor.Visit(*this);
427 }
428 public override bool IsActionToken() const
429 {
430 CodeEvaluationVisitor visitor;
431 if (!successCode.IsNull())
432 {
433 successCode->Accept(visitor);
434 if (!visitor.HasReturn()) return false;
435 if (visitor.HasPass()) return false;
436 }
437 return Child()->IsToken();
438 }
439 private UniquePtr<Cm.Ast.CompoundStatementNode> successCode;
440 private UniquePtr<Cm.Ast.CompoundStatementNode> failCode;
441 }
442 public class Parameter
443 {
444 public Parameter(Cm.Ast.Node* type_, Cm.Ast.IdentifierNode* id_) :
445 type(type_), id(id_), variable(false)
446 {
447 }
448 public Parameter(Cm.Ast.Node* type_, Cm.Ast.IdentifierNode* id_, bool variable_) :
449 type(type_), id(id_), variable(variable_)
450 {
451 }
452 public virtual ~Parameter()
453 {
454 }
455 public UniquePtr<Cm.Ast.Node> type;
456 public UniquePtr<Cm.Ast.IdentifierNode> id;
457 public bool variable;
458 }
459 public class Variable : Parameter
460 {
461 public Variable(Cm.Ast.Node* type_, Cm.Ast.IdentifierNode* id_) :
462 base(type_, id_, true)
463 {
464 }
465 }
466 public class NonterminalParser : Parser
467 {
468 public suppress NonterminalParser(const NonterminalParser&);
469 public NonterminalParser(const ustring& name_, const ustring& ruleName_) :
470 base(name_), ruleName(ruleName_), rule(null)
471 {
472 }
473 public suppress NonterminalParser& operator=(const NonterminalParser&);
474 public const ustring& RuleName() const
475 {
476 return ruleName;
477 }
478 public void SetRule(RuleParser* rule)
479 {
480 this->rule = rule;
481 }
482 public RuleParser* Rule() const
483 {
484 return rule;
485 }
486 public const Cm.Ast.NodeList<Cm.Ast.Node>& Args() const
487 {
488 return args;
489 }
490 public void SetArguments(Cm.Ast.InvokeNode* invoke)
491 {
492 int n = invoke->Arguments().Count();
493 for (int i = 0; i < n; ++i;)
494 {
495 Cm.Ast.Node* arg = invoke->Arguments()[i];
496 Cm.Ast.CloneContext cloneContext;
497 args.Add(arg->Clone(cloneContext));
498 }
499 }
500 public override bool IsNonterminal() const
501 {
502 return true;
503 }
504 public override Parser* Clone() const
505 {
506 NonterminalParser* clone = new NonterminalParser(Name(), ruleName);
507 int n = args.Count();
508 for (int i = 0; i < n; ++i;)
509 {
510 Cm.Ast.CloneContext cloneContext;
511 clone->args.Add(args[i]->Clone(cloneContext));
512 }
513 return clone;
514 }
515 public override void Accept(Visitor& visitor)
516 {
517 visitor.Visit(*this);
518 }
519 private ustring ruleName;
520 private RuleParser* rule;
521 private Cm.Ast.NodeList<Cm.Ast.Node> args;
522 }
523 public class RuleParser : Parser
524 {
525 public RuleParser(const ustring& name_) :
526 base(name_), hasReturn(false), id(-1)
527 {
528 }
529 public Parser* Definition()
530 {
531 return definition.Get();
532 }
533 public const List<UniquePtr<Parameter>>& Parameters() const
534 {
535 return parameters;
536 }
537 public const List<UniquePtr<Variable>>& Variables() const
538 {
539 return variables;
540 }
541 public Cm.Ast.Node* ReturnType() const
542 {
543 return returnType.Get();
544 }
545 public const List<NonterminalParser*>& Nonterminals() const
546 {
547 return nonterminals;
548 }
549 public const ustring& Info() const
550 {
551 return info;
552 }
553 public void SetHasReturn()
554 {
555 hasReturn = true;
556 }
557 public bool HasReturn() const
558 {
559 return hasReturn;
560 }
561 public void SetDefinition(Parser* definition_)
562 {
563 definition.Reset(definition_);
564 definition->SetParent(this);
565 }
566 public void AddParamOrVariable(Parameter* paramOrVariable)
567 {
568 if (paramOrVariable->variable)
569 {
570 variables.Add(UniquePtr<Variable>(cast<Variable*>(paramOrVariable)));
571 }
572 else
573 {
574 parameters.Add(UniquePtr<Parameter>(paramOrVariable));
575 }
576 }
577 public void SetReturnType(Cm.Ast.Node* returnType_)
578 {
579 returnType.Reset(returnType_);
580 }
581 public void AddNonterminal(NonterminalParser* nonterminal)
582 {
583 nonterminals.Add(nonterminal);
584 }
585 public void SetInfo(const ustring& info_)
586 {
587 info = info_;
588 }
589 public override void Accept(Visitor& visitor)
590 {
591 visitor.Visit(*this);
592 }
593 public nothrow void SetId(int id_)
594 {
595 id = id_;
596 }
597 public inline nothrow int Id() const
598 {
599 return id;
600 }
601 private UniquePtr<Parser> definition;
602 private List<UniquePtr<Parameter>> parameters;
603 private List<UniquePtr<Variable>> variables;
604 private UniquePtr<Cm.Ast.Node> returnType;
605 private List<NonterminalParser*> nonterminals;
606 private ustring info;
607 private bool hasReturn;
608 private int id;
609 }
610 public class GrammarParser : Parser
611 {
612 public GrammarParser(const ustring& name_, const ustring& api_) :
613 base(name_), api(api_), main(false)
614 {
615 }
616 public const ustring& Api() const
617 {
618 return api;
619 }
620 public void SetMain()
621 {
622 main = true;
623 }
624 public bool Main() const
625 {
626 return main;
627 }
628 public nothrow bool Start() const
629 {
630 return start;
631 }
632 public nothrow void SetStart()
633 {
634 start = true;
635 }
636 public nothrow bool Nothrow() const
637 {
638 return isNothrow;
639 }
640 public override nothrow bool IsNothrow() const
641 {
642 return isNothrow;
643 }
644 public nothrow void SetNothrow()
645 {
646 isNothrow = true;
647 }
648 public override nothrow bool IsState() const
649 {
650 return state;
651 }
652 public nothrow void SetState()
653 {
654 state = true;
655 }
656 public nothrow bool IsFarthestError() const
657 {
658 return farthestError;
659 }
660 public nothrow void SetFarthestError()
661 {
662 farthestError = true;
663 }
664 public const List<ustring>& Usings() const
665 {
666 return usings;
667 }
668 public void SetLexer(const ustring& lexer_)
669 {
670 lexer = lexer_;
671 }
672 public const ustring& Lexer() const
673 {
674 return lexer;
675 }
676 public const List<UniquePtr<RuleParser>>& Rules() const
677 {
678 return rules;
679 }
680 public const List<Pair<ustring, ustring>>& RuleInfos() const
681 {
682 return ruleInfos;
683 }
684 public void AddUsing(const ustring& using_)
685 {
686 usings.Add(using_);
687 }
688 public void AddRule(RuleParser* rule)
689 {
690 rule->SetParent(this);
691 rules.Add(UniquePtr<RuleParser>(rule));
692 ruleMap[rule->Name()] = rule;
693 }
694 public void MapRule(RuleParser* rule)
695 {
696 ruleMap[rule->Name()] = rule;
697 }
698 public RuleParser* GetRule(const ustring& ruleName) const
699 {
700 Map<ustring, RuleParser*>.ConstIterator it = ruleMap.CFind(ruleName);
701 if (it != ruleMap.CEnd())
702 {
703 return it->second;
704 }
705 else
706 {
707 throw Exception("rule \'" + ToUtf8(ruleName) + "\' not found from parser \'" + ToUtf8(Name()) + "\'");
708 }
709 }
710 public void AddRuleInfo(const ustring& ruleName, const ustring& ruleInfo)
711 {
712 ruleInfos.Add(MakePair(ruleName, ruleInfo));
713 }
714 public override void Accept(Visitor& visitor)
715 {
716 visitor.Visit(*this);
717 }
718 private ustring api;
719 private bool main;
720 private bool start;
721 private bool isNothrow;
722 private bool farthestError;
723 private bool state;
724 private List<ustring> usings;
725 private ustring lexer;
726 private List<UniquePtr<RuleParser>> rules;
727 private Map<ustring, RuleParser*> ruleMap;
728 private List<Pair<ustring, ustring>> ruleInfos;
729 }
730 }