parsley parsing question

  • Thread starter Eric S. Johansson
  • Start date

E

Eric S. Johansson

how do you parse multi line text with parsley? here is a work in
progress and I'm trying to figure out why I need to split the text and
process per line vrs all at one go.

thanks for any help.
--- eric

Here's the whole body of code ---------------------------

import parsley
#
# grammar to parse
#
# uses<ws><keyword><argument><eol>
# template<ws><template name>
# returns<ws>(<stdout>|file: <filename>|<storage name>)
# remembers<ws><storage name>

# alt form
# template<ws><template name>[:<storage name>]

# test targets

def do_uses(a,b):
print "do_uses %s - %s -"% (a,b)
def do_returns(a):
print "do_returns %s"% (a)
def do_template(a):
print "do_templates %s"% (a)

# parsleyfied grammar
TF_grammar = r"""
kwToken = (letter|digit|'_')*
uses_statement = 'uses' ws kwToken:kwT ':' anything*:roL '\n'{0,1} ->
do_uses ("".join(kwT), "".join(roL))
returns_statement = 'returns' ws kwToken:kwT '\n'{0,1} ->
do_returns("".join(kwT))
template_statement = 'template' ws kwToken:kwT '\n'{0,1} ->
do_template("".join(kwT))
bow = (uses_statement | returns_statement | template_statement) ws
"""
#
action_table = {
"do_uses": do_uses,
"do_returns": do_returns,
"do_template": do_template,
}

# alt path: split lines and parse them one at time

def run_bot(body_of_text):
"""break up the body of text"""
for i in body_of_text.split("\n"):
if len(i) != 0: # why is this test needed?
x = parsley.makeGrammar(TF_grammar,action_table)
x(i).bow()

xxx="""uses foo: this is some text
returns xyzzy
template templatename
"""
# multi-line solution
x = parsley.makeGrammar(TF_grammar,action_table)
x(xxx).bow()

test line-at-a-time solution
run_bot(xxx)

---------- bad result (multi-line) is -----------

$ python parsleytest.py
do_uses foo - this is some text
returns xyzzy
template templatename
-

-------- good result should be -------

$ python parsleytest.py
do_uses foo this is some text
do_returns xyzzy
do_template template templatename
 
Ad

Advertisements


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

Top