[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: A string parser like Rexx
- From: Pat Brown <whoopdedo@...>
- Date: Sat, 06 Jun 2015 23:22:25 -0400
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)