Commit f28170f5 authored by Eckhart Arnold's avatar Eckhart Arnold
Browse files

moved parser name initialization to a class method of ParserHeadquarter

parent 5693e242
...@@ -386,7 +386,7 @@ def sane_parser_name(name): ...@@ -386,7 +386,7 @@ def sane_parser_name(name):
class Parser(metaclass=ParserMetaClass): class Parser(metaclass=ParserMetaClass):
def __init__(self, component=None): def __init__(self, component=None):
assert component is None or isinstance(component, str), str(component) assert component is None or isinstance(component, str), str(component)
self.component = '' #component or '' self.component = component or ''
self.headquarter = None # head quater for global variables etc. self.headquarter = None # head quater for global variables etc.
self.visited = dict() self.visited = dict()
self.recursion_counter = dict() self.recursion_counter = dict()
...@@ -413,19 +413,37 @@ class Parser(metaclass=ParserMetaClass): ...@@ -413,19 +413,37 @@ class Parser(metaclass=ParserMetaClass):
class ParserHeadquarter: class ParserHeadquarter:
def __init__(self): @classmethod
# initialize parser components def _connect_commponent_names_to_parsers(cls):
C = self.__class__ """Initializes the `parser.component` fields of those
Cdict = C.__dict__ Parser objects that are directly assigned to a class field with
the field's name, e.g.
class Grammar(ParserHeadquarter):
...
symbol = RE('(?!\\d)\\w+')
After the call of this method symbol.component == "symbol"
holds. If the `component` field has been assigned a different
name in the constructor, a ValueError will be raised.
"""
Cdict = cls.__dict__
for entry in Cdict: for entry in Cdict:
if sane_parser_name(entry): if sane_parser_name(entry):
parser = Cdict[entry] parser = Cdict[entry]
if isinstance(parser, Parser): if isinstance(parser, Parser):
# print(type(parser), parser.component, entry) # print(type(parser), parser.component, entry)
if isinstance(parser, Forward): if isinstance(parser, Forward):
assert not parser.component or parser.component == entry
if parser.component and parser.component != entry:
raise ValueError(("Parser named %s should not be "
" assigned to field with different name: %s"
% (parser.component, entry)))
parser.parser.component = entry parser.parser.component = entry
else: else:
parser.component = entry parser.component = entry
def __init__(self):
self._connect_commponent_names_to_parsers()
self.root__ = copy.deepcopy(self.__class__.root__) self.root__ = copy.deepcopy(self.__class__.root__)
self.root__.apply(self._add_parser) self.root__.apply(self._add_parser)
self.variables = dict() # support for Pop and Retrieve operators self.variables = dict() # support for Pop and Retrieve operators
...@@ -434,6 +452,7 @@ class ParserHeadquarter: ...@@ -434,6 +452,7 @@ class ParserHeadquarter:
self.moving_forward = True self.moving_forward = True
self.unused = True self.unused = True
def _add_parser(self, parser): def _add_parser(self, parser):
"""Adds the copy of the parser object to this instance of ParserHeadquarter. """Adds the copy of the parser object to this instance of ParserHeadquarter.
""" """
...@@ -1499,10 +1518,11 @@ class EBNFCompiler(CompilerBase): ...@@ -1499,10 +1518,11 @@ class EBNFCompiler(CompilerBase):
return "" return ""
def _current_component(self): def _current_component(self):
if self.component in {'', str(None)}: # if self.component in {'', str(None)}:
comp = [] # comp = []
else: # else:
comp = ["component=" + self.component] # comp = ["component=" + self.component]
comp = [] # turn explicit components declaration off
self.component = str(None) self.component = str(None)
return comp return comp
......
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