To preserve the original text that matches an expression in your parser, try
attaching the parse action keepOriginalText.
nC = p.Combine(nE).setParseAction(p.keepOriginalText)
-- Paul
-----Original Message-----
From: pyp...@li...
[mailto:pyp...@li...] On Behalf Of
stefaan.himpe
Sent: Thursday, May 01, 2008 6:23 AM
To: pyp...@li...
Subject: [Pyparsing] SkipToMatching?
Hello,
I am trying to parse the following:
SECTION {
...
}
where the ... stands for C++ code. I would like to extract the raw C++ code
from this section { }. Problem of course is that the C++ code can also
contain { and }.
I have tried using nestedExpr, but I cannot seem to get raw C++ code with
its original white space (or even with a whitespace that keeps the code
compilable).
The code below illustrates my latest attempt (I left out the "SECTION"
as it is not relevant for the question)
import pyparsing as p
s = """
{
// {
if (1) { dosomething; } else { if (0) then { a; } else { b } }
printf ( "{" ); /* {
{
{
*/
{ nog; iets; }
}
"""
nE =
p.nestedExpr("{","}",ignoreExpr=p.quotedString|p.cppStyleComment|p.cStyleCom
ment)
nC = p.Combine(nE)
r = nC.parseString(s)
The "Combine" more or less returns the contents between the outer { and },
but everything is glued together without whitespace, and some { and }
have disappeared:
['// {if(1)dosomething;elseif(0)thena;elsebprintf("{");/* { \n {\n {\n
*/nog;iets;']
Am I going this the right way?
Would it be possible to add something like a SkipToMatching function
instead?
I imagine I could use it as follows:
import pyparsing as p
grammar = p.Literal("{").suppress() + \
p.SkipToMatching("{","}",ignoreExpr=p.quotedString|p.cppStyleComment|p.cStyl
eComment).setResultsName("RawCode")
Best regards,
Stefaan.
-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
Don't miss this year's exciting event. There's still time to save $100.
Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javao
ne
_______________________________________________
Pyparsing-users mailing list
Pyp...@li...
https://lists.sourceforge.net/lists/listinfo/pyparsing-users
|