Parsing lua with Ruby

  • Thread starter Catalin Tilimpea
  • Start date
C

Catalin Tilimpea

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
"<string>", or <number> 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.

Attachments:
http://www.ruby-forum.com/attachment/3865/bcnt.txt
 
L

Lars Christensen

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 =3D {
=A0["settings"] =3D {
=A0 =A0["profile.Default"] =3D {
=A0 =A0 =A0["columnsortcurSort"] =3D 12,
=A0 =A0 =A0["util.beancounter.mailrecolor"] =3D "both",
=A0 =A0 =A0["columnsortcurDir"] =3D -1,
=A0 =A0 =A0["configator.left"] =3D 282.666712026266,
=A0 =A0 =A0["configator.top"] =3D 651.6666718914553,
=A0 =A0},

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
 
F

Fabian Streitel

[Note: parts of this message were removed to make it a legal post.]

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)
(c) 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
 
C

Catalin Tilimpea

Lars said:
� � �["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?
 
G

Gregory Brown

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/
 
C

Catalin Tilimpea

Gregory said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top