Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #72393

parsley parsing question

Path csiph.com!usenet.pasdenom.info!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <esj@harvee.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '-----------': 0.05; '"""': 0.07; 'subject:parsing': 0.09; '--------': 0.10; 'subject:question': 0.10; 'python': 0.11; 'def': 0.12; 'template': 0.14; "':'": 0.16; '-------': 0.16; 'path:': 0.16; 'r"""': 0.16; 'remembers': 0.16; 'targets': 0.16; 'trying': 0.19; 'skip:p 40': 0.19; 'split': 0.19; 'help.': 0.21; 'import': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; '(a)': 0.24; 'parse': 0.24; '---': 0.24; '----------': 0.26; "i'm": 0.30; 'code': 0.31; 'lines': 0.31; 'go.': 0.31; 'figure': 0.32; 'text': 0.33; 'skip:d 20': 0.34; 'test': 0.35; 'thanks': 0.36; 'should': 0.36; 'skip:- 20': 0.37; 'to:addr:python-list': 0.38; 'bad': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'here': 0.66; "'returns'": 0.84; 'bow': 0.84
X-Virus-Scanned amavisd-new at harvee.org
Date Mon, 02 Jun 2014 00:16:55 -0400
From "Eric S. Johansson" <esj@harvee.org>
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.5.0
MIME-Version 1.0
To python-list@python.org
Subject parsley parsing question
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.10536.1401683261.18130.python-list@python.org> (permalink)
Lines 84
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1401683261 news.xs4all.nl 2867 [2001:888:2000:d::a6]:33359
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:72393

Show key headers only | View raw


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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

parsley parsing question "Eric S. Johansson" <esj@harvee.org> - 2014-06-02 00:16 -0400

csiph-web