+-- Keywords that should be bold
+local keywords = {
+ ['case'] = {},
+ ['of'] = {},
+ ['let'] = {},
+ ['letrec'] = {},
+ ['letnonrec'] = {},
+ ['in'] = {},
+ ['DEFAULT'] = {small = true},
+}
+
+local in_block = 0
+local submatches = {}
+local bases = {}
+-- Store the last line for each indent level
+local indentlines = {}
+
+function array_concat(a1, a2)
+ local res = a1
+ for k,v in ipairs(a2) do
+ table.insert(res, v)
+ end
+ return res
+end
+
+
+-- See if str starts with a symbol, and return the remaining string and that
+-- symbol. If no symbol from the table is matched, just returns the first
+-- character. We can do a lookup directly, since symbols can be different in
+-- length, so we just loop over all symbols, trying them in turn.
+local function take_symbol(str)
+ for i,props in ipairs(symbols) do
+ -- Try to remove symbol from the start of str
+ symbol, newstr = utf.match(str, "^(" .. props.symbol .. ")(.*)")
+ if symbol then
+ -- Return this tokens repr, or just the token if it has no
+ -- repr.
+ res = props.repr or symbol
+ -- Enclose the token in {\style .. }
+ if props.style then
+ res = "{\\" .. props.style .. " " .. res .. "}"
+ end
+ return res, newstr
+ end
+ end
+ -- No symbol found, just return the first character
+ return utf.match(str, "^(.)(.*)")
+end
+
+-- Take a single word from str, if posible. Returns the rest of the string and
+-- the word taken.
+local function take_word(str)
+ -- A word must always start with a-z (in particular, λ is not a valid
+ -- start of a word). A word must always end with a letter or a digit
+ res, newstr = utf.match(str, "^([a-zA-Z][%a%d%+%-%,_]*[%a%d]+)(.*)")
+ if not res then
+ -- The above does not catch single letter words
+ res, newstr = utf.match(str, "^([a-zA-Z])(.*)")
+ end
+ return res, newstr or str
+end
+
+-- Tries to match each of the patterns and returns the captures of the first
+-- matching pattern (up to 5 captures are supported). Returns nil when nothing
+-- matches.
+local function match_mul(str, patterns)
+ for i, pat in ipairs(patterns) do
+ a, b, c, d, e = utf.match(str, pat)
+ if a then
+ return a, b, c, d, e
+ end
+ end
+ return nil
+end
+
+-- Find any subscripts in the given word and typeset them
+local function do_subscripts(word)
+ base, sub = match_mul(res, submatches)
+ if sub then
+ word = base .. "\\low{" .. sub .. "}"
+ -- After a word has been used as a base, allow subscripts
+ -- without _, even for non-numbers.
+ if not bases[base] then
+ -- Register that we've added this base
+ bases[base] = true
+ -- Add a patterns for this base. First, the base with a single
+ -- letter or number subscript.
+ submatches[#submatches+1] = "^(" .. base .. ")([%a%d])$"
+ -- Seconde, the base with a longer prefix that includes at least
+ -- one of +-, (to catch things like ri+1, but not return).
+ submatches[#submatches+1] = "^(" .. base .. ")([%a%d]*[%-%+,]+[%a%d%-%+,]*)$"
+ end
+ end
+ return word
+end
+
+-- Do proper aligning for subsequent lines. For example, in
+-- foo = bar
+-- | baz
+-- We replace the spaces in the second line with a skip with the same with as
+-- "foo ", to align the | with the =.
+-- For this, we keep a table "indentlines", which contains all previous lines
+-- with smaller indent levels that are still "in scope" (e.g., have not yet
+-- been followed by a line with a smaller indent level). For example:
+-- line1
+-- line2
+-- line3
+-- line4
+-- line5
+-- After the last line, the table will contain:
+-- { 0 = "line1", 2 = " line4", 4 = " line5"}
+-- In other words, line3 is no longer in scope since it is "hidden" by
+-- line4, and line is no longer in scope since it is replaced by line4.
+local function do_indent(line)
+ newind, rest = utf.match(line, '^(%s*)(.*)')
+ prev = -1
+ -- Loop all the previous lines
+ for indent, unused in pairs(indentlines) do
+ if indent > #newind then
+ -- Remove any lines with a larger indent
+ indentlines[indent] = nil
+ elseif indent < #newind and indent > prev then
+ -- Find the last line (e.g, with the highest indent) with an
+ -- indent smaller than the new indent. This is the line from which
+ -- we need to copy the indent.
+ prev = indent
+ end
+ end
+
+ -- Always store this line, possibly overwriting a previous line with the
+ -- same indent
+ indentlines[#newind] = line