Module:User:Theknightwho/parser/contextualizer

-- Contexts inform the tokenizer of the immediate assumptions it should make, and each records boolean values for all the base context states listed below. To allow rapid setting and checking, they are stored as numbers (not tables), as tens of thousands of them may be generated during a single tokenization. Ideally they would use bitwise operators (one bit for each context state), but the backported bit32 library is too slow. Instead, each context state is assigned a prime number, and multiple context states can be set by simply multiplying them. This is at least an order of magnitude faster. -- The context state can be checked with the Euclidean greatest common divisor algorithm: if the GCD is >1, then the context state is true. This is particularly useful with aggregate context states (e.g. TEMPLATE), where a GCD >1 means that at least one of the component states is true. local contextualizer = {}

function contextualizer.has(c, cxt) repeat c, cxt = cxt, c % cxt until cxt == 0 return c > 1 end

-- Careful: these have no safety checks against double-(un)setting, to maximise speed. function contextualizer.set(c, cxt) return c * cxt end

function contextualizer.unset(c, cxt) return c / cxt end

-- Includes a safety check against double-(un)setting. These should only be used when a base state could be in either state, which is common when dealing with aggregate contexts. function contextualizer.safe_unset(c, cxt) local orig_c = c	repeat c, cxt = cxt, c % cxt until cxt == 0 return orig_c / c end

function contextualizer.safe_set(c, cxt) return contextualizer.safe_unset(c, cxt) * cxt end

return contextualizer