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 joy mondal once stated:
> Hi !
> 
> Lets say you have a language that looks as follows :
> 
> hello =
>   (a,b) ->
>     print 'hello world'
>     1 + 2
> 
> ( It looks a lot like moonscript if that helps )
> 
> hello = \n  (a,b) ->\n    print 'hello world'\n    (1 + 2)\n
> 
> 
> Would the token stream out of your LPEG grammer look ideally like this ?

  Are you asking a particular person or just anyone in general?  

  LPeg doesn't automatically parse text---it's a library of code to generate
code that will parse text.

> [var hello]
> 
> 
> [assign]
> 
> [indent 2]
> 
> [function start a b]
> 
> [indent 4]
> 
> [call print 'hello world']
> 
> [indent 4]
> 
> [call add 1 2]
> 
> [indent 2]

  Yes, the output of an LPeg parser *could* output that, but someone would
have to write the code to do so.  The LPeg I might write might return the
following Lua table:

	{
	  nodetype = 'assignment',
	  to = 'hello',
	  value = 
	  {
	    nodetype = 'function',
	    input = { 'a' , 'b' },
	    code =
	    {
	      {
	        nodetype = 'call',
	        id = 'print',
	        parameters = { 'hello world' }
	      },
	      {
	        nodetype = 'expression',
	        code = 
	        {
	          nodetype = '+',
	          parameters = 
	          {
	            {
	              exprtype = 'immediate',
	              type = 'number',
	              value = 1
	            },
	            {
	              exprtype = 'immediate',
	              type = 'number',
	              value = 2
	            }
	          }
	        }
	      }
	    }
	  }
	}

  It all depends upon the LPeg code.

> One final issue, when using -- lpeg.Ct -- How would I iterate over the
> table since I cannot call .length on it ?

  The table returned from lpeg.Ct() is a Lua table and can be iterated over
like any other Lua table.

	parse = lpeg.Ct(lpeg.C("a")^0)

	x = parse:match "aaaaaaaa"
	print(#x,x)
	for i = 1 , #x do print(x[i]) end

  If you mean "How do I obtain the length of the table returned by lpeg.Ct()
during parsing?" then something like this would work:

	parse = lpeg.Cf(lpeg.Ct"" * lpeg.C"a"^0,function(t,a)
	  if #t < 5 then
	    table.insert(t,a)
	  end
	  return t
	end)

	x = parse:match "aaaaaaaa"
	print(#x,x)
	for i = 1 , #x do print(x[i]) end

  There are other ways to accomplish the same thing, but this is better
served by this:

	parse = lpeg.Ct(lpeg.C"a"^-5) * lpeg.P"a"^0
	
	x = parse:match "aaaaaaaa"
	print(#x,x)
	for i = 1 , #x do print(x[i]) end

where you look for the maximum number of items you want in the table.

  It might help us to know what exactly you are trying to accomplish with
LPeg.

  -spc (And do try the examples and work out how they work)