Commit 8863780a authored by Eckhart Arnold's avatar Eckhart Arnold
Browse files

some refactoring

parent 20dd34fb
...@@ -348,7 +348,8 @@ def compile_source(source: str, ...@@ -348,7 +348,8 @@ def compile_source(source: str,
ast = None # type: Optional[Node] ast = None # type: Optional[Node]
original_text = load_if_file(source) # type: str original_text = load_if_file(source) # type: str
source_name = source if is_filename(source) else 'source' source_name = source if is_filename(source) else 'source'
compiler.source = original_text if isinstance(compiler, Compiler):
compiler.source = original_text
log_file_name = logfile_basename(source, compiler) if is_logging() else '' # type: str log_file_name = logfile_basename(source, compiler) if is_logging() else '' # type: str
if not hasattr(parser, 'free_char_parsefunc__') or parser.history_tracking__: if not hasattr(parser, 'free_char_parsefunc__') or parser.history_tracking__:
# log only for custom parser/transformer/compilers # log only for custom parser/transformer/compilers
...@@ -460,10 +461,8 @@ class TreeProcessor(Compiler): ...@@ -460,10 +461,8 @@ class TreeProcessor(Compiler):
assert result is None or isinstance(result, RootNode), str(result) assert result is None or isinstance(result, RootNode), str(result)
return result return result
TreeProcessorCallable = Union[TreeProcessor, Callable[[RootNode], RootNode], functools.partial]
def process_tree(tp: TransformerCallable, tree: RootNode):
def process_tree(tp: TreeProcessorCallable, tree: RootNode) -> Tuple[RootNode, List[Error]]:
"""Process a tree with the tree-processor `tp` only if no fatal error """Process a tree with the tree-processor `tp` only if no fatal error
has occurred so far. Catch any Python-exceptions in case has occurred so far. Catch any Python-exceptions in case
any normal errors have occurred earlier in the processing pipeline. any normal errors have occurred earlier in the processing pipeline.
...@@ -510,7 +509,6 @@ def process_tree(tp: TreeProcessorCallable, tree: RootNode) -> Tuple[RootNode, L ...@@ -510,7 +509,6 @@ def process_tree(tp: TreeProcessorCallable, tree: RootNode) -> Tuple[RootNode, L
new_msgs = [msg for msg in messages if msg.line < 0] new_msgs = [msg for msg in messages if msg.line < 0]
# Obsolete, because RootNode adjusts error locations whenever an error is added: # Obsolete, because RootNode adjusts error locations whenever an error is added:
# adjust_error_locations(new_msgs, tree.source, tree.source_mapping) # adjust_error_locations(new_msgs, tree.source, tree.source_mapping)
return tree, messages
......
...@@ -2276,7 +2276,7 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil ...@@ -2276,7 +2276,7 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil
text.append(children[i].content) text.append(children[i].content)
i += 1 i += 1
if text: element.text = ''.join(text) if text: element.text = ''.join(text)
lest_element = None last_element = None
while i < L: while i < L:
while i < L and children[i].tag_name not in string_tags: while i < L and children[i].tag_name not in string_tags:
last_element = children[i].as_etree(ET, string_tags, empty_tags) last_element = children[i].as_etree(ET, string_tags, empty_tags)
...@@ -2287,7 +2287,7 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil ...@@ -2287,7 +2287,7 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil
assert not children[i].children assert not children[i].children
text.append(children[i].content) text.append(children[i].content)
i += 1 i += 1
if text: last_element.tail = ''.join(text) if text and last_element is not None: last_element.tail = ''.join(text)
else: else:
element = ET.Element(tag_name, attrib=attributes) element = ET.Element(tag_name, attrib=attributes)
if tag_name in empty_tags: if tag_name in empty_tags:
......
...@@ -151,7 +151,7 @@ ProcessingTableType = Dict[str, Union[Sequence[Callable], TransformationDict]] ...@@ -151,7 +151,7 @@ ProcessingTableType = Dict[str, Union[Sequence[Callable], TransformationDict]]
ConditionFunc = Callable # Callable[[TreeContext], bool] ConditionFunc = Callable # Callable[[TreeContext], bool]
KeyFunc = Callable[[Node], str] KeyFunc = Callable[[Node], str]
CriteriaType = Union[int, str, Callable] CriteriaType = Union[int, str, Callable]
TransformerCallable = Union[Callable[[Node], None], partial] TransformerCallable = Union[Callable[[RootNode], None], partial]
def transformation_factory(t1=None, t2=None, t3=None, t4=None, t5=None): def transformation_factory(t1=None, t2=None, t3=None, t4=None, t5=None):
......
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