From 5465cae6c9f7424e3936a3197ed5728c718c36ff Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Thu, 15 Dec 2016 11:46:04 -0800 Subject: [PATCH] changes --- beautiful-racket-lib/br/main.rkt | 3 +++ beautiful-racket-lib/br/scribblings/br.scrbl | 16 +++++++++++++++- beautiful-racket/br/demo/jsonic-2/colorer.rkt | 15 +++++++++++++-- beautiful-racket/br/demo/jsonic-2/indenter.rkt | 12 +++++++++--- 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/beautiful-racket-lib/br/main.rkt b/beautiful-racket-lib/br/main.rkt index 80b0a00..d6bfdf7 100644 --- a/beautiful-racket-lib/br/main.rkt +++ b/beautiful-racket-lib/br/main.rkt @@ -21,6 +21,9 @@ #'(parameterize ([current-namespace (make-base-namespace)]) (dynamic-require . ARGS))) +(provide values->list) +(define-macro (values->list EXPR) + #'(call-with-values (λ () EXPR) list)) (module reader syntax/module-reader #:language 'br diff --git a/beautiful-racket-lib/br/scribblings/br.scrbl b/beautiful-racket-lib/br/scribblings/br.scrbl index 9c47308..9fc5edc 100644 --- a/beautiful-racket-lib/br/scribblings/br.scrbl +++ b/beautiful-racket-lib/br/scribblings/br.scrbl @@ -467,4 +467,18 @@ source-location information and properties. An alias for @racket[strip-context]. @defproc[(replace-bindings [stx-source (or/c syntax? #f)] [stx-target syntax?]) syntax?]{ Uses the bindings from @racket[stx-source] to replace the bindings of all parts of @racket[stx-target], while preserving source-location -information and properties. An alias for @racket[replace-context].} \ No newline at end of file +information and properties. An alias for @racket[replace-context].} + + +@section{Other helpers} + +@defmodule[br/main] + +@defform[(values->list values)]{ +Convert @racket[values] to a simple list. + +@examples[#:eval my-eval +(split-at '(a b c d e f) 3) +(values->list (split-at '(a b c d e f) 3)) +] +} \ No newline at end of file diff --git a/beautiful-racket/br/demo/jsonic-2/colorer.rkt b/beautiful-racket/br/demo/jsonic-2/colorer.rkt index cf1d63c..b716b95 100644 --- a/beautiful-racket/br/demo/jsonic-2/colorer.rkt +++ b/beautiful-racket/br/demo/jsonic-2/colorer.rkt @@ -4,7 +4,13 @@ (define in-racket-expr? #f) -(define (color-jsonic port) +(define/contract (color-jsonic port) + (input-port? . -> . + (values (or/c string? eof-object?) + symbol? + (or/c symbol? #f) + (or/c exact-positive-integer? #f) + (or/c exact-positive-integer? #f))) (define jsonic-lexer (lexer [(eof) (values lexeme 'eof #f #f #f)] @@ -24,4 +30,9 @@ (not (equal? (peek-string 2 0 port) "$@"))) (racket-lexer port) (jsonic-lexer port))) -(provide color-jsonic) \ No newline at end of file +(provide color-jsonic) + +(module+ test + (require rackunit) + (check-equal? (values->list (color-jsonic (open-input-string "x"))) + (list "x" 'string #f 1 2))) \ No newline at end of file diff --git a/beautiful-racket/br/demo/jsonic-2/indenter.rkt b/beautiful-racket/br/demo/jsonic-2/indenter.rkt index 1531ebe..f64542d 100644 --- a/beautiful-racket/br/demo/jsonic-2/indenter.rkt +++ b/beautiful-racket/br/demo/jsonic-2/indenter.rkt @@ -4,6 +4,12 @@ (define indent-width 2) +(define (left-bracket? c) + (and c (or (char=? c #\{) (char=? c #\[)))) + +(define (right-bracket? c) + (and c (or (char=? c #\}) (char=? c #\])))) + (define (indent-jsonic tb [this-pos 0]) (define this-line (line tb this-pos)) (define prev-line (previous-line tb this-pos)) @@ -11,10 +17,10 @@ (define this-indent (cond ;; if this line begins with }, outdent. - [((char tb (line-start-visible tb this-line)) . char=? . #\}) + [(right-bracket? (char tb (line-start-visible tb this-line))) (- prev-indent indent-width)] ;; if last line begins with {, indent. - [((char tb (line-start-visible tb prev-line)) . char=? . #\{) + [(left-bracket? (char tb (line-start-visible tb prev-line))) (+ prev-indent indent-width)] ;; otherwise use previous indent [else prev-indent])) @@ -35,4 +41,4 @@ here ) (check-equal? (str->indents (test-indenter indent-jsonic test-str)) - (map (λ(x) (* x indent-width)) '(0 0 1 1 2 2 1 1 0)))) \ No newline at end of file + (map (λ(x) (* x indent-width)) '(0 0 1 1 2 2 1 1 0))))