lua-users home
lua-l archive

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


Pat,

I love it. I've got a Lua for z/OS port and REXX is *the* scripting language of the mainframe. It's tricky for REXXers to learn Lua string patterns when they are so used to parse, which is much simpler for lots of use cases. Does your library handle positional parsing?

 /* absolute positional parsing                                        */
/* 0 1 2 3 4 5 */
 /*    position: 12345678901234567890123456789012345678901234567890123 */
   testString = "Doe       John M.   03/03/65  New York            USA";
parse var testString name1 11 name2 21 birthday 31 town 51 country

It would be nice to be able to pre-compile a template pattern for optimization. For example, parsing a large text file.

rexxparse = require("rexxparse.parse")
c = rexxparse.compile([["Lua" version.]])
result = rexxparse.parse(_VERSION, c)
print(result.version) -- 5.3



On 7/06/2015 11:22 AM, Pat Brown wrote:
Any Rexx fans in the audience? I've always thought the PARSE format was a nice
way to do basic pattern matching without all the complexity of a regex. I've
put together a Lua implementation of the Rexx parser. It's still mostly
experimental, I need more sample strings to test against. If you'd like to try
it out, the work repo is https://github.com/ptbrown/rexxparse-lua

Use it like this:

rexxparse = require("rexxparse.parse")
result = rexxparse.parse(_VERSION, [["Lua" version.]])
print(result.version) -- 5.3

Or that's what it should be, that actually doesn't quite work as described
yet. (It will complain about there not being a space before the dot.) But I'm
getting there. Another way to do it is:

parser = rexxparse.parse[["Lua" version.]]
result = parser(_VERSION)
-- or
result = {}
parser(result, _VERSION)

The last one is for when you have a vref in the pattern. Rexx lets you use the
value of a variable to make a match. Instead of "Lua" you might have the
template [[(interpreter) version]] and it will read the value of (interpreter)
from the same table it stores the result in. And if this is Lua 5.2 or 5.3,
that table can be _ENV.

When using the two-argument form of rexxparse.parse, the table is the third
argument.

And finally, you can have multiple templates like this:

parser = rexxparse.parse[[ match1, match2, match3 ]]
results = parser(argument1, argument2, argument3)

This corresponds to the Rexx command PARSE ARG ... But I added a twist that
Rexx didn't allow. A line break in the string will act the same as a comma,
and all blank lines are ignored. That could also be written:

parser = rexxparse.parse[[
      match1
      match2
      match3
]]

Of course putting commas on each line anyway will be fine.

So as you can see, it's a flexible function that I hope can be as close to the
Rexx command as Lua will allow. With lots of fun bugs to be discovered. Again,
the github URL is https://github.com/ptbrown/rexxparse-lua.

Information about Rexx is at http://www.rexxinfo.org. A description of the
Rexx parser is http://www.rexxla.org/events/2005/presentations/chipd.pdf

And if I may hijack my own post, I just uploaded a module for working with
fixed-width text files. It's called "flatfile" on LuaRocks.

(Pat Brown)