1 using System;
2 using System.Collections;
3
4
5
6
7
8 namespace System.RegularExpressions
9 {
10 public const uchar eps = '\0';
11 public abstract class Symbol
12 {
13 public virtual ~Symbol()
14 {
15 }
16 public abstract bool Match(uchar c);
17 }
18 public class Char : Symbol
19 {
20 public Char(uchar chr_) :
21 chr(chr_)
22 {
23 }
24 public uchar Chr() const
25 {
26 return chr;
27 }
28 public override bool Match(uchar c)
29 {
30 return c == chr;
31 }
32 private uchar chr;
33 }
34 public class Any : Symbol
35 {
36 public override bool Match(uchar c)
37 {
38 return true;
39 }
40 }
41 public class Range : Symbol
42 {
43 public Range(uchar start_, uchar end_) :
44 start(start_), end(end_)
45 {
46 }
47 public uchar Start() const
48 {
49 return start;
50 }
51 public uchar End() const
52 {
53 return end;
54 }
55 public override bool Match(uchar c)
56 {
57 return c >= start && c <= end;
58 }
59 private uchar start;
60 private uchar end;
61 }
62 public inline bool operator==(const Range& left, const Range& right)
63 {
64 return left.Start() == right.Start() && left.End() == right.End();
65 }
66 public inline bool operator<(const Range& left, const Range& right)
67 {
68 if (left.Start() < right.Start()) return true;
69 if (left.Start() > right.Start()) return false;
70 return left.End() < right.End();
71 }
72 public class Class : Symbol
73 {
74 public Class() :
75 inverse(false)
76 {
77 }
78 public void SetInverse()
79 {
80 inverse = true;
81 }
82 public void AddSymbol(Symbol* symbol)
83 {
84 symbols.Add(symbol);
85 }
86 public override bool Match(uchar c)
87 {
88 bool match = false;
89 for (Symbol* symbol : symbols)
90 {
91 if (symbol->Match(c))
92 {
93 match = true;
94 break;
95 }
96 }
97 return match != inverse;
98 }
99 private bool inverse;
100 private List<Symbol*> symbols;
101 }
102 }