return-without-srcloc

pull/10/head
Matthew Butterick 8 years ago
parent 5ef1a11a2b
commit d4fe2c92ea

@ -3,7 +3,7 @@
(define basic-lexer (define basic-lexer
(lexer-srcloc (lexer-srcloc
[(eof) 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" "\n") (token 'REM lexeme)]
[(:or "print" "goto" "end" "+" ":") lexeme] [(:or "print" "goto" "end" "+" ":") lexeme]
@ -14,8 +14,11 @@
[(from/to "\"" "\"") [(from/to "\"" "\"")
(token 'STRING (trim-ends "\"" lexeme "\""))])) (token 'STRING (trim-ends "\"" lexeme "\""))]))
(provide (provide basic-lexer)
(contract-out
[basic-lexer
(input-port? . -> . (define (apply-lexer lexer str)
(or/c eof-object? string? srcloc-token?))])) (for/list ([t (in-port lexer (open-input-string str))])
t))
(apply-lexer basic-lexer "10 rem")

@ -7,8 +7,4 @@
(define (next-token) (basic-lexer ip)) (define (next-token) (basic-lexer ip))
next-token) next-token)
(provide (provide make-tokenizer)
(contract-out
[make-tokenizer
((input-port?) (path?) . ->* .
(-> (or/c eof-object? string? srcloc-token?)))]))

@ -167,14 +167,14 @@ are a few examples, using @racket[:] prefixed SRE syntax:
@item{@racket[input-port] --- the input-port being @item{@racket[input-port] --- the input-port being
processed (this is useful for matching input with multiple processed (this is useful for matching input with multiple
lexers).} lexers).}
@item{@racket[(return-without-pos x)] is a function (continuation) that @item{@racket[(return-without-pos x)] and @racket[(return-without-srcloc x)] are functions (continuations) that
immediately returns the value of @racket[x] from the lexer. This useful immediately return the value of @racket[x] from the lexer. This useful
in a src-pos lexer to prevent the lexer from adding source in a src-pos or src-loc lexer to prevent the lexer from adding source
information. For example: information. For example:
@racketblock[ @racketblock[
(define get-token (define get-token
(lexer-src-pos (lexer-srcloc
... ...
((comment) (get-token input-port)) ((comment) (get-token input-port))
...)) ...))
@ -182,7 +182,7 @@ are a few examples, using @racket[:] prefixed SRE syntax:
would wrap the source location information for the comment around would wrap the source location information for the comment around
the value of the recursive call. Using the value of the recursive call. Using
@racket[((comment) (return-without-pos (get-token input-port)))] @racket[((comment) (return-without-srcloc (get-token input-port)))]
will cause the value of the recursive call to be returned without will cause the value of the recursive call to be returned without
wrapping position around it.} wrapping position around it.}
] ]
@ -262,6 +262,7 @@ _action-result lexeme-srcloc)] instead of simply
@defidform[lexeme-srcloc] @defidform[lexeme-srcloc]
@defidform[input-port] @defidform[input-port]
@defidform[return-without-pos] @defidform[return-without-pos]
@defidform[return-without-srcloc]
)]{ )]{
Use of these names outside of a @racket[lexer] action is a syntax Use of these names outside of a @racket[lexer] action is a syntax

@ -46,7 +46,7 @@
[(eq? src-loc-style 'lexer-srcloc) [(eq? src-loc-style 'lexer-srcloc)
#`(let/ec ret #`(let/ec ret
(syntax-parameterize (syntax-parameterize
([return-without-pos (make-rename-transformer #'ret)]) ([return-without-srcloc (make-rename-transformer #'ret)])
(make-srcloc-token #,action lexeme-srcloc)))] (make-srcloc-token #,action lexeme-srcloc)))]
[else action]))) [else action])))
(syntax/loc action (syntax/loc action
@ -405,6 +405,6 @@
... ...
(provide id ...))])) (provide id ...))]))
(provide-lex-keyword start-pos end-pos lexeme lexeme-srcloc input-port return-without-pos) (provide-lex-keyword start-pos end-pos lexeme lexeme-srcloc input-port return-without-pos return-without-srcloc)
) )

Loading…
Cancel
Save