Commit a8bf0742 authored by eckhart's avatar eckhart

- testing-framework extended

parent 0e2c4d73
......@@ -41,7 +41,7 @@ from DHParser.transform import TransformationFunc, traverse, remove_brackets, \
reduce_single_child, replace_by_single_child, remove_expendables, \
remove_tokens, flatten, forbid, assert_content
from DHParser.versionnumber import __version__
from typing import Callable, Dict, List, Set, Tuple, Sequence, Union, Optional, Any
from typing import Callable, Dict, List, Set, Tuple, Sequence, Union, Optional, Any, cast
__all__ = ('get_ebnf_preprocessor',
......@@ -572,7 +572,7 @@ class EBNFCompiler(Compiler):
# transformations = '[reduce_single_child]'
transtable.append(' "' + name + '": %s,' % transformations)
# transtable.append(' ":Token": reduce_single_child,')
# transtable += [' "*": replace_by_single_child', '}', '']
transtable += [' "*": replace_by_single_child', '}', '']
transtable += [TRANSFORMER_FACTORY.format(NAME=self.grammar_name, ID=self.grammar_id)]
return '\n'.join(transtable)
......@@ -1264,10 +1264,12 @@ def get_ebnf_compiler(grammar_name="", grammar_source="") -> EBNFCompiler:
def compile_ebnf(ebnf_source: str, branding: str = 'DSL') \
-> Tuple[Optional[Any], List[Error], Optional[Node]]:
"""Compiles an `ebnf_source` (file_name or EBNF-string) and returns
"""
Compiles an `ebnf_source` (file_name or EBNF-string) and returns
a tuple of the python code of the compiler, a list of warnings or errors
and the abstract syntax tree of the EBNF-source.
This function is merely syntactic sugar."""
This function is merely syntactic sugar.
"""
return compile_source(ebnf_source,
get_ebnf_preprocessor(),
get_ebnf_grammar(),
......
......@@ -44,6 +44,7 @@ __all__ = ('WHITESPACE_PTYPE',
'RootNode',
'parse_sxpr',
'parse_xml',
'parse_tree',
'flatten_sxpr',
'flatten_xml')
......@@ -1081,6 +1082,19 @@ def parse_xml(xml: Union[str, StringView]) -> Node:
assert _.match(RX_WHITESPACE_TAIL)
return tree
def parse_tree(xml_or_sxpr: str) -> Optional[Node]:
if re.match('\s*<', xml_or_sxpr):
return parse_xml(xml_or_sxpr)
elif re.match('\s*\(', xml_or_sxpr):
return parse_sxpr(xml_or_sxpr)
elif re.match('\s*', xml_or_sxpr):
return None
else:
m = re.match('\s*(.*)\n?', xml_or_sxpr)
snippet = m.group(1) if m else ''
raise ValueError('Snippet seems to be neither S-expression nor XML: ' + snippet + ' ...')
# if __name__ == "__main__":
# st = parse_sxpr("(alpha (beta (gamma i\nj\nk) (delta y)) (epsilon z))")
# print(st.as_sxpr())
......
This diff is collapsed.
......@@ -138,6 +138,12 @@ except ModuleNotFoundError:
def recompile_grammar(grammar_src, force):
grammar_tests_dir = os.path.join(scriptpath, 'grammar_tests')
if not os.path.exists(grammar_tests_dir) \
or not any(os.path.isfile(os.path.join(grammar_tests_dir, entry))
for entry in os.listdir(grammar_tests_dir)):
print('No grammar-tests found, generating test templates.')
testing.create_test_templates(grammar_src, grammar_tests_dir)
with DHParser.log.logging(LOGGING):
# recompiles Grammar only if it has changed
if not dsl.recompile_grammar(grammar_src, force=force):
......
......@@ -14,7 +14,7 @@
#######################################################################
#
# Structure and Components
#: Structure and Components
#
#######################################################################
......@@ -25,7 +25,7 @@ group = "(" expression ")"
#######################################################################
#
# "Leaf"-Expressions
#: "Leaf"-Expressions
#
#######################################################################
......
......@@ -59,7 +59,7 @@ class ArithmeticGrammar(Grammar):
r"""Parser for an Arithmetic source file.
"""
expression = Forward()
source_hash__ = "48fe89871e7ba344eb238c1d1a927167"
source_hash__ = "50681341ebb2536b3eadd7eb5540ece0"
parser_initialization__ = ["upon instantiation"]
resume_rules__ = {}
COMMENT__ = r'#.*'
......
[match:symbol]
M1: word
M2: one_word_with_underscores
[fail:symbol]
F1: two words
[match:document]
M1: """This is a sequence of words
extending over several lines"""
M2: """ This sequence contains leading whitespace"""
[fail:document]
F1: """This test should fail, because neither
comma nor full have been defined anywhere."""
......@@ -16,7 +16,7 @@ scriptpath = os.path.dirname(__file__)
try:
from DHParser import dsl
import DHParser.log
from DHParser import testing
from DHParser import testing, create_test_templates
except ModuleNotFoundError:
print('Could not import DHParser. Please adjust sys.path in file '
'"%s" manually' % __file__)
......@@ -24,6 +24,12 @@ except ModuleNotFoundError:
def recompile_grammar(grammar_src, force):
grammar_tests_dir = os.path.join(scriptpath, 'grammar_tests')
if not os.path.exists(grammar_tests_dir) \
or not any(os.path.isfile(os.path.join(grammar_tests_dir, entry))
for entry in os.listdir(grammar_tests_dir)):
print('No grammar-tests found, generating test templates.')
create_test_templates(grammar_src, grammar_tests_dir)
with DHParser.log.logging(LOGGING):
# recompiles Grammar only if it has changed
if not dsl.recompile_grammar(grammar_src, force=force):
......@@ -35,9 +41,10 @@ def recompile_grammar(grammar_src, force):
def run_grammar_tests(glob_pattern):
grammar_tests_dir = os.path.join(scriptpath, 'grammar_tests')
with DHParser.log.logging(LOGGING):
error_report = testing.grammar_suite(
os.path.join(scriptpath, 'grammar_tests'),
grammar_tests_dir,
get_grammar, get_transformer,
fn_patterns=[glob_pattern], report=True, verbose=True)
return error_report
......
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