Commit 060c83bb authored by eckhart's avatar eckhart
Browse files

- dhparser.py: DSLServer.pyi erzeugt selbstständig Compiler-Modul, falls noch nicht vorhanden.

parent 10b3380b
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -69,6 +69,7 @@ def create_project(path: str):
    TEST_DOCUMENT_TEMPLATE = read_template('example_02_test_Structure_and_Components.ini')
    README_TEMPLATE = read_template('readme_template.md')
    GRAMMAR_TEST_TEMPLATE = read_template('tst_DSL_grammar.pyi')
    SERVER_TEMPLATE = read_template('DSLServer.pyi')

    name = os.path.basename(path)
    if not re.match(r'(?!\d)\w+', name):
@@ -97,6 +98,7 @@ def create_project(path: str):
    create_file('README.md', README_TEMPLATE.format(name=name))
    create_file('tst_%s_grammar.py' % name, GRAMMAR_TEST_TEMPLATE.format(
        name=name, reldhparserdir=os.path.relpath(dhparserdir, os.path.abspath('.'))))
    create_file('%sServer.py' %name, SERVER_TEMPLATE.replace('DSL', name))
    create_file('example.dsl', 'Life is but a walking shadow\n')
    os.chmod('tst_%s_grammar.py' % name, 0o755)
    # The following is left to the user as an exercise
+9 −6
Original line number Diff line number Diff line
@@ -25,6 +25,8 @@ import os
import sys


scriptpath = os.path.dirname(__file__)

STOP_SERVER_REQUEST = b"__STOP_SERVER__"   # hardcoded in order to avoid import from DHParser.server
IDENTIFY_REQUEST = "identify()"

@@ -92,12 +94,13 @@ def json_rpc(func, params=[], ID=None) -> str:
    return str({"jsonrpc": "2.0", "method": func.__name__, "params": params, "id": ID})


def DSL_compiler(dateiname):
    from DSLCompiler import compile_source
    return compile_source(dateiname)


def run_server(host, port):
    try:
        from DSLCompiler import compile_src
    except ModuleNotFoundError:
        from tst_DSL_grammar import recompile_grammar
        recompile_grammar(os.path.join(scriptpath, 'DSL.ebnf'), force=False)
        from DSLCompiler import compile_src
    from DHParser.server import LanguageServer
    config_filename = get_config_filename()
    try:
@@ -107,7 +110,7 @@ def run_server(host, port):
        print('PermissionError: Could not write temporary config file: ' + config_filename)

    print('Starting server on %s:%i' % (host, port))
    DSL_server = LanguageServer({'DSL_compiler': DSL_compiler})
    DSL_server = LanguageServer({'DSL_compiler': compile_src})
    DSL_server.run_server(host, port)


+17 −0
Original line number Diff line number Diff line
@@ -39,6 +39,8 @@ class TestDHParserCommandLineTool:
        self.python = 'python3 ' if os.system('python3 -V' + self.nulldevice) == 0 else 'python '

    def teardown(self):
        if os.path.exists('testdata/neu/neuServer.py'):
            os.system(self.python + 'testdata/neu/neuServer.py --stopserver' + self.nulldevice)
        if os.path.exists('testdata/neu') and os.path.isdir('testdata/neu'):
            shutil.rmtree('testdata/neu')
        if os.path.exists('testdata') and not os.listdir('testdata'):
@@ -46,6 +48,7 @@ class TestDHParserCommandLineTool:
        os.chdir(self.cwd)

    def test_dhparser(self):
        # test compiler creation and execution
        os.system(self.python + '../DHParser/scripts/dhparser.py testdata/neu ' + self.nulldevice)
        os.system(self.python + 'testdata/neu/tst_neu_grammar.py ' + self.nulldevice)
        os.system(self.python + 'testdata/neu/neuCompiler.py testdata/neu/example.dsl '
@@ -53,6 +56,20 @@ class TestDHParserCommandLineTool:
        with open('testdata/neu/example.xml', 'r', encoding='utf-8') as f:
            xml = f.read()
        assert xml.find('<document>') >= 0, xml
        os.remove('testdata/neu/neuCompiler.py')
        os.remove('testdata/neu/example.xml')

        # test server
        os.system(self.python + 'testdata/neu/neuServer.py --stopserver')
        os.system(self.python + 'testdata/neu/neuServer.py testdata/neu/example.dsl '
                  '>testdata/neu/example.xml')
        with open('testdata/neu/example.xml', 'r', encoding='utf-8') as f:
            json = f.read()
        assert json.find('document') >= 0, json
        os.system(self.python + 'testdata/neu/neuServer.py testdata/neu/example.dsl '
                  '>testdata/neu/example.xml')



if __name__ == "__main__":
    from DHParser.testing import runner