Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
badw-it
DHParser
Commits
143db220
Commit
143db220
authored
Jun 15, 2018
by
Eckhart Arnold
Browse files
small corrections
parent
9922348b
Changes
4
Hide whitespace changes
Inline
Side-by-side
DHParser/syntaxtree.py
View file @
143db220
...
...
@@ -63,6 +63,8 @@ class ParserBase:
It is defined here, because Node objects require a parser object
for instantiation.
"""
__slots__
=
'_name'
,
'_ptype'
def
__init__
(
self
,
name
=
''
):
# , pbases=frozenset()):
self
.
_name
=
name
# type: str
self
.
_ptype
=
':'
+
self
.
__class__
.
__name__
# type: str
...
...
@@ -124,6 +126,8 @@ class MockParser(ParserBase):
syntax tree (re-)construction. In all other cases where a parser
object substitute is needed, chose the singleton ZOMBIE_PARSER.
"""
__slots__
=
()
def
__init__
(
self
,
name
=
''
,
ptype
=
''
):
# , pbases=frozenset()):
assert
not
ptype
or
ptype
[
0
]
==
':'
super
().
__init__
(
name
)
...
...
@@ -141,6 +145,7 @@ class ZombieParser(MockParser):
object is instantiated.
"""
alive
=
False
__slots__
=
()
def
__init__
(
self
):
super
(
ZombieParser
,
self
).
__init__
(
"__ZOMBIE__"
)
...
...
@@ -247,7 +252,7 @@ class Node(collections.abc.Sized):
S-Expression-output.
"""
__slots__
=
[
'_result'
,
'children'
,
'_len'
,
'_pos'
,
'parser'
,
'errors'
,
'_xml_attr'
,
'_content'
]
__slots__
=
'_result'
,
'children'
,
'_len'
,
'_pos'
,
'parser'
,
'errors'
,
'_xml_attr'
,
'_content'
def
__init__
(
self
,
parser
,
result
:
ResultType
,
leafhint
:
bool
=
False
)
->
None
:
"""
...
...
@@ -563,7 +568,7 @@ class Node(collections.abc.Sized):
def
as_xml
(
self
,
src
:
str
=
None
,
showerrors
:
bool
=
True
,
indentation
:
int
=
2
,
inline_tags
:
Set
[
str
]
=
set
(),
omit_tags
:
Set
[
str
]
=
set
())
->
str
:
inline_tags
:
Set
[
str
]
=
set
(),
omit_tags
:
Set
[
str
]
=
set
()
,
)
->
str
:
"""
Returns content as XML-tree.
...
...
DHParser/transform.py
View file @
143db220
...
...
@@ -460,14 +460,14 @@ def _replace_by(node: Node, child: Node):
child
.
parser
=
MockParser
(
node
.
parser
.
name
,
child
.
parser
.
ptype
)
# parser names must not be overwritten, else: child.parser.name = node.parser.name
node
.
parser
=
child
.
parser
node
.
errors
.
extend
(
child
.
errors
)
#
node.errors.extend(child.errors)
node
.
result
=
child
.
result
if
hasattr
(
child
,
'_xml_attr'
):
node
.
attributes
.
update
(
child
.
attributes
)
def
_reduce_child
(
node
:
Node
,
child
:
Node
):
node
.
errors
.
extend
(
child
.
errors
)
#
node.errors.extend(child.errors)
node
.
result
=
child
.
result
if
hasattr
(
child
,
'_xml_attr'
):
node
.
attributes
.
update
(
child
.
attributes
)
...
...
examples/XML/XMLCompiler.py
View file @
143db220
...
...
@@ -7,10 +7,12 @@
#######################################################################
from
collections
import
OrderedDict
from
functools
import
partial
import
os
import
sys
sys
.
path
.
append
(
r
'/home/eckhart/Entwicklung/DHParser'
)
try
:
...
...
@@ -675,10 +677,24 @@ class XMLCompiler(Compiler):
def
_reset
(
self
):
super
().
_reset
()
# initialize your variables here, not in the constructor!
self
.
mock_parsers
=
dict
()
def
on_document
(
self
,
node
):
return
self
.
fallback_compiler
(
node
)
def
extract_attributes
(
self
,
node_sequence
):
attributes
=
OrderedDict
()
for
node
in
node_sequence
:
if
node
.
tag_name
==
"Attribute"
:
assert
node
[
0
].
tag_name
==
"Name"
assert
node
[
1
].
tag_name
==
"ATTValue"
attributes
[
node
[
0
].
content
]
=
node
[
1
].
content
return
attributes
def
get_parser
(
self
,
tag_name
):
"""Returns a mock parser with the given tag_name as parser name."""
return
self
.
mock_parsers
.
setdefault
(
tag_name
,
MockParser
(
tag_name
))
# def on_prolog(self, node):
# return node
...
...
@@ -853,8 +869,14 @@ class XMLCompiler(Compiler):
# def on_NDataDecl(self, node):
# return node
# def on_element(self, node):
# return node
def
on_element
(
self
,
node
):
stag
=
node
[
'STag'
]
attributes
=
self
.
extract_attributes
(
stag
.
children
)
if
attributes
:
node
.
attributes
.
update
(
attributes
)
node
.
parser
=
self
.
get_parser
(
stag
[
'Name'
].
content
)
node
.
result
=
node
[
'content'
].
result
return
node
# def on_STag(self, node):
# return node
...
...
@@ -862,8 +884,13 @@ class XMLCompiler(Compiler):
# def on_ETag(self, node):
# return node
# def on_EmptyElemTag(self, node):
# return node
def
on_emptyElement
(
self
,
node
):
attributes
=
self
.
extract_attributes
(
node
.
children
)
if
attributes
:
node
.
attributes
.
update
(
attributes
)
node
.
parser
=
self
.
get_parser
(
node
[
'Name'
].
content
)
node
.
result
=
''
return
node
# def on_TagName(self, node):
# return node
...
...
examples/XML/grammar_tests/99_test_document.ini
View file @
143db220
[match:document]
M1*:
"""
<?xml
version
=
"1.0"
encoding="UTF-8"?>
<note>
<note
date
=
"2018-06-14"
>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
...
...
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