1 -- filename : type-lam.lua
2 -- comment : Pretty printing of (extended) lambda calculus
3 -- author : Matthijs Kooijman, Universiteit Twente, NL
4 -- copyright: Matthijs Kooijman
7 local utf = unicode.utf8
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
13 buffers.visualizers.lam.colors = {
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
27 ['*'] = {repr = '\\ast'},
31 -- Keywords that should be bold
32 buffers.visualizers.lam.keywords = {
39 -- See if str starts with a symbol, and return the remaining string and that
40 -- symbol. If no symbol from the table is matched, just returns the first
41 -- character. 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 function buffers.visualizers.lam.take_symbol(str)
44 for symbol,props in pairs(buffers.visualizers.lam.symbols) do
45 -- Try to remove symbol from the start of str
46 symbol, newstr = utf.match(str, "^(" .. symbol .. ")(.*)")
48 -- Return this tokens repr, or just the token if it has no
50 res = props.repr or symbol
51 -- Enclose the token in {\style .. }
53 res = "{\\" .. props.style .. " " .. res .. "}"
58 -- No symbol found, just return the first character
59 return utf.match(str, "^(.)(.*)")
62 -- Take a single word from str, if posible. Returns the rest of the string and
64 function buffers.visualizers.lam.take_word(str)
65 -- A word must always start with a-z (in particular, λ is not a valid
67 res, newstr = utf.match(str, "^([a-z][%a%d_]+)(.*)")
68 return res, newstr or str
71 -- Tries to match each of the patterns and returns the captures of the first
72 -- matching pattern (up to 5 captures are supported). Returns nil when nothing
74 function buffers.visualizers.lam.match_mul(str, patterns)
75 for i, pat in ipairs(patterns) do
76 a, b, c, d, e = utf.match(str, pat)
84 -- Find any subscripts in the given word and typeset them
85 function buffers.visualizers.lam.do_subscripts(word)
86 local match_mul = buffers.visualizers.lam.match_mul
87 base, sub = match_mul(res, submatches)
89 word = base .. "\\low{" .. sub .. "}"
90 -- After a word has been used as a base, allow subscripts
91 -- without _, even for non-numbers.
92 if not bases[base] then
93 -- Register that we've added this base
95 -- Add a pattern for this base
96 submatches[#submatches+1] = "^(" .. base .. ")([%a%d,]+)$"
102 function buffers.visualizers.lam.begin_of_buffer(type, name)
103 -- Initially allow subscripts using _ or just appending a number (later,
104 -- we will add extra patterns here.
105 submatches = {"^(.*)_([%a%d,]+)$", "^(.*[^%d])(%d+)$"}
106 -- This stores all the bases we've encountered so far (to prevent
107 -- duplicates). For each of them there will be a pattern in submatches
112 function buffers.visualizers.lam.flush_line(str,nested)
113 local result, state = { }, 0
114 local finish, change = buffers.finish_state, buffers.change_state
115 local take_symbol = buffers.visualizers.lam.take_symbol
116 local take_word = buffers.visualizers.lam.take_word
117 local do_subscripts = buffers.visualizers.lam.do_subscripts
118 -- Set the colorscheme, which is used by finish_state and change_state
119 buffers.currentcolors = buffers.visualizers.lam.colors
123 -- See if the next token is a word
124 word, str = take_word(str)
126 if buffers.visualizers.lam.keywords[res] then
127 -- Make all keywords bold
128 word = "{\\bold " .. word .. "}"
130 -- Process any subscripts in the word
131 word = do_subscripts(word)
134 -- The next token is not a word, it must be a symbol
135 symbol, str = take_symbol(str)
138 -- Append the resulting token
139 result[#result+1] = word or symbol
142 state = finish(state, result)
143 buffers.flush_result(result,nested)
146 -- vim: set sw=4 sts=4 expandtab ai: