Optimistic apply method hits wall and can't tell ignorant developer why his grammar is wrong

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

Bug Description

I'm reporting this bug in a form that's very specific to my case, but part of the reason I need to is because the library isn't giving me any feedback about what I'm doing wrong.

This is an example of the syntax, grammar and fail case I'm experiencing:
# --------------------------------------------------------
# START of code
# --------------------------------------------------------
from pymeta.grammar import OMeta

#
# Dictionary object in this namspace that our parsing
# actions will be able to affect
d_store = {}

#
# allows us to create the commands we want access to in a dict.
def cmd_dict(d_store):
    """Declares a bunch of functions in d_store and then
    returns it. Note that the functions may choose to
    affect d_store as they run, also."""

    def call(fn_name, **rest):
        d_store[fn_name](*rest)
        return fn_name

    def log(msg):
        print msg
    d_store['log'] = log

    def tok_chr(a):
        print 'hi, %s!'%a
        call('inc_count')
        return a
    d_store['tok_chr'] = tok_chr

    d_store['count'] = 0
    def inc_count():
        d_store['count'] += 1
    d_store['inc_count'] = inc_count

    return d_store
nspace = cmd_dict(d_store)

#
# Create the (likely flawed) grammar
grammar="""
begin ::= '[' <sketch_rest>
sketch_rest ::= ']' | ( ~(']') <sketch_entry> )*
sketch_entry ::= '(' <delimited_string>:key ',' <delimited_string>:val ')' => log('%s:%s'%(key, val))
delimited_string ::= (<dqstring> | <sqstring>)
dqstring ::= <token '"'> (<escapedChar> | ~('"') <anything>)* <token '"'>
sqstring ::= <token '\''> (<escapedChar> | ~('\'') <anything>)* <token '\''>

escapedChar ::= '\\' ('n' => "\n"
                     |'r' => "\r"
                     |'t' => "\t"
                     |'b' => "\b"
                     |'f' => "\f"
                     |'"' => '"'
                     |'\'' => "'"
                     |'\\' => "\\")

ws ::= ' ' | '\t' | '\n'
ws1 ::= <ws>+:s
ws0 ::= <ws>*:s
"""
g_ob = OMeta.makeGrammar(grammar, nspace, name="Sketch_Grammar")

#
# Feed in the sketch
sketch="[('p', 'Watch these'), ('q', 'And these')]"
g = g_ob(sketch)

#
# Fails here
res_apply = g.apply('begin')
# --------------------------------------------------------
# END of code
# --------------------------------------------------------

The error I get back is this:
$ python test.py
Traceback (most recent call last):
  File "test.py", line 60, in <module>
    g_ob = OMeta.makeGrammar(grammar, nspace, name="Sketch_Grammar")
  File "/usr/lib/python2.5/site-packages/pymeta/grammar.py", line 25, in makeGrammar
    return g.parseGrammar(name, PythonBuilder, cls, globals)
  File "/usr/lib/python2.5/site-packages/pymeta/grammar.py", line 120, in parseGrammar
    res = self.apply("grammar")
  File "/usr/lib/python2.5/site-packages/pymeta/runtime.py", line 168, in apply
    return self._apply(r, ruleName, args)
  File "/usr/lib/python2.5/site-packages/pymeta/runtime.py", line 195, in _apply
    [rule(), self.input])
  File "/pymeta_generated_code/pymeta_grammar__Grammar.py", line 352, in rule_grammar
  File "<string>", line 1, in <module>
  File "/usr/lib/python2.5/site-packages/pymeta/builder.py", line 384, in makeGrammar
    code = compile(source, filename, "exec")
  File "/pymeta_generated_code/pymeta_grammar__Sketch_Grammar.py", line 66
    return self.locals['sqstring'] = _locals
                                   ^
SyntaxError: invalid syntax
$

I completely expect it's because I've done something wrong, but the break is very opaque because of the way pymeta dies.

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.