lua-users home
lua-l archive

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


> And it seems a perfect job for ltokenp which I've just announced!

The token filter is attached. It transforms

	function f()
	end

	function g() <>
	end

	function h() <a>
	end

	function j() <a,b>
	end
to

	function f ( ) 
	local _=_ENV end

	function g ( ) local _= _ENV 
	end 

	function h ( ) local _= a 
	end 

	function j ( ) local _= a , b 
	end 

--lhf

-- token filter: allow declaration of upvalues

local function emit(...)
	io.write(...)
	io.write(" ")
end

local state=0
local lastline=1

function FILTER(line,token,text,value)
	local t=text
	if t=="<file>" or t=="<eof>" then return end
	while lastline~=line do lastline=lastline+1 emit("\n") end
	if t=="<string>" then value=string.format("%q",value) end
	if state==0 then
		emit(value)
		if t=="function" then
			state=1
		end
	elseif state==1 then
		emit(value)
		if t==")" then
			state=2
		end
	elseif state==2 then
		if t=="<" then
			emit("local _=")
			state=3
		else
			emit("local _=_ENV")
			emit(value)
			state=0
		end
	elseif state==3 then
		if t==">" then
			emit("_ENV")
			state=0
		else
			state=4
			emit(value)
		end
	elseif state==4 then
		if t==">" then
			state=0
		else
			emit(value)
		end
	end
end