Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
BnfExpression.cs
Go to the documentation of this file.
1 #region License
2 /* **********************************************************************************
3  * Copyright (c) Roman Ivantsov
4  * This source code is subject to terms and conditions of the MIT License
5  * for Irony. A copy of the license can be found in the License.txt file
6  * at the root of this distribution.
7  * By using this source code in any fashion, you are agreeing to be bound by the terms of the
8  * MIT License.
9  * You must not remove this notice from this software.
10  * **********************************************************************************/
11 #endregion
12 
13 using System;
14 using System.Collections.Generic;
15 using System.Text;
16 using System.Diagnostics;
17 
18 namespace Irony.Parsing {
19 
20  //BNF expressions are represented as OR-list of Plus-lists of BNF terms
21  internal class BnfExpressionData : List<BnfTermList> {
22  public override string ToString() {
23  try {
24  string[] pipeArr = new string[this.Count];
25  for (int i = 0; i < this.Count; i++) {
26  BnfTermList seq = this[i];
27  string[] seqArr = new string[seq.Count];
28  for (int j = 0; j < seq.Count; j++)
29  seqArr[j] = seq[j].ToString();
30  pipeArr[i] = String.Join("+", seqArr);
31  }
32  return String.Join("|", pipeArr);
33  } catch(Exception e) {
34  return "(error: " + e.Message + ")";
35  }
36 
37  }
38  }
39 
40  public class BnfExpression : BnfTerm {
41 
42  public BnfExpression(BnfTerm element): this() {
43  Data[0].Add(element);
44  }
45  public BnfExpression() : base(null) {
46  Data = new BnfExpressionData();
47  Data.Add(new BnfTermList());
48  }
49 
50  internal BnfExpressionData Data;
51  public override string ToString() {
52  return Data.ToString();
53  }
54 
55  #region Implicit cast operators
56  public static implicit operator BnfExpression(string symbol) {
57  return new BnfExpression(Grammar.CurrentGrammar.ToTerm(symbol));
58  }
59  //It seems better to define one method instead of the following two, with parameter of type BnfTerm -
60  // but that's not possible - it would be a conversion from base type of BnfExpression itself, which
61  // is not allowed in c#
62  public static implicit operator BnfExpression(Terminal term) {
63  return new BnfExpression(term);
64  }
65  public static implicit operator BnfExpression(NonTerminal nonTerminal) {
66  return new BnfExpression(nonTerminal);
67  }
68  #endregion
69 
70 
71  }//class
72 
73 }//namespace
KeyTerm ToTerm(string text)
Definition: Grammar.cs:460
BnfExpression(BnfTerm element)
static Grammar CurrentGrammar
Definition: Grammar.cs:488
override string ToString()