Parsing lua with Ruby

Im interested in parsing a text file that has a very specific, yet
somewhat complicated (for me) format. Sample attached. Looks something
like this:

BeanCounterDB = {
[“settings”] = {
[“profile.Default”] = {
[“columnsortcurSort”] = 12,
[“util.beancounter.mailrecolor”] = “both”,
[“columnsortcurDir”] = -1,
[“configator.left”] = 282.666712026266,
[“configator.top”] = 651.6666718914553,
},

The file contains a hierarchy of keys and values. Keys look like
“[“string”]”. Values are assigned by “=”. Values can be “{}” or
“”, or or lists of keys/values assignments separated by
“,” and enclosed by “{” and “}” respectively.

This is all very much like xml, just that not sufficiently alike to just
make search and replace and then use an xml parser.

Anyone has any suggestions/pointers what I should do or where I should
look to be able to obtain some sort of datastructure (array of objects,
lists etc.) representing the data in such a file?

Thanks!

NOTE: Attachment contains more data than I put in the post, but still
not a full file, as the only such full file I have is rather large.

On Fri, Jul 3, 2009 at 11:49 AM, Catalin Tilimpea[email protected]
wrote:

 ["configator.left"] = 282.666712026266,
 ["configator.top"] = 651.6666718914553,

},

I wrote this little Treetop based parser for a very small subset of
Lua some time ago. It might help:

http://gist.github.com/140038

Lars C. wrote:

On Fri, Jul 3, 2009 at 11:49 AM, Catalin Tilimpea[email protected]
wrote:

� � �[“configator.left”] = 282.666712026266,
� � �[“configator.top”] = 651.6666718914553,
� �},

I wrote this little Treetop based parser for a very small subset of
Lua some time ago. It might help:

http://gist.github.com/140038

Thanks a bunch. That’s what I’m looking for. However, I can’t require
treetop for some reason. I have even explicitly included the path to
treetop.rb in the path environment var and still no joy (I get “no such
file – treetop”). Any suggestions?

Lars C. wrote:

I wrote this little Treetop based parser for a very small subset of
Lua some time ago. It might help:

http://gist.github.com/140038

Lars,

I’m a maintainer of Treetop. Would you be so kind as to publish
this snippet at http://snippets.dzone.com/tag/Treetop?

Clifford H…

dunno if that’ll be any help, but it looks pretty simmilar to
YAML, here’s a little regex magic to convert it to valid yaml:

http://pastie.org/533386

beware though, that will only work if
(a) the whole file equally well formatted (i.e. no trailing spaces
at the end of lines, no extra spaces between “foo” = “bar” etc.)
(b) you don’t need that " – [1]" stuff at the end of some of the lines
(i didn’t know what that was for)
© indentation is the same for the whole file
(d) you don’t mind that everything is in arrays, even stuff with only
one
entry

Point (a) is pretty relative, the regexps can be easily adjusted to
allow
any
amount of spaces between the different parts and at end of lines.

Disclaimer: I haven’t tested this thoroughly, just ran it over your
snippet.
No guarantees it’ll work with your whole file…

Greetz,
k

On Fri, Jul 3, 2009 at 5:49 AM, Catalin Tilimpea[email protected]
wrote:

Anyone has any suggestions/pointers what I should do or where I should
look to be able to obtain some sort of datastructure (array of objects,
lists etc.) representing the data in such a file?

If you can afford to have Lua installed and execute it to get what you
need, you might consider RubyLuaBridge

http://rubyluabridge.rubyforge.org/

Gregory B. wrote:

On Fri, Jul 3, 2009 at 5:49 AM, Catalin Tilimpea[email protected]
wrote:

Anyone has any suggestions/pointers what I should do or where I should
look to be able to obtain some sort of datastructure (array of objects,
lists etc.) representing the data in such a file?

If you can afford to have Lua installed and execute it to get what you
need, you might consider RubyLuaBridge

http://rubyluabridge.rubyforge.org/

For the whole project I don’t think i can, but maybe for small bits.

Thanks all anyways: combining all the things said in this thread I am
getting started to parse that file.

On Mon, Jul 6, 2009 at 6:18 AM, Gregory B.[email protected]
wrote:

On Fri, Jul 3, 2009 at 5:49 AM, Catalin Tilimpea[email protected] wrote:

Anyone has any suggestions/pointers what I should do or where I should
look to be able to obtain some sort of datastructure (array of objects,
lists etc.) representing the data in such a file?

If you can afford to have Lua installed and execute it to get what you
need, you might consider RubyLuaBridge

http://rubyluabridge.rubyforge.org/

There is also rufus-lua :

http://github.com/jmettraux/rufus-lua/
http://jmettraux.wordpress.com/2009/03/16/ruby-to-lua/

Cheers,

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs