Starting from 2021-07-01, all LRZ GitLab users will be required to explicitly accept the GitLab Terms of Service. Please see the detailed information at https://doku.lrz.de/display/PUBLIC/GitLab and make sure that your projects conform to the requirements.

Commit 481891e3 authored by Eckhart Arnold's avatar Eckhart Arnold
Browse files

Nodes now accept TextView objects upon instatiation

parent 6a26260c
......@@ -77,7 +77,7 @@ except ImportError:
from DHParser.toolkit import is_logging, log_dir, logfile_basename, escape_re, sane_parser_name
from DHParser.syntaxtree import WHITESPACE_PTYPE, TOKEN_PTYPE, ZOMBIE_PARSER, ParserBase, \
Node, TransformationFunc
from DHParser.toolkit import load_if_file, error_messages, line_col
from DHParser.toolkit import TextView, load_if_file, error_messages, line_col
__all__ = ('PreprocessorFunc',
'HistoryRecord',
......@@ -233,7 +233,7 @@ def add_parser_guard(parser_func):
def memoized(parser, location):
node = parser.visited[location]
rlen = location - (0 if node is None else node.len)
rest = grammar.document__[-rlen:] if rlen else ''
rest = TextView(grammar.document__, -rlen) if rlen else ''
return node, rest
# NOTE: An older and simpler implementation of memoization
# relied on `parser.visited[location] == node, rest`. Although,
......@@ -409,7 +409,7 @@ class Parser(ParserBase, metaclass=ParserMetaClass):
self.cycle_detection = set() # type: Set[Callable]
return self
def __call__(self, text: str) -> Tuple[Node, str]:
def __call__(self, text: TextView) -> Tuple[Node, TextView]:
"""Applies the parser to the given `text` and returns a node with
the results or None as well as the text at the position right behind
the matching string."""
......
......@@ -31,7 +31,7 @@ except ImportError:
from .typing34 import AbstractSet, Any, ByteString, Callable, cast, Container, Dict, \
Iterator, List, NamedTuple, Sequence, Union, Text, Tuple
from DHParser.toolkit import is_logging, log_dir, line_col, identity
from DHParser.toolkit import is_logging, log_dir, TextView, line_col, identity
__all__ = ('WHITESPACE_PTYPE',
'MockParser',
......@@ -129,8 +129,8 @@ ZOMBIE_PARSER = ZombieParser()
Error = NamedTuple('Error', [('pos', int), ('msg', str)])
ChildrenType = Tuple['Node', ...]
StrictResultType = Union[ChildrenType, str]
ResultType = Union[ChildrenType, 'Node', str, None]
StrictResultType = Union[ChildrenType, TextView, str]
ResultType = Union[ChildrenType, 'Node', TextView, str, None]
def flatten_sxpr(sxpr: str) -> str:
......@@ -250,7 +250,8 @@ class Node:
# assert ((isinstance(result, tuple) and all(isinstance(child, Node) for child in result))
# or isinstance(result, Node)
# or isinstance(result, str)), str(result)
self._result = (result,) if isinstance(result, Node) else result or '' # type: StrictResultType
self._result = (result,) if isinstance(result, Node) else str(result) \
if isinstance(result, TextView) else result or '' # type: StrictResultType
self.children = cast(ChildrenType, self._result) \
if isinstance(self._result, tuple) else cast(ChildrenType, ()) # type: ChildrenType
self.error_flag = any(r.error_flag for r in self.children) # type: bool
......
......@@ -155,7 +155,7 @@ class TextView:
def __init__(self, text: str, begin: Optional[int] = 0, end: Optional[int] = None) -> None:
self.text = text # type: str
self.begin = begin or 0 # type: int
self.begin = begin or 0 # type: int # TODO: Negative Values!!!
self.end = end or len(text) # type: int
def __str__(self):
......
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