Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Token.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.Collections.Generic;
14 
15 namespace Irony.Parsing
16 {
17 
18  /// <summary>
19  /// Token flags.
20  /// </summary>
21  public enum TokenFlags
22  {
23  IsIncomplete = 0x01,
24  }
25 
26  /// <summary>
27  /// Token category.
28  /// </summary>
29  public enum TokenCategory
30  {
31  /// <summary>
32  /// Content category.
33  /// </summary>
34  Content,
35 
36  /// <summary>
37  /// newLine, indent, dedent
38  /// </summary>
39  Outline,
40 
41  /// <summary>
42  /// Comment category.
43  /// </summary>
44  Comment,
45 
46  /// <summary>
47  /// Directive category.
48  /// </summary>
49  Directive,
50 
51  /// <summary>
52  /// Error category.
53  /// </summary>
54  Error,
55  }
56 
57  /// <summary>
58  /// A List of tokens.
59  /// </summary>
60  public class TokenList : List<Token>
61  {
62  }
63 
64  /// <summary>
65  /// A Stack of tokens.
66  /// </summary>
67  public class TokenStack : Stack<Token>
68  {
69  }
70 
71  /// <summary>
72  /// Tokens are produced by scanner and fed to parser, optionally passing through Token filters in between.
73  /// </summary>
74  public class Token
75  {
76  private string text;
77 
78  /// <summary>
79  /// Initializes a new instance of the <see cref="Token"/> class.
80  /// </summary>
81  /// <param name="term">The term.</param>
82  /// <param name="location">The location.</param>
83  /// <param name="text">The text.</param>
84  /// <param name="value">The value.</param>
85  public Token(Terminal term, SourceLocation location, string text, object value)
86  {
87  SetTerminal(term);
88  KeyTerm = term as KeyTerm;
89  Location = location;
90  Length = text.Length;
91  this.text = text;
92  Value = value;
93  }
94 
95  /// <summary>
96  /// Initializes a new instance of the <see cref="Token"/> class.
97  /// </summary>
98  /// <param name="term">The term.</param>
99  /// <param name="location">The location.</param>
100  /// <param name="length">The length.</param>
101  /// <param name="source">The source.</param>
102  /// <param name="value">The value.</param>
103  public Token(Terminal term, SourceLocation location, int length, string source, object value)
104  {
105  SetTerminal(term);
106  KeyTerm = term as KeyTerm;
107  Location = location;
108  Length = length;
109  SourceCode = source;
110  Value = value;
111  }
112 
113  /// <summary>
114  /// Location in the source code.
115  /// </summary>
116  public readonly SourceLocation Location;
117 
118  /// <summary>
119  /// Gets the terminal.
120  /// </summary>
121  public Terminal Terminal { get; private set; }
122 
123  /// <summary>
124  /// Gets the Key terminal if any.
125  /// </summary>
126  public KeyTerm KeyTerm { get; private set; }
127 
128  /// <summary>
129  /// Gets the length.
130  /// </summary>
131  public int Length { get; private set; }
132 
133  /// <summary>
134  /// Gets the source code.
135  /// </summary>
136  /// <value>
137  /// The source code.
138  /// </value>
139  public string SourceCode { get; private set; }
140 
141  /// <summary>
142  /// Gets the text associated with this token.
143  /// </summary>
144  public string Text
145  {
146  get
147  {
148  if (text == null)
149  {
150  text = SourceCode.Substring(Location.Position, Length);
151  }
152  return text;
153  }
154  }
155 
156  /// <summary>
157  /// Get the Value associated with this token.
158  /// </summary>
159  public object Value;
160 
161  /// <summary>
162  /// Gets the value as a string.
163  /// </summary>
164  public string ValueString
165  {
166  get
167  {
168  return Value == null ? string.Empty : Value.ToString();
169  }
170  }
171 
172  /// <summary>
173  /// Get the flags
174  /// </summary>
176 
177  /// <summary>
178  /// Gets the Editor info.
179  /// </summary>
181 
182  /// <summary>
183  /// Gets the category.
184  /// </summary>
185  public TokenCategory Category
186  {
187  get { return Terminal.Category; }
188  }
189 
190  /// <summary>
191  /// Gets the matching opening/closing brace
192  /// </summary>
193  public Token OtherBrace
194  {
195  get;
196  private set;
197  }
198 
199  /// <summary>
200  /// Scanner state after producing token
201  /// </summary>
202  public short ScannerState;
203 
204  /// <summary>
205  /// Sets the terminal.
206  /// </summary>
207  /// <param name="terminal">The terminal.</param>
208  public void SetTerminal(Terminal terminal)
209  {
210  Terminal = terminal;
211 
212  // Set to term's EditorInfo by default
213  EditorInfo = Terminal.EditorInfo;
214  }
215 
216  /// <summary>
217  /// Determines whether the specified flag is set.
218  /// </summary>
219  /// <param name="flag">The flag.</param>
220  /// <returns>
221  /// <c>true</c> if the specified flag is set; otherwise, <c>false</c>.
222  /// </returns>
223  public bool IsSet(TokenFlags flag)
224  {
225  return (Flags & flag) != 0;
226  }
227 
228 
229  /// <summary>
230  /// Determines whether this instance is error.
231  /// </summary>
232  /// <returns>
233  /// <c>true</c> if this instance is error; otherwise, <c>false</c>.
234  /// </returns>
235  public bool IsError()
236  {
237  return Category == TokenCategory.Error;
238  }
239 
240  /// <summary>
241  /// Links the matching braces.
242  /// </summary>
243  /// <param name="openingBrace">The opening brace.</param>
244  /// <param name="closingBrace">The closing brace.</param>
245  public static void LinkMatchingBraces(Token openingBrace, Token closingBrace)
246  {
247  openingBrace.OtherBrace = closingBrace;
248  closingBrace.OtherBrace = openingBrace;
249  }
250 
251  /// <inheritdoc/>
252  [System.Diagnostics.DebuggerStepThrough]
253  public override string ToString()
254  {
255  return Terminal.TokenToString(this);
256  }
257  }
258 
259  /// <summary>
260  /// Some terminals may need to return a bunch of tokens in one call to TryMatch; MultiToken is a container for these tokens
261  /// </summary>
262  public class MultiToken : Token
263  {
264  /// <summary>
265  /// List of child tokens
266  /// </summary>
268 
269  /// <summary>
270  /// Initializes a new instance of the <see cref="MultiToken"/> class.
271  /// </summary>
272  /// <param name="term">The term.</param>
273  /// <param name="location">The location.</param>
274  /// <param name="childTokens">The child tokens.</param>
275  public MultiToken(Terminal term, SourceLocation location, TokenList childTokens) : base(term, location, string.Empty, null)
276  {
277  ChildTokens = childTokens;
278  }
279  }
280 }
TokenCategory
Token category.
Definition: Token.cs:29
Token(Terminal term, SourceLocation location, string text, object value)
Initializes a new instance of the Token class.
Definition: Token.cs:85
void SetTerminal(Terminal terminal)
Sets the terminal.
Definition: Token.cs:208
TokenFlags Flags
Get the flags
Definition: Token.cs:175
TokenFlags
Token flags.
Definition: Token.cs:21
MultiToken(Terminal term, SourceLocation location, TokenList childTokens)
Initializes a new instance of the MultiToken class.
Definition: Token.cs:275
override string ToString()
Definition: Token.cs:253
short ScannerState
Scanner state after producing token
Definition: Token.cs:202
TokenList ChildTokens
List of child tokens
Definition: Token.cs:267
static void LinkMatchingBraces(Token openingBrace, Token closingBrace)
Links the matching braces.
Definition: Token.cs:245
A Stack of tokens.
Definition: Token.cs:67
Flags
Enumeration of the new Assimp's flags.
readonly SourceLocation Location
Location in the source code.
Definition: Token.cs:116
A List of tokens.
Definition: Token.cs:60
object Value
Get the Value associated with this token.
Definition: Token.cs:159
Directive category.
bool IsError()
Determines whether this instance is error.
Definition: Token.cs:235
TokenEditorInfo EditorInfo
Gets the Editor info.
Definition: Token.cs:180
Comment category.
Tokens are produced by scanner and fed to parser, optionally passing through Token filters in between...
Definition: Token.cs:74
Token(Terminal term, SourceLocation location, int length, string source, object value)
Initializes a new instance of the Token class.
Definition: Token.cs:103
bool IsSet(TokenFlags flag)
Determines whether the specified flag is set.
Definition: Token.cs:223
Some terminals may need to return a bunch of tokens in one call to TryMatch; MultiToken is a containe...
Definition: Token.cs:262
newLine, indent, dedent