-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatementBase.cs
More file actions
175 lines (157 loc) · 3.42 KB
/
Copy pathStatementBase.cs
File metadata and controls
175 lines (157 loc) · 3.42 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System;
using System.Collections.Generic;
using System.Text;
using Sid;
using Sid.Log;
namespace Sid.Parse.TextPatternParser
{
public abstract class StatementBase : HasLogImp
{
public delegate void UserOnMatch(string htmlSource, int pos, string debugStr);
protected Options m_Options;
private UserOnMatch m_fUserOnMatch;
protected StatementBase(
ILog log,
Options options,
UserOnMatch fUserOnMatch=null)
{
Log = log;
m_Options = options;
m_fUserOnMatch = fUserOnMatch;
}
protected StatementBase(
ILog log,
UserOnMatch fUserOnMatch = null)
{
Log = log;
m_Options = null;
m_fUserOnMatch = fUserOnMatch;
}
// Helpful with debugging
public string Name
{
get;
set;
}
// Allows debug hookins and potentially logging in the future
protected void PriorToPerform(
string input,
int pos,
int depth)
{
#if DEBUG
if (Name == "Args number")
{
Misc.Break();
}
#endif
}
protected void AfterPerform(
bool? rv,
string input,
int pos,
int depth,
RunState runState,
int? index)
{
#if DEBUG
string currentStr = string.Empty;
if ((pos + 40) < input.Length)
{
currentStr = input.Substring(pos, 40);
}
if (Name == "pag start")
{
}
if (Name == "open close")
{
}
if (Name == "open")
{
}
if (Name == "no advance" && rv.Value)
{
}
#endif
if ((!rv.HasValue || (rv.HasValue && rv.Value)) && m_fUserOnMatch!=null)
{
string debugStr=string.Empty;
#if DEBUG
int debugLen =Math.Min(50,input.Length- pos);
debugStr = input.Substring(pos, debugLen);
#endif
m_fUserOnMatch(input,pos,debugStr);
}
if (Log!=null)
{
Func<bool> fLogIsMatchResult = () =>
{
return ((Log.GetLevel() & (int)eLogLevel.MatchResult) > 0);
};
bool doLog;
if (rv.HasValue && rv.Value)
{
doLog = fLogIsMatchResult() || ((Log.GetLevel() & (int)eLogLevel.SuccessfulMatch) > 0);
}
else
{
doLog = fLogIsMatchResult();
}
if (doLog)
{
const int outputLen= 40;
string posPortion = input.Substring(pos, Math.Min(outputLen, input.Length - pos));
string indexPortion;
if (index != null && index.Value >= 0 && index.Value < input.Length)
{
indexPortion = input.Substring(index.Value, Math.Min(outputLen, input.Length - index.Value));
}
else
{
indexPortion = string.Empty;
}
StringBuilder textBuilder = new StringBuilder();
// Do the depth
for (int i = 0; i < depth; ++i)
{
textBuilder.Append(" ");
}
if (!string.IsNullOrEmpty(Name))
{
textBuilder.Append(string.Format("** {0} ** ", Name));
}
textBuilder.Append(ToString());
textBuilder.Append(" ");
// Return value
bool showAfter = true;
if (rv.HasValue)
{
textBuilder.Append(
string.Format("match={0}, ", rv.Value));
showAfter = rv.Value;
}
// Position/string
textBuilder.Append(
string.Format("pos={0} {1}",
pos,
posPortion));
// After
if (showAfter)
{
textBuilder.Append(
string.Format(", after={0} {1}",
index,
indexPortion));
}
if (rv.HasValue && rv.Value)
{
textBuilder.Append(" *********************");
}
Log.LogLine(textBuilder.ToString());
}
}
if(this.Name!=null)
runState.ExecutePostPerformAssertions(this.Name,input,pos,index,rv,Log);
}
}
}