add `apply-lexer`

pull/10/head
Matthew Butterick 8 years ago
parent d4fe2c92ea
commit 4032a65eb8

@ -1,11 +1,11 @@
#lang br #lang br
(require brag/support racket/contract) (require brag/support)
(define basic-lexer (define basic-lexer
(lexer-srcloc (lexer-srcloc
[(eof) (return-without-srcloc eof)] [(eof) (return-without-srcloc eof)]
[whitespace (token lexeme #:skip? #t)] [whitespace (token lexeme #:skip? #t)]
[(from/to "rem" "\n") (token 'REM lexeme)] [(from/to "rem" #\newline) (token 'REM lexeme)]
[(:or "print" "goto" "end" "+" ":") lexeme] [(:or "print" "goto" "end" "+" ":") lexeme]
[(:+ numeric) (token 'INTEGER (string->number lexeme))] [(:+ numeric) (token 'INTEGER (string->number lexeme))]
[(:or (:seq (:+ numeric) ".") [(:or (:seq (:+ numeric) ".")
@ -15,10 +15,3 @@
(token 'STRING (trim-ends "\"" lexeme "\""))])) (token 'STRING (trim-ends "\"" lexeme "\""))]))
(provide basic-lexer) (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")

@ -60,6 +60,11 @@
(exn:fail:parsing-srclocs instance))) (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 (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)
@ -76,7 +81,7 @@
[(srcloc-token? tok) [(srcloc-token? tok)
(srcloc-token-token tok)] (srcloc-token-token tok)]
[else tok]))))]) [else tok]))))])
token)) token))
(provide trim-ends) (provide trim-ends)
(define (trim-ends left lexeme right) (define (trim-ends left lexeme right)

Loading…
Cancel
Save