Commit d2abb898 authored by eckhart's avatar eckhart
Browse files

some work on EBNF-decompiler

parent 42b0c722
......@@ -49,9 +49,11 @@ from DHParser.versionnumber import __version__
__all__ = ('get_ebnf_preprocessor',
'get_ebnf_grammar',
'get_ebnf_transformer',
'get_ebnf_decompiler',
'get_ebnf_compiler',
'EBNFGrammar',
'EBNFTransform',
'EBNFDecompiler',
'EBNFCompilerError',
'EBNFCompiler',
'grammar_changed',
......@@ -350,6 +352,11 @@ class EBNFDecompiler(Compiler):
super().reset()
# initialize your variables here, not in the constructor!
def on_ZOMBIE__(self, node: Node) -> str:
result = ['Errors in abstract syntax tree of EBNF-grammar! Reconstructing fragments: ']
result.extend([str(self.compile(child)) for child in node.children])
return '\n\n'.join(result)
def on_syntax(self, node):
return '\n'.join(''.join(self.compile(child)) for child in node.children)
......@@ -394,8 +401,8 @@ class EBNFDecompiler(Compiler):
def on_flowmarker(self, node):
return [node.content]
# def on_retrieveop(self, node):
# return node
def on_retrieveop(self, node):
return [node.content]
def on_unordered(self, node):
assert len(node.children) == 1
......@@ -444,9 +451,13 @@ class EBNFDecompiler(Compiler):
# return node
def serialize_AST_as_EBNF(ast: Node) -> str:
"""Converts the AST generated from and EBNF-text back to EBNF."""
pass
def get_ebnf_decompiler() -> EBNFGrammar:
try:
grammar = THREAD_LOCALS.ebnf_grammar_singleton
return grammar
except AttributeError:
THREAD_LOCALS.ebnf_grammar_singleton = EBNFDecompiler()
return THREAD_LOCALS.ebnf_grammar_singleton
########################################################################
......
......@@ -1535,7 +1535,7 @@ class RegExp(Parser):
return 'whitespace__'
except (AttributeError, NameError):
pass
return escape_control_characters('/%s/' % abbreviate_middle(pattern, 120))
return '/' + escape_control_characters('%s' % abbreviate_middle(pattern, 118)) + '/'
def DropToken(text: str) -> Token:
......
......@@ -165,11 +165,11 @@ def escape_re(strg: str) -> str:
def escape_control_characters(strg: str) -> str:
r"""
Replace all control characters (e.g. `\n` `\t`) as well as the
forward slash `/` in a string by their backslashed representation.
"""
Replace all control characters (e.g. \n \t) in a string by their backslashed representation.
"""
return repr(strg).replace('\\\\', '\\')[1:-1]
return repr(strg).replace('\\\\', '\\').replace('/', '\\/')[1:-1]
def lstrip_docstring(docstring: str) -> str:
......
......@@ -474,7 +474,8 @@ class TestSeries:
def test_ebnf_serialization(self):
ebnf_grammar = get_ebnf_grammar()
# print(ebnf_grammar.as_ebnf())
# TODO: Add test here
print(ebnf_grammar.as_ebnf())
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment