1 using System;
2 using System.Collections;
3
4
5
6
7
8 namespace System.RegularExpressions
9 {
10 public class Context
11 {
12 public Context() :
13 any(), epsilon(eps)
14 {
15 }
16 public ~Context()
17 {
18 for (NfaState* state : nfaStates)
19 {
20 delete state;
21 }
22 for (Symbol* symbol : symbols)
23 {
24 delete symbol;
25 }
26 }
27 public Symbol* MakeAny()
28 {
29 return &any;
30 }
31 public Symbol* MakeEpsilon()
32 {
33 return ε
34 }
35 public NfaState* MakeNfaState()
36 {
37 NfaState* state = new NfaState(cast<int>(nfaStates.Count()));
38 nfaStates.Add(state);
39 return state;
40 }
41 public Symbol* MakeChar(uchar c)
42 {
43 Map<uchar, Symbol*>.ConstIterator it = charSymbols.CFind(c);
44 if (it != charSymbols.CEnd())
45 {
46 return it->second;
47 }
48 else
49 {
50 Symbol* symbol = new Char(c);
51 symbols.Add(symbol);
52 charSymbols[c] = symbol;
53 return symbol;
54 }
55 }
56 public Symbol* MakeRange(uchar start, uchar end)
57 {
58 Range range(start, end);
59 Map<Range, Symbol*>.ConstIterator it = rangeSymbols.CFind(range);
60 if (it != rangeSymbols.CEnd())
61 {
62 return it->second;
63 }
64 else
65 {
66 Symbol* symbol = new Range(start, end);
67 symbols.Add(symbol);
68 rangeSymbols[range] = symbol;
69 return symbol;
70 }
71 }
72 public Class* MakeClass()
73 {
74 Class* cls = new Class();
75 symbols.Add(cls);
76 return cls;
77 }
78 public NfaState* GetState(int id) const
79 {
80 if (id >= 0 && id < nfaStates.Count())
81 {
82 return nfaStates[id];
83 }
84 else
85 {
86 return null;
87 }
88 }
89 public void Dump()
90 {
91 for (int i = 0; i < nfaStates.Count(); ++i;)
92 {
93 NfaState* state = nfaStates[i];
94 state->Dump();
95 }
96 }
97 private List<NfaState*> nfaStates;
98 private List<Symbol*> symbols;
99 private Any any;
100 private Char epsilon;
101 private Map<uchar, Symbol*> charSymbols;
102 private Map<Range, Symbol*> rangeSymbols;
103 }
104 }