Commit a003bd41 authored by eckhart's avatar eckhart

- syntaxtree.Node.attributes renamed to Node.attr

parent be997cc5
......@@ -209,7 +209,7 @@ def adjust_error_locations(errors: List[Error],
Returns:
The list of errors. (Returning the list of errors is just syntactical
sugar. Be aware that the line, col and orig_pos attributes have been
sugar. Be aware that the line, col and orig_pos attr have been
changed in place.)
"""
line_breaks = linebreaks(original_text)
......
......@@ -1542,7 +1542,7 @@ class _ProtocolMeta(GenericMeta):
# Every class is a subclass of the empty protocol.
return True
# Find all attributes defined in the protocol.
# Find all attr defined in the protocol.
attrs = self._get_protocol_attrs()
for attr in attrs:
......@@ -1557,11 +1557,11 @@ class _ProtocolMeta(GenericMeta):
if getattr(c, '_is_protocol', False) and c.__name__ != '_Protocol':
protocol_bases.append(c)
# Get attributes included in protocol.
# Get attr included in protocol.
attrs = set()
for base in protocol_bases:
for attr in base.__dict__.keys():
# Include attributes not defined in any non-protocol bases.
# Include attr not defined in any non-protocol bases.
for c in self.__mro__:
if (c is not base and attr in c.__dict__ and
not getattr(c, '_is_protocol', False)):
......
......@@ -250,8 +250,8 @@ class Node(collections.abc.Sized):
errors (list): A list of all errors that occured on this node.
attributes (dict): An optional dictionary of XML-attributes. This
dictionary is created lazily upon first usage. The attributes
attr (dict): An optional dictionary of XML-attr. This
dictionary is created lazily upon first usage. The attr
will only be shown in the XML-Representation, not in the
S-Expression-output.
"""
......@@ -265,7 +265,7 @@ class Node(collections.abc.Sized):
"""
self.errors = [] # type: List[Error]
self._pos = -1 # type: int
# Assignment to self.result initializes the attributes _result, children and _len
# Assignment to self.result initializes the attr _result, children and _len
# The following if-clause is merely an optimization, i.e. a fast-path for leaf-Nodes
if leafhint:
self._result = result # type: StrictResultType
......@@ -480,9 +480,9 @@ class Node(collections.abc.Sized):
@property
def attributes(self):
def attr(self):
"""
Returns a dictionary of XML-Attributes attached to the Node.
Returns a dictionary of XML-attr attached to the node.
"""
if not hasattr(self, '_xml_attr'):
self._xml_attr = OrderedDict()
......@@ -577,7 +577,7 @@ class Node(collections.abc.Sized):
txt = [left_bracket, node.tag_name]
# s += " '(pos %i)" % node.add_pos
if hasattr(node, '_xml_attr'):
txt.extend(' `(%s "%s")' % (k, v) for k, v in node.attributes.items())
txt.extend(' `(%s "%s")' % (k, v) for k, v in node.attr.items())
if src:
txt.append(" `(pos %i %i %i)" % (node.pos, *line_col(lbreaks, node.pos)))
if showerrors and node.errors:
......@@ -615,7 +615,7 @@ class Node(collections.abc.Sized):
inline_tags: A set of tag names, the content of which will always be written
on a single line, unless it contains explicit line feeds ('\n').
omit_tags: A set of tags from which only the content will be printed, but
neither the opening tag nor its attributes nor the closing tag. This
neither the opening tag nor its attr nor the closing tag. This
allows producing a mix of plain text and child tags in the output,
which otherwise is not supported by the Node object, because it
requires its content to be either a tuple of children or string content.
......@@ -629,9 +629,9 @@ class Node(collections.abc.Sized):
return ''
txt = ['<', node.tag_name]
has_reserved_attrs = hasattr(node, '_xml_attr') \
and any (r in node.attributes for r in {'err', 'line', 'col'})
and any (r in node.attr for r in {'err', 'line', 'col'})
if hasattr(node, '_xml_attr'):
txt.extend(' %s="%s"' % (k, v) for k, v in node.attributes.items())
txt.extend(' %s="%s"' % (k, v) for k, v in node.attr.items())
if src and not has_reserved_attrs:
txt.append(' line="%i" col="%i"' % line_col(line_breaks, node.pos))
if showerrors and node.errors and not has_reserved_attrs:
......@@ -657,7 +657,7 @@ class Node(collections.abc.Sized):
printed on several lines to avoid unwanted gaps in the output.
"""
return node.tag_name in inline_tags or (hasattr(node, '_xml_attr') \
and node.attributes.get('xml:space', 'default') == 'preserve')
and node.attr.get('xml:space', 'default') == 'preserve')
line_breaks = linebreaks(src) if src else []
return self._tree_repr(' ' * indentation, opening, closing,
......@@ -886,7 +886,7 @@ def parse_sxpr(sxpr: str) -> Node:
else:
lines = []
while sxpr and sxpr[0:1] != ')':
# parse attributes
# parse attr
while sxpr[:2] == "`(":
i = sxpr.find('"')
k = sxpr.find(')')
......@@ -899,7 +899,7 @@ def parse_sxpr(sxpr: str) -> Node:
while m >= 0 and m < k:
m = sxpr.find('(', k)
k = max(k, sxpr.find(')', max(m, 0)))
# read attributes
# read attr
else:
attr = sxpr[2:i].strip()
value = sxpr[i:k].strip()[1:-1]
......@@ -922,7 +922,7 @@ def parse_sxpr(sxpr: str) -> Node:
result = "\n".join(lines)
node = Node(mock_parsers.setdefault(tagname, MockParser(name, ':' + class_name)), result)
if attributes:
node.attributes.update(attributes)
node.attr.update(attributes)
return node
return inner_parser(sxpr)
......@@ -941,7 +941,7 @@ def parse_xml(xml: str) -> Node:
def parse_attributes(s: StringView) -> Tuple[StringView, OrderedDict]:
"""Parses a sqeuence of XML-Attributes. Returns the string-slice
beginning after the end of the attributes."""
beginning after the end of the attr."""
attributes = OrderedDict()
restart = 0
for match in s.finditer(re.compile(r'\s*(?P<attr>\w+)\s*=\s*"(?P<value>.*)"\s*')):
......@@ -952,7 +952,7 @@ def parse_xml(xml: str) -> Node:
def parse_opening_tag(s: StringView) -> Tuple[StringView, str, OrderedDict, bool]:
"""Parses an opening tag. Returns the string segment following the
the opening tag, the tag name, a dictionary of attributes and
the opening tag, the tag name, a dictionary of attr and
a flag indicating whether the tag is actually a solitary tag as
indicated by a slash at the end, i.e. <br/>."""
match = s.match(re.compile(r'<\s*(?P<tagname>[\w:]+)\s*'))
......
......@@ -494,14 +494,14 @@ def _replace_by(node: Node, child: Node):
# node.errors.extend(child.errors)
node.result = child.result
if hasattr(child, '_xml_attr'):
node.attributes.update(child.attributes)
node.attr.update(child.attr)
def _reduce_child(node: Node, child: Node):
# node.errors.extend(child.errors)
node.result = child.result
if hasattr(child, '_xml_attr'):
node.attributes.update(child.attributes)
node.attr.update(child.attr)
# def _pick_child(context: List[Node], criteria: CriteriaType):
......
......@@ -29,7 +29,7 @@ from DHParser.ebnf import get_ebnf_grammar, get_ebnf_transformer, get_ebnf_compi
from DHParser.log import logging
from DHParser.toolkit import re
LOGGING = True
LOGGING = False
dhparserdir = os.path.dirname(os.path.realpath(__file__))
......
......@@ -684,7 +684,7 @@ class LaTeXCompiler(Compiler):
if 'config' in node:
for it in {part.strip() for part in node['config'].content.split(',')}:
if it in self.KNOWN_LANGUAGES:
if 'language' in node.attributes:
if 'language' in node.attr:
self.metadata['language'] = it
else:
self.tree.new_error(node, 'Only one document language supported. '
......
......@@ -650,7 +650,7 @@ def get_transformer() -> TransformationFunc:
# def internalize(context):
# """Sets the node's parser type to the tag name and internalizes
# XML attributes."""
# XML attr."""
# node = context[-1]
# if node.parser.name == 'element':
# node.parser = MockParser(node['STag']['Name'].content, ':element')
......@@ -660,11 +660,11 @@ def get_transformer() -> TransformationFunc:
# node.result = node.result[1:]
# else:
# assert node.parser.ptype in [':element', ':emptyElement'], \
# "Tried to internalize tag name and attributes for non element component!"
# "Tried to internalize tag name and attr for non element component!"
# return
# for nd in node.result:
# if nd.parser.name == 'Attribute':
# node.attributes[nd['Name'].content] = nd['AttValue'].content
# node.attr[nd['Name'].content] = nd['AttValue'].content
# remove_nodes(context, {'Attribute'})
......@@ -725,7 +725,7 @@ class XMLCompiler(Compiler):
attributes['standalone'] = s
self.value_constraint(node, s, {'yes', 'no'})
if attributes:
node.attributes.update(attributes)
node.attr.update(attributes)
node.result = ''
self.tree.empty_tags.add('?xml')
node.parser = self.get_parser('?xml')
......@@ -905,7 +905,7 @@ class XMLCompiler(Compiler):
attributes = self.extract_attributes(stag.children)
preserve_whitespace = tag_name in self.tree.inline_tags
if attributes:
node.attributes.update(attributes)
node.attr.update(attributes)
preserve_whitespace |= attributes.get('xml:space', '') == 'preserve'
node.parser = self.get_parser(tag_name)
content = self.compile_children(node.get('content', ZOMBIE_NODE))
......@@ -929,7 +929,7 @@ class XMLCompiler(Compiler):
def on_emptyElement(self, node):
attributes = self.extract_attributes(node.children)
if attributes:
node.attributes.update(attributes)
node.attr.update(attributes)
node.parser = self.get_parser(node['Name'].content)
node.result = ''
self.tree.empty_tags.add(node.tag_name)
......
......@@ -220,10 +220,10 @@ class TestSerialization:
def test_sexpr_attributes(self):
tree = parse_sxpr('(A "B")')
tree.attributes['attr'] = "value"
tree.attr['attr'] = "value"
tree2 = parse_sxpr('(A `(attr "value") "B")')
assert tree.as_sxpr() == tree2.as_sxpr()
tree.attributes['attr2'] = "value2"
tree.attr['attr2'] = "value2"
tree3 = parse_sxpr('(A `(attr "value") `(attr2 "value2") "B")')
assert tree.as_sxpr() == tree3.as_sxpr()
......@@ -257,7 +257,7 @@ class TestSerialization:
assert tree.as_xml() == "<A>\n <B>C</B>\n <D>E</D>\n</A>", xml
tree.attributes['xml:space'] = 'preserve'
tree.attr['xml:space'] = 'preserve'
xml = tree.as_xml()
assert xml == '<A xml:space="preserve"><B>C</B><D>E</D></A>', xml
......
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