forked from rebolek/red-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paf.red
70 lines (69 loc) · 1.3 KB
/
paf.red
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
Red [
Title: "PAF - parse files"
]
paf: function [
path
pattern
/quiet "Do not print any output"
/only "Return only logic! value to indicate match"
] [
matches: make block! 100
lines: 1
found?: false
line-start: none
mark: none
dir: none
filepath: none
unless dir? path [append path #"/"]
dirs: reduce copy [path]
find-line-end: function [
text
] [
unless mark: find text newline [mark: tail text]
mark
]
pattern: compose/deep [
some [
(either block? pattern [append/only copy [] pattern] [pattern])
mark:
(quote (
found?: true
unless only [append last matches mark]
unless quiet [print rejoin [filepath #"@" lines ": " copy/part line-start find-line-end mark]]
))
; to end
| #"^/" line-start: (quote (lines: lines + 1))
| skip
]
]
scan-file: func [
path
] [
lines: 1
unless error? try [file: read path] [
unless only [repend matches [path make block! 100]]
parse file pattern
all [
not only empty?
last matches
remove/part skip tail matches -2 2
]
]
]
scan-dir: func [
path
] [
dir: read path
foreach file dir [
either dir? file [
append dirs file
scan-dir to file! dirs
take/last dirs
] [
scan-file filepath: append to file! dirs file
]
]
]
scan-dir path
either only [found?] [matches]
]