syntax colorer support for the custom command character.

This commit is contained in:
Georges Dupéron 2017-04-26 01:30:29 +02:00
parent f7ec1fbb5f
commit 08cb9cb52c
4 changed files with 62 additions and 16 deletions

View File

@ -12,7 +12,8 @@
"sexp-diff" "sexp-diff"
"tr-immutable" "tr-immutable"
"typed-map-lib" "typed-map-lib"
"debug-scopes")) "debug-scopes"
"syntax-color-lib"))
(define build-deps '("scribble-lib" (define build-deps '("scribble-lib"
"racket-doc" "racket-doc"
"rackunit-doc" "rackunit-doc"

View File

@ -3,7 +3,9 @@
(require racket/port) (require racket/port)
(provide read-whole-first-line (provide read-whole-first-line
read-syntax-whole-first-line) read-syntax-whole-first-line
narrow-to-one-line
read-line-length)
(define (read-line-length port) (define (read-line-length port)
(let* ([peeking (peeking-input-port port)] (let* ([peeking (peeking-input-port port)]

View File

@ -10,7 +10,8 @@
"../comments/hide-comments.rkt") "../comments/hide-comments.rkt")
(provide meta-read-inside (provide meta-read-inside
meta-read-syntax-inside) meta-read-syntax-inside
get-command-char)
(define (make-at-reader+comments #:syntax? [syntax? #t] (define (make-at-reader+comments #:syntax? [syntax? #t]
#:inside? [inside? #f] #:inside? [inside? #f]
@ -26,7 +27,7 @@
#:unsyntax #f)))) #:unsyntax #f))))
(define (get-command-char rd1) (define (get-command-char rd1)
(define rd1-datum (if (syntax? rd1) (syntax->datum rd1) rd1)) (define rd1-datum (syntax->datum (datum->syntax #f rd1)))
(if (and (pair? rd1-datum) (if (and (pair? rd1-datum)
(keyword? (car rd1-datum)) (keyword? (car rd1-datum))
(= 1 (string-length (keyword->string (car rd1-datum))))) (= 1 (string-length (keyword->string (car rd1-datum)))))

View File

@ -9,7 +9,7 @@ hyper-literate/lang
;; don't use scribble-base-info for the #:info arg, since ;; don't use scribble-base-info for the #:info arg, since
;; scribble/lp files are not directly scribble'able. ;; scribble/lp files are not directly scribble'able.
#:language-info (scribble-base-language-info) #:language-info (scribble-base-language-info)
#:info orig-scribble-base-reader-info ;(wrapped-scribble-base-reader-info) #:info (wrapped-scribble-base-reader-info)
(require "meta-first-line.rkt" (require "meta-first-line.rkt"
(only-in scribble/base/reader (only-in scribble/base/reader
scribble-base-reader-info scribble-base-reader-info
@ -18,20 +18,62 @@ hyper-literate/lang
(define orig-scribble-base-reader-info (define orig-scribble-base-reader-info
(scribble-base-reader-info)) (scribble-base-reader-info))
(require syntax-color/scribble-lexer
syntax-color/racket-lexer
racket/port)
(define (wrapped-scribble-base-reader-info) (define (wrapped-scribble-base-reader-info)
(define (read/at-exp in offset x-mode)
(define-values (mode2 lexr command-char mode)
(apply values x-mode))
(define-values (r1 r2 r3 r4 r5 max-back-up new-mode)
(lexr in offset mode))
(define new-x-mode (list 'main lexr command-char new-mode))
(values r1 r2 r3 r4 r5 max-back-up new-x-mode))
(define (make-lexr command-char)
(make-scribble-inside-lexer #:command-char (or command-char #\@)))
(define (read/options in offset x-mode)
(define-values (line column position) (port-next-location in))
(define-values (mode2 command-char)
(apply values x-mode))
(define-values (txt type paren start end) (racket-lexer in))
;; TODO: limit the number of newlines to a single newline.
(if (and (eq? type 'white-space)
(regexp-match #px"\n" txt))
(values txt type paren start end
0 (list 'main (make-lexr command-char) command-char #f))
(let ()
(define new-command-char
(or command-char
(if (memq type '(comment sexp-comment white-space))
#f
(if (eq? type 'hash-colon-keyword)
(let ([rd (read (open-input-string txt))])
(if (and (keyword? rd)
(= (string-length (keyword->string rd)) 1))
(string-ref (keyword->string rd) 0)
#\@))
#\@))))
(values txt type paren start end
0 (list 'options new-command-char)))))
(lambda (key defval default) (lambda (key defval default)
(case key (case key
[(color-lexer) [(color-lexer)
(let ([lexr (orig-scribble-base-reader-info key defval default)]) (λ (in offset x-mode)
(let ([first? #t]) (define-values (line column position) (port-next-location in))
(λ (in offset mode) (cond
(when first? [(eq? x-mode #f)
(set! first? #f) (read/options in offset (list 'options #f))]
;; TODO: should return (values "#:opt" 'hash-colon-keyword …) for [(eq? (car x-mode) 'options)
;; the options (read/options in offset x-mode)]
(read-syntax-whole-first-line (object-name in) in)) [else
;; Note that offset and mode are optional (read/at-exp in offset x-mode)]))]
(lexr in offset mode))))]
[else [else
(orig-scribble-base-reader-info key defval default)]))) (orig-scribble-base-reader-info key defval default)])))