[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Question about lstrip
- From: Luiz Henrique de Figueiredo <lhf@...>
- Date: Wed, 12 Sep 2007 16:22:34 -0300
> I have a project that needs the same functionality as lstrip -d,
> except that the input and output need to be strings rather than a file
> and stdout respectively.
If you mean from within Lua, and not as a standalone application, then
the easiest way I think is to use a token filter in C that calls a Lua
callback with whatever representation of each token you need. See below
for an example.
Build Lua with the patched llex.c and then run this test.lua:
function INSPECTOR(line,token,value)
print("INSPECTOR",line,token,value)
end
assert(loadfile"test.lua")
To turn the INSPECTOR off, just set it to nil.
I hope it helps.
--lhf
/*
* proxy.c
* lexer proxy for Lua parser -- implements token inspection
* Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br>
* 12 Sep 2007 16:14:04
* This code is hereby placed in the public domain.
* Add <<#include "proxy.c">> just before the definition of luaX_next in llex.c
*/
#define INSPECTOR "INSPECTOR"
static int nexttoken(LexState *ls, SemInfo *seminfo)
{
lua_State *L=ls->L;
lua_getglobal(L,INSPECTOR);
if (lua_isnil(L,-1))
{
lua_pop(L,1);
return llex(ls,seminfo);
}
else
{
for (;;)
{
int t=llex(ls,seminfo);
lua_pushvalue(L,-1);
lua_pushinteger(L,ls->linenumber);
if (t<FIRST_RESERVED)
{
char s=t;
lua_pushlstring(L,&s,1);
}
else
lua_pushstring(L,luaX_tokens[t-FIRST_RESERVED]);
if (t==TK_NAME || t==TK_STRING)
lua_pushlstring(L,getstr(seminfo->ts),seminfo->ts->tsv.len);
else if (t==TK_NUMBER)
lua_pushnumber(L,seminfo->r);
else
lua_pushnil(L);
lua_call(L,3,0);
if (t==TK_EOS) break;
}
lua_pop(L,1);
return TK_EOS;
}
}
#define llex nexttoken