Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
SymbolTable.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Globalization;
6 
7 namespace Irony.Parsing {
8  //First sketch of Symbol object and Symbol table
9  public class Symbol {
10  public readonly string Text;
11  //used for symbol comparison in case-insensitive environments
12  public readonly Symbol LowerSymbol;
13  private int _hashCode;
14 
15  internal Symbol(string text, Symbol lowerSymbol) {
16  Text = text;
17  LowerSymbol = lowerSymbol?? this; //lowerSymbol == null means "text is all lowercase, so use 'this' as LowerSymbol"
18  _hashCode = Text.GetHashCode();
19  }
20 
21  public override int GetHashCode() {
22  return _hashCode;
23  }
24  public override string ToString() {
25  return Text;
26  }
27 
28  public static bool AreEqual(Symbol first, Symbol second, bool caseSensitive) {
29  return (caseSensitive ? first == second : first.LowerSymbol == second.LowerSymbol);
30  }
31 
32  }//Symbol class
33 
34  public class SymbolSet : HashSet<Symbol> { }
35  public class SymbolList : List<Symbol> { }
36 
37  internal class SymbolDictionary : Dictionary<string, Symbol> {
38  internal SymbolDictionary() : base(1000) { }
39  }
40 
41  public class SymbolTable {
42  SymbolDictionary _dictionary = new SymbolDictionary();
43  object _lockObject = new object();
44 
45  public static SymbolTable Symbols = new SymbolTable();
46 
47  private SymbolTable() { }
48 
49  public int Count {
50  get { return _dictionary.Count; }
51  }
52 
53  public Symbol this[string text] {
54  get {
55  lock(_lockObject) {
56  return _dictionary[text];
57  }
58  }
59  }
60 
61  public Symbol FindSymbol(string text) {
62  Symbol symbol;
63  lock(_lockObject) {
64  _dictionary.TryGetValue(text, out symbol);
65  }
66  return symbol;
67  }
68 
69  public Symbol TextToSymbol(string text) {
70  Symbol symbol, lowerSymbol;
71  lock(_lockObject) {
72  if(_dictionary.TryGetValue(text, out symbol))
73  return symbol;
74  //Create symbol; first find/create lower symbol
75  var lowerText = text.ToLower(CultureInfo.InvariantCulture); //ToLowerInvariant looks better but it's not in Silverlight, so using ToLower
76  if(!_dictionary.TryGetValue(lowerText, out lowerSymbol))
77  lowerSymbol = NewSymbol(lowerText, null);
78  //if the text is all lower, return lowerSymbol as result
79  if(lowerText == text)
80  return lowerSymbol;
81  //otherwise create new symbol
82  return NewSymbol(text, lowerSymbol);
83  }
84  }//method
85 
86  private Symbol NewSymbol(string text, Symbol lowerSymbol) {
87  var result = new Symbol(text, lowerSymbol);
88  _dictionary.Add(text, result);
89  return result;
90  }
91 
92  }//class
93 
94  public class CaseSensitiveSymbolComparer : IComparer<Symbol> {
95  public int Compare(Symbol x, Symbol y) {
96  return x == y ? 0 : 1;
97  }
98  }
99 
100 }
override string ToString()
Definition: SymbolTable.cs:24
Symbol FindSymbol(string text)
Definition: SymbolTable.cs:61
static bool AreEqual(Symbol first, Symbol second, bool caseSensitive)
Definition: SymbolTable.cs:28
_In_ size_t _In_ DXGI_FORMAT _In_ size_t _In_ float size_t y
Definition: DirectXTexP.h:191
readonly Symbol LowerSymbol
Definition: SymbolTable.cs:12
override int GetHashCode()
Definition: SymbolTable.cs:21
Symbol TextToSymbol(string text)
Definition: SymbolTable.cs:69
readonly string Text
Definition: SymbolTable.cs:10