From 4032a65eb875111c3f6b206f09c2330c6ee426d7 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Sun, 29 Jan 2017 23:28:36 -0800 Subject: [PATCH] add `apply-lexer` --- beautiful-racket-demo/basic-demo/lexer.rkt | 11 ++--------- brag/brag/support.rkt | 7 ++++++- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/beautiful-racket-demo/basic-demo/lexer.rkt b/beautiful-racket-demo/basic-demo/lexer.rkt index fc98294..842d221 100644 --- a/beautiful-racket-demo/basic-demo/lexer.rkt +++ b/beautiful-racket-demo/basic-demo/lexer.rkt @@ -1,11 +1,11 @@ #lang br -(require brag/support racket/contract) +(require brag/support) (define basic-lexer (lexer-srcloc [(eof) (return-without-srcloc eof)] [whitespace (token lexeme #:skip? #t)] - [(from/to "rem" "\n") (token 'REM lexeme)] + [(from/to "rem" #\newline) (token 'REM lexeme)] [(:or "print" "goto" "end" "+" ":") lexeme] [(:+ numeric) (token 'INTEGER (string->number lexeme))] [(:or (:seq (:+ numeric) ".") @@ -15,10 +15,3 @@ (token 'STRING (trim-ends "\"" lexeme "\""))])) (provide basic-lexer) - - -(define (apply-lexer lexer str) -(for/list ([t (in-port lexer (open-input-string str))]) - t)) - -(apply-lexer basic-lexer "10 rem") diff --git a/brag/brag/support.rkt b/brag/brag/support.rkt index 5328323..77e3a61 100755 --- a/brag/brag/support.rkt +++ b/brag/brag/support.rkt @@ -60,6 +60,11 @@ (exn:fail:parsing-srclocs instance))) +(provide apply-lexer) +(define (apply-lexer lexer val) + (for/list ([t (in-port lexer (if (string? val) (open-input-string val) val))]) + t)) + (provide apply-tokenizer-maker (rename-out [apply-tokenizer-maker apply-tokenizer])) (define (apply-tokenizer-maker tokenize in) @@ -76,7 +81,7 @@ [(srcloc-token? tok) (srcloc-token-token tok)] [else tok]))))]) - token)) + token)) (provide trim-ends) (define (trim-ends left lexeme right)