Castle: The best Real-Time/Embedded/HighTech language EVER. Attempt 2
Révision | 2698c678f331b930955b06d039dcccce32f48ca3 (tree) |
---|---|
l'heure | 2022-02-08 23:30:23 |
Auteur | Albert Mietus < albert AT mietus DOT nl > |
Commiter | Albert Mietus < albert AT mietus DOT nl > |
AST-2-XML: UnorderedGroup works. Refactored log and renamed aux-class: StdSequence_withAsserts
@@ -9,7 +9,7 @@ | ||
9 | 9 | |
10 | 10 | class XML_Serialize(serialization.Serialize): |
11 | 11 | def serialize(self, ast) -> str: |
12 | - logger.debug(f"ast={ast._valType(ast)}") | |
12 | + logger.debug(f"serialize:: ast={ast._valType(ast)}") | |
13 | 13 | |
14 | 14 | tree = self._ast2xml(ast) |
15 | 15 | return ET.tostring(tree, encoding="unicode") |
@@ -21,7 +21,7 @@ | ||
21 | 21 | |
22 | 22 | method_name = f'{type(ast).__name__}2xml' |
23 | 23 | visitor = getattr(self, method_name, None) |
24 | - logger.debug(f'visitor={visitor}') | |
24 | + logger.debug(f'_ast2xml:: visitor={visitor}') | |
25 | 25 | |
26 | 26 | if visitor: |
27 | 27 | visitor(ast=ast, parent=parent) # Grow the tree |
@@ -31,7 +31,7 @@ | ||
31 | 31 | |
32 | 32 | |
33 | 33 | def ID2xml(self, ast, parent) ->None: |
34 | - logger.debug(f"ast={ast._valType(ast)} parent={parent} ast.name={ast.name}") | |
34 | + logger.debug(f"ID2xml:: ast={ast._valType(ast)} parent={parent} ast.name={ast.name}") | |
35 | 35 | ET.SubElement(parent, 'ID', name=ast.name) |
36 | 36 | |
37 | 37 |
@@ -71,12 +71,19 @@ | ||
71 | 71 | logger.debug(f'Rules2xml type(child)={type(child)}') |
72 | 72 | self._ast2xml(child, parent=parent) |
73 | 73 | |
74 | + def UnorderedGroup2xml(self, ast, parent) ->None: | |
75 | + g = ET.SubElement(parent, 'UnorderedGroup') | |
76 | + self._ast2xml(ast.expr, g) | |
77 | + | |
74 | 78 | ############# |
75 | 79 | |
76 | -# def Setting2xml(self, ast, parent) ->None: ... | |
77 | -# def Settings2xml(self, ast, parent) ->None: ... | |
80 | +## def Setting2xml(self, ast, parent) ->None: | |
81 | +## logger.debug(f"Setting2xml:: ast[{len(ast)}]") | |
82 | +## ET.SubElement(parent, 'Setting', name=ast.name, value=ast.value) | |
83 | + | |
84 | +## def Settings2xml(self, ast, parent) ->None: ... | |
78 | 85 | # def Grammar2xml(self, ast, parent) ->None: ... |
79 | -# def UnorderedGroup2xml(self, ast, parent) ->None: ... | |
86 | + | |
80 | 87 | # def Quantity2xml(self, ast, parent) ->None: ... |
81 | 88 | |
82 | 89 | # def OrderedChoice2xml(self, ast, parent) ->None: ... |
@@ -5,7 +5,7 @@ | ||
5 | 5 | from castle.ast import peg, serialization |
6 | 6 | |
7 | 7 | |
8 | -class Sequence: | |
8 | +class StdSequence_withAsserts: | |
9 | 9 | def __init__(self): |
10 | 10 | self.n1, self.v2, self.v3 = 'ID_1', 'str_2', 'regexp_3' |
11 | 11 | e1 = peg.ID(name=self.n1) |
@@ -22,6 +22,12 @@ | ||
22 | 22 | def assert_xml_Element(txt, tag, |
23 | 23 | version="0.0", |
24 | 24 | **attribs,): |
25 | + """Partially verify an xml-string; focusing on 'tag' -- a real tag, or a (limited) XPATH-expression. | |
26 | + | |
27 | + This `tag` (expression) should result in a single hit!. *Use e.g `[0]` as suffix to select one from a list*. | |
28 | + Pass ``key=value`` **attribs** to verify the found tag has those attribs and values | |
29 | + """ | |
30 | + | |
25 | 31 | tree = ET.fromstring(txt) |
26 | 32 | if version: |
27 | 33 | assert tree.attrib['version'] == version |
@@ -65,7 +71,7 @@ | ||
65 | 71 | |
66 | 72 | |
67 | 73 | def test_Sequence_3(xml_serialize): |
68 | - seq = Sequence() | |
74 | + seq = StdSequence_withAsserts() | |
69 | 75 | txt= xml_serialize(seq.seq) |
70 | 76 | logger.debug(f'XML:: {txt}') |
71 | 77 |
@@ -87,7 +93,7 @@ | ||
87 | 93 | |
88 | 94 | def test_Rule_Sequence(xml_serialize): |
89 | 95 | rule_name = "Rule_Sequence" |
90 | - seq = Sequence() | |
96 | + seq = StdSequence_withAsserts() | |
91 | 97 | |
92 | 98 | txt = xml_serialize(peg.Rule(name=peg.ID(name=rule_name), expr=seq.seq)) |
93 | 99 | logger.debug(f'XML:: {txt}') |
@@ -110,3 +116,11 @@ | ||
110 | 116 | |
111 | 117 | assert tree.findall('.//Rule[1]')[0].attrib['name'] == 'rule_1' |
112 | 118 | assert tree.findall('.//Rule[2]//StrTerm')[0].attrib['value'] == 'str2' |
119 | + | |
120 | +def test_UnorderedGroup(xml_serialize): | |
121 | + seq = StdSequence_withAsserts() | |
122 | + txt = xml_serialize(peg.UnorderedGroup(expr=seq.seq)) | |
123 | + logger.debug(f'XML:: {txt}') | |
124 | + | |
125 | + assert_xml_Element(txt, 'UnorderedGroup') | |
126 | + seq.assert_xml_Element(txt) |