In January 2021 we will introduce a 10 GB quota for project repositories. Higher limits for individual projects will be available on request. Please see https://doku.lrz.de/display/PUBLIC/GitLab for more information.

Commit f117b6de authored by eckhart's avatar eckhart

- error.py: removed unneccessary level attribute

- ebnf.py: attempt to redefine directives will issue a warning
parent def999dd
......@@ -375,6 +375,7 @@ class EBNFCompiler(Compiler):
WHITESPACE = {'horizontal': r'[\t ]*', # default: horizontal
'linefeed': r'[ \t]*\n?(?!\s*\n)[ \t]*',
'vertical': r'\s*'}
REPEATABLE_DIRECTIVES = {'tokens'}
def __init__(self, grammar_name="", grammar_source=""):
......@@ -393,13 +394,14 @@ class EBNFCompiler(Compiler):
self.recursive = set() # type: Set[str]
self.definitions = {} # type: Dict[str, str]
self.deferred_tasks = [] # type: List[Callable]
self.root_symbol = "" # type: str
self.root_symbol = "" # type: str
self.directives = {'whitespace': self.WHITESPACE['horizontal'],
'comment': '',
'literalws': ['right'],
'literalws': {'right'},
'tokens': set(), # alt. 'preprocessor_tokens'
'filter': dict(), # alt. 'filter'
'ignorecase': False}
'filter': dict()} # alt. 'filter'
# self.directives['ignorecase']: False
self.defined_directives = set() # type: Set[str]
@property
def result(self) -> str:
......@@ -548,7 +550,7 @@ class EBNFCompiler(Compiler):
defined_symbols.difference_update(self.RESERVED_SYMBOLS)
def remove_connections(symbol):
"""Recursiviely removes all symbols which appear in the
"""Recursively removes all symbols which appear in the
definiens of a particular symbol."""
if symbol in defined_symbols:
defined_symbols.remove(symbol)
......@@ -576,12 +578,6 @@ class EBNFCompiler(Compiler):
def on_syntax(self, node: Node) -> str:
definitions = [] # type: List[Tuple[str, str]]
# transfer directives to re_flags where needed
if self.directives['ignorecase']:
self.re_flags.add('i')
elif 'i' in self.re_flags:
self.re_flags.remove('i')
# drop the wrapping sequence node
if len(node.children) == 1 and not node.children[0].parser.name:
node = node.children[0]
......@@ -658,6 +654,14 @@ class EBNFCompiler(Compiler):
key = str(node.children[0]).lower()
assert key not in self.directives['tokens']
if key not in self.REPEATABLE_DIRECTIVES:
if key in self.defined_directives:
node.add_error('Directive "%s" has already been defined earlier. ' % key + \
'Later definition will be ignored!',
code=Error.REDEFINED_DIRECTIVE_WARNING)
return ""
self.defined_directives.add(key)
if key in {'comment', 'whitespace'}:
if node.children[1].parser.name == "list_":
if len(node.children[1].result) != 1:
......@@ -683,12 +687,8 @@ class EBNFCompiler(Compiler):
self.directives[key] = value
elif key == 'ignorecase':
value = str(node.children[1]).lower() not in {"off", "false", "no"}
self.directives['ignorecase'] == value
if value:
if str(node.children[1]).lower() not in {"off", "false", "no"}:
self.re_flags.add('i')
elif 'i' in self.re_flags:
self.re_flags.remove('i')
# elif key == 'testing':
# value = str(node.children[1])
......@@ -706,7 +706,13 @@ class EBNFCompiler(Compiler):
self.directives[key] = list(wsp)
elif key in {'tokens', 'preprocessor_tokens'}:
self.directives['tokens'] |= self.compile(node.children[1])
tokens = self.compile(node.children[1])
redeclared = self.directives['tokes'] & tokens
if redeclared:
node.add_error('Tokens %s have already been declared earlier. '
% str(redeclared) + 'Later declaration will be ignored',
code=Error.REDECLARED_TOKEN_WARNING)
self.directives['tokens'] |= tokens - redeclared
elif key.endswith('_filter'):
filter_set = self.compile(node.children[1])
......
......@@ -42,15 +42,18 @@ class Error:
ERROR = 1000
HIGHEST = ERROR
# warning codes
REDEFINED_DIRECTIVE_WARNING = 101
# error codes
MANDATORY_CONTINUATION = 1001
def __init__(self, message: str, level: int = ERROR, code: int = 0,
def __init__(self, message: str, code: int = ERROR,
pos: int = -1, line: int = -1, column: int = -1) -> None:
self.message = message
assert level >= 0
self.level = level or Error.ERROR
assert code >= 0
self.code = code
self.pos = pos
self.line = line
......@@ -70,17 +73,17 @@ class Error:
def level_str(self):
"""Returns a string representation of the error level, e.g. "warning".
"""
return "Warning" if is_warning(self.level) else "Error"
return "Warning" if is_warning(self.code) else "Error"
def is_warning(level: int) -> bool:
def is_warning(code: int) -> bool:
"""Returns True, if error is merely a warning."""
return level < Error.ERROR
return code < Error.ERROR
def is_error(level: int) -> bool:
def is_error(code: int) -> bool:
"""Returns True, if error is an error, not just a warning."""
return level >= Error.ERROR
return code >= Error.ERROR
def has_errors(messages: Iterable[Error], level: int = Error.ERROR) -> bool:
......@@ -89,7 +92,7 @@ def has_errors(messages: Iterable[Error], level: int = Error.ERROR) -> bool:
least the given error `level`.
"""
for err_obj in messages:
if err_obj.level >= level:
if err_obj.code >= level:
return True
return False
......
......@@ -370,8 +370,7 @@ class Node(collections.abc.Sized):
def add_error(self,
message: str,
level: int = Error.ERROR,
code: int = 0) -> 'Node':
code: int = Error.ERROR) -> 'Node':
"""
Adds an error to this Node.
Parameters:
......@@ -379,8 +378,8 @@ class Node(collections.abc.Sized):
level(int): The error level (error or warning)
code(Hashable): An error code to identify the kind of error
"""
self._errors.append(Error(message, level, code))
self.error_flag = max(self.error_flag, self._errors[-1].level)
self._errors.append(Error(message, code))
self.error_flag = max(self.error_flag, self._errors[-1].code)
return self
......
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