Castle: The best Real-Time/Embedded/HighTech language EVER. Attempt 2
Revision | 937f62a25c9654fa389ade623863ae6d59fd0f04 (tree) |
---|---|
Time | 2022-02-16 06:51:05 |
Author | Albert Mietus < albert AT mietus DOT nl > |
Commiter | Albert Mietus < albert AT mietus DOT nl > |
Merged: Sequence is now a list/tuple of children; ._value is gone
@@ -16,10 +16,10 @@ | ||
16 | 16 | |
17 | 17 | python-demo: |
18 | 18 | @echo Running all 'dem*.py' python-files |
19 | - for d in `find demos -type f -iname \*.py `; do echo "=== $$d ==="; python $$d; echo "=========="; done | |
19 | + export PYTHONPATH=`pwd`; for d in `find demos -type f -iname \*.py `; do echo "=== $$d ==="; python $$d; echo "=========="; done | |
20 | 20 | |
21 | 21 | pytest-demo: |
22 | - pytest -s demos | |
22 | + PYTHONPATH=`pwd` pytest -s demos | |
23 | 23 | |
24 | 24 | |
25 | 25 | missing_visitor: castle/readers/parser/grammar.py |
@@ -57,9 +57,9 @@ | ||
57 | 57 | def RegExpTerm2xml(self, ast, parent): self._MixIn_value_attribute2xml(ast, parent, 'RegExpTerm') |
58 | 58 | |
59 | 59 | def Sequence2xml(self, ast, parent) ->None: |
60 | - logger.debug(f"Sequence2xml::ast={ast._valType(ast.value)}") | |
60 | + logger.debug(f"Sequence2xml::ast={ast._valType(ast._children)}") | |
61 | 61 | seq = ET.SubElement(parent, 'Sequence') |
62 | - for elm in ast.value: | |
62 | + for elm in ast: | |
63 | 63 | self._ast2xml(elm, parent=seq) |
64 | 64 | |
65 | 65 | def Rule2xml(self, ast, parent) ->None: |
@@ -44,13 +44,13 @@ | ||
44 | 44 | logger.debug(f'{self._typeName(self)}.MixIn_children_tuple:: children[{len(children)}]:=' + |
45 | 45 | ('[[' +', '.join(f'{c}:{type(c).__name__}' for c in children) + ']]') if isinstance(children, list) else f's>>{children}<<') |
46 | 46 | super().__init__(**kwargs) |
47 | - self._childeren = tuple(children) | |
47 | + self._children = tuple(children) | |
48 | 48 | |
49 | 49 | def __len__(self): |
50 | - return len(self._childeren) | |
50 | + return len(self._children) | |
51 | 51 | def __getitem__(self, key): |
52 | - return self._childeren[key] | |
53 | - def __iter__(self): return self._childeren.__iter__() | |
52 | + return self._children[key] | |
53 | + def __iter__(self): return self._children.__iter__() | |
54 | 54 | |
55 | 55 | |
56 | 56 | ## |
@@ -125,21 +125,20 @@ | ||
125 | 125 | """An expression with Quantification; like optional, or repetition. The subclasses defines which Quantification""" |
126 | 126 | |
127 | 127 | |
128 | -class Sequence(MixIn_value_attribute, Expression): | |
129 | - """A _list_ of expressions; can be of length=1""" | |
130 | - # __init__ (see MixIn) sets self._value; assuming it is a list | |
131 | - | |
132 | - def __len__(self): return len(self._value) | |
133 | - def __getitem__(self, n): return self._value[n] | |
128 | +#class Sequence(MixIn_value_attribute, Expression): | |
129 | +class Sequence(MixIn_children_tuple, Expression): | |
130 | + """A sequence of expressions; can be of length=1""" | |
131 | + # __init__ (see MixIn) sets self._children; assuming it is a list | |
134 | 132 | |
135 | 133 | def __str__(self): # mostly for debugging |
136 | - return "Seq{{" + " ; ".join(f"{c}" for c in self._value) + "}}" # XXX ToDo: _value -> children | |
134 | + return "Seq{{" + " ; ".join(f"{c}" for c in self) + "}}" | |
135 | + | |
137 | 136 | |
138 | 137 | class OrderedChoice(MixIn_children_tuple, Expression): # A | B | C | ... the order is relevant |
139 | 138 | """OC: A _tuple_ of alternative expressions""" |
140 | 139 | |
141 | 140 | def __str__(self): # mostly for debugging |
142 | - return "OC{{" + " | ".join(f"{c}" for c in self._childeren) + "}}" | |
141 | + return "OC{{" + " | ".join(f"{c}" for c in self._children) + "}}" | |
143 | 142 | |
144 | 143 | class Optional(Quantity):pass |
145 | 144 | class ZeroOrMore(Quantity):pass |
@@ -91,7 +91,7 @@ | ||
91 | 91 | # OneOrMore(single_expr) |
92 | 92 | def visit_sequence(self, node, children) -> peg.Sequence: |
93 | 93 | logger.debug(f'visit_sequence::{self._logstr_node_children(node, children)}') |
94 | - return peg.Sequence(value=children, parse_tree=node) | |
94 | + return peg.Sequence(children=children, parse_tree=node) | |
95 | 95 | |
96 | 96 | |
97 | 97 | def visit_predicate(self, node, children): |
@@ -12,7 +12,7 @@ | ||
12 | 12 | e1 = peg.ID(name=self.n1) |
13 | 13 | e2 = peg.StrTerm(value=self.v2) |
14 | 14 | e3 = peg.RegExpTerm(value=self.v3) |
15 | - self.seq = peg.Sequence(value=[e1, e2, e3]) | |
15 | + self.seq = peg.Sequence(children=[e1, e2, e3]) | |
16 | 16 | def assert_xml_Element(self, txt): |
17 | 17 | assert_xml_Element(txt, tag='.//Sequence') |
18 | 18 | assert_xml_Element(txt, tag='.//ID', name=self.n1) |
@@ -30,7 +30,7 @@ | ||
30 | 30 | |
31 | 31 | def test_Sequence_1(xml_serialize): |
32 | 32 | e1 = peg.ID(name='ID_1') |
33 | - txt= xml_serialize(peg.Sequence(value=[e1])) | |
33 | + txt= xml_serialize(peg.Sequence(children=[e1])) | |
34 | 34 | logger.debug(f'XML:: {txt}') |
35 | 35 | assert_xml_Element(txt, tag='Sequence') |
36 | 36 | assert_xml_Element(txt, tag='.//ID', name='ID_1') |
@@ -48,7 +48,7 @@ | ||
48 | 48 | def test_Rule_1ID(xml_serialize): |
49 | 49 | rule_name = "RuleName" |
50 | 50 | xref = "cross_ref" |
51 | - expr = peg.Sequence(value=[peg.ID(name=xref)]) | |
51 | + expr = peg.Sequence(children=[peg.ID(name=xref)]) | |
52 | 52 | |
53 | 53 | txt = xml_serialize(peg.Rule(name=peg.ID(name=rule_name), expr=expr)) |
54 | 54 | logger.debug(f'XML:: {txt}') |
@@ -69,8 +69,8 @@ | ||
69 | 69 | |
70 | 70 | |
71 | 71 | def test_Rules(xml_serialize): |
72 | - r1 = peg.Rule(name=peg.ID(name='rule_1'), expr=peg.Sequence(value=[peg.ID(name='id1')])) | |
73 | - r2 = peg.Rule(name=peg.ID(name='rule_2'), expr=peg.Sequence(value=[peg.StrTerm(value='str2')])) | |
72 | + r1 = peg.Rule(name=peg.ID(name='rule_1'), expr=peg.Sequence(children=[peg.ID(name='id1')])) | |
73 | + r2 = peg.Rule(name=peg.ID(name='rule_2'), expr=peg.Sequence(children=[peg.StrTerm(value='str2')])) | |
74 | 74 | |
75 | 75 | txt = xml_serialize(peg.Rules(children=[r1,r2])) |
76 | 76 | logger.debug(f'XML:: {txt}') |
@@ -35,7 +35,7 @@ | ||
35 | 35 | |
36 | 36 | def assert_Seq(ast, length=None, ids=None): |
37 | 37 | assert isinstance(ast, peg.Sequence) |
38 | - assert isinstance(ast, peg.Expression), "A sequence is aslo an Expression()" | |
38 | + assert isinstance(ast, peg.Expression), "A sequence is also an Expression()" | |
39 | 39 | if length: |
40 | 40 | assert len(ast) == length, f" ... of specified length=={length}" |
41 | 41 | if ids: |
@@ -55,5 +55,5 @@ | ||
55 | 55 | ast = parse(txt, grammar.expression) |
56 | 56 | # result is same a above |
57 | 57 | assert isinstance(ast, peg.Expression), "A (str)term is also an Expression" |
58 | - assert len(ast.value) == 1, "An expression with length==1" | |
59 | - assert ast.value[0].value == txt[1:-1], "It's correct value should be without quotes" | |
58 | + assert len(ast) == 1, "with a lengt of 1 -- note: use: ``len(sequence)`` not ``len(sequence._children)``!!" | |
59 | + assert ast[0].value == txt[1:-1], "It's correct value should be without quotes" |
@@ -29,5 +29,5 @@ | ||
29 | 29 | ast = parse(txt, grammar.expression) |
30 | 30 | |
31 | 31 | assert isinstance(ast, peg.Expression), "A crossref is also an Expression" |
32 | - assert len(ast.value) == 1, "An expression with length==1" | |
33 | - assert_ID(ast.value[0], name=txt, err_message= "The name of the (ID of the) Expression-value is still the same") | |
32 | + assert len(ast) == 1, "An expression with length==1" | |
33 | + assert_ID(ast[0], name=txt, err_message= "The name of the (ID of the) Expression-value is still the same") |
@@ -25,16 +25,14 @@ | ||
25 | 25 | def test_seq_of_two_as_expression(): |
26 | 26 | txt = "A B" |
27 | 27 | ast = parse(txt, grammar.expression) |
28 | + logger.debug(f'seq2expr:: ast={ast}:{type(ast).__name__}') | |
29 | + assert_Seq(ast, length=2, ids=('A', 'B')) | |
28 | 30 | |
29 | - assert_Seq(ast, 2, ids=('A', 'B')) | |
30 | - assert isinstance(ast.value, list), "It will be an `arpeggio.SemanticActionResult` which is a subclass of list" | |
31 | 31 | |
32 | 32 | def test_seq_of_three_as_expression(): |
33 | 33 | txt = "A B C" |
34 | 34 | ast = parse(txt, grammar.expression) |
35 | - | |
36 | - assert_Seq(ast, 3, ids=('A', 'B', 'C')) | |
37 | - assert isinstance(ast.value, list), "It will be an `arpeggio.SemanticActionResult` which is a subclass of list" | |
35 | + assert_Seq(ast, length=3, ids=('A', 'B', 'C')) | |
38 | 36 | |
39 | 37 | |
40 | 38 | def test_seq_of_three_with_quantification(): |