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