lua-users home
lua-l archive

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


Here's how I would rewrite the line checking code:

require "lpeg"
local P , R , S , V = lpeg.P , lpeg.R , lpeg.S , lpeg.V
local C , Carg , Cb , Cc , Cg , Ct , Cmt = lpeg.C , lpeg.Carg ,
lpeg.Cb , lpeg.Cc , lpeg.Cg , lpeg.Ct , lpeg.Cmt
local locale = lpeg.locale ( )

local eof = P(-1)
local newline = S"\r\n"
local comment = S"#" * (P(1)-newline)^0
local line = ( comment + P"hooah" ) * newline

local err = Cmt(P(1), function(s, i, t)
	local line = 1
	for _ in s:sub(1, i):gmatch("\n") do
		line = line+1
	end
	error("bad line: "..line)
end)

local patt = line^1 * (eof + err)


print( patt:match[[
#this is a comment
hooah
hooah
hooah
hooah
hooah
This line will error with line number.
#not parsed
]] )