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