Castle: The best Real-Time/Embedded/HighTech language EVER. Attempt 2
Revision | c7e6c0ed81fdf4acb79e63c1b88be96fd5833c2f (tree) |
---|---|
Time | 2022-02-07 01:52:39 |
Author | Albert Mietus < albert AT mietus DOT nl > |
Commiter | Albert Mietus < albert AT mietus DOT nl > |
code specific for ast2xml not longer in generic serialization-file
@@ -25,7 +25,7 @@ | ||
25 | 25 | QAZ := ${shell grep '^ *class ' castle/ast/peg.py | sed 's/class //g' | sed 's/[:( ].*$$//g' } |
26 | 26 | missing_serialization: |
27 | 27 | @for R in ${QAZ} ; do \ |
28 | - if ! grep -q -E "^ *((def)|(# *NO_VISITOR_NEEDED:)) $${R}2xml" castle/ast/serialization.py > /dev/null ; then\ | |
28 | + if ! grep -q -E "^ *((def)|(# *NO_VISITOR_NEEDED:)) $${R}2xml" castle/ast/ast2xml.py > /dev/null ; then\ | |
29 | 29 | echo "Warning: $${R} has no xml-serializer (nor is marked as to need none)" ;\ |
30 | 30 | fi ;\ |
31 | 31 | done |
@@ -1,10 +1,12 @@ | ||
1 | -import logging; logger = logging.getLogger(__name__) | |
1 | + | |
2 | 2 | |
3 | 3 | class Serialize (): |
4 | + | |
4 | 5 | def __new__(cls, strategy=None): |
5 | 6 | if strategy is None: |
6 | 7 | return object.__new__(cls) |
7 | 8 | if str(strategy).upper() == "XML": |
9 | + from .ast2xml import XML_Serialize | |
8 | 10 | return XML_Serialize() |
9 | 11 | else: |
10 | 12 | raise NotImplementedError(f"No Serializer of {strategy} available") |
@@ -12,86 +14,3 @@ | ||
12 | 14 | def serialize(self, ast): |
13 | 15 | raise NotImplementedError(f"Implement in subclass") |
14 | 16 | |
15 | -from xml.etree import ElementTree as ET | |
16 | - | |
17 | - | |
18 | -class XML_Serialize(Serialize): | |
19 | - def serialize(self, ast) -> str: | |
20 | - logger.debug(f"ast={ast._valType(ast)}") | |
21 | - | |
22 | - tree = self._ast2xml(ast) | |
23 | - return ET.tostring(tree, encoding="unicode") | |
24 | - | |
25 | - | |
26 | - def _ast2xml(self, ast, parent=None) -> ET.Element: | |
27 | - if parent is None: | |
28 | - parent = ET.Element('AST2XML', version="0.0") | |
29 | - | |
30 | - method_name = f'{type(ast).__name__}2xml' | |
31 | - visitor = getattr(self, method_name, None) | |
32 | - logger.debug(f'visitor={visitor}') | |
33 | - | |
34 | - if visitor: | |
35 | - visitor(ast=ast, parent=parent) # Grow the tree | |
36 | - else: | |
37 | - logger.info(f'No visitor >>{method_name}<<, skipping ... (fingers crossed)') | |
38 | - return parent | |
39 | - | |
40 | - | |
41 | - def ID2xml(self, ast, parent) ->None: | |
42 | - logger.debug(f"ast={ast._valType(ast)} parent={parent} ast.name={ast.name}") | |
43 | - ET.SubElement(parent, 'ID', name=ast.name) | |
44 | - | |
45 | - | |
46 | -#NO_VISITOR_NEEDED: PEG2xml ## Pure Abstract | |
47 | -#NO_VISITOR_NEEDED: MixIn_value_attribute2xml ## MixIn | |
48 | -#NO_VISITOR_NEEDED: MixIn_expr_attribute2xml ## MixIn | |
49 | -#NO_VISITOR_NEEDED: MixIn_children_tuple2xml ## MixIn | |
50 | -#NO_VISITOR_NEEDED: Terminal2xml ## Pure Abstract | |
51 | -#NO_VISITOR_NEEDED: NonTerminal2xml ## Pure Abstract | |
52 | -#NO_VISITOR_NEEDED: Expression2xml ## Pure Abstract | |
53 | -#NO_VISITOR_NEEDED: Predicate2xml ## Pure Abstract | |
54 | -#NO_VISITOR_NEEDED: Group2xml ## Pure Abstract | |
55 | -#NO_VISITOR_NEEDED: Markers2xml ## Pure Abstract | |
56 | - | |
57 | - | |
58 | - def _MixIn_value_attribute2xml(self, ast, parent, cls_name): | |
59 | - logger.debug(f"{cls_name}2xml:: ast={ast._valType(ast.value)}") | |
60 | - ET.SubElement(parent, cls_name, value=ast.value) | |
61 | - | |
62 | - def StrTerm2xml(self, ast, parent): self._MixIn_value_attribute2xml(ast, parent, 'StrTerm') | |
63 | - def RegExpTerm2xml(self, ast, parent): self._MixIn_value_attribute2xml(ast, parent, 'RegExpTerm') | |
64 | - | |
65 | - def Sequence2xml(self, ast, parent) ->None: | |
66 | - logger.debug(f"Sequence2xml::ast={ast._valType(ast.value)}") | |
67 | - seq = ET.SubElement(parent, 'Sequence') | |
68 | - for elm in ast.value: | |
69 | - self._ast2xml(elm, parent=seq) | |
70 | - | |
71 | - def Rule2xml(self, ast, parent) ->None: | |
72 | - logger.debug(f"Rule2xml:: ast:Rule.name={ast.name.name}") | |
73 | - rule = ET.SubElement(parent, 'Rule', name=ast.name.name) | |
74 | - self._ast2xml(ast.expr, parent=rule) | |
75 | - | |
76 | - def Rules2xml(self, ast, parent) ->None: | |
77 | - logger.debug(f"Rules2xml:: ast[{len(ast)}]") | |
78 | - for child in ast: | |
79 | - logger.debug(f'Rules2xml type(child)={type(child)}') | |
80 | - self._ast2xml(child, parent=parent) | |
81 | - | |
82 | -############# | |
83 | - | |
84 | -# def Setting2xml(self, ast, parent) ->None: ... | |
85 | -# def Settings2xml(self, ast, parent) ->None: ... | |
86 | -# def Grammar2xml(self, ast, parent) ->None: ... | |
87 | -# def UnorderedGroup2xml(self, ast, parent) ->None: ... | |
88 | -# def Quantity2xml(self, ast, parent) ->None: ... | |
89 | - | |
90 | -# def OrderedChoice2xml(self, ast, parent) ->None: ... | |
91 | -# def Optional2xml(self, ast, parent) ->None: ... | |
92 | -# def ZeroOrMore2xml(self, ast, parent) ->None: ... | |
93 | -# def OneOrMore2xml(self, ast, parent) ->None: ... | |
94 | -# def AndPredicate2xml(self, ast, parent) ->None: ... | |
95 | -# def NotPredicate2xml(self, ast, parent) ->None: ... | |
96 | - | |
97 | -# def EOF2xml(self, ast, parent) ->None: pass # Needed |
@@ -64,7 +64,6 @@ | ||
64 | 64 | assert_xml_Element(txt, tag='.//ID', name='ID_1') |
65 | 65 | |
66 | 66 | |
67 | - | |
68 | 67 | def test_Sequence_3(xml_serialize): |
69 | 68 | seq = Sequence() |
70 | 69 | txt= xml_serialize(seq.seq) |