Commit 0c9c4171 authored by Eckhart Arnold's avatar Eckhart Arnold
Browse files

dsl.parser_factory renamed to dsl.grammar_provider

parent cfe146e1
...@@ -43,7 +43,7 @@ __all__ = ('GrammarError', ...@@ -43,7 +43,7 @@ __all__ = ('GrammarError',
'compileDSL', 'compileDSL',
'raw_compileEBNF', 'raw_compileEBNF',
'compileEBNF', 'compileEBNF',
'parser_factory', 'grammar_provider',
'compile_on_disk') 'compile_on_disk')
...@@ -250,9 +250,9 @@ def compileEBNF(ebnf_src: str, branding="DSL") -> str: ...@@ -250,9 +250,9 @@ def compileEBNF(ebnf_src: str, branding="DSL") -> str:
return '\n'.join(src) return '\n'.join(src)
def parser_factory(ebnf_src: str, branding="DSL") -> Grammar: def grammar_provider(ebnf_src: str, branding="DSL") -> Grammar:
""" """
Compiles an EBNF grammar and returns a grammar-parser factory Compiles an EBNF grammar and returns a grammar-parser provider
function for that grammar. function for that grammar.
Args: Args:
...@@ -262,7 +262,7 @@ def parser_factory(ebnf_src: str, branding="DSL") -> Grammar: ...@@ -262,7 +262,7 @@ def parser_factory(ebnf_src: str, branding="DSL") -> Grammar:
suite source code. suite source code.
Returns: Returns:
A factory function for a grammar-parser for texts in the A provider function for a grammar object for texts in the
language defined by ``ebnf_src``. language defined by ``ebnf_src``.
""" """
grammar_src = compileDSL(ebnf_src, nil_preprocessor, get_ebnf_grammar(), grammar_src = compileDSL(ebnf_src, nil_preprocessor, get_ebnf_grammar(),
......
...@@ -25,7 +25,7 @@ import sys ...@@ -25,7 +25,7 @@ import sys
sys.path.extend(['../', './']) sys.path.extend(['../', './'])
from DHParser.parser import Grammar, Compiler from DHParser.parser import Grammar, Compiler
from DHParser.dsl import compile_on_disk, run_compiler, compileEBNF, parser_factory, \ from DHParser.dsl import compile_on_disk, run_compiler, compileEBNF, grammar_provider, \
load_compiler_suite load_compiler_suite
ARITHMETIC_EBNF = """ ARITHMETIC_EBNF = """
...@@ -45,7 +45,7 @@ class TestCompileFunctions: ...@@ -45,7 +45,7 @@ class TestCompileFunctions:
parser_src = compileEBNF(ARITHMETIC_EBNF, branding="CustomDSL") parser_src = compileEBNF(ARITHMETIC_EBNF, branding="CustomDSL")
assert isinstance(parser_src, str), str(type(parser_src)) assert isinstance(parser_src, str), str(type(parser_src))
assert parser_src.find('CustomDSLGrammar') >= 0 assert parser_src.find('CustomDSLGrammar') >= 0
factory = parser_factory(ARITHMETIC_EBNF, branding="TestDSL") factory = grammar_provider(ARITHMETIC_EBNF, branding="TestDSL")
assert callable(factory) assert callable(factory)
parser = factory() parser = factory()
result = parser("5 + 3 * 4") result = parser("5 + 3 * 4")
......
...@@ -32,7 +32,7 @@ sys.path.extend(['../', './']) ...@@ -32,7 +32,7 @@ sys.path.extend(['../', './'])
from DHParser.toolkit import compile_python_object from DHParser.toolkit import compile_python_object
from DHParser.parser import compile_source, WHITESPACE_PTYPE, nil_preprocessor from DHParser.parser import compile_source, WHITESPACE_PTYPE, nil_preprocessor
from DHParser.ebnf import get_ebnf_grammar, get_ebnf_transformer, EBNFTransformer, get_ebnf_compiler from DHParser.ebnf import get_ebnf_grammar, get_ebnf_transformer, EBNFTransformer, get_ebnf_compiler
from DHParser.dsl import CompilationError, compileDSL, DHPARSER_IMPORTS, parser_factory from DHParser.dsl import CompilationError, compileDSL, DHPARSER_IMPORTS, grammar_provider
class TestDirectives: class TestDirectives:
...@@ -46,7 +46,7 @@ class TestDirectives: ...@@ -46,7 +46,7 @@ class TestDirectives:
def test_whitespace_linefeed(self): def test_whitespace_linefeed(self):
lang = "@ whitespace = linefeed\n" + self.mini_language lang = "@ whitespace = linefeed\n" + self.mini_language
MinilangParser = parser_factory(lang) MinilangParser = grammar_provider(lang)
parser = MinilangParser() parser = MinilangParser()
assert parser assert parser
syntax_tree = parser("3 + 4 * 12") syntax_tree = parser("3 + 4 * 12")
...@@ -62,7 +62,7 @@ class TestDirectives: ...@@ -62,7 +62,7 @@ class TestDirectives:
def test_whitespace_vertical(self): def test_whitespace_vertical(self):
lang = "@ whitespace = vertical\n" + self.mini_language lang = "@ whitespace = vertical\n" + self.mini_language
parser = parser_factory(lang)() parser = grammar_provider(lang)()
assert parser assert parser
syntax_tree = parser("3 + 4 * 12") syntax_tree = parser("3 + 4 * 12")
assert not syntax_tree.collect_errors() assert not syntax_tree.collect_errors()
...@@ -75,7 +75,7 @@ class TestDirectives: ...@@ -75,7 +75,7 @@ class TestDirectives:
def test_whitespace_horizontal(self): def test_whitespace_horizontal(self):
lang = "@ whitespace = horizontal\n" + self.mini_language lang = "@ whitespace = horizontal\n" + self.mini_language
parser = parser_factory(lang)() parser = grammar_provider(lang)()
assert parser assert parser
syntax_tree = parser("3 + 4 * 12") syntax_tree = parser("3 + 4 * 12")
assert not syntax_tree.collect_errors() assert not syntax_tree.collect_errors()
...@@ -269,7 +269,7 @@ class TestBoundaryCases: ...@@ -269,7 +269,7 @@ class TestBoundaryCases:
unconnected = /.*/ unconnected = /.*/
""" """
try: try:
grammar = parser_factory(ebnf)() grammar = grammar_provider(ebnf)()
assert False, "EBNF compiler should complain about unconnected rules." assert False, "EBNF compiler should complain about unconnected rules."
except CompilationError as err: except CompilationError as err:
grammar_src = err.result grammar_src = err.result
...@@ -288,7 +288,7 @@ class TestBoundaryCases: ...@@ -288,7 +288,7 @@ class TestBoundaryCases:
pass pass
ebnf_testing = "@testing = True\n" + ebnf ebnf_testing = "@testing = True\n" + ebnf
try: try:
grammar = parser_factory(ebnf_testing)() grammar = grammar_provider(ebnf_testing)()
except CompilationError: except CompilationError:
assert False, "EBNF compiler should not complain about unconnected " \ assert False, "EBNF compiler should not complain about unconnected " \
"rules when directive @testing is set." "rules when directive @testing is set."
...@@ -299,7 +299,7 @@ class TestSynonymDetection: ...@@ -299,7 +299,7 @@ class TestSynonymDetection:
ebnf = """a = b ebnf = """a = b
b = /b/ b = /b/
""" """
grammar = parser_factory(ebnf)() grammar = grammar_provider(ebnf)()
assert grammar['a'].name == 'a', grammar['a'].name assert grammar['a'].name == 'a', grammar['a'].name
assert grammar['b'].name == 'b', grammar['b'].name assert grammar['b'].name == 'b', grammar['b'].name
assert grammar('b').as_sxpr().count('b') == 2 assert grammar('b').as_sxpr().count('b') == 2
......
...@@ -27,7 +27,7 @@ sys.path.extend(['../', './']) ...@@ -27,7 +27,7 @@ sys.path.extend(['../', './'])
from DHParser.toolkit import is_logging, logging, compile_python_object from DHParser.toolkit import is_logging, logging, compile_python_object
from DHParser.parser import compile_source, Retrieve, Grammar, Forward, Token, ZeroOrMore, RE from DHParser.parser import compile_source, Retrieve, Grammar, Forward, Token, ZeroOrMore, RE
from DHParser.ebnf import get_ebnf_grammar, get_ebnf_transformer, get_ebnf_compiler from DHParser.ebnf import get_ebnf_grammar, get_ebnf_transformer, get_ebnf_compiler
from DHParser.dsl import parser_factory, DHPARSER_IMPORTS from DHParser.dsl import grammar_provider, DHPARSER_IMPORTS
class TestInfiLoopsAndRecursion: class TestInfiLoopsAndRecursion:
...@@ -41,7 +41,7 @@ class TestInfiLoopsAndRecursion: ...@@ -41,7 +41,7 @@ class TestInfiLoopsAndRecursion:
# example: "5 + 3 * 4" # example: "5 + 3 * 4"
""" """
snippet = "9 + 8 + 7 + 6 + 5 + 3 * 4" snippet = "9 + 8 + 7 + 6 + 5 + 3 * 4"
parser = parser_factory(minilang)() parser = grammar_provider(minilang)()
assert parser assert parser
syntax_tree = parser(snippet) syntax_tree = parser(snippet)
assert not syntax_tree.error_flag, str(syntax_tree.collect_errors()) assert not syntax_tree.error_flag, str(syntax_tree.collect_errors())
...@@ -61,7 +61,7 @@ class TestInfiLoopsAndRecursion: ...@@ -61,7 +61,7 @@ class TestInfiLoopsAndRecursion:
# example: "5 + 3 * 4" # example: "5 + 3 * 4"
""" """
snippet = "9 + 8 + 7 + 6 + 5 + 3 * 4" snippet = "9 + 8 + 7 + 6 + 5 + 3 * 4"
parser = parser_factory(minilang)() parser = grammar_provider(minilang)()
assert parser assert parser
syntax_tree = parser(snippet) syntax_tree = parser(snippet)
assert not syntax_tree.error_flag, syntax_tree.collect_errors() assert not syntax_tree.error_flag, syntax_tree.collect_errors()
...@@ -74,7 +74,7 @@ class TestInfiLoopsAndRecursion: ...@@ -74,7 +74,7 @@ class TestInfiLoopsAndRecursion:
Sum = Expr { ('+' | '-') Expr }+ Sum = Expr { ('+' | '-') Expr }+
Value = /[0-9.]+/~ | '(' Expr ')' Value = /[0-9.]+/~ | '(' Expr ')'
""" """
parser = parser_factory(minilang)() parser = grammar_provider(minilang)()
assert parser assert parser
snippet = "8 * 4" snippet = "8 * 4"
syntax_tree = parser(snippet) syntax_tree = parser(snippet)
...@@ -91,7 +91,7 @@ class TestInfiLoopsAndRecursion: ...@@ -91,7 +91,7 @@ class TestInfiLoopsAndRecursion:
def test_inifinite_loops(self): def test_inifinite_loops(self):
minilang = """not_forever = { // } \n""" minilang = """not_forever = { // } \n"""
snippet = " " snippet = " "
parser = parser_factory(minilang)() parser = grammar_provider(minilang)()
syntax_tree = parser(snippet) syntax_tree = parser(snippet)
assert syntax_tree.error_flag assert syntax_tree.error_flag
# print(syntax_tree.collect_errors()) # print(syntax_tree.collect_errors())
...@@ -216,9 +216,9 @@ class TestPopRetrieve: ...@@ -216,9 +216,9 @@ class TestPopRetrieve:
""" """
def setup(self): def setup(self):
self.minilang_parser = parser_factory(self.mini_language)() self.minilang_parser = grammar_provider(self.mini_language)()
self.minilang_parser2 = parser_factory(self.mini_lang2)() self.minilang_parser2 = grammar_provider(self.mini_lang2)()
self.minilang_parser3 = parser_factory(self.mini_lang3)() self.minilang_parser3 = grammar_provider(self.mini_lang3)()
@staticmethod @staticmethod
def opening_delimiter(node, name): def opening_delimiter(node, name):
...@@ -267,7 +267,7 @@ class TestPopRetrieve: ...@@ -267,7 +267,7 @@ class TestPopRetrieve:
variable = /\w+/~ variable = /\w+/~
""" """
case = "(secret)*. secret" case = "(secret)*. secret"
gr = parser_factory(lang)() gr = grammar_provider(lang)()
st = gr(case) st = gr(case)
assert not st.error_flag, str(st.collect_errors()) assert not st.error_flag, str(st.collect_errors())
...@@ -337,7 +337,7 @@ class TestWhitespaceHandling: ...@@ -337,7 +337,7 @@ class TestWhitespaceHandling:
""" """
def setup(self): def setup(self):
self.gr = parser_factory(self.minilang)() self.gr = grammar_provider(self.minilang)()
def test_token_whitespace(self): def test_token_whitespace(self):
st = self.gr("AB", 'doc') st = self.gr("AB", 'doc')
......
...@@ -27,7 +27,7 @@ from DHParser.syntaxtree import Node, TOKEN_PTYPE, mock_syntax_tree ...@@ -27,7 +27,7 @@ from DHParser.syntaxtree import Node, TOKEN_PTYPE, mock_syntax_tree
from DHParser.transform import traverse, reduce_single_child, \ from DHParser.transform import traverse, reduce_single_child, \
replace_by_single_child, flatten, remove_expendables replace_by_single_child, flatten, remove_expendables
from DHParser.ebnf import get_ebnf_grammar, get_ebnf_transformer, get_ebnf_compiler from DHParser.ebnf import get_ebnf_grammar, get_ebnf_transformer, get_ebnf_compiler
from DHParser.dsl import parser_factory from DHParser.dsl import grammar_provider
class TestNode: class TestNode:
...@@ -64,7 +64,7 @@ class TestNode: ...@@ -64,7 +64,7 @@ class TestNode:
"factor": [remove_expendables, reduce_single_child], "factor": [remove_expendables, reduce_single_child],
(TOKEN_PTYPE): [remove_expendables, reduce_single_child], (TOKEN_PTYPE): [remove_expendables, reduce_single_child],
"?": [remove_expendables, replace_by_single_child]} "?": [remove_expendables, replace_by_single_child]}
parser = parser_factory(ebnf)() parser = grammar_provider(ebnf)()
tree = parser("20 / 4 * 3") tree = parser("20 / 4 * 3")
traverse(tree, att) traverse(tree, att)
compare_tree = mock_syntax_tree("(term (term (factor 20) (:Token /) (factor 4)) (:Token *) (factor 3))") compare_tree = mock_syntax_tree("(term (term (factor 20) (:Token /) (factor 4)) (:Token *) (factor 3))")
......
...@@ -28,7 +28,7 @@ from DHParser.toolkit import compact_sexpr ...@@ -28,7 +28,7 @@ from DHParser.toolkit import compact_sexpr
from DHParser.syntaxtree import TOKEN_PTYPE, mock_syntax_tree from DHParser.syntaxtree import TOKEN_PTYPE, mock_syntax_tree
from DHParser.transform import traverse, remove_expendables, \ from DHParser.transform import traverse, remove_expendables, \
replace_by_single_child, reduce_single_child, flatten replace_by_single_child, reduce_single_child, flatten
from DHParser.dsl import parser_factory from DHParser.dsl import grammar_provider
from DHParser.testing import grammar_unit from DHParser.testing import grammar_unit
ARITHMETIC_EBNF = """ ARITHMETIC_EBNF = """
...@@ -104,7 +104,7 @@ class TestGrammarTest: ...@@ -104,7 +104,7 @@ class TestGrammarTest:
} }
def test_testing_grammar(self): def test_testing_grammar(self):
parser_fac = parser_factory(ARITHMETIC_EBNF) parser_fac = grammar_provider(ARITHMETIC_EBNF)
trans_fac = lambda : ARITHMETIC_EBNFTransform trans_fac = lambda : ARITHMETIC_EBNFTransform
errata = grammar_unit(self.cases, parser_fac, trans_fac) errata = grammar_unit(self.cases, parser_fac, trans_fac)
assert not errata, str(errata) assert not errata, str(errata)
......
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