# HG changeset patch # User Paul Boddie # Date 1483906530 -3600 # Node ID 64b6d3ef6f49fe89a2428c684f0409d79b2cf9da # Parent 051349b537b21177327177f4c7667675e08b1102 Introduced a special non-terminal node providing encoding information for compatibility with parser module output and compiler module expectations. diff -r 051349b537b2 -r 64b6d3ef6f49 pyparser/parser.py --- a/pyparser/parser.py Sun Jan 08 20:20:39 2017 +0100 +++ b/pyparser/parser.py Sun Jan 08 21:15:30 2017 +0100 @@ -164,6 +164,14 @@ assert 0, "should be unreachable" +class NonterminalEnc(Nonterminal1): + def __init__(self, type, child, encoding): + Nonterminal1.__init__(self, type, child) + self.encoding = encoding + + def __repr__(self): + return "NonterminalEnc(type=%s, child=%r, encoding=%r)" % (self.type, self._child, self.encoding) + class ParseError(Exception): diff -r 051349b537b2 -r 64b6d3ef6f49 pyparser/pyparse.py --- a/pyparser/pyparse.py Sun Jan 08 20:20:39 2017 +0100 +++ b/pyparser/pyparse.py Sun Jan 08 21:15:30 2017 +0100 @@ -161,6 +161,9 @@ self.root = None if enc is not None: compile_info.encoding = enc + # Wrap the tree in a special encoding declaration for parser module + # compatibility. + tree = parser.NonterminalEnc(pygram.syms.encoding_decl, tree, enc) return tree def parse(filename): @@ -188,6 +191,8 @@ l = [tree.type] for i in range(0, tree.num_children()): l.append(st2tuple(tree.get_child(i))) + if isinstance(tree, parser.NonterminalEnc): + l.append(tree.encoding) return tuple(l) elif isinstance(tree, parser.Terminal): l = [tree.type, tree.value]