Skip to content

removed dependency to six in generated code #2

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions fastidious/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ def generate(klass, executable):

if executable:
sys.stdout.write("""
import sys
res = Calculator.p_parse(" ".join(sys.argv[1:]))
print(res)
""")
if __name__ == '__main__':
import sys
res = %s.p_parse(" ".join(sys.argv[1:]))
print(res)
""" % klass.rsplit(".", 1)[-1])


def graph(klass):
Expand Down
22 changes: 21 additions & 1 deletion fastidious/fastidious_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,21 @@ def visit_rule(self, node):
self.output.write("\n\n")


_SRE_Pattern = type(re.compile("."))


def _repr(obj):
# actually only needed for Py2 that does not produce
# evaluable repr from regexps
if isinstance(obj, dict):
return "{%s}" % (", ".join("%s: %s" % (_repr(k), _repr(v))
for k, v in obj.items()))
elif isinstance(obj, _SRE_Pattern):
return "re.compile(%r, %s)" % (obj.pattern, obj.flags)
else:
return repr(obj)


class FastidiousCompiler(object):
def __init__(self, gen_code=True, memoize=True, debug=False):
self.gen_code = gen_code
Expand Down Expand Up @@ -530,7 +545,9 @@ def gen_py_code(self, parser, out):
'''.format(cmd))
out.write("""
import re
import six

if not hasattr(__builtins__, 'basestring'):
basestring = str

""")

Expand All @@ -549,6 +566,9 @@ def __init__(self, **kwargs):
out.write(" class ParserError(Exception):\n pass\n\n")
out.write(body)

# print _p_py_constants
out.write(" _p_py_constants = %s\n" % _repr(parser._p_py_constants))

# print parsr methods and attributes
from fastidious.parser_base import ParserMixin
_, mixin_body = inspect.getsource(ParserMixin).split("\n", 1)
Expand Down
5 changes: 4 additions & 1 deletion fastidious/parser_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
UPPERCASE = string.uppercase
LOWERCASE = string.lowercase

if not hasattr(__builtins__, "basestring"):
basestring = str


class ParserError(Exception):
pass
Expand Down Expand Up @@ -188,7 +191,7 @@ def p_flatten(self, obj, **kwargs):
'abc'

"""
if isinstance(obj, six.string_types):
if isinstance(obj, basestring):
return obj
result = ""
for i in obj:
Expand Down