1. Volker Birk
  2. pyPEG
  3. Issues
Issue #11 resolved

xmlast.py: global name 'word' is not defined

Alexandre Kandalintsev
created an issue

Hi!

First of all thanks for the great parser. I'm a newcomer and not sure if I get the idea behind pyPEG correctly.

I think thing2xml should print the result because the code was succesively parsed. But it fails with a strange error "global name 'word' is not defined". May be it's an indicator of a bug in xmlast.py that do not import pypeg2.word? If we change this way it will work. However, the xml output will be slightly different depending on the input: "x : Int64 = 1" parses to

<Parameter name="x" typing="Int64" default="1"></Parameter>

while "x : Int64" parses to

<Parameter name="x" typing="Int64"><NoneType/></Parameter>

I would expect default=None attribute in the latter case. But I'm no expert.

Here is the code that triggers the problem:

#!/usr/bin/env python3

from pypeg2 import *
from pypeg2.xmlast import thing2xml

class Type(Keyword):
  grammar = Enum( K("Int64"), K("Float"), K("Str") )

class Value(str):
  grammar = word

class Parameter(str):
  grammar = name(), ':', attr("typing", Type), optional('=',attr('default', Value))
  default = None

if __name__ == '__main__':
  p = parse("x:Int64", Parameter)
  print(p.name, ":",p.typing, '=', p.default)
  xml = thing2xml(p, pretty=True)
  print(xml.decode())
$ ./pegbug.py 
x : Int64 = None
Traceback (most recent call last):
  File "/usr/lib/python3.3/site-packages/pypeg2/xmlast.py", line 50, in create_tree
    grammar = type(thing).grammar
AttributeError: type object 'NoneType' has no attribute 'grammar'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./pegbug.py", line 19, in <module>
    xml = thing2xml(p, pretty=True)
  File "/usr/lib/python3.3/site-packages/pypeg2/xmlast.py", line 122, in thing2xml
    tree = create_tree(thing, None, object_names)
  File "/usr/lib/python3.3/site-packages/pypeg2/xmlast.py", line 83, in create_tree
    create_tree(value, me, object_names)
  File "/usr/lib/python3.3/site-packages/pypeg2/xmlast.py", line 55, in create_tree
    grammar = word
NameError: global name 'word' is not defined

Comments (2)

  1. Log in to comment