Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Symbol.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 //
4 // ----------------------------------------------------------------------
5 // Gold Parser engine.
6 // See more details on http://www.devincook.com/goldparser/
7 //
8 // Original code is written in VB by Devin Cook (GOLDParser@DevinCook.com)
9 //
10 // This translation is done by Vladimir Morozov (vmoroz@hotmail.com)
11 //
12 // The translation is based on the other engine translations:
13 // Delphi engine by Alexandre Rai (riccio@gmx.at)
14 // C# engine by Marcus Klimstra (klimstra@home.nl)
15 // ----------------------------------------------------------------------
16 #region Using directives
17 
18 using System;
19 using System.Text;
20 
21 #endregion
22 
23 namespace GoldParser
24 {
25  /// <summary>
26  /// Represents a terminal or nonterminal symbol used by the Deterministic
27  /// Finite Automata (DFA) and LR Parser.
28  /// </summary>
29  /// <remarks>
30  /// Symbols can be either terminals (which represent a class of
31  /// tokens - such as identifiers) or nonterminals (which represent
32  /// the rules and structures of the grammar). Terminal symbols fall
33  /// into several categories for use by the GOLD Parser Engine
34  /// which are enumerated in <c>SymbolType</c> enumeration.
35  /// </remarks>
36  internal class Symbol
37  {
38  internal int m_index; // symbol index in symbol table
39  private string m_name; // name of the symbol
40  internal SymbolType m_symbolType; // type of the symbol
41  private string m_text; // printable representation of symbol
42 
43  private const string m_quotedChars = "|-+*?()[]{}<>!";
44 
45  /// <summary>
46  /// Creates a new instance of <c>Symbol</c> class.
47  /// </summary>
48  /// <param name="index">Symbol index in symbol table.</param>
49  /// <param name="name">Name of the symbol.</param>
50  /// <param name="symbolType">Type of the symbol.</param>
51  public Symbol(int index, string name, SymbolType symbolType)
52  {
53  m_index = index;
54  m_name = name;
55  m_symbolType = symbolType;
56  }
57 
58  /// <summary>
59  /// Returns the index of the symbol in the GOLDParser object's Symbol Table.
60  /// </summary>
61  public int Index
62  {
63  get { return m_index; }
64  }
65 
66  /// <summary>
67  /// Returns the name of the symbol.
68  /// </summary>
69  public string Name
70  {
71  get { return m_name; }
72  }
73 
74  /// <summary>
75  /// Returns an enumerated data type that denotes
76  /// the class of symbols that the object belongs to.
77  /// </summary>
78  public SymbolType SymbolType
79  {
80  get { return m_symbolType; }
81  }
82 
83  /// <summary>
84  /// Returns the text representation of the symbol.
85  /// In the case of nonterminals, the name is delimited by angle brackets,
86  /// special terminals are delimited by parenthesis
87  /// and terminals are delimited by single quotes
88  /// (if special characters are present).
89  /// </summary>
90  /// <returns>String representation of symbol.</returns>
91  public override string ToString()
92  {
93  if (m_text == null)
94  {
95  switch (SymbolType)
96  {
97  case SymbolType.NonTerminal:
98  m_text = '<' + Name + '>';
99  break;
100 
101  case SymbolType.Terminal:
102  m_text = FormatTerminalSymbol(Name);
103  break;
104 
105  default:
106  m_text = '(' + Name + ')';
107  break;
108  }
109  }
110  return m_text;
111  }
112 
113  private static string FormatTerminalSymbol(string source)
114  {
115  StringBuilder result = new StringBuilder();
116  for (int i = 0; i < source.Length; i++)
117  {
118  char ch = source[i];
119  if (ch == '\'')
120  {
121  result.Append("''");
122  }
123  else if (IsQuotedChar(ch) || (ch == '"'))
124  {
125  result.Append(new Char[] {'\'', ch, '\''});
126  }
127  else
128  {
129  result.Append(ch);
130  }
131  }
132  return result.ToString();
133  }
134 
135  private static bool IsQuotedChar(char value)
136  {
137  return (m_quotedChars.IndexOf(value) >= 0);
138  }
139  }
140 }