Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
badw-it
DHParser
Commits
d9b26f0c
Commit
d9b26f0c
authored
Aug 30, 2017
by
di68kap
Browse files
- parser.py: full memoization simplified
parent
6d96b270
Changes
1
Hide whitespace changes
Inline
Side-by-side
DHParser/parser.py
View file @
d9b26f0c
...
...
@@ -240,8 +240,8 @@ def add_parser_guard(parser_func):
# if location has already been visited by the current parser,
# return saved result
if
location
in
parser
.
visited
:
node
,
rlen
=
parser
.
visited
[
location
]
assert
rlen
=
=
location
-
(
0
if
node
is
None
else
node
.
len
)
node
=
parser
.
visited
[
location
]
rlen
=
location
-
(
0
if
node
is
None
else
node
.
len
)
rest
=
grammar
.
document__
[
-
rlen
:]
if
rlen
else
''
return
node
,
rest
...
...
@@ -261,15 +261,15 @@ def add_parser_guard(parser_func):
if
node
is
None
:
# retrieve an earlier match result (from left recursion) if it exists
node
,
rlen
=
parser
.
visited
.
get
(
location
,
(
None
,
len
(
rest
))
)
assert
rlen
=
=
location
-
(
0
if
node
is
None
else
node
.
len
)
node
=
parser
.
visited
.
get
(
location
,
None
)
rlen
=
location
-
(
0
if
node
is
None
else
node
.
len
)
rest
=
grammar
.
document__
[
-
rlen
:]
if
rlen
else
''
# don't overwrite any positive match (i.e. node not None) in the cache
# and don't add empty entries for parsers returning from left recursive calls!
# TODO: uncomment the following for full memoizazion
if
node
is
None
and
location
not
in
grammar
.
recursion_locations__
:
# otherwise also cache None-results
parser
.
visited
[
location
]
=
None
,
len
(
rest
)
parser
.
visited
[
location
]
=
None
else
:
# variable manipulating parsers will be excluded, though,
# because caching would interfere with changes of variable state
...
...
@@ -278,7 +278,7 @@ def add_parser_guard(parser_func):
# matches will store its result in the cache
# TODO: remove if clause for full memoization
# if location in grammar.recursion_locations__:
parser
.
visited
[
location
]
=
(
node
,
len
(
rest
))
parser
.
visited
[
location
]
=
node
parser
.
recursion_counter
[
location
]
-=
1
...
...
@@ -358,8 +358,8 @@ class Parser(ParserBase, metaclass=ParserMetaClass):
contained parser is repeated zero times.
Attributes:
visited:
Dictionary
of places this parser has already been to
during the current parsing process
and
the
results
the
visited:
Mapping
of places this parser has already been to
during the current parsing process
onto
the
node
the
parser returned at the respective place. This dictionary
is used to implement memoizing.
...
...
@@ -398,7 +398,7 @@ class Parser(ParserBase, metaclass=ParserMetaClass):
"""Initializes or resets any parser variables. If overwritten,
the `reset()`-method of the parent class must be called from the
`reset()`-method of the derived class."""
self
.
visited
=
dict
()
# type: Dict[int,
Tuple[Node, str]
]
self
.
visited
=
dict
()
# type: Dict[int,
Node
]
self
.
recursion_counter
=
dict
()
# type: Dict[int, int]
self
.
cycle_detection
=
set
()
# type: Set[Callable]
return
self
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment