# Reflection in ConTeXt

The answer to ConTeXt: avoid enumeration items across pages was a nice introduction to modifying TeX before rendering, like HTML with JavaScript.

``````\startsetups [userdata:itemize]
\setbox\scratchbox\vbox {
\blank[disable]
\startitem
\getinlineuserdata
\stopitem
}
\ifdim\ht\scratchbox>\textheight
\page\unvbox\scratchbox
\else
\box\scratchbox
\fi
\stopsetups
``````

Is more advanced reflection possible? For example, is it possible to iterate over the contents of `\getinlineuserdata`? Is it possible to iterate with an index? Is it possible to iterate only over matching items - blocks or environments or groups or whatever the answer to the question ConTeXt: Block vs Environment vs Group is?

• You could use the Lua interface to get a deeper level (up to the characters or nodes respectively if you want). – TeXnician Dec 5 '18 at 21:01

You can retrieve the buffer contents in Lua an then manipulate them as you like. Here for example I replace `zapf` with `knuth` to typeset a different sample file.

``````\defineuserdataalternative
[itemize]
[renderingsetup=userdata:itemize]

\startsetups [userdata:itemize]
\startluacode
local text = buffers.raw("userdata")
text = string.gsub(text, "zapf", "knuth")
buffers.assign("userdata", text)
\stopluacode
\startitem
\getinlineuserdata
\stopitem
\stopsetups

\defineuserdata [item] [alternative=itemize]

\starttext

\startitemize
\startuserdata [item]
\samplefile{zapf}
\stopuserdata
\stopitemize

\stoptext
``````
• I was hoping it would be possible to access the buffer as a pre-parsed AST rather than as raw text, i.e. to iterate over environments. – user19087 Dec 6 '18 at 5:04
• @user19087 TeX doesn't have an AST because it cannot be expressed as a context-free grammar. – Henri Menke Dec 6 '18 at 7:55
• Oh, that's unfortunate. Is there actually any benefit to not being context-free? I for one do not enjoy the vague error messages. That said some unrestricted and context-sensitive grammars can be expressed as trees (or graphs). Whether or not TeX can, there must be some internal data organization: nodes representing words, lines, paragraphs and sections, or groups? – user19087 Dec 6 '18 at 17:39
• @user19087 TeX is open source, you can look all that up. And yes, there is some kind of internal data organization that does turn input into output, but note that TeX does not scan the whole document first, but sequentially turns it into a compiled result. Aside: One of the reasons why TeX is not context-free is its nature as macro expansion language with concepts like category codes etc. Surely, you could express TeX's grammar as some tree, but that would not be a meaningful one (btw: it has been asked before). – TeXnician Dec 8 '18 at 8:21