# Getting error undefined controlled sequence when trying to make a function of module

In the following code, the function named json_parser is meant to parse the JSON. I had initially kept this function as a separate module named json.lua and required it using:

local json = require("json")


but for some reason, I wanted to pack this in one file and tried to create a function out of it. It worked fine but after making it a function, I am getting an error. I am using this code inside a latex file and running it using lualatex.

    \begin{luacode}
local function json_parser()
local lpeg = assert(require("lpeg"))
local C, Cf, Cg, Ct, P, R, S, V =
lpeg.C, lpeg.Cf, lpeg.Cg, lpeg.Ct, lpeg.P, lpeg.R, lpeg.S, lpeg.V

-- number parsing
local digit    = R"09"
local dot      = P"."
local eE       = S"eE"
local sign     = S"+-"^-1
local mantissa = digit^1 * dot * digit^0 + dot * digit^1 + digit^1
local exponent = (eE * sign * digit^1)^-1
local real     = sign * mantissa * exponent / tonumber

-- optional whitespace
local ws = S" \t\n\r"^0

-- match a literal string surrounded by whitespace
local lit = function(str)
return ws * P(str) * ws
end

-- match a literal string and synthesize an attribute
local attr = function(str,attr)
return ws * P(str) / function() return attr end * ws
end

-- JSON grammar
local json = P{
"object",

value =
V"null_value" +
V"bool_value" +
V"string_value" +
V"real_value" +
V"array" +
V"object",

null_value =
attr("null", nil),

bool_value =
attr("true", true) + attr("false", false),

string_value =
ws * P'"' * C((P'\\"' + 1 - P'"')^0) * P'"' * ws,

real_value =
ws * real * ws,

array =
lit"[" * Ct((V"value" * lit","^-1)^0) * lit"]",

member_pair =
Cg(V"string_value" * lit":" * V"value") * lit","^-1,

object =
lit"{" * Cf(Ct"" * V"member_pair"^0, rawset) * lit"}"
}

return { parse = function(str) return assert(json:match(str)) end }
end

local socket = require("socket")

local json = json_parser()
local file = io.open("sample-true.json")
local start = socket.gettime()
texio.write_nl("Time passed parsing JSON: " .. socket.gettime() - start .. " s\string\n")
file:close()
\end{luacode}


The part in the yellow is the error, that I get. What could be the reason for it?

• Always try to make complete examples so that other people don't have to build the test document themselves (and guess what you have in the preamble). – Ulrike Fischer May 9 at 8:07
• If you are using non-trivial amounts of Lua, it is much better to put it into a separate file and require() that from \directlua. The differences in handling of characters by Lua and TeX make intermixing beyond short fragments painful. – Joseph Wright May 9 at 10:11

The luacode-environment still executes tex-commands and so the \t explodes. Use the luacode* environment:

\documentclass{article}
\usepackage{luacode}

% gives error:
\begin{luacode}
-- \t
\end{luacode}

% works:
\begin{luacode*}
-- \t
\end{luacode*}

\begin{document}

bkbkbk
\end{document}

• It gives an error saying invalid escape sequence near '\s' Is there any other change I could need to make? – Amanda May 9 at 8:38
• \string should be imho \\string. Or perhaps remove that line ... – Ulrike Fischer May 9 at 8:42
• Okay! Could you tell the difference between \begin{luacode} and \begin{luacode*}? Will be helpful, if you could update in the original answer. – Amanda May 9 at 8:44