add `apply-port-proc`

pull/14/head
Matthew Butterick 6 years ago
parent 9cbb3e7a5c
commit 6dff5018c6

@ -1080,19 +1080,23 @@ In addition to the exports shown below, the @racketmodname[brag/support] module
DrRacket should highlight the offending locations in the source.} DrRacket should highlight the offending locations in the source.}
@defproc[(apply-port-proc [proc procedure?]
@defproc[(apply-tokenizer-maker [tokenizer-maker procedure?] [port (or/c string? input-port?) (current-input-port)])
[source (or/c string?
input-port?)])
list?]{ list?]{
Repeatedly apply @racket[tokenizer-maker] to @racket[source], gathering the resulting tokens into a list. @racket[source] can be a string or an input port. Useful for testing or debugging a tokenizer. Repeatedly apply @racket[proc] to @racket[port], gathering the results into a list. @racket[port] can be an input port or a string (which is converted to a string port). Useful for testing or debugging a lexer or tokenizer.
} }
@defproc[(apply-lexer [lexer procedure?] @defproc[(apply-lexer [lexer procedure?]
[source (or/c string? [port (or/c string? input-port?) (current-input-port)])
input-port?)]) list?]{
Alias for @racket[apply-port-proc].
}
@defproc[(apply-tokenizer-maker [tokenizer-maker procedure?]
[port (or/c string? input-port?) (current-input-port)])
list?]{ list?]{
Repeatedly apply @racket[lexer] to @racket[source], gathering the resulting tokens into a list. @racket[source] can be a string or an input port. Useful for testing or debugging a lexer. Repeatedly apply @racket[tokenizer-maker] to @racket[port], gathering the resulting tokens into a list. @racket[port] can be an input port or a string (which is converted to a string port).
} }

@ -60,14 +60,15 @@
(exn:fail:parsing-srclocs instance))) (exn:fail:parsing-srclocs instance)))
(provide apply-lexer) (provide (rename-out [apply-port-proc apply-lexer])
(define (apply-lexer lexer val) apply-port-proc)
(for/list ([t (in-port lexer (if (string? val) (open-input-string val) val))]) (define (apply-port-proc proc [val (current-input-port)])
(for/list ([t (in-port proc (if (string? val) (open-input-string val) val))])
t)) t))
(provide apply-tokenizer-maker (provide apply-tokenizer-maker
(rename-out [apply-tokenizer-maker apply-tokenizer])) (rename-out [apply-tokenizer-maker apply-tokenizer]))
(define (apply-tokenizer-maker tokenize in) (define (apply-tokenizer-maker tokenize [in (current-input-port)])
(define input-port (if (string? in) (define input-port (if (string? in)
(open-input-string in) (open-input-string in)
in)) in))

Loading…
Cancel
Save