Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
KeyTerm.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 KeyTermTable : Dictionary<string, KeyTerm> {
20  public KeyTermTable(StringComparer comparer) : base(100, comparer) {}
21  }
22  public class KeyTermList : List<KeyTerm> { }
23 
24  //Keyterm is a keyword or a special symbol used in grammar rules, for example: begin, end, while, =, *, etc.
25  // So "key" comes from the Keyword.
26  public class KeyTerm : Terminal {
27  public KeyTerm(string text, string name) : base(name) {
28  Text = text;
29  base.ErrorAlias = name;
30 
31  }
32 
33  public string Text {get; private set;}
34 
35  //Normally false, meaning keywords (symbols in grammar consisting of letters) cannot be followed by a letter or digit
36  public bool AllowAlphaAfterKeyword = false;
37 
38  #region overrides: TryMatch, Init, GetPrefixes(), ToString()
39  public override void Init(GrammarData grammarData) {
40  base.Init(grammarData);
41 
42  #region comments about keyterms priority
43  // Priority - determines the order in which multiple terminals try to match input for a given current char in the input.
44  // For a given input char the scanner looks up the collection of terminals that may match this input symbol. It is the order
45  // in this collection that is determined by Priority value - the higher the priority, the earlier the terminal gets a chance
46  // to check the input.
47  // Keywords found in grammar by default have lowest priority to allow other terminals (like identifiers)to check the input first.
48  // Additionally, longer symbols have higher priority, so symbols like "+=" should have higher priority value than "+" symbol.
49  // As a result, Scanner would first try to match "+=", longer symbol, and if it fails, it will try "+".
50  // Reserved words are the opposite - they have the highest priority
51  #endregion
52  if (FlagIsSet(TermFlags.IsReservedWord))
53  base.Priority = ReservedWordsPriority + Text.Length;
54  else
55  base.Priority = LowestPriority + Text.Length;
56  //Setup editor info
57  if (this.EditorInfo != null) return;
58  TokenType tknType = TokenType.Identifier;
59  if (FlagIsSet(TermFlags.IsOperator))
60  tknType |= TokenType.Operator;
61  else if (FlagIsSet(TermFlags.IsDelimiter | TermFlags.IsPunctuation))
62  tknType |= TokenType.Delimiter;
63  TokenTriggers triggers = TokenTriggers.None;
64  if (this.FlagIsSet(TermFlags.IsBrace))
65  triggers |= TokenTriggers.MatchBraces;
66  if (this.FlagIsSet(TermFlags.IsMemberSelect))
67  triggers |= TokenTriggers.MemberSelect;
68  TokenColor color = TokenColor.Text;
69  if (FlagIsSet(TermFlags.IsKeyword))
70  color = TokenColor.Keyword;
71  this.EditorInfo = new TokenEditorInfo(tknType, color, triggers);
72  }
73 
74  public override Token TryMatch(ParsingContext context, ISourceStream source) {
75  if (!source.MatchSymbol(Text, !Grammar.CaseSensitive))
76  return null;
77  source.PreviewPosition += Text.Length;
78  //In case of keywords, check that it is not followed by letter or digit
79  if (this.FlagIsSet(TermFlags.IsKeyword) && !AllowAlphaAfterKeyword) {
80  var previewChar = source.PreviewChar;
81  if (char.IsLetterOrDigit(previewChar) || previewChar == '_') return null; //reject
82  }
83  var token = source.CreateToken(this.OutputTerminal, Text);
84  return token;
85  }
86 
87  public override IList<string> GetFirsts() {
88  return new string[] { Text };
89  }
90  public override string ToString() {
91  if (Name != Text) return Name;
92  return Text;
93  }
94  public override string TokenToString(Token token) {
95  var keyw = FlagIsSet(TermFlags.IsKeyword)? Resources.LabelKeyword : Resources.LabelKeySymbol ; //"(Keyword)" : "(Key symbol)"
96  var result = (token.ValueString ?? token.Text) + " " + keyw;
97  return result;
98  }
99  #endregion
100 
101  [System.Diagnostics.DebuggerStepThrough]
102  public override bool Equals(object obj) {
103  return base.Equals(obj);
104  }
105 
106  [System.Diagnostics.DebuggerStepThrough]
107  public override int GetHashCode() {
108  return Text.GetHashCode();
109  }
110 
111  }//class
112 
113 
114 }
KeyTermTable(StringComparer comparer)
Definition: KeyTerm.cs:20
override string TokenToString(Token token)
Definition: KeyTerm.cs:94
static string LabelKeySymbol
Looks up a localized string similar to (Key symbol).
override int GetHashCode()
Definition: KeyTerm.cs:107
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
override IList< string > GetFirsts()
Definition: KeyTerm.cs:87
override bool Equals(object obj)
Definition: KeyTerm.cs:102
override Token TryMatch(ParsingContext context, ISourceStream source)
Definition: KeyTerm.cs:74
Tokens are produced by scanner and fed to parser, optionally passing through Token filters in between...
Definition: Token.cs:74
bool MatchSymbol(string symbol, bool ignoreCase)
Tries to match the symbol with the text at current preview position.
override string ToString()
Definition: KeyTerm.cs:90
override void Init(GrammarData grammarData)
Definition: KeyTerm.cs:39
KeyTerm(string text, string name)
Definition: KeyTerm.cs:27