Skip to content

Commit

Permalink
Let build_grammars ensure that generated lexer/parser are on sys.path
Browse files Browse the repository at this point in the history
  • Loading branch information
akosthekiss committed Feb 24, 2020
1 parent fcb6616 commit 06ab3c0
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 9 deletions.
4 changes: 0 additions & 4 deletions grammarinator/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import json
import os
import shutil
import sys

from argparse import ArgumentParser
from math import inf
Expand Down Expand Up @@ -55,9 +54,6 @@ def __init__(self, grammars, parser_dir,
self.parser_dir = parser_dir
os.makedirs(self.parser_dir, exist_ok=True)

if self.parser_dir not in sys.path:
sys.path.append(self.parser_dir)

grammars = grammars if isinstance(grammars, list) else json.loads(grammars)
for i, grammar in enumerate(grammars):
shutil.copy(grammar, self.parser_dir)
Expand Down
7 changes: 6 additions & 1 deletion grammarinator/parser_builder.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# Copyright (c) 2017-2019 Renata Hodovan, Akos Kiss.
# Copyright (c) 2017-2020 Renata Hodovan, Akos Kiss.
#
# Licensed under the BSD 3-Clause License
# <LICENSE.rst or https://opensource.org/licenses/BSD-3-Clause>.
# This file may not be copied, modified, or distributed except
# according to those terms.

import logging
import sys

from os import listdir
from os.path import basename, commonprefix, split, splitext
Expand Down Expand Up @@ -71,6 +72,10 @@ def file_endswith(end_pattern):
# The name of the generated listeners differs if Python or other language target is used.
listener = file_endswith('{listener_format}.{ext}'.format(listener_format=languages['python']['listener_format'], ext=languages['python']['ext']))

# Add the path of the built lexer and parser to the Python path to be available for importing.
if out not in sys.path:
sys.path.append(out)

return (getattr(__import__(x, globals(), locals(), [x], 0), x) for x in [lexer, parser, listener])
except Exception as e:
logger.error('Exception while loading parser modules', exc_info=e)
Expand Down
4 changes: 0 additions & 4 deletions grammarinator/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
# according to those terms.

import re
import sys

from argparse import ArgumentParser
from collections import defaultdict
Expand Down Expand Up @@ -550,9 +549,6 @@ def __init__(self, work_dir=None, antlr=default_antlr_path):

antlr_dir = join(self.work_dir, 'antlr')
makedirs(antlr_dir, exist_ok=True)
# Add the path of the built grammars to the Python path to be available at parsing.
if antlr_dir not in sys.path:
sys.path.append(antlr_dir)

# Copy the grammars from the package to the given working directory.
antlr_resources = ['ANTLRv4Lexer.g4', 'ANTLRv4Parser.g4', 'LexBasic.g4', 'LexerAdaptor.py']
Expand Down

0 comments on commit 06ab3c0

Please sign in to comment.