|
1
|
1 |
|
from typing import Any, Optional |
|
2
|
|
|
|
|
3
|
1 |
|
from cleo.io.io import IO |
|
4
|
|
|
|
|
5
|
1 |
|
from sdoc.sdoc2 import in_scope, node_store, out_scope |
|
6
|
1 |
|
from sdoc.sdoc2.node.Node import Node |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
1 |
View Code Duplication |
class Formatter: |
|
|
|
|
|
|
10
|
|
|
""" |
|
11
|
|
|
Abstract parent class for all formatters for generating the output of nodes in a requested format. |
|
12
|
|
|
""" |
|
13
|
|
|
|
|
14
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
|
15
|
1 |
|
def __init__(self, io: IO, parent): |
|
16
|
|
|
""" |
|
17
|
|
|
Object constructor. |
|
18
|
|
|
|
|
19
|
|
|
:param OutputStyle io: The IO object. |
|
20
|
|
|
:param sdoc.sdoc2.formatter.Formatter.Formatter|None parent: The formatter for the parent node. |
|
21
|
|
|
""" |
|
22
|
1 |
|
self._io: IO = io |
|
23
|
|
|
""" |
|
24
|
|
|
The IO object. |
|
25
|
|
|
""" |
|
26
|
|
|
|
|
27
|
1 |
|
self._parent = parent |
|
28
|
|
|
""" |
|
29
|
|
|
The formatter for the parent node. |
|
30
|
|
|
|
|
31
|
|
|
:type: sdoc.sdoc2.formatter.Formatter.Formatter |
|
32
|
|
|
""" |
|
33
|
|
|
|
|
34
|
1 |
|
self._errors: int = 0 |
|
35
|
1 |
|
""" |
|
36
|
|
|
The error count. |
|
37
|
|
|
""" |
|
38
|
|
|
|
|
39
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
|
40
|
1 |
|
@property |
|
41
|
1 |
|
def errors(self) -> int: |
|
42
|
|
|
""" |
|
43
|
|
|
Getter for the error count. |
|
44
|
|
|
|
|
45
|
|
|
:rtype: int |
|
46
|
|
|
""" |
|
47
|
1 |
|
if self._parent: |
|
48
|
|
|
return self._parent.errors |
|
49
|
|
|
|
|
50
|
1 |
|
return self._errors |
|
51
|
|
|
|
|
52
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
|
53
|
1 |
|
def error(self, message: str, node: Optional[Node] = None) -> None: |
|
54
|
|
|
""" |
|
55
|
|
|
Logs an error. |
|
56
|
|
|
|
|
57
|
|
|
:param str message: The error message. This message will be appended with 'at filename:line.column' ot the token. |
|
58
|
|
|
:param Node node: The node where the error occurred. |
|
59
|
|
|
""" |
|
60
|
1 |
|
if self._parent: |
|
61
|
1 |
|
self._parent.error(message, node) |
|
62
|
|
|
else: |
|
63
|
1 |
|
self._errors += 1 |
|
64
|
|
|
|
|
65
|
1 |
|
messages = [message] |
|
66
|
1 |
|
if node: |
|
67
|
1 |
|
filename = node.position.file_name |
|
68
|
1 |
|
line_number = node.position.start_line |
|
69
|
1 |
|
column_number = node.position.start_column + 1 |
|
70
|
1 |
|
messages.append('Position: {0!s}:{1:d}.{2:d}'.format(filename, line_number, column_number)) |
|
71
|
1 |
|
self._io.write_error(messages) |
|
72
|
|
|
|
|
73
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
|
74
|
1 |
|
def generate(self, node: Node, file: Any) -> None: |
|
75
|
|
|
""" |
|
76
|
|
|
Generates the representation of a node in the requested output format. |
|
77
|
|
|
|
|
78
|
|
|
:param Node node: The node for which the output must be generated. |
|
79
|
|
|
:param any file: The output file. |
|
80
|
|
|
""" |
|
81
|
1 |
|
for node_id in node.child_nodes: |
|
82
|
1 |
|
child_node = in_scope(node_id) |
|
83
|
|
|
|
|
84
|
1 |
|
formatter = node_store.create_formatter(self._io, child_node.get_command(), self) |
|
85
|
1 |
|
formatter.generate(child_node, file) |
|
86
|
|
|
|
|
87
|
1 |
|
out_scope(child_node) |
|
88
|
|
|
|
|
89
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
|
90
|
|
|
|