Commit 3ee27952 authored by eckhart's avatar eckhart

- parse.pxd / syntax.pxd: More cython optimizations

parent 76a3caa8
......@@ -2,3 +2,115 @@
#cython: language_level=3
#cython: c_string_type=unicode
#cython: c_string_encoding=utf-8
cdef class Parser:
cdef public str pname
cdef public str tag_name
cdef _grammar
cdef object visited
cdef object recursion_counter
cdef object cycle_detection
# cpdef _parse(self, text)
# cdef class Grammar:
# cdef public set all_parsers__
# cdef public object start_parser__
# cdef bint _dirty_flag__
# cdef public bint history_tracking__
# cdef public bint memoization__
# cdef public bint left_recursion_handling__
# cdef public object root__
# cdef public object tree__
# cdef public object document__
# cdef public object _reversed__
# cdef public int document_length__
# cdef public list document_lbreaks__
# cdef public object variables__
# cdef public list rollback__
# cdef public int last_rb__loc__
# cdef public list call_stack__
# cdef public list history__
# cdef public bint moving_forward__
# cdef public set recursion_locations__
cdef class PreprocessorToken(Parser):
pass
cdef class ZombieParser(Parser):
pass
cdef class Token(Parser):
cdef public str text
cdef public int len
cdef class RegExp(Parser):
cdef public object regexp
cdef class Whitespace(RegExp):
pass
cdef class UnaryOperator(Parser):
cdef public object parser
cdef class NaryOperator(Parser):
cdef public object parsers
cdef class Option(UnaryOperator):
pass
cdef class ZeroOrMore(Option):
pass
cdef class OneOrMore(UnaryOperator):
pass
cdef class Series(NaryOperator):
cdef public int mandatory
cdef public object err_msgs
cdef public object skip
cdef class Alternative(NaryOperator):
pass
cdef class AllOf(NaryOperator):
cdef public int num_parsers
cdef public int mandatory
cdef public object err_msgs
cdef public object skip
cdef class SomeOf(NaryOperator):
pass
cdef class FlowOperator(UnaryOperator):
pass
cdef class Lookahead(FlowOperator):
pass
cdef class NegativeLookahead(Lookahead):
pass
cdef class Lookbehind(FlowOperator):
cdef public object regexp
cdef public str text
cdef class NegativeLookbehind(Lookbehind):
pass
cdef class Capture(UnaryOperator):
pass
cdef class Retrieve(Parser):
cdef public object symbol
cdef public object filter
cdef class Pop(Retrieve):
pass
cdef class Synonym(UnaryOperator):
pass
cdef class Forward(Parser):
cdef public object parser
cdef public bint cycle_reached
......@@ -30,7 +30,7 @@ for an example.
"""
from collections import defaultdict, OrderedDict
from collections import defaultdict
import copy
from DHParser.error import Error, linebreaks, line_col
......@@ -209,7 +209,7 @@ class Parser:
# assert isinstance(name, str), str(name)
self.pname = '' # type: str
self.tag_name = self.ptype # type: str
if not hasattr(self.__class__, 'alive'):
if not isinstance(self, ZombieParser):
self._grammar = ZOMBIE_GRAMMAR # type: Grammar
self.reset()
......@@ -446,6 +446,8 @@ class Parser:
self._apply(func, positive_flip)
zombie_alive = False
class ZombieParser(Parser):
"""
Serves as a substitute for a Parser instance.
......@@ -456,18 +458,15 @@ class ZombieParser(Parser):
these (or one of these properties) is needed, but no real Parser-
object is instantiated.
"""
alive = False
__slots__ = ()
def __init__(self):
global zombie_alive
super().__init__()
self.pname = ZOMBIE
self.tag_name = ZOMBIE
# no need to call super class constructor
assert not self.__class__.alive, "There can be only one!"
assert not zombie_alive, "There can be only one!"
assert self.__class__ == ZombieParser, "No derivatives, please!"
self.__class__.alive = True
zombie_alive = True
self.reset()
def __copy__(self):
......@@ -703,7 +702,7 @@ class Grammar:
python_src__ = '' # type: str
root__ = ZOMBIE_PARSER # type: Parser
# root__ must be overwritten with the root-parser by grammar subclass
parser_initialization__ = "pending" # type: str
parser_initialization__ = "pending" # type: list[str]
resume_rules__ = dict() # type: Dict[str, ResumeList]
# some default values
# COMMENT__ = r'' # type: str # r'#.*(?:\n|$)'
......@@ -1057,7 +1056,7 @@ class PreprocessorToken(Parser):
def __deepcopy__(self, memo):
duplicate = self.__class__(self.pname)
# duplicate.pname = self.pname # will be written by the constructor, anyway
duplicate.tage_name = self.tag_name
duplicate.tag_name = self.tag_name
return duplicate
def _parse(self, text: StringView) -> Tuple[Optional[Node], StringView]:
......
......@@ -2,3 +2,21 @@
#cython: language_level=3
#cython: c_string_type=unicode
#cython: c_string_encoding=utf-8
cdef class Node:
cdef public list errors
cdef public int _pos
cdef public object _result
cdef str _content
cdef public tuple children
cdef public int _len
cdef public str tag_name
cdef object _xml_attr
cdef class RootNode(Node):
cdef public list all_errors
cdef public int error_flag
cdef public set inline_tags
cdef public set omit_tags
cdef public set empty_tags
......@@ -12,7 +12,6 @@ import cython
# cpdef decorator(f)
# cpdef gen_special(*args)
# cpdef gen_partial(*args, **kwargs)
cpdef key_parser_name(node: Node)
cpdef key_tag_name(node: Node)
# cpdef traverse(root_node: Node, processing_table: ProcessingTableType,
# key_func: KeyFunc = ?)
......
#!/bin/sh
rm DHParser/*.c
rm DHParser/*.so
# rm DHParser/*.c
# rm DHParser/*.so
# rm DHParser/parse.c
# rm DHParser/parse.cpython*.so
rm DHParser/syntaxtree.c
rm DHParser/syntaxtree.cpython*.so
# CFLAGS="-O3 -march=native -mtune=native"
python3 setup.py build_ext --inplace
......
......@@ -16,9 +16,9 @@ setup(
version=__version__,
packages=['DHParser'],
ext_modules=cythonize(['DHParser/stringview.py',
# 'DHParser/syntaxtree.py',
# 'DHParser/parse.py',
# 'DHParser/transform.py',
'DHParser/syntaxtree.py',
'DHParser/parse.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