lua-users home
lua-l archive

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


> In a recent thread [1], I brought up the concept of a 'begin' keyword
> that can be used to create anonymous, argument-less functions.

Here a token filter in C that does that. Enjoy.

/*
* proxy.c
* lexer proxy for Lua parser -- implements 'begin' as sugar for 'function ()'.
* Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br>
* 02 Oct 2009 23:59:54
* 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)
{
	static int state=0;
	int t;
	if (state==1)  {
		state=2;
		return '(';
	}
	else if (state==2)  {
		state=0;
		return ')';
	}
	t=llex(ls,seminfo);
	if (t==TK_NAME && strcmp(getstr(seminfo->ts),"begin")==0) {
		t=TK_FUNCTION;
		state=1;
	}
	return t;
}

#define llex nexttoken