-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathExplicitDFAMatcher.cs
More file actions
84 lines (66 loc) · 3.07 KB
/
ExplicitDFAMatcher.cs
File metadata and controls
84 lines (66 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.Linq;
using ParserCombinators;
using RegexParser.Patterns;
using RegexParser.Transforms;
using Utility.BaseTypes;
using Utility.ConsLists;
namespace RegexParser.Matchers
{
public class ExplicitDFAMatcher : BaseMatcher
{
public ExplicitDFAMatcher(string patternText, RegexOptionsEx options)
: base(patternText, options)
{
Parser = createParser(Pattern);
}
protected override BasePattern TransformAST(BasePattern pattern)
{
pattern = base.TransformAST(pattern);
return new StringASTTransform().Transform(pattern);
}
protected Parser<char, string> Parser { get; private set; }
protected override Result<char, Match2> Parse(ArrayConsList<char> consList, int afterLastMatchIndex)
{
Result<char, string> result = Parser(consList);
if (result != null)
return new Result<char, Match2>(
new Match2(consList.ArrayIndex, result.Tree.Length, result.Tree),
result.Rest);
else
return new Result<char, Match2>(Match2.Empty, consList);
}
private Parser<char, string> createParser(BasePattern pattern)
{
if (pattern == null)
throw new ArgumentNullException("pattern.", "Pattern is null when creating match parser.");
switch (pattern.Type)
{
case PatternType.Group:
return from vs in
CharParsers.Sequence(((GroupPattern)pattern).Patterns
.Select(p => createParser(p)))
select vs.JoinStrings();
case PatternType.Quantifier:
QuantifierPattern quant = (QuantifierPattern)pattern;
return from vs in CharParsers.Count(quant.MinOccurrences,
quant.MaxOccurrences,
createParser(quant.ChildPattern))
select vs.JoinStrings();
case PatternType.Alternation:
return CharParsers.Choice(((AlternationPattern)pattern).Alternatives
.Select(p => createParser(p))
.ToArray());
case PatternType.String:
return CharParsers.String(((StringPattern)pattern).Value);
case PatternType.Char:
return from c in CharParsers.Satisfy(((CharPattern)pattern).IsMatch)
select new string(c, 1);
default:
throw new ApplicationException(
string.Format("ExplicitDFAMatcher: unrecognized pattern type ({0}).",
pattern.GetType().Name));
}
}
}
}