Commit a9dd76b8 authored by di68kap's avatar di68kap
Browse files

- bugs fixed

parent 4b9b6c8a
...@@ -243,7 +243,7 @@ class Parser(ParserBase): ...@@ -243,7 +243,7 @@ class Parser(ParserBase):
# add "aspect oriented" wrapper around parser calls # add "aspect oriented" wrapper around parser calls
# for memoizing, left recursion and tracing # for memoizing, left recursion and tracing
if not isinstance(self, Forward): # should Forward-Parser no be guarded? Not sure... if not isinstance(self, Forward): # should Forward-Parser not be guarded? Not sure...
guarded_parser_call = add_parser_guard(self.__class__.__call__) guarded_parser_call = add_parser_guard(self.__class__.__call__)
# The following check is necessary for classes that don't override # The following check is necessary for classes that don't override
# the __call__() method, because in these cases the non-overridden # the __call__() method, because in these cases the non-overridden
...@@ -260,7 +260,7 @@ class Parser(ParserBase): ...@@ -260,7 +260,7 @@ class Parser(ParserBase):
""" """
duplicate = self.__class__() duplicate = self.__class__()
duplicate.name = self.name duplicate.name = self.name
duplicate.ptype = self.ptype # duplicate.ptype = self.ptype
return duplicate return duplicate
def reset(self): def reset(self):
...@@ -560,11 +560,9 @@ class Grammar: ...@@ -560,11 +560,9 @@ class Grammar:
... ...
symbol = _RE('(?!\\d)\\w+') symbol = _RE('(?!\\d)\\w+')
After the call of this method symbol.name == "symbol" After the call of this method symbol.name == "symbol" holds.
holds. Names assigned via the ``name``-parameter of the Parser names starting or ending with a double underscore like
constructor will not be overwritten. Parser names starting or ``root__`` will be ignored. See :func:`sane_parser_name()`
ending with a double underscore like ``root__`` will be
ignored. See :func:`sane_parser_name()`
This is done only once, upon the first instantiation of the This is done only once, upon the first instantiation of the
grammar class! grammar class!
...@@ -579,10 +577,11 @@ class Grammar: ...@@ -579,10 +577,11 @@ class Grammar:
cdict = cls.__dict__ cdict = cls.__dict__
for entry, parser in cdict.items(): for entry, parser in cdict.items():
if isinstance(parser, Parser) and sane_parser_name(entry): if isinstance(parser, Parser) and sane_parser_name(entry):
if not parser.name: if isinstance(parser, Forward):
if not cast(Forward, parser).parser.name:
cast(Forward, parser).parser.name = entry
else: # if not parser.name:
parser.name = entry parser.name = entry
if isinstance(parser, Forward) and (not cast(Forward, parser).parser.name):
cast(Forward, parser).parser._name = entry
cls.parser_initialization__ = "done" cls.parser_initialization__ = "done"
...@@ -683,7 +682,8 @@ class Grammar: ...@@ -683,7 +682,8 @@ class Grammar:
assert parser.name not in self.__dict__ or \ assert parser.name not in self.__dict__ or \
isinstance(self.__dict__[parser.name], parser.__class__), \ isinstance(self.__dict__[parser.name], parser.__class__), \
('Cannot add parser "%s" because a field with the same name ' ('Cannot add parser "%s" because a field with the same name '
'already exists in grammar object!' % parser.name) 'already exists in grammar object: %s!'
% (parser.name, str(self.__dict__[parser.name])))
setattr(self, parser.name, parser) setattr(self, parser.name, parser)
self.all_parsers__.add(parser) self.all_parsers__.add(parser)
parser.grammar = self parser.grammar = self
...@@ -908,7 +908,7 @@ class PlainText(Parser): ...@@ -908,7 +908,7 @@ class PlainText(Parser):
def __deepcopy__(self, memo): def __deepcopy__(self, memo):
duplicate = self.__class__(self.text) duplicate = self.__class__(self.text)
duplicate.name = self.name duplicate.name = self.name
duplicate.ptype = self.ptype # duplicate.ptype = self.ptype
return duplicate return duplicate
def __call__(self, text: StringView) -> Tuple[Optional[Node], StringView]: def __call__(self, text: StringView) -> Tuple[Optional[Node], StringView]:
...@@ -952,7 +952,7 @@ class RegExp(Parser): ...@@ -952,7 +952,7 @@ class RegExp(Parser):
regexp = self.regexp.pattern regexp = self.regexp.pattern
duplicate = self.__class__(regexp) duplicate = self.__class__(regexp)
duplicate.name = self.name duplicate.name = self.name
duplicate.ptype = self.ptype # duplicate.ptype = self.ptype
return duplicate return duplicate
def __call__(self, text: StringView) -> Tuple[Optional[Node], StringView]: def __call__(self, text: StringView) -> Tuple[Optional[Node], StringView]:
...@@ -1077,7 +1077,7 @@ class _RE(Parser): ...@@ -1077,7 +1077,7 @@ class _RE(Parser):
regexp = self.main.regexp.pattern regexp = self.main.regexp.pattern
duplicate = self.__class__(regexp, self.rx_wsl, self.rx_wsr) duplicate = self.__class__(regexp, self.rx_wsl, self.rx_wsr)
duplicate.name = self.name duplicate.name = self.name
duplicate.ptype = self.ptype # duplicate.ptype = self.ptype
return duplicate return duplicate
def __call__(self, text: StringView) -> Tuple[Optional[Node], StringView]: def __call__(self, text: StringView) -> Tuple[Optional[Node], StringView]:
...@@ -1140,7 +1140,7 @@ class _Token(_RE): ...@@ -1140,7 +1140,7 @@ class _Token(_RE):
def __deepcopy__(self, memo={}): def __deepcopy__(self, memo={}):
duplicate = self.__class__(self.token, self.rx_wsl, self.rx_wsr) duplicate = self.__class__(self.token, self.rx_wsl, self.rx_wsr)
duplicate.name = self.name duplicate.name = self.name
duplicate.ptype = self.ptype # duplicate.ptype = self.ptype
return duplicate return duplicate
def __repr__(self): def __repr__(self):
...@@ -1178,7 +1178,7 @@ class UnaryOperator(Parser): ...@@ -1178,7 +1178,7 @@ class UnaryOperator(Parser):
parser = copy.deepcopy(self.parser, memo) parser = copy.deepcopy(self.parser, memo)
duplicate = self.__class__(parser) duplicate = self.__class__(parser)
duplicate.name = self.name duplicate.name = self.name
duplicate.ptype = self.ptype # duplicate.ptype = self.ptype
return duplicate return duplicate
def apply(self, func: Parser.ApplyFunc) -> bool: def apply(self, func: Parser.ApplyFunc) -> bool:
...@@ -1209,7 +1209,7 @@ class NaryOperator(Parser): ...@@ -1209,7 +1209,7 @@ class NaryOperator(Parser):
parsers = copy.deepcopy(self.parsers, memo) parsers = copy.deepcopy(self.parsers, memo)
duplicate = self.__class__(*parsers) duplicate = self.__class__(*parsers)
duplicate.name = self.name duplicate.name = self.name
duplicate.ptype = self.ptype # duplicate.ptype = self.ptype
return duplicate return duplicate
def apply(self, func: Parser.ApplyFunc) -> bool: def apply(self, func: Parser.ApplyFunc) -> bool:
...@@ -1394,7 +1394,7 @@ class Series(NaryOperator): ...@@ -1394,7 +1394,7 @@ class Series(NaryOperator):
parsers = copy.deepcopy(self.parsers, memo) parsers = copy.deepcopy(self.parsers, memo)
duplicate = self.__class__(*parsers, mandatory=self.mandatory) duplicate = self.__class__(*parsers, mandatory=self.mandatory)
duplicate.name = self.name duplicate.name = self.name
duplicate.ptype = self.ptype # duplicate.ptype = self.ptype
return duplicate return duplicate
def __call__(self, text: StringView) -> Tuple[Optional[Node], StringView]: def __call__(self, text: StringView) -> Tuple[Optional[Node], StringView]:
...@@ -1840,7 +1840,7 @@ class Retrieve(Parser): ...@@ -1840,7 +1840,7 @@ class Retrieve(Parser):
def __deepcopy__(self, memo): def __deepcopy__(self, memo):
duplicate = self.__class__(self.symbol, self.filter) duplicate = self.__class__(self.symbol, self.filter)
duplicate.name = self.name duplicate.name = self.name
duplicate.ptype = self.ptype # duplicate.ptype = self.ptype
return duplicate return duplicate
def __call__(self, text: StringView) -> Tuple[Optional[Node], StringView]: def __call__(self, text: StringView) -> Tuple[Optional[Node], StringView]:
...@@ -1961,8 +1961,8 @@ class Forward(Parser): ...@@ -1961,8 +1961,8 @@ class Forward(Parser):
def __deepcopy__(self, memo): def __deepcopy__(self, memo):
duplicate = self.__class__() duplicate = self.__class__()
duplicate.name = self.name # duplicate.name = self.name # Forward-Parsers should never have a name!
duplicate.ptype = self.ptype # duplicate.ptype = self.ptype
memo[id(self)] = duplicate memo[id(self)] = duplicate
parser = copy.deepcopy(self.parser, memo) parser = copy.deepcopy(self.parser, memo)
duplicate.set(parser) duplicate.set(parser)
......
...@@ -131,7 +131,8 @@ class MockParser(ParserBase): ...@@ -131,7 +131,8 @@ class MockParser(ParserBase):
assert not ptype or ptype[0] == ':' assert not ptype or ptype[0] == ':'
super().__init__() super().__init__()
self.name = name self.name = name
self.ptype = ptype or ':' + self.__class__.__name__ if ptype:
self.ptype = ptype # or ':' + self.__class__.__name__
class ZombieParser(MockParser): class ZombieParser(MockParser):
......
...@@ -29,7 +29,7 @@ from DHParser.ebnf import get_ebnf_grammar, get_ebnf_transformer, get_ebnf_compi ...@@ -29,7 +29,7 @@ from DHParser.ebnf import get_ebnf_grammar, get_ebnf_transformer, get_ebnf_compi
from DHParser.log import logging from DHParser.log import logging
from DHParser.toolkit import re from DHParser.toolkit import re
LOGGING = True LOGGING = False
dhparserdir = os.path.dirname(os.path.realpath(__file__)) dhparserdir = os.path.dirname(os.path.realpath(__file__))
......
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