-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind
executable file
·300 lines (257 loc) · 10.6 KB
/
find
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/env escript
%% this is the pascal code diagram
%% --
%% find
%% getpat
%% makepat
%% getccl
%% dodash
%% stclose
%% match
%% amatch
%% omatch
%% locate
%% patsize
%% find -- find patterns in text
%%
%% for each line, if a match is found, return line
%% this is a mock version, should `getlines` first
find(Pattern, Input) ->
case match(Pattern, Input) of
true -> Input;
false -> []
end.
%% match -- find match anywhere on line
%%
%% returns true for match, false for mismatch
match(Pattern, Line) ->
case amatch(Pattern, Line) of
[] -> false;
_ -> true
end.
%% amatch -- look for match of pattern at line
%%
%% returns the position of all matches as a list
%%
%% General cases should be lower in order, metacharacters and classes
%% need to check pattern matching first.
amatch(Pattern, Line) ->
amatch({Pattern, Pattern}, Line, 1, []).
%% Test before base case for $ -- end of line
amatch({Pattern, [PH|PT]}, [], Pos, PosList) when PH =:= $$ ->
amatch({Pattern, PT}, {[], []}, Pos, PosList);
amatch({Pattern, [PH|[]]}, {[], []}, Pos, PosList) when PH =:= $$ ->
% test case example: true = match("a$", [$b, $\n, $a]),
amatch({Pattern, Pattern}, [], Pos+1, lists:concat([PosList, [Pos]]));
%% Base cases
amatch(_, [], _, PosList) ->
PosList;
amatch({_, []}, {[], []}, Pos, PosList) ->
lists:concat([PosList, [Pos]]);
amatch(_, {[], []}, _, PosList) ->
PosList;
%% $ -- match the end of line
%% we detect the end of the line by an empty Head
amatch({Pattern, [PH|_]}, {InitT, []}, Pos, PosList) when PH =:= $$ ->
amatch({Pattern, Pattern}, InitT, Pos+1, lists:concat([PosList, [Pos]]));
%% if there is a endline as the last sequential match, it's a match
amatch({Pattern, [PH|PT]}, {InitT, [$\n|T]}, Pos, PosList) when PH =:= $$ ->
case T =:= [] of
true -> amatch({Pattern, PT}, {InitT, T}, Pos, PosList);
false -> amatch({Pattern, Pattern}, InitT, Pos+1, PosList)
end;
%% % -- match the beginning of line
%% 1) if it's the beginning of line, keep position and
%% test sequentially starting from the same position
%% hence {[H|T], [H|T]} instead of {T, T}.
%% 2) if isn't the beginning of line, go to next position and
%% test same pattern in Tail, it will obviously never match
%% since Tail isn't the beginning anymore.
%%
%% Further tests is needed for this behavior.
amatch({Pattern, [PH|PT]}, [H|T], Pos, PosList) when PH =:= $% ->
case Pos =:= 1 of
true -> amatch({Pattern, PT}, {[H|T], [H|T]}, Pos, PosList);
false -> amatch({Pattern, Pattern}, T, Pos+1, PosList)
end;
%% [^...] -- match character "not in" class
%% i.e. [^a-z] matches everything that it's not a lower case letter
amatch({Pattern, [$[, $^, $]|PT]}, Line, Pos, PosList) ->
amatch({Pattern, PT, {parse, not_class, [$]]}}, Line, Pos, PosList);
amatch({Pattern, [$[, $^|PT]}, Line, Pos, PosList) ->
amatch({Pattern, PT, {parse, not_class, []}}, Line, Pos, PosList);
%% [...] -- match character class
%% i.e. [a-z], [A-Z], [a-zA-Z], [0-9], [0-9a-zA-Z]
amatch({Pattern, [$[|PT]}, Line, Pos, PosList) ->
amatch({Pattern, PT, {parse, in_class, []}}, Line, Pos, PosList);
amatch({Pattern, [$], $]|PT], {parse, ClassType, Class}}, Line, Pos, PosList) ->
NewClass = lists:concat([Class, "]"]),
amatch({Pattern, PT, {class, ClassType, NewClass}}, Line, Pos, PosList);
amatch({Pattern, [$]|PT], {parse, ClassType, Class}}, Line, Pos, PosList) ->
amatch({Pattern, PT, {class, ClassType, Class}}, Line, Pos, PosList);
amatch({Pattern, [C1, $-, $]|PT], {parse, ClassType, Class}}, Line, Pos, PosList) ->
NewClass = lists:concat([Class, [C1, $-]]), % case for not-ranged dash i.e. [b-]
amatch({Pattern, [$]|PT], {parse, ClassType, NewClass}}, Line, Pos, PosList);
amatch({Pattern, [C1, $-, C2|PT], {parse, ClassType, Class}}, Line, Pos, PosList) ->
NewClass = lists:concat([Class, lists:seq(C1, C2)]),
amatch({Pattern, PT, {parse, ClassType, NewClass}}, Line, Pos, PosList);
amatch({Pattern, [PH|PT], {parse, ClassType, Class}}, Line, Pos, PosList) ->
NewClass = lists:concat([Class, [PH]]), % case for a single char i.e. [a], [ab], [a-zb]
amatch({Pattern, PT, {parse, ClassType, NewClass}}, Line, Pos, PosList);
amatch({Pattern, PT, {class, in_class, Class}}, [H|T], Pos, PosList) ->
case lists:member(H, Class) of
true -> amatch({Pattern, PT}, {T, T}, Pos, PosList);
false -> amatch({Pattern, Pattern}, T, Pos+1, PosList)
end;
amatch({Pattern, PT, {class, not_class, Class}}, [H|T], Pos, PosList) ->
case not lists:member(H, Class) of
true -> amatch({Pattern, PT}, {T, T}, Pos, PosList);
false -> amatch({Pattern, Pattern}, T, Pos+1, PosList)
end;
amatch({Pattern, PT, {class, in_class, Class}}, {InitT, [H|T]}, Pos, PosList) ->
case lists:member(H, Class) of
true -> amatch({Pattern, PT}, {InitT, T}, Pos, PosList);
false -> amatch({Pattern, Pattern}, InitT, Pos+1, PosList)
end;
amatch({Pattern, PT, {class, not_class, Class}}, {InitT, [H|T]}, Pos, PosList) ->
case not lists:member(H, Class) of
true -> amatch({Pattern, PT}, {InitT, T}, Pos, PosList);
false -> amatch({Pattern, Pattern}, InitT, Pos+1, PosList)
end;
%% ? -- match any single character except newline
amatch({Pattern, [PH|PT]}, [H|T], Pos, PosList) when PH =:= $? ->
case H =/= $\n of
true -> amatch({Pattern, PT}, {T, T}, Pos, PosList);
false -> amatch({Pattern, Pattern}, T, Pos+1, PosList)
end;
amatch({Pattern, [PH|PT]}, {InitT, [H|T]}, Pos, PosList) when PH =:= $? ->
case H =/= $\n of
true -> amatch({Pattern, PT}, {InitT, T}, Pos, PosList);
false -> amatch({Pattern, Pattern}, InitT, Pos+1, PosList)
end;
%% sequence of literal characters
amatch({Pattern, []}, {InitT, _}, Pos, PosList) ->
amatch({Pattern, Pattern}, InitT, Pos+1, lists:concat([PosList, [Pos]]));
amatch({Pattern, [PH|PT]}, {InitT, [H|T]}, Pos, PosList) ->
case H =:= PH of
true -> amatch({Pattern, PT}, {InitT, T}, Pos, PosList);
false -> amatch({Pattern, Pattern}, InitT, Pos+1, PosList)
end;
%% if a single match is found, will test a sequential match
%% a single literal is a sequence of a literal + empty list,
%% so we can reuse code from a sequence of literal characters
amatch({Pattern, [PH|PT]}, [H|T], Pos, PosList) ->
case H =:= PH of
true -> amatch({Pattern, PT}, {T, T}, Pos, PosList);
false -> amatch({Pattern, Pattern}, T, Pos+1, PosList)
end.
main([Pattern, File|_]) ->
{Pattern, File};
main(["-t"|_]) ->
test().
test() ->
%% test 1 -- lines are properly read and written
Input1 = "This is a single input.",
Input1 = find("i", Input1),
[] = find("z", Input1),
%% test 2 -- find single character 'x' in text
%% -- find literal sequence 'xx' in text
[] = amatch("z", "this is a single input"),
[3, 6, 12] = amatch([$i], "This is a line"),
[1,2,3,4,5,6] = amatch("a", "aaaaaa"),
[6] = amatch("et", "Two letters"),
[1,2,3,4,5] = amatch("aa", "aaaaaa"),
%% test 3 -- match returns true or false
true = match("et", "Two letters"),
false = match("foobar", "foo bar"),
%% test 4 -- ? matches any character except newline
true = match("?", [32]), % ASCII 32 = whitespace
true = match("?", " "),
false = match("?", ""),
true = match("?", "aaa"),
false = match("?", [$\n]),
AnyInput = "Match everything",
AnyRange = lists:seq(1, erlang:length(AnyInput)),
true = match("?", AnyInput),
false = match("?z", AnyInput),
AnyRange = amatch("?", AnyInput),
AnyRange = amatch("?", AnyInput++[$\n]),
[2, 11, 18, 27] = amatch("?t", AnyInput++AnyInput),
[2, 11, 19, 28] = amatch("?t", AnyInput++[$\n]++AnyInput),
true = match("?t?", AnyInput),
[2, 11] = amatch("?t?", AnyInput),
%% test 5 -- % beginning of line
true = match("%abc", "abcc"),
true = match("%", "abc"),
true = match("%?", "abc"),
true = match("%a?", "abc"),
true = match("%a?c", "abc"),
false = match("%a?b", "abc"),
false = match("%bc", "abc"),
false = match("%?c", "abc"),
true = match("%?b", "abc"),
false = match("%b", [$a, $\n, $b, $\n, $c]),
[1] = amatch("%abc", "abcc"),
[1] = amatch("%?", "abc"),
[1] = amatch("%?b", "abc"),
[] = amatch("%?c", "abc"),
%% test 6 -- $ end of line
true = match("abc$", "abc"),
true = match("$", "abc"),
true = match("abc$", "aabc"),
false = match("abc$", "aabcd"),
true = match("%abc$", "abc"),
false = match("%abc$", "abcc"),
false = match("%abc$", "aabc"),
true = match("$", [$a, $\n, $b, $\n]),
true = match("$", [$a, $\n, $b]),
false = match("a$", [$a, $\n, $b, $\n]),
true = match("a$", [$b, $\n, $a, $\n]),
true = match("a$", [$b, $\n, $a]),
false = match("aa$", [$a, $a, $\n, $b, $\n]),
true = match("aa$", [$b, $\n, $a, $a, $\n]),
true = match("aa$", [$b, $\n, $a, $a]),
%% test 7 - character class
false = match("[k]", "ab"),
false = match("a[bc]d", "abc"),
true = match("a[bc]d", "abd"),
true = match("a[b]d", "abd"),
true = match("[a][b][d]", "abd"),
true = match("?[b]?", "abd"),
false = match("?[b]?", "aBd"),
false = match("a[a-d]e", "abd"),
true = match("a[b-d]e", "ace"),
true = match("a[b-d]", "aac"),
true = match("a[-b]", "a-"), % means "dash or b"
true = match("a[b-]", "a-"), % means "b or dash"
%% TODO -- are those cases correct?
false = match("[b-a]", "-"), % should return error (perl) but will behave as empty class
false = match("a[]b", "-"), % should return error (perl), but will behave as empty class
false = match("a[", "-"), % should return error (perl), unknown behavior (not covered)
true = match("a]", "a]"),
true = match("a[]]b", "a]b"),
true = match("a[a-cz]", "ab"),
true = match("a[a-cz]", "az"),
false = match("a[a-cz]", "ad"),
true = match("[a-zA-Z_][a-zA-Z0-9_]", "alpha"),
false = match("[_A-Z]", "}"),
true = match("[89]", "9"),
true = match("[3-6]", "5"),
false = match("[1-57-9]", "6"),
true = match("[1-57-9]", "4"),
true = match("[1-57-9]", "9"),
%% test 8 - "not in" character class
true = match("a[^bc]d", "aed"),
false = match("a[^bc]d", "abd"),
true = match("a[^-b]c", "adc"),
false = match("a[^-b]c", "a-c"),
false = match("a[^]b]c", "a]c"),
true = match("a[^]b]c", "adc"),
ok.
%% TODO
%%
%% * closure (zero or more occurrences of previous pattern)
%%
%% @c escaped character (@%, @[, @*, @^, @-, @@, @])
%% an escape sequence consist of @n (newline), @t (tab)
%% or @c (escaped character)