Commit e1092e1b authored by Eckhart Arnold's avatar Eckhart Arnold

cython compatibility

parent 7f34f386
......@@ -417,7 +417,7 @@ class HistoryRecord:
else:
s = self.text
excerpt = s[:36] + ' ...' if len(s) > 36 else s
excerpt = escape_control_characters(excerpt)
excerpt = escape_control_characters(str(excerpt))
return excerpt
# @property
......
......@@ -14,7 +14,7 @@ cdef class Parser:
cdef object visited
cdef object recursion_counter
cdef object cycle_detection
cdef object __parse
cdef object _parse_proxy
cpdef _parse(self, text)
cpdef reset(self)
......@@ -88,19 +88,19 @@ cdef class ZeroOrMore(Option):
cdef class OneOrMore(UnaryParser):
pass
cdef class Series(NaryParser):
cdef class MandatoryNary(NaryParser):
cdef public int mandatory
cdef public object err_msgs
cdef public object skip
cdef class Series(MandatoryNary):
pass
cdef class Alternative(NaryParser):
pass
cdef class AllOf(NaryParser):
cdef class AllOf(MandatoryNary):
cdef public int num_parsers
cdef public int mandatory
cdef public object err_msgs
cdef public object skip
cdef class SomeOf(NaryParser):
pass
......
......@@ -486,10 +486,11 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil
existence of any attributes.
"""
try:
if self._xml_attr is None: # cython compatibility
return False
return attr in self._xml_attr if bool(attr) else bool(self._xml_attr)
except (AttributeError, TypeError):
pass
return False
return False
@property
def attr(self):
......
#cython: infer_types=True
#cython: language_level=3
#cython: c_string_type=unicode
#cython: c_string_encoding=utf-8
cpdef trace_history(self, text)
......@@ -31,34 +31,38 @@ from DHParser.stringview import StringView
from DHParser.syntaxtree import Node, REGEXP_PTYPE, TOKEN_PTYPE, WHITESPACE_PTYPE, ZOMBIE_TAG
from DHParser.log import freeze_callstack, HistoryRecord
from DHParser.parse import Grammar, Parser, ParserError, ParseFunc
from DHParser.toolkit import cython
__all__ = ('trace_history', 'all_descendants', 'set_tracer',
'resume_notices_on')
@cython.locals(location=cython.int, loc=cython.int, delta=cython.int, cs_len=cython.int)
def trace_history(self: Parser, text: StringView) -> Tuple[Optional[Node], StringView]:
grammar = self._grammar
location = grammar.document_length__ - text._len
grammar = self._grammar # type: Grammar
location = grammar.document_length__ - text._len # type: int
if grammar.most_recent_error__:
# add resume notice (mind that skip notices are added by
# `parse.MandatoryElementsParser.mandatory_violation()`
pe = grammar.most_recent_error__
mre = grammar.most_recent_error__ # type: ParserError
grammar.most_recent_error__ = None
errors = [pe.error]
errors = [mre.error] # List[Error]
# ignore inflated length due to gap jumping (see parse.Parser.__call__)
loc = sum(len(nd) for nd in pe.node.select_if(lambda n: True, include_root=True)
if not nd.children and nd.tag_name != ZOMBIE_TAG)
text_ = pe.rest[loc:]
lc = line_col(grammar.document_lbreaks__, pe.error.pos)
loc = 0 # type: int
for nd in mre.node.select_if(lambda n: True, include_root=True):
if not nd.children and nd.tag_name != ZOMBIE_TAG:
loc += len(nd)
text_ = mre.rest[loc:]
lc = line_col(grammar.document_lbreaks__, mre.error.pos)
target = text
if len(target) >= 10:
target = target[:7] + '...'
if pe.first_throw:
if mre.first_throw:
# resume notice
notice = Error('Resuming from parser "{}" with parser "{}" at point: {}'
.format(pe.node.tag_name, grammar.call_stack__[-1][0],
.format(mre.node.tag_name, grammar.call_stack__[-1][0],
repr(target)),
grammar.document_length__ - len(text_), Error.RESUME_NOTICE)
else:
......@@ -67,10 +71,10 @@ def trace_history(self: Parser, text: StringView) -> Tuple[Optional[Node], Strin
.format(grammar.call_stack__[-1][0], repr(target)),
self._grammar.document_length__ - len(text_), Error.RESUME_NOTICE)
if grammar.resume_notices__:
grammar.tree__.add_error(pe.node, notice)
grammar.tree__.add_error(mre.node, notice)
errors.append(notice)
grammar.history__.append(HistoryRecord(
getattr(pe, 'frozen_callstack', grammar.call_stack__), pe.node, text_, lc, errors))
getattr(mre, 'frozen_callstack', grammar.call_stack__), mre.node, text_, lc, errors))
grammar.call_stack__.append(
((self.repr if self.tag_name in (REGEXP_PTYPE, TOKEN_PTYPE)
......
......@@ -581,15 +581,26 @@ def swap_attributes(node: Node, other: Node):
NA = node.has_attr()
OA = other.has_attr()
if NA or OA:
save = node._xml_attr if NA else None
if OA:
node._xml_attr = other._xml_attr
elif NA:
node._xml_attr = None
if NA:
other._xml_attr = save
elif OA:
other._xml_attr = None
try:
save = node._xml_attr if NA else None
if OA:
node._xml_attr = other._xml_attr
elif NA:
node._xml_attr = None
if NA:
other._xml_attr = save
elif OA:
other._xml_attr = None
except AttributeError: # cython compatibility
save = node.attr if NA else None
if OA:
node.attr = other.attr
elif NA:
node.attr = None
if NA:
other.attr = save
elif OA:
other.attr = None
def _replace_by(node: Node, child: Node):
......
......@@ -15,14 +15,15 @@ setup(
name='DHParser',
version=__version__,
packages=['DHParser', 'DHParser.scripts'],
ext_modules=cythonize(['DHParser/stringview.py',
ext_modules=cythonize([#'DHParser/stringview.py',
#'DHParser/toolkit.py',
#'DHParser/preprocess.py',
#'DHParser/error.py',
#'DHParser/syntaxtree.py',
'DHParser/syntaxtree.py',
#'DHParser/log.py',
#'DHParser/parse.py',
#'DHParser/transform.py',
#'DHParser/trace.py',
'DHParser/transform.py',
#'DHParser/compile.py',
#'DHParser/ebnf.py',
],
......
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