Let pret-lam recognize the "in" keyword.
[matthijs/master-project/report.git] / pret-lam.lua
1 -- filename : type-lam.lua
2 -- comment  : Pretty printing of (extended) lambda calculus
3 -- author   : Matthijs Kooijman, Universiteit Twente, NL
4 -- copyright: Matthijs Kooijman
5 -- license  : None
6
7 local utf = unicode.utf8
8
9 if not buffers                 then buffers                 = { } end
10 if not buffers.visualizers     then buffers.visualizers     = { } end
11 if not buffers.visualizers.lam then buffers.visualizers.lam = { } end
12
13 buffers.visualizers.lam.colors = {
14     "prettytwo",
15     "prettyone",
16     "prettythree",
17     "prettyfour"
18 }
19
20 buffers.visualizers.lam.tokens = {
21     [' '] = {repr = '\\obs '},
22     ['_'] = {repr = '\\_'},
23     ['->'] = {repr = '\\rightarrow'},
24     ['case'] = {style = 'bold'},
25     ['of'] = {style = 'bold'},
26     ['let'] = {style = 'bold'},
27     ['in'] = {style = 'bold'},
28 }
29
30 function buffers.visualizers.lam.flush_line(str,nested)
31     local result, state = { }, 0
32     local finish, change = buffers.finish_state, buffers.change_state
33     -- Set the colorscheme, which is used by finish_state and change_state
34     buffers.currentcolors = buffers.visualizers.lam.colors
35
36     while str ~= "" do
37         found = false
38         for tok,props in pairs(buffers.visualizers.lam.tokens) do
39             -- Try to remove tok from the start of str 
40             str, count = utf.gsub(str, "^" .. tok, "")
41             if count ~= 0 then
42                 -- Replace the token with the repr, or just itself
43                 result[#result+1] = props.repr or tok
44                 -- Enclose the token in {\style .. }
45                 if props.style then
46                     result[#result] = "{\\" .. props.style ..
47                                         " " .. result[#result] ..
48                                         "}"
49                 end
50                 -- We found a token, now start over
51                 found = true
52                 break
53             end
54         end
55         if not found then
56             result[#result+1] = utf.sub(str, 1, 1)
57             str = utf.sub(str, 2)
58         end
59     end
60     state = finish(state, result)
61     buffers.flush_result(result,nested)
62 end
63
64 -- vim: set sw=4 sts=4 expandtab ai: