Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
_Terminal.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 
17 namespace Irony.Parsing {
18 
19  public class Terminal : BnfTerm {
20  #region Constructors
21  public Terminal(string name) : this(name, TokenCategory.Content, TermFlags.None) { }
22  public Terminal(string name, TokenCategory category) : this(name, category, TermFlags.None) { }
23  public Terminal(string name, string errorAlias, TokenCategory category, TermFlags flags) : this(name, category, flags) {
24  this.ErrorAlias = errorAlias;
25  }
26  public Terminal(string name, TokenCategory category, TermFlags flags) : base(name) {
27  Category = category;
28  this.Flags |= flags;
29  if (Category == TokenCategory.Outline)
30  this.SetFlag(TermFlags.IsPunctuation);
31  OutputTerminal = this;
32  }
33  #endregion
34 
35  #region fields and properties
36  public TokenCategory Category = TokenCategory.Content;
37  // Priority is used when more than one terminal may match the input char.
38  // It determines the order in which terminals will try to match input for a given char in the input.
39  // For a given input char the scanner uses the hash table to look up the collection of terminals that may match this input symbol.
40  // It is the order in this collection that is determined by Priority property - the higher the priority,
41  // the earlier the terminal gets a chance to check the input.
42  public int Priority; //default is 0
43 
44  //Terminal to attach to the output token. By default is set to the Terminal itself
45  // Use SetOutputTerminal method to change it. For example of use see TerminalFactory.CreateSqlIdentifier and sample SQL grammar
46  public Terminal OutputTerminal { get; private set; }
47 
49  public byte MultilineIndex;
51  #endregion
52 
53  #region virtual methods: GetFirsts(), TryMatch, Init, TokenToString
54 
55  //"Firsts" (chars) collections are used for quick search for possible matching terminal(s) using current character in the input stream.
56  // A terminal might declare no firsts. In this case, the terminal is tried for match for any current input character.
57  public virtual IList<string> GetFirsts() {
58  return null;
59  }
60 
61  public virtual Token TryMatch(ParsingContext context, ISourceStream source) {
62  return null;
63  }
64 
65  public virtual string TokenToString(Token token) {
66  if (token.ValueString == this.Name)
67  return token.ValueString;
68  else
69  return (token.ValueString ?? token.Text) + " (" + Name + ")";
70  }
71 
72 
73  #endregion
74 
75  #region Events: ValidateToken
76  public event EventHandler<ParsingEventArgs> ValidateToken;
77  protected internal virtual void InvokeValidateToken(ParsingContext context) {
78  if (ValidateToken != null)
79  ValidateToken(this, context.SharedParsingEventArgs);
80  }
81  #endregion
82 
83  #region static comparison methods
84  public static int ByName(Terminal x, Terminal y) {
85  return string.Compare(x.ToString(), y.ToString());
86  }
87  public static int ByPriorityReverse(Terminal x, Terminal y) {
88  if (x.Priority > y.Priority)
89  return -1;
90  if (x.Priority == y.Priority)
91  return 0;
92  return 1;
93  }
94  #endregion
95 
96  #region Miscellaneous: SetOutputTerminal
97  public void SetOutputTerminal(Grammar grammar, Terminal outputTerminal) {
98  OutputTerminal = outputTerminal;
99  grammar.NonGrammarTerminals.Add(this);
100  }
101 
102  #endregion
103  //Priority constants
104  public const int LowestPriority = -1000;
105  public const int HighestPriority = 1000;
106  public const int ReservedWordsPriority = 900; //almost top one
107 
108  public static string TerminalsToString(IEnumerable<Terminal> terminals, string separator) {
109  var sb = new StringBuilder();
110  foreach (var term in terminals) {
111  sb.Append(term.ToString());
112  sb.Append(separator);
113  }
114  return sb.ToString().Trim();
115  }
116 
117  }//class
118 
119  public class TerminalSet : HashSet<Terminal> {
120  public override string ToString() {
121  return Terminal.TerminalsToString(this, " ");
122  }
123  }
124 
125  //No-duplicates list of terminals
126  public class TerminalList : List<Terminal> {
127  public new void Add(Terminal terminal) {
128  if (!Contains(terminal))
129  base.Add(terminal);
130  }
131  public new void AddRange(IEnumerable<Terminal> terminals) {
132  foreach(var terminal in terminals)
133  Add(terminal);
134  }
135  public override string ToString() {
136  return Terminal.TerminalsToString(this, " ");
137  }
138  }
139 
140 
141 }//namespace
TokenCategory
Token category.
Definition: Token.cs:29
new void Add(Terminal terminal)
Definition: _Terminal.cs:127
static int ByName(Terminal x, Terminal y)
Definition: _Terminal.cs:84
virtual Token TryMatch(ParsingContext context, ISourceStream source)
Definition: _Terminal.cs:61
Terminal(string name, TokenCategory category, TermFlags flags)
Definition: _Terminal.cs:26
_In_ size_t _In_ DXGI_FORMAT _In_ size_t _In_ DXGI_FORMAT _In_ DWORD flags
Definition: DirectXTexP.h:170
_In_ size_t _In_ DXGI_FORMAT _In_ size_t _In_ float size_t y
Definition: DirectXTexP.h:191
static string TerminalsToString(IEnumerable< Terminal > terminals, string separator)
Definition: _Terminal.cs:108
override string ToString()
Definition: BnfTerm.cs:84
string Text
Gets the text associated with this token.
Definition: Token.cs:145
Terminal(string name, string errorAlias, TokenCategory category, TermFlags flags)
Definition: _Terminal.cs:23
Interface for Terminals to access the source stream and produce tokens.
Terminal(string name)
Definition: _Terminal.cs:21
string ValueString
Gets the value as a string.
Definition: Token.cs:165
override string ToString()
Definition: _Terminal.cs:120
void SetOutputTerminal(Grammar grammar, Terminal outputTerminal)
Definition: _Terminal.cs:97
TokenEditorInfo EditorInfo
Definition: _Terminal.cs:48
EventHandler< ParsingEventArgs > ValidateToken
Definition: _Terminal.cs:76
static int ByPriorityReverse(Terminal x, Terminal y)
Definition: _Terminal.cs:87
virtual IList< string > GetFirsts()
Definition: _Terminal.cs:57
Terminal(string name, TokenCategory category)
Definition: _Terminal.cs:22
Tokens are produced by scanner and fed to parser, optionally passing through Token filters in between...
Definition: Token.cs:74
override string ToString()
Definition: _Terminal.cs:135
virtual string TokenToString(Token token)
Definition: _Terminal.cs:65
new void AddRange(IEnumerable< Terminal > terminals)
Definition: _Terminal.cs:131