Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ConstantTerminal.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  //This terminal allows to declare a set of constants in the input language
19  // It should be used when constant symbols do not look like normal identifiers; e.g. in Scheme, #t, #f are true/false
20  // constants, and they don't fit into Scheme identifier pattern.
21  public class ConstantsTable : Dictionary<string, object> { }
22  public class ConstantTerminal : Terminal {
23  public readonly ConstantsTable Constants = new ConstantsTable();
24  public ConstantTerminal(string name, Type nodeType) : base(name) {
25  base.SetFlag(TermFlags.IsConstant);
26  AstNodeType = nodeType;
27  }
28 
29  public void Add(string lexeme, object value) {
30  this.Constants[lexeme] = value;
31  }
32 
33  public override void Init(GrammarData grammarData) {
34  base.Init(grammarData);
35  if (this.EditorInfo == null)
36  this.EditorInfo = new TokenEditorInfo(TokenType.Unknown, TokenColor.Text, TokenTriggers.None);
37  }
38 
39  public override Token TryMatch(ParsingContext context, ISourceStream source) {
40  string text = source.Text;
41  foreach (var entry in Constants) {
42  var constant = entry.Key;
43  if (source.PreviewPosition + constant.Length > text.Length) continue;
44  if (source.MatchSymbol(constant, !Grammar.CaseSensitive)) {
45  source.PreviewPosition += constant.Length;
46  return source.CreateToken(this.OutputTerminal, entry.Value);
47  }
48  }
49  return null;
50  }
51  public override IList<string> GetFirsts() {
52  string[] array = new string[Constants.Count];
53  Constants.Keys.CopyTo(array, 0);
54  return array;
55  }
56 
57  }//class
58 
59 
60 
61 }
override Token TryMatch(ParsingContext context, ISourceStream source)
ConstantTerminal(string name, Type nodeType)
override void Init(GrammarData grammarData)
global::MonoTouch.Constants Constants
Definition: TouchRunner.cs:42
Interface for Terminals to access the source stream and produce tokens.
readonly bool CaseSensitive
Gets case sensitivity of the grammar. Read-only, true by default. Can be set to false only through a ...
Definition: Grammar.cs:27
void Add(string lexeme, object value)
Tokens are produced by scanner and fed to parser, optionally passing through Token filters in between...
Definition: Token.cs:74
override IList< string > GetFirsts()