Castle: The best Real-Time/Embedded/HighTech language EVER. Attempt 2
Revision | 290017360158b3308b186c4cda53422df45f72fb (tree) |
---|---|
Zeit | 2022-04-21 21:56:47 |
Autor | Albert Mietus < albert AT mietus DOT nl > |
Commiter | Albert Mietus < albert AT mietus DOT nl > |
Refactored: moved (AST) serialization into subdir
@@ -32,7 +32,7 @@ | ||
32 | 32 | |
33 | 33 | missing_serialization: |
34 | 34 | @for R in ${shell grep '^ *class ' castle/ast/peg.py | sed 's/class //g' | sed 's/[:( ].*$$//g' } ; do \ |
35 | - if ! grep -q -E "^ *((def)|(# *NO_VISITOR_NEEDED:)) $${R}2xml" castle/ast/ast2xml.py > /dev/null ; then\ | |
35 | + if ! grep -q -E "^ *((def)|(# *NO_VISITOR_NEEDED:)) $${R}2xml" castle/ast/serialization/ast2xml.py > /dev/null ; then\ | |
36 | 36 | echo "Warning: $${R} has no xml-serializer (nor is marked as to need none)" ;\ |
37 | 37 | fi ;\ |
38 | 38 | done |
@@ -1,117 +0,0 @@ | ||
1 | -""" Support to translate an AST into XML. | |
2 | -This file should be used directly, instead use :class:`castle.ast.serialization.Serialize` as in ``Serialize('XML')`` | |
3 | -""" | |
4 | - | |
5 | -import logging; logger = logging.getLogger(__name__) | |
6 | -from castle.ast import serialization | |
7 | -from xml.etree import ElementTree as ET | |
8 | - | |
9 | - | |
10 | -class XML_Serialize(serialization.Serialize): | |
11 | - def serialize(self, ast) -> str: | |
12 | - logger.debug(f"serialize:: ast={ast._valType(ast)}") | |
13 | - | |
14 | - tree = self._ast2xml(ast) | |
15 | - return ET.tostring(tree, encoding="unicode") | |
16 | - | |
17 | - | |
18 | - def _ast2xml(self, ast, parent=None) -> ET.Element: | |
19 | - if parent is None: | |
20 | - parent = ET.Element('AST2XML', version="0.0") | |
21 | - | |
22 | - method_name = f'{type(ast).__name__}2xml' | |
23 | - visitor = getattr(self, method_name, None) | |
24 | - logger.debug(f'_ast2xml:: visitor={visitor}') | |
25 | - | |
26 | - if visitor: | |
27 | - visitor(ast=ast, parent=parent) # Grow the tree | |
28 | - else: | |
29 | - logger.info(f'No visitor >>{method_name}<<, skipping ... (fingers crossed)') | |
30 | - return parent | |
31 | - | |
32 | - | |
33 | - def ID2xml(self, ast, parent) ->None: | |
34 | - logger.debug(f"ID2xml:: ast={ast._valType(ast)} parent={parent} ast.name={ast.name}") | |
35 | - ET.SubElement(parent, 'ID', name=ast.name) | |
36 | - | |
37 | - | |
38 | -#NO_VISITOR_NEEDED: PEG2xml ## Pure Abstract | |
39 | -#NO_VISITOR_NEEDED: MixIn_value_attribute2xml ## MixIn | |
40 | -#NO_VISITOR_NEEDED: MixIn_expr_attribute2xml ## MixIn | |
41 | -#NO_VISITOR_NEEDED: MixIn_children_tuple2xml ## MixIn | |
42 | -#NO_VISITOR_NEEDED: Terminal2xml ## Pure Abstract | |
43 | -#NO_VISITOR_NEEDED: NonTerminal2xml ## Pure Abstract | |
44 | -#NO_VISITOR_NEEDED: Expression2xml ## Pure Abstract | |
45 | -#NO_VISITOR_NEEDED: Predicate2xml ## Pure Abstract | |
46 | -#NO_VISITOR_NEEDED: Group2xml ## Pure Abstract | |
47 | -#NO_VISITOR_NEEDED: Markers2xml ## Pure Abstract | |
48 | -#NO_VISITOR_NEEDED: Quantity2xml ## Pure Abstract | |
49 | -#NO_VISITOR_NEEDED: EOF2xml ## Not a real token | |
50 | -#NO_VISITOR_NEEDED: ParseRules2xml ## Handle in Rules2xml | |
51 | -#NO_VISITOR_NEEDED: Settings2xml ## Handle in Rules2xml | |
52 | - | |
53 | - | |
54 | - def _MixIn_value_attribute2xml(self, ast, parent, cls_name): | |
55 | - logger.debug(f"{cls_name}2xml:: ast={ast._valType(ast.value)}") | |
56 | - ET.SubElement(parent, cls_name, value=ast.value) | |
57 | - | |
58 | - def StrTerm2xml(self, ast, parent): self._MixIn_value_attribute2xml(ast, parent, 'StrTerm') | |
59 | - def RegExpTerm2xml(self, ast, parent): self._MixIn_value_attribute2xml(ast, parent, 'RegExpTerm') | |
60 | - | |
61 | - def Sequence2xml(self, ast, parent) ->None: | |
62 | - logger.debug(f"Sequence2xml::ast={ast._valType(ast._children)}") | |
63 | - seq = ET.SubElement(parent, 'Sequence') | |
64 | - for elm in ast: | |
65 | - self._ast2xml(elm, parent=seq) | |
66 | - | |
67 | - def Rule2xml(self, ast, parent) ->None: | |
68 | - logger.debug(f"Rule2xml:: ast:Rule.name={ast.name.name}") | |
69 | - rule = ET.SubElement(parent, 'Rule', name=ast.name.name) | |
70 | - self._ast2xml(ast.expr, parent=rule) | |
71 | - | |
72 | - def Rules2xml(self, ast, parent) ->None: | |
73 | - logger.debug(f"Rules2xml:: ast[{len(ast)}]") | |
74 | - for child in ast: | |
75 | - logger.debug(f'Rules2xml type(child)={type(child)}') | |
76 | - self._ast2xml(child, parent=parent) | |
77 | - | |
78 | - def _quantity_op2xml(self, ast, parent, tagName) -> None: | |
79 | - g = ET.SubElement(parent, tagName) | |
80 | - self._ast2xml(ast.expr, g) | |
81 | - | |
82 | - def UnorderedGroup2xml(self, ast, parent): self._quantity_op2xml(ast, parent, 'UnorderedGroup') | |
83 | - def Optional2xml(self, ast, parent): self._quantity_op2xml(ast, parent, 'Optional') | |
84 | - def ZeroOrMore2xml(self, ast, parent): self._quantity_op2xml(ast, parent, 'ZeroOrMore') | |
85 | - def OneOrMore2xml(self, ast, parent): self._quantity_op2xml(ast, parent, 'OneOrMore') | |
86 | - | |
87 | - | |
88 | - def OrderedChoice2xml(self, ast, parent) ->None: | |
89 | - oc = ET.SubElement(parent, 'OrderedChoice') | |
90 | - for c in ast: | |
91 | - self._ast2xml(c,oc) | |
92 | - | |
93 | - def _Predicate2xml(self, ast, parent, tagName) ->None: | |
94 | - logger.debug(f"_Predicate2xml.{tagName}:: expr: {ast.expr}:{type(ast.expr).__name__}") | |
95 | - predicate = ET.SubElement(parent, tagName) | |
96 | - self._ast2xml(ast.expr, predicate) | |
97 | - | |
98 | - def AndPredicate2xml(self, ast, parent): self._Predicate2xml(ast, parent,'AndPredicate') | |
99 | - def NotPredicate2xml(self, ast, parent): self._Predicate2xml(ast, parent,'NotPredicate') | |
100 | - | |
101 | - | |
102 | - def Number2xml(self, ast, parent) ->None: | |
103 | - logger.debug(f"Number2xml:: ast: {ast}:{type(ast).__name__}") | |
104 | - n = ET.SubElement(parent, 'Number') | |
105 | - n.text=ast.value | |
106 | - | |
107 | - | |
108 | - def Setting2xml(self, ast, parent) ->None: | |
109 | - logger.debug(f"Setting2xml:: ast: {ast}:{type(ast).__name__}") | |
110 | - setting = ET.SubElement(parent, 'Setting') | |
111 | - self._ast2xml(ast.name, setting) | |
112 | - self._ast2xml(ast.value, setting) | |
113 | - | |
114 | - def Grammar2xml(self, ast, parent) ->None: | |
115 | - g = ET.SubElement(parent, 'Grammar', no_parse_rules=str(len(ast.parse_rules)), no_settings=str(len(ast.settings))) | |
116 | - self._ast2xml(ast._all_rules, g) | |
117 | - |
@@ -1,16 +0,0 @@ | ||
1 | - | |
2 | - | |
3 | -class Serialize (): | |
4 | - | |
5 | - def __new__(cls, strategy=None): | |
6 | - if strategy is None: | |
7 | - return object.__new__(cls) | |
8 | - if str(strategy).upper() == "XML": | |
9 | - from .ast2xml import XML_Serialize | |
10 | - return XML_Serialize() | |
11 | - else: | |
12 | - raise NotImplementedError(f"No Serializer of {strategy} available") | |
13 | - | |
14 | - def serialize(self, ast): | |
15 | - raise NotImplementedError(f"Implement in subclass") | |
16 | - |
@@ -0,0 +1,16 @@ | ||
1 | + | |
2 | + | |
3 | +class Serialize (): | |
4 | + | |
5 | + def __new__(cls, strategy=None): | |
6 | + if strategy is None: | |
7 | + return object.__new__(cls) | |
8 | + if str(strategy).upper() == "XML": | |
9 | + from .ast2xml import XML_Serialize | |
10 | + return XML_Serialize() | |
11 | + else: | |
12 | + raise NotImplementedError(f"No Serializer of {strategy} available") | |
13 | + | |
14 | + def serialize(self, ast): | |
15 | + raise NotImplementedError(f"Implement in subclass") | |
16 | + |
@@ -0,0 +1,117 @@ | ||
1 | +""" Support to translate an AST into XML. | |
2 | +This file should be used directly, instead use :class:`castle.ast.serialization.Serialize` as in ``Serialize('XML')`` | |
3 | +""" | |
4 | + | |
5 | +import logging; logger = logging.getLogger(__name__) | |
6 | +from castle.ast import serialization | |
7 | +from xml.etree import ElementTree as ET | |
8 | + | |
9 | + | |
10 | +class XML_Serialize(serialization.Serialize): | |
11 | + def serialize(self, ast) -> str: | |
12 | + logger.debug(f"serialize:: ast={ast._valType(ast)}") | |
13 | + | |
14 | + tree = self._ast2xml(ast) | |
15 | + return ET.tostring(tree, encoding="unicode") | |
16 | + | |
17 | + | |
18 | + def _ast2xml(self, ast, parent=None) -> ET.Element: | |
19 | + if parent is None: | |
20 | + parent = ET.Element('AST2XML', version="0.0") | |
21 | + | |
22 | + method_name = f'{type(ast).__name__}2xml' | |
23 | + visitor = getattr(self, method_name, None) | |
24 | + logger.debug(f'_ast2xml:: visitor={visitor}') | |
25 | + | |
26 | + if visitor: | |
27 | + visitor(ast=ast, parent=parent) # Grow the tree | |
28 | + else: | |
29 | + logger.info(f'No visitor >>{method_name}<<, skipping ... (fingers crossed)') | |
30 | + return parent | |
31 | + | |
32 | + | |
33 | + def ID2xml(self, ast, parent) ->None: | |
34 | + logger.debug(f"ID2xml:: ast={ast._valType(ast)} parent={parent} ast.name={ast.name}") | |
35 | + ET.SubElement(parent, 'ID', name=ast.name) | |
36 | + | |
37 | + | |
38 | +#NO_VISITOR_NEEDED: PEG2xml ## Pure Abstract | |
39 | +#NO_VISITOR_NEEDED: MixIn_value_attribute2xml ## MixIn | |
40 | +#NO_VISITOR_NEEDED: MixIn_expr_attribute2xml ## MixIn | |
41 | +#NO_VISITOR_NEEDED: MixIn_children_tuple2xml ## MixIn | |
42 | +#NO_VISITOR_NEEDED: Terminal2xml ## Pure Abstract | |
43 | +#NO_VISITOR_NEEDED: NonTerminal2xml ## Pure Abstract | |
44 | +#NO_VISITOR_NEEDED: Expression2xml ## Pure Abstract | |
45 | +#NO_VISITOR_NEEDED: Predicate2xml ## Pure Abstract | |
46 | +#NO_VISITOR_NEEDED: Group2xml ## Pure Abstract | |
47 | +#NO_VISITOR_NEEDED: Markers2xml ## Pure Abstract | |
48 | +#NO_VISITOR_NEEDED: Quantity2xml ## Pure Abstract | |
49 | +#NO_VISITOR_NEEDED: EOF2xml ## Not a real token | |
50 | +#NO_VISITOR_NEEDED: ParseRules2xml ## Handle in Rules2xml | |
51 | +#NO_VISITOR_NEEDED: Settings2xml ## Handle in Rules2xml | |
52 | + | |
53 | + | |
54 | + def _MixIn_value_attribute2xml(self, ast, parent, cls_name): | |
55 | + logger.debug(f"{cls_name}2xml:: ast={ast._valType(ast.value)}") | |
56 | + ET.SubElement(parent, cls_name, value=ast.value) | |
57 | + | |
58 | + def StrTerm2xml(self, ast, parent): self._MixIn_value_attribute2xml(ast, parent, 'StrTerm') | |
59 | + def RegExpTerm2xml(self, ast, parent): self._MixIn_value_attribute2xml(ast, parent, 'RegExpTerm') | |
60 | + | |
61 | + def Sequence2xml(self, ast, parent) ->None: | |
62 | + logger.debug(f"Sequence2xml::ast={ast._valType(ast._children)}") | |
63 | + seq = ET.SubElement(parent, 'Sequence') | |
64 | + for elm in ast: | |
65 | + self._ast2xml(elm, parent=seq) | |
66 | + | |
67 | + def Rule2xml(self, ast, parent) ->None: | |
68 | + logger.debug(f"Rule2xml:: ast:Rule.name={ast.name.name}") | |
69 | + rule = ET.SubElement(parent, 'Rule', name=ast.name.name) | |
70 | + self._ast2xml(ast.expr, parent=rule) | |
71 | + | |
72 | + def Rules2xml(self, ast, parent) ->None: | |
73 | + logger.debug(f"Rules2xml:: ast[{len(ast)}]") | |
74 | + for child in ast: | |
75 | + logger.debug(f'Rules2xml type(child)={type(child)}') | |
76 | + self._ast2xml(child, parent=parent) | |
77 | + | |
78 | + def _quantity_op2xml(self, ast, parent, tagName) -> None: | |
79 | + g = ET.SubElement(parent, tagName) | |
80 | + self._ast2xml(ast.expr, g) | |
81 | + | |
82 | + def UnorderedGroup2xml(self, ast, parent): self._quantity_op2xml(ast, parent, 'UnorderedGroup') | |
83 | + def Optional2xml(self, ast, parent): self._quantity_op2xml(ast, parent, 'Optional') | |
84 | + def ZeroOrMore2xml(self, ast, parent): self._quantity_op2xml(ast, parent, 'ZeroOrMore') | |
85 | + def OneOrMore2xml(self, ast, parent): self._quantity_op2xml(ast, parent, 'OneOrMore') | |
86 | + | |
87 | + | |
88 | + def OrderedChoice2xml(self, ast, parent) ->None: | |
89 | + oc = ET.SubElement(parent, 'OrderedChoice') | |
90 | + for c in ast: | |
91 | + self._ast2xml(c,oc) | |
92 | + | |
93 | + def _Predicate2xml(self, ast, parent, tagName) ->None: | |
94 | + logger.debug(f"_Predicate2xml.{tagName}:: expr: {ast.expr}:{type(ast.expr).__name__}") | |
95 | + predicate = ET.SubElement(parent, tagName) | |
96 | + self._ast2xml(ast.expr, predicate) | |
97 | + | |
98 | + def AndPredicate2xml(self, ast, parent): self._Predicate2xml(ast, parent,'AndPredicate') | |
99 | + def NotPredicate2xml(self, ast, parent): self._Predicate2xml(ast, parent,'NotPredicate') | |
100 | + | |
101 | + | |
102 | + def Number2xml(self, ast, parent) ->None: | |
103 | + logger.debug(f"Number2xml:: ast: {ast}:{type(ast).__name__}") | |
104 | + n = ET.SubElement(parent, 'Number') | |
105 | + n.text=ast.value | |
106 | + | |
107 | + | |
108 | + def Setting2xml(self, ast, parent) ->None: | |
109 | + logger.debug(f"Setting2xml:: ast: {ast}:{type(ast).__name__}") | |
110 | + setting = ET.SubElement(parent, 'Setting') | |
111 | + self._ast2xml(ast.name, setting) | |
112 | + self._ast2xml(ast.value, setting) | |
113 | + | |
114 | + def Grammar2xml(self, ast, parent) ->None: | |
115 | + g = ET.SubElement(parent, 'Grammar', no_parse_rules=str(len(ast.parse_rules)), no_settings=str(len(ast.settings))) | |
116 | + self._ast2xml(ast._all_rules, g) | |
117 | + |