Last active
August 29, 2015 14:02
-
-
Save NalaGinrut/4ddc9839683bd4c9aff4 to your computer and use it in GitHub Desktop.
string lexer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(use-modules (ice-9 rdelim) (rnrs)) | |
(define (k f init val pred) | |
(if (pred val) | |
init | |
(call-with-values (lambda () (f init val)) | |
(lambda (lst rst) (k f lst rst pred))))) | |
(define (lexer str delimiters) | |
(define (-> c) | |
(if (eof-object? c) '() (list (string c)))) | |
(define (tokenizer lst str) | |
(call-with-input-string str | |
(lambda (port) | |
(let* ((token (read-delimited delimiters port 'peek)) | |
(delim (-> (read-char port))) | |
(rest (get-string-all port))) | |
(values (if (string-null? token) `(,@lst ,@delim) `(,@lst ,token ,@delim)) | |
rest))))) | |
(k tokenizer '() str string-null?)) | |
Author
NalaGinrut
commented
Jun 18, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment