File Line Macros

lua-users home
wiki

Showing revision 4
Here is a simple C token filter that will implement a __FILE__ and __LINE__ macro system. Note that filenames will appear as @test.lua and strings will appear in full. You may want to cook __FILE__ a little, as done in luaU_undump.

/*
* proxy.c
* lexer proxy for Lua parser -- implements __FILE__ and __LINE__
* Luiz Henrique de Figueiredo
* This code is hereby placed in the public domain.
* Add <<#include "proxy.c">> just before the definition of luaX_next in llex.c
*/

#include <string.h>

static int nexttoken(LexState *ls, SemInfo *seminfo)
{
  int t=llex(ls,seminfo);
  if (t==TK_NAME) {
    if (strcmp(getstr(seminfo->ts),"__FILE__")==0) {
      t=TK_STRING;
      seminfo->ts = ls->source;
    }
    else if (strcmp(getstr(seminfo->ts),"__LINE__")==0) {
      t=TK_NUMBER;
      seminfo->r = ls->linenumber;
    }
  }
  return t;
}

#define llex nexttoken

RecentChanges · preferences
edit · history · current revision
Edited September 18, 2007 3:06 am GMT (diff)