From 9e02db875ff4eea5b82fe33daed7d845a08e22a5 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Thu, 6 Oct 2016 20:31:54 -0700 Subject: [PATCH] rudimentary jsonic lexer --- beautiful-racket-lib/br/main.rkt | 5 ++++ .../br/demo/jsonic/drracket-indenter.rkt | 13 +++++++- .../br/demo/jsonic/drracket-lexer.rkt | 30 ++++++++++++------- .../br/demo/jsonic/jsonic-test.rkt | 7 ++--- beautiful-racket/br/demo/jsonic/main.rkt | 4 +-- 5 files changed, 42 insertions(+), 17 deletions(-) diff --git a/beautiful-racket-lib/br/main.rkt b/beautiful-racket-lib/br/main.rkt index c6e9127..2ebf01a 100644 --- a/beautiful-racket-lib/br/main.rkt +++ b/beautiful-racket-lib/br/main.rkt @@ -16,6 +16,11 @@ (define-namespace-anchor nsa) (eval DATUM (namespace-anchor->namespace nsa)))) +(provide really-dynamic-require) +(define-macro (really-dynamic-require . ARGS) + #'(parameterize ([current-namespace (make-base-namespace)]) + (dynamic-require . ARGS))) + (module reader syntax/module-reader #:language 'br #:info br-get-info diff --git a/beautiful-racket/br/demo/jsonic/drracket-indenter.rkt b/beautiful-racket/br/demo/jsonic/drracket-indenter.rkt index 7bc35af..824d809 100644 --- a/beautiful-racket/br/demo/jsonic/drracket-indenter.rkt +++ b/beautiful-racket/br/demo/jsonic/drracket-indenter.rkt @@ -1 +1,12 @@ -#lang racket/base +#lang br +(require racket/class) +(provide drracket-indenter) + +(define (drracket-indenter t pos) + #;(with-handlers ([exn:fail? (λ(exn) #f)]) ; this function won't work until gui-lib 1.26 + (send t compute-racket-amount-to-indent pos (λ(x) + (case x + [("with-pattern" "with-shared-id") 'lambda] + [("define-macro") 'define] + [else #f])))) + #f) \ No newline at end of file diff --git a/beautiful-racket/br/demo/jsonic/drracket-lexer.rkt b/beautiful-racket/br/demo/jsonic/drracket-lexer.rkt index 4b78e64..b677bd9 100644 --- a/beautiful-racket/br/demo/jsonic/drracket-lexer.rkt +++ b/beautiful-racket/br/demo/jsonic/drracket-lexer.rkt @@ -1,14 +1,24 @@ -#lang racket/base +#lang br (require parser-tools/lex + syntax-color/racket-lexer (prefix-in : parser-tools/lex-sre)) -(provide default-lexer) - - -(define default-lexer - (lexer - [(eof) (values lexeme 'eof #f #f #f)] - [(:seq "//" (:* (char-complement "\n"))) - (values lexeme 'comment #f (position-offset start-pos) (position-offset end-pos))] - [any-char (values lexeme 'no-color #f (position-offset start-pos) (position-offset end-pos))])) \ No newline at end of file +(provide drracket-lexer) + +(define drracket-lexer + (let ([in-racket-expr? #f]) + (lexer + [(eof) (values lexeme 'eof #f #f #f)] + ["@$" (begin + (set! in-racket-expr? #t) + (values lexeme 'parenthesis '|(| (position-offset start-pos) (position-offset end-pos)))] + ["$@" (begin + (set! in-racket-expr? #f) + (values lexeme 'parenthesis '|)| (position-offset start-pos) (position-offset end-pos)))] + [(: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 diff --git a/beautiful-racket/br/demo/jsonic/jsonic-test.rkt b/beautiful-racket/br/demo/jsonic/jsonic-test.rkt index e67f6c5..02ad6a4 100644 --- a/beautiful-racket/br/demo/jsonic/jsonic-test.rkt +++ b/beautiful-racket/br/demo/jsonic/jsonic-test.rkt @@ -1,8 +1,7 @@ #lang br/demo/jsonic { - "number": @$(* 6 7)$@, - "string": @$(string-append "foo" "bar")$@, - "array": @$(range 5)$@, - "object": @$(hash "k1" "valstring" (format "~a" 42) (hash "k1" (range 10) "k2" 42))$@ +"string": @$(string-append "foo" "bar")$@, +"array": @$(range 5)$@, +"object": @$(hash "k1" "valstring" (format "~a" 42) (hash "k1" (range 10) "k2" 42))$@ // "bar" : } \ No newline at end of file diff --git a/beautiful-racket/br/demo/jsonic/main.rkt b/beautiful-racket/br/demo/jsonic/main.rkt index da271dc..5b227e6 100644 --- a/beautiful-racket/br/demo/jsonic/main.rkt +++ b/beautiful-racket/br/demo/jsonic/main.rkt @@ -28,9 +28,9 @@ (λ (key default) (case key [(color-lexer) - (dynamic-require 'br/demo/jsonic/drracket-lexer 'default-lexer (λ () #f))] + (really-dynamic-require 'br/demo/jsonic/drracket-lexer 'drracket-lexer (λ () #f))] [(drracket:indentation) - (dynamic-require 'scribble/private/indentation 'determine-spaces)] + (really-dynamic-require 'br/demo/jsonic/drracket-indenter 'drracket-indenter (λ () #f))] [else default]))) (define (test-tokenize str)