From dd67b321d527dd6c507940e6ea76609fdaca8e98 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Fri, 7 Oct 2016 11:45:11 -0700 Subject: [PATCH] improve jsonic coloring lexer --- .../br/demo/jsonic/drracket-lexer.rkt | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/beautiful-racket/br/demo/jsonic/drracket-lexer.rkt b/beautiful-racket/br/demo/jsonic/drracket-lexer.rkt index b677bd9..2db12dd 100644 --- a/beautiful-racket/br/demo/jsonic/drracket-lexer.rkt +++ b/beautiful-racket/br/demo/jsonic/drracket-lexer.rkt @@ -6,8 +6,18 @@ (provide drracket-lexer) -(define drracket-lexer - (let ([in-racket-expr? #f]) +(define in-racket-expr? #f) + +(define (at-racket-boundary? input-port) + (equal? (peek-string 2 0 input-port) "$@")) + +(module+ test + (require rackunit) + (check-true (at-racket-boundary? (open-input-string "$@foo"))) + (check-false (at-racket-boundary? (open-input-string "foo$@")))) + +(define (drracket-lexer input-port) + (define this-lexer (lexer [(eof) (values lexeme 'eof #f #f #f)] ["@$" (begin @@ -19,6 +29,7 @@ [(:seq "//" (:* (char-complement #\newline))) (values lexeme 'comment #f (position-offset start-pos) (position-offset end-pos))] [any-char - (if in-racket-expr? - (racket-lexer (transplant-input-port (open-input-string lexeme) #f (position-offset start-pos))) - (values lexeme 'string #f (position-offset start-pos) (position-offset end-pos)))]))) \ No newline at end of file + (values lexeme 'string #f (position-offset start-pos) (position-offset end-pos))])) + (if (and in-racket-expr? (not (at-racket-boundary? input-port))) + (racket-lexer input-port) + (this-lexer input-port))) \ No newline at end of file