Help with LPeg

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Help with LPeg

Christopher Tallman
Hi, I'm trying to accomplish a string find/replace on a Lua file,
essentially updating the values stored in a table with new values,
while preserving the structure, layout, and comments of the file (I
don't want to just serialize the table, since that would drop
formatting and comments).

For example, let's say I have a file Lua file containing the following:

<code>
-- Range descriptions
ranges = {
  [1] = {0, 100}, -- primary
  [2] = {101, 500}, -- secondary
  [3] = {500,1000}, -- final range
}
</code>

I'm trying to write a lua program that will read in this file and the
values in the subtables with new values and write the file back out,
preserving the rest of the file.  I'm pretty sure this is a simple
problem to solve with LPeg, but I've never used it before.  I could
probably get this working with plain regex, but I would like to learn
something new in the process and try doing it with LPeg.  Any advice
or pointers on how I can get this to work?  Good resources for someone
unfamiliar with PEG?

Many thanks,
Chris

Reply | Threaded
Open this post in threaded view
|

Re: Help with LPeg

Dirk Laurie-2
2017-05-25 18:36 GMT+02:00 Christopher Tallman <[hidden email]>:

> Hi, I'm trying to accomplish a string find/replace on a Lua file,
> essentially updating the values stored in a table with new values,
> while preserving the structure, layout, and comments of the file (I
> don't want to just serialize the table, since that would drop
> formatting and comments).
>
> For example, let's say I have a file Lua file containing the following:
>
> <code>
> -- Range descriptions
> ranges = {
>   [1] = {0, 100}, -- primary
>   [2] = {101, 500}, -- secondary
>   [3] = {500,1000}, -- final range
> }
> </code>
>
> I'm trying to write a lua program that will read in this file and the
> values in the subtables with new values and write the file back out,
> preserving the rest of the file.  I'm pretty sure this is a simple
> problem to solve with LPeg, but I've never used it before.  I could
> probably get this working with plain regex, but I would like to learn
> something new in the process and try doing it with LPeg.  Any advice
> or pointers on how I can get this to work?  Good resources for someone
> unfamiliar with PEG?

You could try:

https://rawgit.com/dlaurie/lua-notes/master/lpeg-brief.html

Reply | Threaded
Open this post in threaded view
|

Re: Help with LPeg

Luiz Henrique de Figueiredo
In reply to this post by Christopher Tallman
You don't need LPeg for that, gsub does it nicely if the format is fixed:

s = io.read"*a"
io.write(s:gsub("%[(.-)%].-{(.-),%s*(.-)}",function (i,a,b)
                return string.format("[%s] = {%s, %s}",i+10,b,a)
        end)

Reply | Threaded
Open this post in threaded view
|

Re: Help with LPeg

Christopher Tallman
In reply to this post by Dirk Laurie-2
On Thu, May 25, 2017 at 10:00 AM, Dirk Laurie <[hidden email]> wrote:

> 2017-05-25 18:36 GMT+02:00 Christopher Tallman <[hidden email]>:
>> Hi, I'm trying to accomplish a string find/replace on a Lua file,
>> essentially updating the values stored in a table with new values,
>> while preserving the structure, layout, and comments of the file (I
>> don't want to just serialize the table, since that would drop
>> formatting and comments).
>>
>> For example, let's say I have a file Lua file containing the following:
>>
>> <code>
>> -- Range descriptions
>> ranges = {
>>   [1] = {0, 100}, -- primary
>>   [2] = {101, 500}, -- secondary
>>   [3] = {500,1000}, -- final range
>> }
>> </code>
>>
>> I'm trying to write a lua program that will read in this file and the
>> values in the subtables with new values and write the file back out,
>> preserving the rest of the file.  I'm pretty sure this is a simple
>> problem to solve with LPeg, but I've never used it before.  I could
>> probably get this working with plain regex, but I would like to learn
>> something new in the process and try doing it with LPeg.  Any advice
>> or pointers on how I can get this to work?  Good resources for someone
>> unfamiliar with PEG?
>
> You could try:
>
> https://rawgit.com/dlaurie/lua-notes/master/lpeg-brief.html
>

This is more or less what I was hoping to find.  LPeg is something
that's always intrigued me, but it seemed like it has a steep learning
curve to use.

Thanks for the resource!

Reply | Threaded
Open this post in threaded view
|

Re: Help with LPeg

Sean Conner
In reply to this post by Christopher Tallman
It was thus said that the Great Christopher Tallman once stated:

> Hi, I'm trying to accomplish a string find/replace on a Lua file,
> essentially updating the values stored in a table with new values,
> while preserving the structure, layout, and comments of the file (I
> don't want to just serialize the table, since that would drop
> formatting and comments).
>
> For example, let's say I have a file Lua file containing the following:
>
> <code>
> -- Range descriptions
> ranges = {
>   [1] = {0, 100}, -- primary
>   [2] = {101, 500}, -- secondary
>   [3] = {500,1000}, -- final range
> }
> </code>
>
> I'm trying to write a lua program that will read in this file and the
> values in the subtables with new values and write the file back out,
> preserving the rest of the file.  I'm pretty sure this is a simple
> problem to solve with LPeg, but I've never used it before.  I could
> probably get this working with plain regex, but I would like to learn
> something new in the process and try doing it with LPeg.  Any advice
> or pointers on how I can get this to work?  Good resources for someone
> unfamiliar with PEG?

  I have a couple of Github repositories of example LPeg code.  The first is
the code from an LPeg talk I gave at work.  It has several examples of
parsing dates using various features of LPeg:

        https://rawgit.com/dlaurie/lua-notes/master/lpeg-brief.html

  The second repository contains code to parse some Internet formats (email,
IP addresses, JSON files and INI files):

        https://github.com/spc476/LPeg-Parsers

  This should give you a feeling for what LPeg looks like and how one goes
about using it.

  -spc

Reply | Threaded
Open this post in threaded view
|

Re: Help with LPeg

Martin
In reply to this post by Christopher Tallman
On 05/25/2017 09:36 AM, Christopher Tallman wrote:

> Hi, I'm trying to accomplish a string find/replace on a Lua file,
> essentially updating the values stored in a table with new values,
> while preserving the structure, layout, and comments of the file (I
> don't want to just serialize the table, since that would drop
> formatting and comments).
>
> For example, let's say I have a file Lua file containing the following:
>
> <code>
> -- Range descriptions
> ranges = {
>   [1] = {0, 100}, -- primary
>   [2] = {101, 500}, -- secondary
>   [3] = {500,1000}, -- final range
> }
> </code>
>
> I'm trying to write a lua program that will read in this file and the
> values in the subtables with new values and write the file back out,
> preserving the rest of the file.  I'm pretty sure this is a simple
> problem to solve with LPeg, but I've never used it before.  I could
> probably get this working with plain regex, but I would like to learn
> something new in the process and try doing it with LPeg.  Any advice
> or pointers on how I can get this to work?  Good resources for someone
> unfamiliar with PEG?
>
> Many thanks,
> Chris
>

AFAIK there are no currently public tools that can do this. There are
some custom lexers/parsers but all they drop whitespaces.

Some time ago I wrote parser-based Lua code reformatter. Parser can be
tuned to keep comments and whitespeces in returned structure. But your
code for processing such structure will be quite complex.

Probably the simplest and fastest way for your case is just use gsub, as
Luiz recommended.

-- Martin