sighingnow/parsec.py

the examples jsonc.py and sexpr.py work only under python3

perlawk opened this issue · 6 comments

Since there are return statment in the generator code of some functions like:
@lexeme
@generate
def quoted():
'''Parse quoted string.'''
yield string('"')
body = yield many(charseq())
yield string('"')
return ''.join(body)
in jsonc.py
When run in python2, with:
python2 jsonc.py
always gives an error:
File "jsonc.py", line 62
return ''.join(body)
SyntaxError: 'return' with argument inside generator

Is there any way to surround the return statment and use yield instead ?
Thanks for your notice.

Because cannot return in the generator of Python2

So you can try this method in Python2:

def r(v):
    e = StopIteration(v)
    e.value = v
    raise e


@lexeme
@generate
def quoted():
    '''Parse quoted string.'''
    yield string('"')
    body = yield many(charseq())
    yield string('"')
    r(''.join(body))  # instead of `return ''.join(body)` in Python3

It works. Thank you very much!

Close as fixed.

tejom commented

Still using python 2... Are you open to a pull request that adds a function that will raise StopIteration with value set?

pjt33 commented

NB this is also reproducible in Python 3.2.

I won't add such function, but I have added an example to test suite: https://github.com/sighingnow/parsec.py/blob/master/tests/test_parsec.py#L329 and link to this issue.