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

Commit 5a5efa7b authored by eckhart's avatar eckhart

- ebnf.py: compile_ebnf added

parent 3ec7a844
......@@ -113,10 +113,10 @@ def compile_src(source, log_dir=''):
with logging(log_dir):
compiler = get_compiler()
cname = compiler.__class__.__name__
result = compile_source(source, get_preprocessor(),
get_grammar(),
get_transformer(), compiler)
return result
result_tuple = compile_source(source, get_preprocessor(),
get_grammar(),
get_transformer(), compiler)
return result_tuple
if __name__ == "__main__":
......
......@@ -29,7 +29,7 @@ from functools import partial
import keyword
import os
from DHParser.compile import CompilerError, Compiler
from DHParser.compile import CompilerError, Compiler, compile_source
from DHParser.error import Error
from DHParser.parse import Grammar, mixin_comment, Forward, RegExp, Whitespace, \
NegativeLookahead, Alternative, Series, Option, OneOrMore, ZeroOrMore, Token
......@@ -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, Union
from typing import Callable, Dict, List, Set, Tuple, Union, Optional, Any
__all__ = ('get_ebnf_preprocessor',
......@@ -53,6 +53,7 @@ __all__ = ('get_ebnf_preprocessor',
'EBNFCompilerError',
'EBNFCompiler',
'grammar_changed',
'compile_ebnf',
'PreprocessorFactoryFunc',
'ParserFactoryFunc',
'TransformerFactoryFunc',
......@@ -1195,4 +1196,29 @@ def get_ebnf_compiler(grammar_name="", grammar_source="") -> EBNFCompiler:
return compiler
########################################################################
#
# EBNF compiler
#
########################################################################
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
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."""
return compile_source(ebnf_source,
get_ebnf_preprocessor(),
get_ebnf_grammar(),
get_ebnf_transformer(),
get_ebnf_compiler(branding, ebnf_source))
########################################################################
#
# Presets
#
########################################################################
CONFIG_PRESET['add_grammar_source_to_parser_docstring'] = False
......@@ -30,7 +30,8 @@ from DHParser.preprocess import nil_preprocessor
from DHParser import compile_source
from DHParser.error import has_errors, Error
from DHParser.syntaxtree import WHITESPACE_PTYPE
from DHParser.ebnf import get_ebnf_grammar, get_ebnf_transformer, EBNFTransform, get_ebnf_compiler
from DHParser.ebnf import get_ebnf_grammar, get_ebnf_transformer, EBNFTransform, \
get_ebnf_compiler, compile_ebnf
from DHParser.dsl import CompilationError, compileDSL, DHPARSER_IMPORTS, grammar_provider
from DHParser.testing import grammar_unit
......@@ -504,8 +505,7 @@ class TestErrorCustomization:
series = "A" § "B" "C"
other = "X" | "Y" | "Z"
"""
result, messages, ast = compile_source(lang, None, get_ebnf_grammar(),
get_ebnf_transformer(), get_ebnf_compiler())
result, messages, ast = compile_ebnf(lang)
assert messages[0].code == Error.UNUSED_ERROR_MSG_WARNING
......
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