Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Rule.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  /// Rule is the logical structures of the grammar.
27  /// </summary>
28  /// <remarks>
29  /// Rules consist of a head containing a nonterminal
30  /// followed by a series of both nonterminals and terminals.
31  /// </remarks>
32  internal class Rule
33  {
34  private int m_index;
35  internal Symbol m_nonTerminal;
36  internal Symbol[] m_symbols;
37  internal bool m_hasOneNonTerminal;
38 
39  /// <summary>
40  /// Creates a new instance of <c>Rule</c> class.
41  /// </summary>
42  /// <param name="index">Index of the rule in the grammar rule table.</param>
43  /// <param name="nonTerminal">Nonterminal of the rule.</param>
44  /// <param name="symbols">Terminal and nonterminal symbols of the rule.</param>
45  public Rule(int index, Symbol nonTerminal, Symbol[] symbols)
46  {
47  m_index = index;
48  m_nonTerminal = nonTerminal;
49  m_symbols = symbols;
50  m_hasOneNonTerminal = (symbols.Length == 1)
51  && (symbols[0].SymbolType == SymbolType.NonTerminal);
52  }
53 
54  /// <summary>
55  /// Gets index of the rule in the rule table.
56  /// </summary>
57  public int Index
58  {
59  get { return m_index; }
60  }
61 
62  /// <summary>
63  /// Gets the head symbol of the rule.
64  /// </summary>
65  public Symbol NonTerminal
66  {
67  get { return m_nonTerminal; }
68  }
69 
70  /// <summary>
71  /// Gets name of the rule.
72  /// </summary>
73  public string Name
74  {
75  get { return '<' + m_nonTerminal.Name + '>'; }
76  }
77 
78  /// <summary>
79  /// Gets number of symbols.
80  /// </summary>
81  public int Count
82  {
83  get { return m_symbols.Length; }
84  }
85 
86  /// <summary>
87  /// Gets symbol by its index.
88  /// </summary>
89  public Symbol this[int index]
90  {
91  get { return m_symbols[index]; }
92  }
93 
94  /// <summary>
95  /// Gets true if the rule contains exactly one symbol.
96  /// </summary>
97  /// <remarks>Used by the Parser object to TrimReductions</remarks>
98  public bool ContainsOneNonTerminal
99  {
100  get { return m_hasOneNonTerminal; }
101  }
102 
103  /// <summary>
104  /// Gets the rule definition.
105  /// </summary>
106  public string Definition
107  {
108  get
109  {
110  StringBuilder result = new StringBuilder();
111  for (int i = 0; i < m_symbols.Length; i++)
112  {
113  result.Append(m_symbols[i].ToString());
114  if (i < m_symbols.Length - 1)
115  result.Append(' ');
116  }
117  return result.ToString();
118  }
119  }
120 
121  /// <summary>
122  /// Returns the Backus-Noir representation of the rule.
123  /// </summary>
124  /// <returns></returns>
125  public override string ToString()
126  {
127  return Name + " ::= " + Definition;
128  }
129  }
130 }
Normal nonterminal