Parsing files through LuaTeX

Following the two comments for the actual cases I wrote above, I think the general approach goes like this:

  • If the file format you want to parse is more or less standardized, there usually is already some lua code out there in the wild that can read it and produce a lua table from the file's contents. Otherwise, usually the best solution is to use LPEG to do the same (see links in Aditya's reply).

  • Once that is done, you have to figure out how to feed the file name to the existing (or new lua) code that does the actual parsing, and what the structure is of the returns values.

  • Chances are high that the result of parsing is a Lua table, and you just have to tex.sprint() the bits you want.

For example, here is how the \directlua input for the inilazy module could look:

 \directlua { require ("inilazy")
   local tt = assert (inilazy.get"try.ini")
   for k,v in pairs(tt['graphicx']['keys']) do
     tex.print(k)
   end
 }

One possibility is to use LPEG. ConTeXt uses LPEG (with some enhancements) to

  • parse XML (this is just a wrapper file that loads a bunch of other files),
  • parse bibtex,
  • parse chemical formulas,
  • parse asciimath and calculator math,
  • parse TeX and Metapost for pretty printing (also see other buff-imp-* files).
  • parse CSV
  • parse ini files (third party module).

These files in themselves are not documented. To understand the usage, you will need to read the corresponding *.mkiv file where the lua functions are called.