Make pret-lam remember subscripted words.
[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.symbols = {
21     [' '] = {repr = '\\obs '},
22     ['_'] = {repr = '\\_'},
23     ['->'] = {repr = '\\rightarrow'},
24     -- The default * sits very high above the baseline, \ast (u+2217) looks
25     -- better.
26     ['*'] = {repr = '\\ast'},
27 }
28
29 buffers.visualizers.lam.keywords = {
30     ['case'] = {},
31     ['of'] = {},
32     ['let'] = {},
33     ['in'] = {},
34 }
35
36
37 function buffers.visualizers.lam.take_symbol(str)
38     -- See if str starts with a symbol, and return the remaining
39     -- string and that symbol. If no symbol from the table is matched,
40     -- just returns the first character.
41     -- We can do a lookup directly, since symbols can be different in
42     -- length, so we just loop over all symbols, trying them in turn.
43     for symbol,props in pairs(buffers.visualizers.lam.symbols) do
44         -- Try to remove symbol from the start of str 
45         symbol, newstr = utf.match(str, "^(" .. symbol .. ")(.*)")
46         if symbol then
47             -- Return this tokens repr, or just the token if it has no
48             -- repr.
49             res = props.repr or symbol
50             -- Enclose the token in {\style .. }
51             if props.style then
52                 res = "{\\" .. props.style ..  " " .. res ..  "}"
53             end
54             return res, newstr
55         end
56     end
57     -- No symbol found, just return the first character
58     return utf.match(str, "^(.)(.*)")
59 end
60
61 -- Take a single word from str, if posible. Returns the rest of the string and
62 -- the word taken.
63 function buffers.visualizers.lam.take_word(str)
64         res, newstr = utf.match(str, "^(%a[%a%d_]+)(.*)")
65         return res, newstr or str
66 end
67
68 -- Tries to match each of the patterns and returns the captures of the first
69 -- matching pattern (up to 5 captures are supported). Returns nil when nothing
70 -- matches.
71 function buffers.visualizers.lam.match_mul(str, patterns)
72     for i, pat in ipairs(patterns) do
73         a, b, c, d, e = utf.match(str, pat)
74         if a then
75             return a, b, c, d, e
76         end
77     end
78     return nil
79 end
80
81 function buffers.visualizers.lam.begin_of_buffer()
82     -- Initially allow subscripts using _ or just appending a number (later,
83     -- we will add extra patterns here.
84     submatches = {"^(.*)_([%a%d,]+)$", "^(.*[^%d])(%d+)$"}
85     -- This stores all the bases we've encountered so far (to prevent
86     -- duplicates). For each of them there will be a pattern in submatches
87     -- above.
88     bases = {}
89 end
90
91 function buffers.visualizers.lam.end_of_buffer()
92     -- Reset submatches and bases, since flush_line can be called without
93     -- begin / end_of_buffer for \type.
94     buffers.visualizers.lam.begin_of_buffer()
95     bases = nil
96 end
97
98 function buffers.visualizers.lam.do_subscripts(word)
99     local match_mul = buffers.visualizers.lam.match_mul
100     base, sub = match_mul(res, submatches)
101     if sub then
102         word = base .. "\\low{" .. sub .. "}"
103         -- After a word has been used as a base, allow subscripts
104         -- without _, even for non-numbers.
105         if bases and not bases[base] then
106             -- Register that we've added this base
107             bases[base] = true
108             -- Add a pattern for this base
109             submatches[#submatches+1] = "^(" .. base .. ")([%a%d,]+)$"
110         end
111     end
112     return word
113 end
114
115 function buffers.visualizers.lam.flush_line(str,nested)
116     local result, state = { }, 0
117     local finish, change = buffers.finish_state, buffers.change_state
118     local take_symbol = buffers.visualizers.lam.take_symbol
119     local take_word = buffers.visualizers.lam.take_word
120     local do_subscripts = buffers.visualizers.lam.do_subscripts
121     -- Set the colorscheme, which is used by finish_state and change_state
122     buffers.currentcolors = buffers.visualizers.lam.colors
123     while str ~= "" do
124         local found = false
125         local word, symbol
126         -- See if the next token is a word
127         word, str = take_word(str)
128         if word then
129             if buffers.visualizers.lam.keywords[res] then
130                 -- Make all keywords bold
131                 word = "{\\bold " .. word ..  "}"
132             else
133                 -- Process any subscripts in the word
134                 word = do_subscripts(word)
135             end
136         else
137             -- The next token is not a word, it must be a symbol
138             symbol, str = take_symbol(str)
139         end
140
141         -- Append the resulting token
142         result[#result+1] = word or symbol
143     end
144
145     state = finish(state, result)
146     buffers.flush_result(result,nested)
147 end
148
149 -- Call end_of_buffer once to set up submatches (since \type doesn't call
150 -- begin_of_buffer / end_of_buffer).
151 buffers.visualizers.lam.end_of_buffer()
152
153 -- vim: set sw=4 sts=4 expandtab ai: