base.py 9.88 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
"""base.py - various base classes that are used across several other
             the DHParser-modules.

Copyright 2016  by Eckhart Arnold (arnold@badw.de)
                Bavarian Academy of Sciences an Humanities (badw.de)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.  See the License for the specific language governing
permissions and limitations under the License.
"""
import collections
from typing import Hashable, Iterable, Iterator, Optional, Tuple


__all__ = ('ParserBase',
           'WHITESPACE_PTYPE',
           'TOKEN_PTYPE',
           'MockParser',
           'ZombieParser',
           'ZOMBIE_PARSER',
           'Error',
           'is_error',
           'is_warning',
           'has_errors',
           'only_errors',
           'StringView',
           'EMPTY_STRING_VIEW')


#######################################################################
#
# parser base and mock parsers
#
#######################################################################


class ParserBase:
    """
    ParserBase is the base class for all real and mock parser classes.
    It is defined here, because Node objects require a parser object
    for instantiation.
    """
    def __init__(self, name=''):  # , pbases=frozenset()):
        self.name = name  # type: str
        self._ptype = ':' + self.__class__.__name__  # type: str

    def __repr__(self):
        return self.name + self.ptype

    def __str__(self):
        return self.name + (' = ' if self.name else '') + repr(self)

    @property
    def ptype(self) -> str:
        return self._ptype

    @property
    def repr(self) -> str:
        return self.name if self.name else repr(self)


WHITESPACE_PTYPE = ':Whitespace'
TOKEN_PTYPE = ':Token'


class MockParser(ParserBase):
    """
    MockParser objects can be used to reconstruct syntax trees from a
    serialized form like S-expressions or XML. Mock objects can mimic
    different parser types by assigning them a ptype on initialization.

    Mock objects should not be used for anything other than
    syntax tree (re-)construction. In all other cases where a parser
    object substitute is needed, chose the singleton ZOMBIE_PARSER.
    """
    def __init__(self, name='', ptype=''):  # , pbases=frozenset()):
        assert not ptype or ptype[0] == ':'
        super(MockParser, self).__init__(name)
        self.name = name
        self._ptype = ptype or ':' + self.__class__.__name__


class ZombieParser(MockParser):
    """
    Serves as a substitute for a Parser instance.

    ``ZombieParser`` is the class of the singelton object
    ``ZOMBIE_PARSER``. The  ``ZOMBIE_PARSER`` has a name and can be
    called, but it never matches. It serves as a substitute where only
    these (or one of these properties) is needed, but no real Parser-
    object is instantiated.
    """
    alive = False

    def __init__(self):
        super(ZombieParser, self).__init__("__ZOMBIE__")
        assert not self.__class__.alive, "There can be only one!"
        assert self.__class__ == ZombieParser, "No derivatives, please!"
        self.__class__.alive = True

    def __copy__(self):
        return self

    def __deepcopy__(self, memo):
        return self

    def __call__(self, text):
        """Better call Saul ;-)"""
        return None, text


ZOMBIE_PARSER = ZombieParser()


#######################################################################
#
# error reporting
#
#######################################################################


class Error:
    __slots__ = ['message', 'level', 'code', 'pos', 'line', 'column']

    WARNING   = 1
    ERROR     = 1000
    HIGHEST   = ERROR

    def __init__(self, message: str, level: int=ERROR, code: Hashable=0):
        self.message = message
        assert level >= 0
        self.level = level or Error.ERROR
        self.code = code
        self.pos = -1
        self.line = -1
        self.column = -1

    def __str__(self):
        prefix = ''
        if self.line > 0:
            prefix = "line: %3i, column: %2i, " % (self.line, self.column)
        return prefix + "%s: %s" % (self.level_str, self.message)

    @property
    def level_str(self):
        return "Warning" if is_warning(self.level) else "Error"


def is_warning(level: int) -> bool:
    return level < Error.ERROR


def is_error(level:  int) -> bool:
    return level >= Error.ERROR


def has_errors(messages: Iterable[Error], level: int=Error.ERROR) -> bool:
    """
    Returns True, if at least one entry in `messages` has at
    least the given error `level`.
    """
    for err_obj in messages:
        if err_obj.level >= level:
            return True
    return False


def only_errors(messages: Iterable[Error], level: int=Error.ERROR) -> Iterator[Error]:
    """
    Returns an Iterator that yields only those messages that have
    at least the given error level.
    """
    return (err for err in messages if err.level >= level)


#######################################################################
#
# string view
#
#######################################################################


class StringView(collections.abc.Sized):
    """"A rudimentary StringView class, just enough for the use cases
    in parser.py.

    Slicing Python-strings always yields copies of a segment of the original
    string. See: https://mail.python.org/pipermail/python-dev/2008-May/079699.html
    However, this becomes costly (in terms of space and as a consequence also
    time) when parsing longer documents. Unfortunately, Python's `memoryview`
    does not work for unicode strings. Hence, the StringView class.
    """

    __slots__ = ['text', 'begin', 'end', 'len', 'fullstring_flag']

    def __init__(self, text: str, begin: Optional[int] = 0, end: Optional[int] = None) -> None:
        self.text = text  # type: str
        self.begin = 0  # type: int
        self.end = 0    # type: int
        self.begin, self.end = StringView.real_indices(begin, end, len(text))
        self.len = max(self.end - self.begin, 0)
        self.fullstring_flag = (self.begin == 0 and self.len == len(self.text))

    @staticmethod
    def real_indices(begin, end, len):
        def pack(index, len):
            index = index if index >= 0 else index + len
            return 0 if index < 0 else len if index > len else index
        if begin is None:  begin = 0
        if end is None:  end = len
        return pack(begin, len), pack(end, len)

    def __bool__(self):
        return bool(self.text) and self.end > self.begin

    def __len__(self):
        return self.len

    def __str__(self):
        if self.fullstring_flag:  # optimization: avoid slicing/copying
            return self.text
        return self.text[self.begin:self.end]

    def __getitem__(self, index):
        # assert isinstance(index, slice), "As of now, StringView only allows slicing."
        # assert index.step is None or index.step == 1, \
        #     "Step sizes other than 1 are not yet supported by StringView"
        start, stop = StringView.real_indices(index.start, index.stop, self.len)
        return StringView(self.text, self.begin + start, self.begin + stop)

    def __eq__(self, other):
        return str(self) == str(other)  # PERFORMANCE WARNING: This creates copies of the strings

    def count(self, sub, start=None, end=None) -> int:
        if self.fullstring_flag:
            return self.text.count(sub, start, end)
        elif start is None and end is None:
            return self.text.count(sub, self.begin, self.end)
        else:
            start, end = StringView.real_indices(start, end, self.len)
            return self.text.count(sub, self.begin + start, self.begin + end)

    def find(self, sub, start=None, end=None) -> int:
        if self.fullstring_flag:
            return self.text.find(sub, start, end)
        elif start is None and end is None:
            return self.text.find(sub, self.begin, self.end) - self.begin
        else:
            start, end = StringView.real_indices(start, end, self.len)
            return self.text.find(sub, self.begin + start, self.begin + end) - self.begin

    def rfind(self, sub, start=None, end=None) -> int:
        if self.fullstring_flag:
            return self.text.rfind(sub, start, end)
        if start is None and end is None:
            return self.text.rfind(sub, self.begin, self.end) - self.begin
        else:
            start, end = StringView.real_indices(start, end, self.len)
            return self.text.rfind(sub, self.begin + start, self.begin + end) - self.begin

    def startswith(self, prefix: str, start:int = 0, end:Optional[int] = None) -> bool:
        start += self.begin
        end = self.end if end is None else self.begin + end
        return self.text.startswith(prefix, start, end)

    def match(self, regex):
        return regex.match(self.text, pos=self.begin, endpos=self.end)

    def index(self, absolute_index: int) -> int:
        """
        Converts an index for a string watched by a StringView object
        to an index relative to the string view object, e.g.:
        >>> sv = StringView('xxIxx')[2:3]
        >>> match = sv.match(re.compile('I'))
        >>> match.end()
        3
        >>> sv.index(match.end())
        1
        """
        return absolute_index - self.begin

    def indices(self, absolute_indices: Iterable[int]) -> Tuple[int, ...]:
        """Converts indices for a string watched by a StringView object
        to indices relative to the string view object. See also: `sv_index()`
        """
        return tuple(index - self.begin for index in absolute_indices)

    def search(self, regex):
        return regex.search(self.text, pos=self.begin, endpos=self.end)


EMPTY_STRING_VIEW = StringView('')