lua-users home
lua-l archive

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


So, how about making it into lua's C API?just you can process lua
tokens in a standard way, and open a door for in-place
meta-programming?

2012/3/11 Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br>:
>> so if  Lua can provider a oken-filter
>
> Adding a token filter to Lua 5.2 remains just as easy as it was in Lua 5.1:
>
> 1. get a copy of llex.c.
> 2. add <<#include "proxy.c">> just before the definition of luaX_next in llex.c.
> 3. write a filter in proxy.c. It can be one that does simple token shuffling
>   or a generic one that calls Lua. Token filters written in C tend to be
>   easy to get right for simple tasks such as say allowing reserved words
>   as field names. See the code below.
> 4. rebuild Lua and enjoy.
>
> /*
> * proxy.c
> * lexer proxy for Lua parser -- allows reserved words as field names
> * Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br>
> * 18 Nov 2010 14:59: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 LAST_RESERVED TK_WHILE
>
> static int nexttoken(LexState *ls, SemInfo *seminfo)
> {
>  static int last=TK_EOS;
>  int t=llex(ls,seminfo);
>  if (last=='.' && t>=FIRST_RESERVED && t<=LAST_RESERVED) {
>        return TK_NAME;
>  }
>  last=t;
>  return t;
> }
>
> #define llex nexttoken
>