From bfd3690eca02d5fa86e297e31a0764db50618c3c Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Tue, 24 Jan 2017 00:03:38 -0500 Subject: [PATCH] start basic colorer --- beautiful-racket-demo/basic-demo/colorer.rkt | 48 +++++++++++++++++++ beautiful-racket-demo/basic-demo/main.rkt | 16 ++++++- .../basic-demo/tokenizer.rkt | 8 ++-- 3 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 beautiful-racket-demo/basic-demo/colorer.rkt diff --git a/beautiful-racket-demo/basic-demo/colorer.rkt b/beautiful-racket-demo/basic-demo/colorer.rkt new file mode 100644 index 0000000..d490ced --- /dev/null +++ b/beautiful-racket-demo/basic-demo/colorer.rkt @@ -0,0 +1,48 @@ +#lang br +(require brag/support syntax-color/racket-lexer racket/contract + basic-demo/tokenizer sugar/coerce) + +;(values lexeme 'parenthesis '|(| (pos lexeme-start) (pos lexeme-end)) + +(define (color-basic ip) + (define postok ((tokenize ip))) + (define tok-or-str (position-token-token postok)) + (define type (if (string? tok-or-str) + 'string + (token-struct-type tok-or-str))) + (define val (if (string? tok-or-str) + tok-or-str + (->string (or (token-struct-val tok-or-str) "")))) + (values val + (caseq type + [(WHITE) 'white-space] + [(COMMENT) 'comment] + [(NUMBER) 'constant] + [(STRING) 'string] + [else 'no-color]) + #f + (position-offset (position-token-start-pos postok)) + (position-offset (position-token-end-pos postok)))) + + +(provide + (contract-out + [color-basic + (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)))])) + +(module+ main + (define p (open-input-string #<