Cut operator "error on fail"

Bug #569878 reported by Cory Dodt
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
PyMeta
New
Undecided
Unassigned

Bug Description

Given the following grammar, I need a way to mark that beginning a block without ending it is an error. In http://simpleparse.sourceforge.net/simpleparse_grammars.html#Modifiers the "cut" is described as one way to do this. After a <cut> terminal, any token that is not matched is a syntax error, propagated to the top. Here's an example where you'd need it:

    from pymeta.grammar import OMeta

    g = """
    brace1 ::= <token '{'>
    brace2 ::= <token '}'>
    bracket1 ::= <token '['>
    bracket2 ::= <token ']'>

    braceBlock ::= <brace1> <blockContent "}">:x <brace2> => x
    bracketBlock ::= <bracket1> <blockContent "]">:x <bracket2> => x
    block ::= (<braceBlock>|<bracketBlock>)
    junk :closer ::= (~<exactly closer> <anything>)+:t => ''.join(t)
    blockContent :closer ::= (<block>|<junk closer>)*

    file ::= <block>+:b
    """

    testOK = "{ asdf { blah [ blah [ blah ] {whatever} ] } } [ etc. ]"
    testImproper = "{ one [ two } { etc. }"

    Blocks = OMeta.makeGrammar(g, {}, "Blocks")

    print Blocks(testOK).apply("file")
    print Blocks(testImproper).apply("file")

Run the above. The second test contains an improperly closed block. Notice that the second print statement prints out: [[' one [ two '], [' etc. ']]
The first parsed token contains an opening "[". But I want this to be an error.

The <cut> concept provides an easy way to do that. If I could do something like this:

    bracketBlock ::= <bracket1> <cut> <blockContent "]">:x <bracket2> => x

then an opening bracket that is not eventually followed by a matching closing bracket (before its enclosing syntax ends) is an error.

Revision history for this message
Cory Dodt (corydodt) wrote :

Well this test isn't set up quite right because testOK doesn't produce the right output. I can fix this up if you need a better test to get the idea.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.