lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

> I am piecing together a large string for evaluation by lua_buffer()
> and would like to include the equivalent of the C preprocessor
> #line directive.  I can easily add special comments to the string,
> and intercept them in a chunk reader, but as far as I can see,
> there is no way to access the lexer state without replacing llex.c.

This seems a reasonable requirement, but it's hard to provide access to
the line number counter in the lexer because it's stored in a private
struct that the chunk reader has not access to. Sorry.

Try adding the (tested!) code below just after the #includes in lparser.c.
This will add line directives in the form "$ NUMBER" with minimal impact
on the core code.

static int luaY_lex(LexState *ls, SemInfo *seminfo)
 int c=luaX_lex(ls,seminfo);
 if (c!='$') return c;
 if (c!=TK_NUMBER) return '$';
 return luaX_lex(ls,seminfo);
#define luaX_lex	luaY_lex

(You can even save those lines to myline.c and add #include "myline.c"
in lparser.c in the proper place. This will allow you to easily patch
future versions, if needed.)