~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/Misc/Reports/Irony/CompilerServices/Grammar/BnfExpression.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.CompilerServices {
 
19
 
 
20
  //BNF expressions are represented as OR-list of Plus-lists of BNF terms
 
21
  internal class BnfExpressionData : List<BnfTermList> { }
 
22
 
 
23
  public class BnfExpression : BnfTerm {
 
24
 
 
25
    public BnfExpression(BnfTerm element): this() {
 
26
      Data[0].Add(element);
 
27
    }
 
28
    public BnfExpression() : base(null) {
 
29
      Data = new BnfExpressionData();
 
30
      Data.Add(new BnfTermList());
 
31
    }
 
32
 
 
33
    #region properties: Data
 
34
    internal BnfExpressionData Data;
 
35
    #endregion
 
36
 
 
37
    #region overrides: ToString()
 
38
    private string _toString;
 
39
    public override string ToString() {
 
40
      if (_toString != null) return _toString;
 
41
      try {
 
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);
 
49
        }
 
50
        _toString = String.Join("|", pipeArr);
 
51
        return _toString; 
 
52
      } catch(Exception e) {
 
53
        return "(error: " + e.Message + ")";
 
54
      }
 
55
    } 
 
56
    #endregion
 
57
 
 
58
    #region Implicit cast operators
 
59
    public static implicit operator BnfExpression(string symbol) {
 
60
      return new BnfExpression(Grammar.CurrentGrammar.Symbol(symbol));
 
61
    }
 
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);
 
67
    }
 
68
    public static implicit operator BnfExpression(NonTerminal nonTerminal) {
 
69
      return new BnfExpression(nonTerminal);
 
70
    }
 
71
    #endregion
 
72
 
 
73
 
 
74
  }//class
 
75
 
 
76
}//namespace