lua-users home
lua-l archive

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


2012/1/7 Gavin Wraith <gavin@wra1th.plus.com>:
> In message
> <CACh33Fo+K0Hyvt_xcDVhaM3txYZ+=LO8Ta3EUE+1aTikTOdygQ@mail.gmail.com>
> you wrote:
>
>> Take a look at the Lua Parser on [1].
>> [1] http://lua-users.org/wiki/LpegRecipes
>
> Thanks.
>
>> Some other changes you can do to make the code as compact as possible:
>
> I am not too concerned about compactness. More interesting to my mind
> is the choice of representation of parse tree that lpeg.match
> should return.
>
> Just for reference here is my (no doubt buggy) code - it uses
> Peter Shook's patch, '\' for 'function' and '=>' for 'return'.
> The source to be analysed is held in the file named by arg[1].
>
>  #! lua
>  -- strip out comments from Lua source
>  local S,P,Cg,C,Cmt,Cb,Ct,match in lpeg
>  local ret = P "\n"
>
>  local shortstring
>  local squote, dquote = P "'", P '"'
>  do
>   local slash, blank = P "\\", ret + " "
>   local z = slash*"z"*( blank^0 )
>   local esc = slash*(1 - blank)
>   local sq = squote*(z + esc + 1 - (squote + slash + ret))^0*squote
>   local dq = dquote*(z + esc + 1 - (dquote + slash + ret))^0*dquote
>   shortstring = C(sq + dq)
>  end
>
>  local longstring
>  do
>   local bra,ket,eqs= P "[",P "]",(P "=")^0
>   local open = bra*Cg(eqs,"init")*bra*ret^(-1)
>   local close = ket*C(eqs)*ket
>   local closeeq = Cmt(close*(Cb "init"),\(s,i,a,b) => a==b end)
>   longstring = C(open*((1-closeeq)^0)*close)/(\(s,_) => s end)
>  end
>
>  local anystring = shortstring + longstring
>
>  local strip
>  do
>   local mm = P "--"
>   local comment = mm*(longstring + C((1 - ret)^0))/\(...) => "" end
>   local notcomment = (anystring + C(1 - squote - dquote - mm))^1
>   strip = Ct((comment + notcomment)^0)/table.concat
>  end
>
>  local f = io.open(arg[1],"r")
>  local s = f:read "*a"
>  f:close()
>
>  print(strip:match(s))
>
> I think I know what the right mathematical definition of subgrammar
> is now. But I do not want to inflict 2-categories on this list ;)
> --
> Gavin Wraith (gavin@wra1th.plus.com)
> Home page: http://www.wra1th.plus.com/
>

Only curious, How do you implement local ... in structure? just a code
filter or a patch to Lua 5.2?