forked from okuoku/xitomatl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
irregex-tool.sls
97 lines (93 loc) · 3.91 KB
/
irregex-tool.sls
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
#!r6rs
;; Copyright 2009 Derick Eddington. My MIT-style license is in the file named
;; LICENSE from the original collection this file is distributed with.
(library (xitomatl irregex-tool)
(export
lines-enumerator
single-enumerator)
(import
(except (rnrs) file-exists? delete-file)
(xitomatl irregex)
(xitomatl irregex extras)
(xitomatl irregex counting)
(xitomatl enumerators)
(xitomatl file-system base)
(xitomatl file-system paths))
(define (files/dirs-enumerator files/dirs proc seeds)
(define (apply-proc filename seeds)
(call-with-input-file filename
(lambda (fip)
(let-values (((c . s) (apply proc fip filename seeds)))
(values c s)))))
(if (null? files/dirs)
(let-values (((continue . seeds)
(apply proc (current-input-port) 'current-input-port seeds)))
(apply values seeds))
(let loop ((files/dirs files/dirs) (seeds seeds))
(if (null? files/dirs)
(apply values seeds)
(let ((e (car files/dirs)))
(let-values
(((continue seeds)
(if (file-directory? e)
(fold/enumerator (directory-walk-enumerator) e
(lambda (path dirs files syms _ seeds)
(let-values
(((c s)
(fold/enumerator list-enumerator
(append files
(filter (lambda (s)
(file-regular? (path-join path s)))
syms))
(lambda (f _ seeds)
(let-values (((c s)
(apply-proc (path-join path f) seeds)))
(values c c s)))
#T seeds)))
(let ((d (and c dirs)))
(values d d s))))
#T seeds)
(apply-proc e seeds))))
(if continue
(loop (cdr files/dirs) seeds)
(apply values seeds))))))))
(define (lines-enumerator irx)
(let ((irx (irregex irx 'fast)))
(lambda (files/dirs proc seeds)
(let ((last-seeds
(fold/enumerator files/dirs-enumerator files/dirs
(lambda (fip filename seeds)
(let loop ((line-num 0) (seeds seeds))
(let ((l (get-line fip)))
(if (eof-object? l)
(values #T seeds)
(let ((m (irregex-search irx l)))
(if m
(let-values (((c . s)
(apply proc filename line-num l m seeds)))
(if c
(loop (+ 1 line-num) s)
(values #F s)))
(loop (+ 1 line-num) seeds)))))))
seeds)))
(apply values last-seeds)))))
(define (single-enumerator irx chunk-size)
(define pc (make-port-chunker chunk-size))
(define pe
(irregex-chunk-enumerator
(irregex irx 'single-line 'fast)
(counted-chunking-make-chunker pc)
(counted-chunking-make-lose-refs port-chunking-lose-refs)))
(lambda (files/dirs proc seeds)
(let ((last-seeds
(fold/enumerator files/dirs-enumerator files/dirs
(lambda (fip filename seeds)
(fold/enumerator pe (counted-chunking-make-initial-chunk pc
(port-chunking-make-initial-chunk fip))
(lambda (m _ seeds)
(let-values (((c . s) (apply proc filename m seeds)))
(values c c s)))
#T seeds))
seeds)))
(apply values last-seeds))))
)