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
9
* You must not remove this notice from this software.
10
* **********************************************************************************/
14
using System.Collections.Generic;
16
using System.Diagnostics;
18
namespace Irony.CompilerServices {
20
//BNF expressions are represented as OR-list of Plus-lists of BNF terms
21
internal class BnfExpressionData : List<BnfTermList> { }
23
public class BnfExpression : BnfTerm {
25
public BnfExpression(BnfTerm element): this() {
28
public BnfExpression() : base(null) {
29
Data = new BnfExpressionData();
30
Data.Add(new BnfTermList());
33
#region properties: Data
34
internal BnfExpressionData Data;
37
#region overrides: ToString()
38
private string _toString;
39
public override string ToString() {
40
if (_toString != null) return _toString;
42
string[] pipeArr = new string[Data.Count];
43
for (int i = 0; i < Data.Count; i++) {
44
BnfTermList seq = Data[i];
45
string[] seqArr = new string[seq.Count];
46
for (int j = 0; j < seq.Count; j++)
47
seqArr[j] = seq[j].ToString();
48
pipeArr[i] = String.Join("+", seqArr);
50
_toString = String.Join("|", pipeArr);
52
} catch(Exception e) {
53
return "(error: " + e.Message + ")";
58
#region Implicit cast operators
59
public static implicit operator BnfExpression(string symbol) {
60
return new BnfExpression(Grammar.CurrentGrammar.Symbol(symbol));
62
//It seems better to define one method instead of the following two, with parameter of type BnfElement -
63
// but that's not possible - it would be a conversion from base type of BnfExpression itself, which
64
// is not allowed in c#
65
public static implicit operator BnfExpression(Terminal term) {
66
return new BnfExpression(term);
68
public static implicit operator BnfExpression(NonTerminal nonTerminal) {
69
return new BnfExpression(nonTerminal);