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 vis = buffers.newvisualizer("lam")
18 -- Symbols that should have a different representation
20 [' '] = {repr = '\\obs '},
21 ['_'] = {repr = '\\_'},
22 ['->'] = {repr = '\\rightarrow'},
23 -- The default * sits very high above the baseline, \ast (u+2217) looks
25 ['*'] = {repr = '\\ast'},
26 ['~'] = {repr = '\\sim'},
30 -- Keywords that should be bold
38 -- See if str starts with a symbol, and return the remaining string and that
39 -- symbol. If no symbol from the table is matched, just returns the first
40 -- character. We can do a lookup directly, since symbols can be different in
41 -- length, so we just loop over all symbols, trying them in turn.
42 local function take_symbol(str)
43 for symbol,props in pairs(symbols) do
44 -- Try to remove symbol from the start of str
45 symbol, newstr = utf.match(str, "^(" .. symbol .. ")(.*)")
47 -- Return this tokens repr, or just the token if it has no
49 res = props.repr or symbol
50 -- Enclose the token in {\style .. }
52 res = "{\\" .. props.style .. " " .. res .. "}"
57 -- No symbol found, just return the first character
58 return utf.match(str, "^(.)(.*)")
61 -- Take a single word from str, if posible. Returns the rest of the string and
63 local function take_word(str)
64 -- A word must always start with a-z (in particular, λ is not a valid
66 res, newstr = utf.match(str, "^([a-zA-Z][%a%d_]+)(.*)")
67 return res, newstr or str
70 -- Tries to match each of the patterns and returns the captures of the first
71 -- matching pattern (up to 5 captures are supported). Returns nil when nothing
73 local function match_mul(str, patterns)
74 for i, pat in ipairs(patterns) do
75 a, b, c, d, e = utf.match(str, pat)
83 -- Find any subscripts in the given word and typeset them
84 local function do_subscripts(word)
85 base, sub = match_mul(res, submatches)
87 word = base .. "\\low{" .. sub .. "}"
88 -- After a word has been used as a base, allow subscripts
89 -- without _, even for non-numbers.
90 if not bases[base] then
91 -- Register that we've added this base
93 -- Add a pattern for this base
94 submatches[#submatches+1] = "^(" .. base .. ")([%a%d,]+)$"
100 function vis.begin_of_display()
101 -- Initially allow subscripts using _ or just appending a number (later,
102 -- we will add extra patterns here.
103 submatches = {"^(.*)_([%a%d,]+)$", "^(.*[^%d])(%d+)$"}
104 -- This stores all the bases we've encountered so far (to prevent
105 -- duplicates). For each of them there will be a pattern in submatches
110 -- Make things work for inline typeing (e.g., \type{}) as well.
111 vis.begin_of_inline = vis.begin_of_display
112 vis.end_of_inline = vis.end_of_display
114 function vis.flush_line(str,nested)
115 local result, state = { }, 0
116 local finish, change = buffers.finish_state, buffers.change_state
117 -- Set the colorscheme, which is used by finish_state and change_state
118 buffers.currentcolors = colors
122 -- See if the next token is a word
123 word, str = take_word(str)
125 if keywords[res] then
126 -- Make all keywords bold
127 word = "{\\bold " .. word .. "}"
129 -- Process any subscripts in the word
130 word = do_subscripts(word)
133 -- The next token is not a word, it must be a symbol
134 symbol, str = take_symbol(str)
137 -- Append the resulting token
138 result[#result+1] = word or symbol
141 state = finish(state, result)
142 buffers.flush_result(result,nested)
145 -- vim: set sw=4 sts=4 expandtab ai: