\documentclass{article}
\usepackage{luacode}
\begin{document}
\luaexec{%
local upper = unicode.utf8.upper
local char = unicode.utf8.char
local unpack = unpack or table.unpack
bracket_level = 0
start = true
stop_bracket = false
mytokens = function()
local t = token.get_next()
local command, modifier, cs = unpack(t)
if cs == 0 and command == 11 then
if start or bracket_level > 0 then
print(upper(char(t[2])), t[1], t[3], bracket_level, start)
end
elseif command == 1 then
if not stop_bracket then
bracket_level = bracket_level + 1