1.1 --- a/pyparser/parser.py Sun Jan 08 20:20:39 2017 +0100
1.2 +++ b/pyparser/parser.py Sun Jan 08 21:15:30 2017 +0100
1.3 @@ -164,6 +164,14 @@
1.4 assert 0, "should be unreachable"
1.5
1.6
1.7 +class NonterminalEnc(Nonterminal1):
1.8 + def __init__(self, type, child, encoding):
1.9 + Nonterminal1.__init__(self, type, child)
1.10 + self.encoding = encoding
1.11 +
1.12 + def __repr__(self):
1.13 + return "NonterminalEnc(type=%s, child=%r, encoding=%r)" % (self.type, self._child, self.encoding)
1.14 +
1.15
1.16 class ParseError(Exception):
1.17
2.1 --- a/pyparser/pyparse.py Sun Jan 08 20:20:39 2017 +0100
2.2 +++ b/pyparser/pyparse.py Sun Jan 08 21:15:30 2017 +0100
2.3 @@ -161,6 +161,9 @@
2.4 self.root = None
2.5 if enc is not None:
2.6 compile_info.encoding = enc
2.7 + # Wrap the tree in a special encoding declaration for parser module
2.8 + # compatibility.
2.9 + tree = parser.NonterminalEnc(pygram.syms.encoding_decl, tree, enc)
2.10 return tree
2.11
2.12 def parse(filename):
2.13 @@ -188,6 +191,8 @@
2.14 l = [tree.type]
2.15 for i in range(0, tree.num_children()):
2.16 l.append(st2tuple(tree.get_child(i)))
2.17 + if isinstance(tree, parser.NonterminalEnc):
2.18 + l.append(tree.encoding)
2.19 return tuple(l)
2.20 elif isinstance(tree, parser.Terminal):
2.21 l = [tree.type, tree.value]