• R/O
  • SSH

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

Castle: The best Real-Time/Embedded/HighTech language EVER. Attempt 2


Commit MetaInfo

Revisionc7e6c0ed81fdf4acb79e63c1b88be96fd5833c2f (tree)
Time2022-02-07 01:52:39
AuthorAlbert Mietus < albert AT mietus DOT nl >
CommiterAlbert Mietus < albert AT mietus DOT nl >

Log Message

code specific for ast2xml not longer in generic serialization-file

Change Summary

Incremental Difference

diff -r c88877e2c9a9 -r c7e6c0ed81fd Makefile
--- a/Makefile Sun Feb 06 17:34:07 2022 +0100
+++ b/Makefile Sun Feb 06 17:52:39 2022 +0100
@@ -25,7 +25,7 @@
2525 QAZ := ${shell grep '^ *class ' castle/ast/peg.py | sed 's/class //g' | sed 's/[:( ].*$$//g' }
2626 missing_serialization:
2727 @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\
2929 echo "Warning: $${R} has no xml-serializer (nor is marked as to need none)" ;\
3030 fi ;\
3131 done
diff -r c88877e2c9a9 -r c7e6c0ed81fd castle/ast/serialization.py
--- a/castle/ast/serialization.py Sun Feb 06 17:34:07 2022 +0100
+++ b/castle/ast/serialization.py Sun Feb 06 17:52:39 2022 +0100
@@ -1,10 +1,12 @@
1-import logging; logger = logging.getLogger(__name__)
1+
22
33 class Serialize ():
4+
45 def __new__(cls, strategy=None):
56 if strategy is None:
67 return object.__new__(cls)
78 if str(strategy).upper() == "XML":
9+ from .ast2xml import XML_Serialize
810 return XML_Serialize()
911 else:
1012 raise NotImplementedError(f"No Serializer of {strategy} available")
@@ -12,86 +14,3 @@
1214 def serialize(self, ast):
1315 raise NotImplementedError(f"Implement in subclass")
1416
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
diff -r c88877e2c9a9 -r c7e6c0ed81fd pytst/ast/XML_serialization/test_1_simpleXML.py
--- a/pytst/ast/XML_serialization/test_1_simpleXML.py Sun Feb 06 17:34:07 2022 +0100
+++ b/pytst/ast/XML_serialization/test_1_simpleXML.py Sun Feb 06 17:52:39 2022 +0100
@@ -64,7 +64,6 @@
6464 assert_xml_Element(txt, tag='.//ID', name='ID_1')
6565
6666
67-
6867 def test_Sequence_3(xml_serialize):
6968 seq = Sequence()
7069 txt= xml_serialize(seq.seq)