Skip to content

Commit 907dd27

Browse files
committed
use racket-lexer* when there is no #lang line
closes #13
1 parent 8885933 commit 907dd27

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

syntax-color-lib/syntax-color/module-lexer.rkt

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,26 @@
1010

1111
#|
1212
13-
mode : (or/c #f 'before-lang-line
14-
'no-lang-line
13+
mode : (or/c #f
14+
before-lang-line?
15+
no-lang-line?
1516
(cons lexer mode)
1617
lexer)
1718
1819
the module lexer tracks any white-space and comments before
1920
the #lang line (if any) explicitly by wrapping calls to the
20-
scheme-lexer (in #f or 'before-lang-line mode).
21+
racket-lexer (in #f or before-lang-line mode).
2122
Once it finds a non-white-space and non-comment
2223
token, it checks to see if there is a #lang line and, if so
2324
changes the mode to be the lexer that the #lang indicates,
2425
delegating to it (the last two modes listed above).
2526
If there is no #lang line, then it continues
26-
to delegate to the scheme-lexer (in the 'no-lang-line mode).
27+
to delegate to the racket-lexer (in the no-lang-line mode).
2728
2829
|#
2930

3031
(struct before-lang-line (racket-lexer-mode) #:prefab)
32+
(struct no-lang-line (racket-lexer-mode) #:prefab)
3133
(define (do-module-lexer* in offset mode can-return-attribs-hash? filter-lexer)
3234
(cond
3335
[(or (not mode) (before-lang-line? mode))
@@ -114,17 +116,17 @@ to delegate to the scheme-lexer (in the 'no-lang-line mode).
114116
;; sync ports
115117
(for ([i (in-range (file-position in) (file-position p))])
116118
(read-byte-or-special in))
117-
(values lexeme 'error data new-token-start end-pos 0 'no-lang-line)]
119+
(values lexeme 'error data new-token-start end-pos 0 (no-lang-line #f))]
118120
[else
119121
(for ([i (in-range (file-position in) (file-position lexer-port))])
120122
(read-byte-or-special in))
121123
(values lexeme
122124
(if can-return-attribs-hash? type (attribs->symbol type))
123-
data new-token-start new-token-end 0 'no-lang-line)])])]
124-
[(eq? mode 'no-lang-line)
125-
(let-values ([(lexeme type data new-token-start new-token-end)
126-
(racket-lexer in)])
127-
(values lexeme type data new-token-start new-token-end 0 'no-lang-line))]
125+
data new-token-start new-token-end 0 (no-lang-line #f))])])]
126+
[(no-lang-line? mode)
127+
(define-values (lexeme type data new-token-start new-token-end backup new-mode)
128+
(racket-lexer* in offset (no-lang-line-racket-lexer-mode mode)))
129+
(values lexeme type data new-token-start new-token-end backup (no-lang-line new-mode))]
128130
[(pair? mode)
129131
;; #lang-selected language consumes and produces a mode:
130132
(let-values ([(lexeme type data new-token-start new-token-end backup-delta new-mode)

syntax-color-test/tests/syntax-color/module-lexer.rkt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
rackunit)
66

77
(struct before-lang-line (racket-lexer-mode) #:prefab)
8+
(struct no-lang-line (racket-lexer-mode) #:prefab)
89
(define (lex input count? #:modes [modes (list module-lexer module-lexer*)])
910
(define results
1011
(for/list ([lexer (in-list modes)])
@@ -34,6 +35,7 @@
3435
[(procedure? mode)
3536
`(proc ,(object-name mode))]
3637
[(before-lang-line? mode) 'before-lang-line]
38+
[(no-lang-line? mode) 'no-lang-line]
3739
[(and (pair? mode)
3840
(procedure? (car mode)))
3941
;; a hack: translate 'racket-lexer* shape to 'racket-lexer
@@ -162,6 +164,25 @@
162164
(")" #hash((comment? . #t) (type . parenthesis)) 27 28 (proc racket-lexer))
163165
(,eof eof #f #f (proc racket-lexer))))
164166

167+
;; check sexp comment handling when in before-lang-line mode and in there-is-no-lang-line mode
168+
(check-equal? (lex "#;(a b)\n1\n#;(a b)" #t #:modes (list module-lexer*))
169+
`(("#;" sexp-comment 1 3 #f)
170+
("(" #hash((comment? . #t) (type . parenthesis)) 3 4 before-lang-line)
171+
("a" #hash((comment? . #t) (type . symbol)) 4 5 before-lang-line)
172+
(" " #hash((comment? . #t) (type . white-space)) 5 6 before-lang-line)
173+
("b" #hash((comment? . #t) (type . symbol)) 6 7 before-lang-line)
174+
(")" #hash((comment? . #t) (type . parenthesis)) 7 8 before-lang-line)
175+
("\n" white-space 8 9 before-lang-line)
176+
("1" constant 9 10 before-lang-line)
177+
("\n" white-space 10 11 no-lang-line)
178+
("#;" sexp-comment 11 13 no-lang-line)
179+
("(" #hash((comment? . #t) (type . parenthesis)) 13 14 no-lang-line)
180+
("a" #hash((comment? . #t) (type . symbol)) 14 15 no-lang-line)
181+
(" " #hash((comment? . #t) (type . white-space)) 15 16 no-lang-line)
182+
("b" #hash((comment? . #t) (type . symbol)) 16 17 no-lang-line)
183+
(")" #hash((comment? . #t) (type . parenthesis)) 17 18 no-lang-line)
184+
(,eof eof #f #f no-lang-line)))
185+
165186
(check same?
166187
(lex "#lang at-exp racket/base\n1\n" #t)
167188
`(("#lang at-exp racket/base" other 1 25 #f)

0 commit comments

Comments
 (0)