Discussion:
[Pyparsing] [Fwd: 1. .suppress() does not work; 2. a new API call suggestion]
Victor Porton
2015-07-27 21:31:21 UTC
Permalink
Probably it is a bug in pyparsing, but most probably is my
misunderstanding.

Note that it uses modified pyparsing with new method .addCondition()
(attached).

When I run my script (attached):

$ ./DuplicateRefs.py chap-filt.lyx

it produces output like:

['name "chap-filt"']

In my opinion, it should instead produce

['chap-filt']

because I use .suppress() in my code.

Sorry, that I package the data in a separate file, not in a string, but
the real example file is long.

What is wrong? How to make it to produce only label name (like 'chap
-filt'), not like ['name "chap-filt"']?

Additional issue:

Because of peculiarity of the syntax of the .lyx file (attached) I
analyze, I first split it into tokens and then parse the tokens
themselves (with another parser). See for example:

LabelNameLineParser = \
pyparsing.Keyword("name").suppress() + pyparsing.White("
").suppress() + \
pyparsing.Literal('"').suppress() + pyparsing.CharsNotIn('"') +
pyparsing.Literal('"').suppress()

LabelNameLine = Line.copy().addCondition(lambda self, loc, toks:
LabelNameLineParser.parseString(toks[0], True))

Maybe, we should introduce a shorter API for tasks like this? (I am
unsure whether this situation is often enough to deserve a special
API.) What is your opinion? What if I will write a patch which does
this? will you use it?
--
Victor Porton - http://portonvictor.org
Paul McGuire
2015-07-28 12:40:42 UTC
Permalink
Since you are using LabelNameLineParser as a more complete parser of the results from LabelNameLine, you can't add the lambda as a condition (which only evaluates whether the condition holds, but does *not* modify the parsed results), you have to add it as a parse action. If you use the LabelNameLineParser in a parse action, then the results of the more complex parser will be returned - addCondition just raises or doesn't raise ParseException.

Change this line:

LabelNameLine = Line.copy().addCondition(lambda self, loc, toks: LabelNameLineParser.parseString(toks[0], True))

To:

LabelNameLine = Line.copy().addParseAction(lambda self, loc, toks: LabelNameLineParser.parseString(toks[0], True))


With this change, I get these results:

['chap-filt']
['filt-eq-char']
['fmslat-filt']
['fmslat-two']
['fmslat-one']
['up-straight']
['thm1:prim-exists']
['free-alt-star']
['free-alt-eq']
['free-alt-impl']
['two-diags']
...

-- Paul



-----Original Message-----
From: Victor Porton [mailto:***@narod.ru]
Sent: Monday, July 27, 2015 4:31 PM
To: pyparsing-***@lists.sourceforge.net
Subject: [Pyparsing] [Fwd: 1. .suppress() does not work; 2. a new API call suggestion]

Probably it is a bug in pyparsing, but most probably is my misunderstanding.

Note that it uses modified pyparsing with new method .addCondition() (attached).

When I run my script (attached):

$ ./DuplicateRefs.py chap-filt.lyx

it produces output like:

['name "chap-filt"']

In my opinion, it should instead produce

['chap-filt']

because I use .suppress() in my code.

Sorry, that I package the data in a separate file, not in a string, but the real example file is long.

What is wrong? How to make it to produce only label name (like 'chap -filt'), not like ['name "chap-filt"']?

Additional issue:

Because of peculiarity of the syntax of the .lyx file (attached) I analyze, I first split it into tokens and then parse the tokens themselves (with another parser). See for example:

LabelNameLineParser = \
pyparsing.Keyword("name").suppress() + pyparsing.White("
").suppress() + \
pyparsing.Literal('"').suppress() + pyparsing.CharsNotIn('"') +
pyparsing.Literal('"').suppress()

LabelNameLine = Line.copy().addCondition(lambda self, loc, toks:
LabelNameLineParser.parseString(toks[0], True))

Maybe, we should introduce a shorter API for tasks like this? (I am unsure whether this situation is often enough to deserve a special
API.) What is your opinion? What if I will write a patch which does this? will you use it?

--
Victor Porton - http://portonvictor.org


---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus


------------------------------------------------------------------------------
Loading...