The best kittens, technology, and video games blog in the world.

Wednesday, May 30, 2007

Token-based syntax highlighter for RLisp

One, two, three.... by cadmanof50s from flickr (CC-BY)

One of the most important parts of language infrastructure is a good syntax highlighter. I'm not claiming the highlighter I just committed to the repository is any good - it simply reuses the lexer, and outputs tokens in appropriate <span style='color: #00FF00'>...</span>. Making it use CSS classes would be nicer, but then I wouldn't be able to use it on my blog :-)

A more serious syntax highlighter should at least recognize what's quoted and what's not, which symbols are standard macros and control structures, and so on. Still, it's much better to have some syntax highlighting than none.

#!/home/taw/everything/rlisp/trunk/rlisp

(require "rlunit.rl")

; It's much easier to temporarily replace (gensym)
; with a mock one than to design an assertion system
; which can handle the real one.
(defun start-readable-gensym ()
(let sid 0)
(set! gensym
(fn ()
(let sym [(+ "tmp-" [sid to_s]) to_sym])
(set! sid (+ 1 sid))
sym
)
)
)
(defun restore-correct-gensym ()
(set! gensym (fn () (ruby-eval "$gensym||=0; $gensym+=1; ('#:G' + $gensym.to_s).to_sym")))
)

(test-suite Readable_Gensym
(test gensym
(start-readable-gensym)
(assert 'tmp-0 == (gensym))
(assert 'tmp-1 == (gensym))
(assert 'tmp-2 == (gensym))
(restore-correct-gensym)
(assert [(gensym) to_s] =~ /\A#:G\d+\Z/)
(start-readable-gensym)
(assert 'tmp-0 == (gensym))
(assert 'tmp-1 == (gensym))
(start-readable-gensym)
(assert 'tmp-0 == (gensym))
(restore-correct-gensym)
(assert [(gensym) to_s] =~ /\A#:G\d+\Z/)
(assert (gensym) != (gensym))
)
(method teardown () (restore-correct-gensym))
)

No comments: