Commit a33838c1 authored by eckhart's avatar eckhart

- DHParser/parse.py MetaParser._return_values(): flatten anonymous nodes already while parsing

parent cd0e4a9e
DHParser Version 0.8.5 (DATE?) DHParser Version 0.8.5 (DATE?)
.............................. ..............................
- pending optimization: flattens anonymous nodes already while parsing - optimization: flattens anonymous nodes already while parsing
- optimization: drops empty anonymous nodes already during parsing - optimization: drops empty anonymous nodes already during parsing
- optimization: optionally allows to drop anonymous whitespace and tokens - optimization: optionally allows to drop anonymous whitespace and tokens
while parsing while parsing
......
...@@ -1247,6 +1247,7 @@ class DropWhitespace(Whitespace): ...@@ -1247,6 +1247,7 @@ class DropWhitespace(Whitespace):
class MetaParser(Parser): class MetaParser(Parser):
# TODO: Allow to turn optimization off
def _return_value(self, node: Optional[Node]) -> Node: def _return_value(self, node: Optional[Node]) -> Node:
# Node(self.tag_name, node) # unoptimized code # Node(self.tag_name, node) # unoptimized code
...@@ -1267,9 +1268,14 @@ class MetaParser(Parser): ...@@ -1267,9 +1268,14 @@ class MetaParser(Parser):
# return Node(self.tag_name, results) # unoptimized code # return Node(self.tag_name, results) # unoptimized code
assert isinstance(results, tuple) assert isinstance(results, tuple)
N = len(results) N = len(results)
# TODO: if N >= 2, flatten results!
if N > 1: if N > 1:
return Node(self.tag_name, results) nr = []
for child in results:
if child.children and child.tag_name[0] == ':': # faster than c.is_anonymous():
nr.extend(child.children)
else:
nr.append(child)
return Node(self.tag_name, tuple(nr))
elif N == 1: elif N == 1:
return self._return_value(results[0]) return self._return_value(results[0])
elif self.pname: elif self.pname:
......
...@@ -99,8 +99,8 @@ class TestLoggingAndLoading: ...@@ -99,8 +99,8 @@ class TestLoggingAndLoading:
except AttributeError: except AttributeError:
pass pass
with logging("TESTLOGS"): with logging("TESTLOGS"):
assert not os.path.exists("TESTSLOGS"), \ assert os.path.exists("TESTLOGS"), \
"Log dir should not be created before first use!" "Log dir should be created if it does not exist!"
dirname = log_dir() dirname = log_dir()
assert dirname == "TESTLOGS" assert dirname == "TESTLOGS"
assert is_logging(), "is_logging() should return True, if logging is on" assert is_logging(), "is_logging() should return True, if logging is on"
......
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