Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
9.2.2023: Due to updates GitLab will be unavailable for some minutes between 9:00 and 11:00.
Open sidebar
badw-it
DHParser
Commits
481891e3
Commit
481891e3
authored
Aug 31, 2017
by
Eckhart Arnold
Browse files
Nodes now accept TextView objects upon instatiation
parent
6a26260c
Changes
3
Hide whitespace changes
Inline
Side-by-side
DHParser/parser.py
View file @
481891e3
...
...
@@ -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."""
...
...
DHParser/syntaxtree.py
View file @
481891e3
...
...
@@ -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
...
...
DHParser/toolkit.py
View file @
481891e3
...
...
@@ -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
):
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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