Skip to content

Instantly share code, notes, and snippets.

@NalaGinrut
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NalaGinrut/4ddc9839683bd4c9aff4 to your computer and use it in GitHub Desktop.
Save NalaGinrut/4ddc9839683bd4c9aff4 to your computer and use it in GitHub Desktop.
string lexer
(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?))
@NalaGinrut
Copy link
Author

(lexer "hello.x+(bye)"  ".+()")

==> ("hello" "." "x" "+" "(" "bye" ")")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment