Some times ago I created a similar parser function with LuaLaTeX. I used it to read text files, count and change some chars and put some LaTeX commands into the output.
\documentclass{book}
\usepackage{filecontents}
%create a test text file
\begin{filecontents*}{lorem.txt}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr,
sed diam nonumy eirmod tempor invidunt ut labore et dolore
magna aliquyam erat, sed diam voluptua. At vero eos et accusam
et justo duo dolores et ea rebum.
\end{filecontents*}
%create a lua script file
\begin{filecontents*}{luaFunctions.lua}
function createReplaceTable()
replaceTable = {}
-- create a table with all ASCII chars
-- the name and(!) the value of each table item is the ASCII char
-- this is important if the char shouldn't be replaced
-- the table have 128 items each filled with the corresponding char
for i = 1, 128, 1 do
replaceTable[string.char(i-1)] = string.char(i-1)
end
end
function parseString(input)
outputString = ""
-- for each char in the given string we replace
-- the char with the content of the table item
-- because the table items have the same name like the chars
-- we have access to the table item via the given char
for i = 1, string.len(input) do
char = input:sub(i, i)
outputString = outputString..replaceTable[char]
end
tex.print(outputString)
end
function parseFile(fileName)
-- open file
local input = io.open('lorem.txt', 'r')
-- parse each line
for line in input:lines() do
parseString(line)
end
end
function fillReplaceTable()
-- here we fill/override the replacements for each ASCII char
replaceTable["L"] = "\\textbf{\\large L}\\marginpar{\\tiny 'L'(\\stepcounter{counterForL}\\#\\thecounterForL)}"
replaceTable["o"] = "\\underline{o}"
replaceTable["e"] = ""
end
\end{filecontents*}
% read the external lua file to declare the functions,
% but without execute the Lua commands and functions
\directlua{dofile("luaFunctions.lua")}
%create and fill the tables
\directlua{createReplaceTable()}
\directlua{fillReplaceTable()}
% latex commands to execute the lua functions
\def\parseString#1{\directlua{parseString("#1")}}
\def\parseFile#1{\directlua{parseFile("#1")}}
%counter for the letter 'L'
\newcounter{counterForL}
\begin{document}
\parseString{%
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.%
}
\parseFile{lorem.txt}
\end{document}

Ahasn't been defined using\pgfparserdefand leads to errors. – yannisl Feb 24 '12 at 21:02pgf? – Ahmed Musa Feb 24 '12 at 22:04\meaning(it is rather tricky to get). I wonder if the parser doesn't accept spaces correctly? – Ryan Reich Feb 24 '12 at 22:21\meaningof a blank space is a blank, but sure it does not get accepted. – yannisl Feb 25 '12 at 04:28\meaningof a blank space isblank space(with two trailing spaces, hence the difficulty to get it into TeX). – Bruno Le Floch Feb 25 '12 at 04:48<catcode description><space><character>. For instance,the letter<space>A, or, say for a [catcode=10, charcode=64] token,blank space<space>@. To see that, try\def\showmeaning#1{\showtokens\expandafter{\meaning#1}}\lccode32=64\lowercase{\showmeaning{ }}. – Bruno Le Floch Feb 25 '12 at 07:36