10.12., 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

Commit 091a584b authored by Eckhart Arnold's avatar Eckhart Arnold

- thrown out test_bed: bad (i.e. unneccesary) idea

parent 7595cd9c
......@@ -124,16 +124,16 @@ if __name__ == "__main__":
'''
def get_grammar_instance(grammar):
def grammar_instance(grammar_representation):
"""Returns a grammar object and the source code of the grammar, from
the given `grammar`-data which can be either a file name, ebnf-code,
python-code, a GrammarBase-derived grammar class or an instance of
such a class (i.e. a grammar object already).
"""
if isinstance(grammar, str):
if isinstance(grammar_representation, str):
# read grammar
grammar_src = load_if_file(grammar)
if is_python_code(grammar):
grammar_src = load_if_file(grammar_representation)
if is_python_code(grammar_representation):
parser_py, errors, AST = grammar_src, '', None
else:
parser_py, errors, AST = full_compilation(grammar_src, None,
......@@ -144,11 +144,11 @@ def get_grammar_instance(grammar):
else:
# assume that dsl_grammar is a ParserHQ-object or Grammar class
grammar_src = ''
if isinstance(grammar, GrammarBase):
parser_root = grammar
if isinstance(grammar_representation, GrammarBase):
parser_root = grammar_representation
else:
# assume `grammar` is a grammar class and get the root object
parser_root = grammar()
parser_root = grammar_representation()
return parser_root, grammar_src
......@@ -162,7 +162,7 @@ def compileDSL(text_or_file, scanner, dsl_grammar, ast_transformation, compiler)
"""
assert isinstance(text_or_file, str)
assert isinstance(compiler, CompilerBase)
parser_root, grammar_src = get_grammar_instance(dsl_grammar)
parser_root, grammar_src = grammar_instance(dsl_grammar)
src = load_if_file(text_or_file)
result, errors, AST = full_compilation(src, scanner, parser_root,
ast_transformation, compiler)
......
......@@ -332,6 +332,9 @@ class GrammarBase:
Args:
document (str): The source text to be parsed.
start_parser (str): The name of the parser with which to
start. This is useful for testing particular parsers
(i.e. particular parts of the EBNF-Grammar.)
Returns:
Node: The root node ot the parse tree.
"""
......
......@@ -20,8 +20,6 @@ permissions and limitations under the License.
import itertools
import os
from functools import partial
try:
import regex as re
except ImportError:
......@@ -165,9 +163,12 @@ class Node:
return "".join(str(child) for child in self.result)
return str(self.result)
def __eq__(self, other):
return str(self.parser) == str(other.parser) and self.result == other.result
@property
def tag_name(self):
return self.parser.name or self.parser.__class__.__name__
return str(self.parser)
# ONLY FOR DEBUGGING: return self.parser.name + ':' + self.parser.__class__.__name__
@property
......
......@@ -85,6 +85,11 @@ class TestNode:
assert len(found) == 2
assert found[0].result == 'x' and found[1].result == 'y'
def test_equality(self):
assert self.unique_tree == self.unique_tree
assert self.recurr_tree != self.unique_tree
assert mock_syntax_tree('(a (b c))') != mock_syntax_tree('(a (b d))')
class TestErrorHandling:
def test_error_flag_propagation(self):
......@@ -101,4 +106,4 @@ class TestErrorHandling:
if __name__ == "__main__":
from run import runner
runner("", globals())
runner("TestNode", globals())
Markdown is supported
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