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

Commit 872abf78 authored by Eckhart Arnold's avatar Eckhart Arnold

- parsers.py: __repr__ and __str__ methods of Parsers adjusted

parent cea76f20
...@@ -805,7 +805,7 @@ class Synonym(UnaryOperator): ...@@ -805,7 +805,7 @@ class Synonym(UnaryOperator):
return None, text return None, text
def __repr__(self): def __repr__(self):
return self.name + '=' + self.parser.name return self.name or self.parser.repr
class Optional(UnaryOperator): class Optional(UnaryOperator):
...@@ -826,7 +826,8 @@ class Optional(UnaryOperator): ...@@ -826,7 +826,8 @@ class Optional(UnaryOperator):
return Node(self, ()), text return Node(self, ()), text
def __repr__(self): def __repr__(self):
return '[' + repr(self.parser) + ']' return '[' + (self.parser.repr[1:-1] if isinstance(self.parser, Alternative)
and not self.parser.name else self.parser.repr) + ']'
class ZeroOrMore(Optional): class ZeroOrMore(Optional):
def __call__(self, text: str) -> Tuple[Node, str]: def __call__(self, text: str) -> Tuple[Node, str]:
...@@ -843,7 +844,8 @@ class ZeroOrMore(Optional): ...@@ -843,7 +844,8 @@ class ZeroOrMore(Optional):
return Node(self, results), text return Node(self, results), text
def __repr__(self): def __repr__(self):
return '{' + repr(self.parser) + '}' return '{' + (self.parser.repr[1:-1] if isinstance(self.parser, Alternative)
and not self.parser.name else self.parser.repr) + '}'
class OneOrMore(UnaryOperator): class OneOrMore(UnaryOperator):
...@@ -870,7 +872,8 @@ class OneOrMore(UnaryOperator): ...@@ -870,7 +872,8 @@ class OneOrMore(UnaryOperator):
return Node(self, results), text_ return Node(self, results), text_
def __repr__(self): def __repr__(self):
return '{' + repr(self.parser) + '}+' return '{' + (self.parser.repr[1:-1] if isinstance(self.parser, Alternative)
and not self.parser.name else self.parser.repr) + '}+'
class Series(NaryOperator): class Series(NaryOperator):
...@@ -891,6 +894,9 @@ class Series(NaryOperator): ...@@ -891,6 +894,9 @@ class Series(NaryOperator):
assert len(results) <= len(self.parsers) assert len(results) <= len(self.parsers)
return Node(self, results), text_ return Node(self, results), text_
def __repr__(self):
return " ".join(parser.repr for parser in self.parsers)
def __add__(self, other: Parser) -> 'Series': def __add__(self, other: Parser) -> 'Series':
other_parsers = cast('Series', other).parsers if isinstance(other, Series) else (other,) other_parsers = cast('Series', other).parsers if isinstance(other, Series) else (other,)
return Series(*(self.parsers + other_parsers)) return Series(*(self.parsers + other_parsers))
...@@ -904,9 +910,6 @@ class Series(NaryOperator): ...@@ -904,9 +910,6 @@ class Series(NaryOperator):
self.parsers += other_parsers self.parsers += other_parsers
return self return self
def __repr__(self):
return '(' + " ".join(repr(parser) for parser in self.parsers) + ')'
class Alternative(NaryOperator): class Alternative(NaryOperator):
"""Matches if at least one of several alternatives matches. Returns """Matches if at least one of several alternatives matches. Returns
...@@ -940,7 +943,7 @@ class Alternative(NaryOperator): ...@@ -940,7 +943,7 @@ class Alternative(NaryOperator):
return None, text return None, text
def __repr__(self): def __repr__(self):
return " | ".join(repr(parser) for parser in self.parsers) return '(' + ' | '.join(parser.repr for parser in self.parsers) + ')'
def __or__(self, other: Parser) -> 'Alternative': def __or__(self, other: Parser) -> 'Alternative':
other_parsers = cast('Alternative', other).parsers \ other_parsers = cast('Alternative', other).parsers \
...@@ -986,7 +989,7 @@ class Required(FlowOperator): ...@@ -986,7 +989,7 @@ class Required(FlowOperator):
return node, text_ return node, text_
def __repr__(self): def __repr__(self):
return '§' + repr(self.parser) return '§' + self.parser.repr
class Lookahead(FlowOperator): class Lookahead(FlowOperator):
...@@ -1001,7 +1004,7 @@ class Lookahead(FlowOperator): ...@@ -1001,7 +1004,7 @@ class Lookahead(FlowOperator):
return None, text return None, text
def __repr__(self): def __repr__(self):
return '&' + repr(self.parser) return '&' + self.parser.repr
def sign(self, bool_value) -> bool: def sign(self, bool_value) -> bool:
return bool_value return bool_value
...@@ -1009,7 +1012,7 @@ class Lookahead(FlowOperator): ...@@ -1009,7 +1012,7 @@ class Lookahead(FlowOperator):
class NegativeLookahead(Lookahead): class NegativeLookahead(Lookahead):
def __repr__(self): def __repr__(self):
return '!' + repr(self.parser) return '!' + self.parser.repr
def sign(self, bool_value) -> bool: def sign(self, bool_value) -> bool:
return not bool_value return not bool_value
...@@ -1043,7 +1046,7 @@ class Lookbehind(FlowOperator): ...@@ -1043,7 +1046,7 @@ class Lookbehind(FlowOperator):
return None, text return None, text
def __repr__(self): def __repr__(self):
return '-&' + repr(self.parser) return '-&' + self.parser.repr
def sign(self, bool_value) -> bool: def sign(self, bool_value) -> bool:
return bool_value return bool_value
...@@ -1061,7 +1064,7 @@ class Lookbehind(FlowOperator): ...@@ -1061,7 +1064,7 @@ class Lookbehind(FlowOperator):
class NegativeLookbehind(Lookbehind): class NegativeLookbehind(Lookbehind):
def __repr__(self): def __repr__(self):
return '-!' + repr(self.parser) return '-!' + self.parser.repr
def sign(self, bool_value) -> bool: def sign(self, bool_value) -> bool:
return not bool_value return not bool_value
...@@ -1091,7 +1094,7 @@ class Capture(UnaryOperator): ...@@ -1091,7 +1094,7 @@ class Capture(UnaryOperator):
return None, text return None, text
def __repr__(self): def __repr__(self):
return repr(self.parser) return self.parser.repr
RetrieveFilter = Callable[[List[str]], str] RetrieveFilter = Callable[[List[str]], str]
...@@ -1124,7 +1127,7 @@ class Retrieve(Parser): ...@@ -1124,7 +1127,7 @@ class Retrieve(Parser):
return self.call(text) # allow call method to be called from subclass circumventing the parser guard return self.call(text) # allow call method to be called from subclass circumventing the parser guard
def __repr__(self): def __repr__(self):
return ':' + repr(self.symbol) return ':' + self.symbol.repr
def call(self, text: str) -> Tuple[Node, str]: def call(self, text: str) -> Tuple[Node, str]:
try: try:
...@@ -1151,7 +1154,7 @@ class Pop(Retrieve): ...@@ -1151,7 +1154,7 @@ class Pop(Retrieve):
return nd, txt return nd, txt
def __repr__(self): def __repr__(self):
return '::' + repr(self.symbol) return '::' + self.symbol.repr
######################################################################## ########################################################################
...@@ -1186,9 +1189,6 @@ class Forward(Parser): ...@@ -1186,9 +1189,6 @@ class Forward(Parser):
self.cycle_reached = False self.cycle_reached = False
return s return s
def __str__(self):
return "Forward->" + str(self.parser)
def set(self, parser: Parser): def set(self, parser: Parser):
# assert isinstance(parser, Parser) # assert isinstance(parser, Parser)
# self.name = parser.name # redundant, see Grammar-constructor # self.name = parser.name # redundant, see Grammar-constructor
......
...@@ -83,13 +83,19 @@ class ParserBase: ...@@ -83,13 +83,19 @@ class ParserBase:
self.name = name # type: str self.name = name # type: str
self._ptype = ':' + self.__class__.__name__ # type: str self._ptype = ':' + self.__class__.__name__ # type: str
def __repr__(self):
self.name + self.ptype
def __str__(self): def __str__(self):
return self.name or self.ptype return self.name or repr(self)
@property @property
def ptype(self) -> str: def ptype(self) -> str:
return self._ptype return self._ptype
@property
def repr(self) -> str:
return self.name if self.name else repr(self)
class MockParser(ParserBase): class MockParser(ParserBase):
""" """
...@@ -107,8 +113,8 @@ class MockParser(ParserBase): ...@@ -107,8 +113,8 @@ class MockParser(ParserBase):
self.name = name self.name = name
self._ptype = ptype or ':' + self.__class__.__name__ self._ptype = ptype or ':' + self.__class__.__name__
def __repr__(self): # def __repr__(self):
return repr_call(self.__init__, (self.name, self.ptype)) # return repr_call(self.__init__, (self.name, self.ptype))
class ZombieParser(MockParser): class ZombieParser(MockParser):
...@@ -129,9 +135,6 @@ class ZombieParser(MockParser): ...@@ -129,9 +135,6 @@ class ZombieParser(MockParser):
assert self.__class__ == ZombieParser, "No derivatives, please!" assert self.__class__ == ZombieParser, "No derivatives, please!"
self.__class__.alive = True self.__class__.alive = True
def __repr__(self):
return "ZOMBIE_PARSER"
def __copy__(self): def __copy__(self):
return self return self
......
...@@ -36,6 +36,11 @@ def selftest(file_name): ...@@ -36,6 +36,11 @@ def selftest(file_name):
grammar = f.read() grammar = f.read()
compiler_name = os.path.basename(os.path.splitext(file_name)[0]) compiler_name = os.path.basename(os.path.splitext(file_name)[0])
parser = get_ebnf_grammar() parser = get_ebnf_grammar()
print("\nAlphabetical List of Parsers:\n")
parser_list = sorted([p for p in parser.all_parsers__ if p.name], key=lambda p: p.name)
for p in parser_list:
print("%s = %s" % (p.name, repr(p)))
print('\n\n')
transformer = get_ebnf_transformer() transformer = get_ebnf_transformer()
compiler = get_ebnf_compiler(compiler_name, grammar) compiler = get_ebnf_compiler(compiler_name, grammar)
result, errors, syntax_tree = compile_source(grammar, None, parser, result, errors, syntax_tree = compile_source(grammar, None, parser,
......
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