added BaseContainer and attached methods

This commit is contained in:
vicky 2022-03-28 19:50:39 +02:00
parent d41dc62aa5
commit d6ec5d9afa
1 changed files with 64 additions and 7 deletions

View File

@ -1,14 +1,71 @@
# Interprets where the parser is located and resolves errors
class BaseContainer:
pass
# Must define __init__, __str__, __repr__ and a print_all method
# will contain all sub-tokens and/or sub-containers
# Defines a method to get all token elements (__iter__) and a method to get a specific element (__getitem__)
# Defines a method to get the number of elements (__len__)
# Defines a method to get the name of the container (__name__)
# Defines a method to the optionals inside the container ( ? .optional())
"""
Creates container holding all the elements from the start of a parsed element to its end.
"""
def __init__(self):
self.content = []
self.optionals = []
def add(self, other):
self.content.append(other)
def class_name(self):
return self.__class__.__name__
def __len__(self):
return len(self.content)
def __iter__(self):
for content in self.content:
yield content
def __getitem__(self, item):
return self.content[item]
def __setitem__(self, key, value):
self.content[key] = value
def __getslice__(self, start, end):
return self.content[start:end]
def __eq__(self, other):
if len(self) != len(other):
return False
for e, f in zip(self, other):
if e != f:
return False
return True
def __invert__(self):
return self.optionals
def __str__(self):
return '{} ({})'.format(", ".join(map(str, self.content)),
", ".join(map(str, self.optionals)) if self.optionals != [] else "")
def __repr__(self):
representation = {}
for content in self.content:
if type(content) in representation:
representation[type(content)] += 1
else:
representation[type(content)] = 1
return "{}({})".format(
self.class_name(),
", ".join(
[f'{index.__name__}: {value}' for index, value in representation.items()]
)
)
# Define containers all the Enhanced text elements, divs, headers, list and any element that can be a container
class TextContainer(BaseContainer):
pass
class EtEmContainer(BaseContainer):
pass
class ContextManager: