lua-users home
lua-l archive

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


It was thus said that the Great Pierre-Yves Gérardy once stated:
> This is a partial implemetation of LPeg in pure Lua.
> 
> https://gist.github.com/pygy/5320097
> 
> Captures and locales are not yet implemented, but the rest works quite well.

  [ snip ]

> This should work as expected.
> 
> P"foo"
> P(1)
> P(true)
> P{}
> P(function) -- works as expected, but captures are thrown out.
> B"abc"
> R"az"
> S"abc"
> V"name"
> p1 + p2
> p1 * p2
> pt^n
> -pt
> #pt
> 
> plpeg.print(patern)

  Okay, I was curious enough to try this out.  I was wondering how it
handles P(foo) where foo is a variable of type string, so I whipped this up
(based on some stuff I'm doing at work [1]):

	--lpeg = require "lpeg"
	lpeg = require "PureLPeg"  

	local P = lpeg.P
	local R = lpeg.R

	x = { "foo" , "bar" , "baz" , "foobar" }

	pattern = P": "
	for i = 1 , #x do
	  pattern = pattern * P(x[i]) * P"=" * R"09"^1 * P" "^0
	end
   
	t1 = [[: foo=2 bar=0 baz=34343 foobar=222]]
	t2 = [[: foo=2 bar=r baz=34343 foobar=222]]

	print(pattern:match(t1))
	print(pattern:match(t2))

And the different between the two is striking.  LPeg:

[spc]lucy:/tmp/foo>lua test.lua 
35
nil

PureLPeg:
[spc]lucy:/tmp/foo>lua test.lua 
lua: ./PureLPeg.lua:1101: attempt to compare number with nil
stack traceback:
        ./PureLPeg.lua:1101: in function 'matcher'
        ./PureLPeg.lua:1164: in function '?'
        ./PureLPeg.lua:1146: in function 'matcher'
        ./PureLPeg.lua:1217: in function 'match'
        test.lua:20: in main chunk
        [C]: ?

  Oops.  

  -spc (I hope this report helps ... )

[1]	http://boston.conman.org/2013/04/03.1