File Line Macros

lua-users home

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) {
      seminfo->ts = ls->source;
    else if (strcmp(getstr(seminfo->ts),"__LINE__")==0) {
      seminfo->r = ls->linenumber;
  return t;

#define llex nexttoken

An equivalent pure Lua code could be:

function __FILE__() return debug.getinfo(2,'S').source end
function __LINE__() return debug.getinfo(2, 'l').currentline end

RecentChanges · preferences
edit · history
Last edited September 18, 2007 3:11 am GMT (diff)