Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes export failures on nested parses #99

Closed
wants to merge 1 commit into from
Closed

Fixes export failures on nested parses #99

wants to merge 1 commit into from

Conversation

imaduro
Copy link

@imaduro imaduro commented Nov 12, 2018

While using Python2, if a grammar can have multiple nested expressions, the exporter will fail to export these if Strings are present within them.

Example.tx

Expression:
    ArithmeticExpression | SubExpression
;

ArithmeticExpression:
    operand=Operand (operator=Operator operand=Operand)+
;

SubExpression:
    leftParen="(" sub=Expression rightParen=")"
;

Operand:
    ID | INT | SubExpression
;

Operator:
    "+" | "-"
;

then using the string (value + (exp + bla)) - blabla

Python 2.7.15 (default, May  1 2018, 16:44:08)
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from textx import metamodel_from_file
>>> example_meta = metamodel_from_file('example.tx')
>>> my_string = "(value + (exp + bla)) - blabla"
>>> parsed = example_meta.model_from_str(my_string)
>>> from textx.export import model_export
>>> model_export("./", "my_file.dot")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/textx/export.py", line 163, in model_export
    model_export_to_file(f, model, repo)
  File "/usr/local/lib/python2.7/site-packages/textx/export.py", line 266, in model_export_to_file
    _export(model)
  File "/usr/local/lib/python2.7/site-packages/textx/export.py", line 194, in _export
    for attr_name, attr in obj_cls._tx_attrs.items():
AttributeError: type object 'str' has no attribute '_tx_attrs'

This bug only affects Python 2, Python 3 does not run into this error.

Exports can fail if multiple nested blocks are present:
  File "/usr/local/lib/python2.7/site-packages/textx/export.py", line 194, in _export
    for attr_name, attr in obj_cls._tx_attrs.items():
AttributeError: type object 'str' has no attribute '_tx_attrs'

This adds in "str" to primitive types to avoid this.
@igordejanovic
Copy link
Member

Your example works fine with both Python 2.7 and 3.x.
The problem is incorrect use of model_export.
Instead of model_export("./", "my_file.dot") you should write model_export(parsed, "my_file.dot") where parsed is your model. With the example above you've tried to export string "./".

@imaduro
Copy link
Author

imaduro commented Nov 14, 2018

My bad, though I tried it again with the same model and code:

Python 2.7.15 (default, May  1 2018, 16:44:08)
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from textx import metamodel_from_file
>>> example_meta = metamodel_from_file('example.tx')
>>> my_string = "(value + (exp + bla)) - blabla"
>>> parsed = example_meta.model_from_str(my_string)
>>> from textx.export import model_export

>>> model_export(parsed, "my_file.dot")

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/textx/export.py", line 163, in model_export
    model_export_to_file(f, model, repo)
  File "/usr/local/lib/python2.7/site-packages/textx/export.py", line 267, in model_export_to_file
    _export(model)
  File "/usr/local/lib/python2.7/site-packages/textx/export.py", line 222, in _export
    _export(list_obj)
  File "/usr/local/lib/python2.7/site-packages/textx/export.py", line 242, in _export
    _export(attr_value)
  File "/usr/local/lib/python2.7/site-packages/textx/export.py", line 222, in _export
    _export(list_obj)
  File "/usr/local/lib/python2.7/site-packages/textx/export.py", line 195, in _export
    for attr_name, attr in obj_cls._tx_attrs.items():
AttributeError: type object 'str' has no attribute '_tx_attrs'

I wonder if it's specific to my machine's python2 then.

@igordejanovic
Copy link
Member

@imaduro I follow your exact steps and managed to reproduce. I'll investigate. Thanks for reporting.

@igordejanovic
Copy link
Member

Ah, I got it. textX is using unicode strings. Your input string must be a unicode. I guess we should make some checks in *_from_str functions/methods to give better error responses.
Please leave this issue open until we do. In the mean time just use u in front of your input strings or from __future__ import unicode_literals at the top of your files.

@igordejanovic
Copy link
Member

Please leave this issue open until we do.

I meant please open an issue for this since this is a PR :)

@imaduro
Copy link
Author

imaduro commented Nov 14, 2018

Will do! Thanks!

@imaduro imaduro deleted the add_str_to_primite_types branch November 14, 2018 19:26
igordejanovic added a commit that referenced this pull request Nov 21, 2018
This should prevent problems with Python 2.
See:
#105
#99
#117
igordejanovic added a commit that referenced this pull request Nov 21, 2018
This should prevent problems with Python 2.
See:
#105
#99
#117
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants