• R/O
  • SSH

Commit

Tags
Keine 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

Revision68702f02a7efd0206dfc5aa3f4829135969924f9 (tree)
Zeit2022-02-26 07:39:53
AutorAlbert Mietus < albert AT mietus DOT nl >
CommiterAlbert Mietus < albert AT mietus DOT nl >

Log Message

AST2XML:: Added Grammar-ast/class/xml; needed to refactor/rewrite the peg.Grammar inerface; to store the order. Adappted some tests

Ändern Zusammenfassung

Diff

diff -r 969863cb6c95 -r 68702f02a7ef castle/ast/ast2xml.py
--- a/castle/ast/ast2xml.py Fri Feb 25 17:04:14 2022 +0100
+++ b/castle/ast/ast2xml.py Fri Feb 25 23:39:53 2022 +0100
@@ -111,21 +111,7 @@
111111 self._ast2xml(ast.name, setting)
112112 self._ast2xml(ast.value, setting)
113113
114-
115-
116-#############
117-
118-
119-## logger.debug(f"Setting2xml:: ast[{len(ast)}]")
120-## ET.SubElement(parent, 'Setting', name=ast.name, value=ast.value)
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)
121117
122-## def Settings2xml(self, ast, parent) ->None: ...
123-# def Grammar2xml(self, ast, parent) ->None: ...
124-
125-
126-
127-
128-
129-# def NotPredicate2xml(self, ast, parent) ->None: ...
130-
131-
diff -r 969863cb6c95 -r 68702f02a7ef castle/ast/peg.py
--- a/castle/ast/peg.py Fri Feb 25 17:04:14 2022 +0100
+++ b/castle/ast/peg.py Fri Feb 25 23:39:53 2022 +0100
@@ -2,6 +2,7 @@
22
33 from ._base import AST_BASE, ID, IDError
44
5+import typing
56
67 class PEG (AST_BASE): # abstract
78 """Base class of all PEG classes"""
@@ -103,13 +104,13 @@
103104
104105 class Grammar(NonTerminal):
105106 def __init__(self, *,
106- rules: ParseRules=None,
107- settings: Settings=None,
107+ all_rules: typing.Sequence[Rule],
108108 **kwargs):
109- logger.debug(f'{self._typeName(self)}:: rules={rules}; settings={settings}, kwargs={kwargs}')
110109 super().__init__(**kwargs)
111- self.rules = rules
112- self.settings = settings
110+ self._all_rules = Rules(children=all_rules) # store `all_rules` in once, to be able to remember the order
111+ self.parse_rules = ParseRules( children=[r for r in self._all_rules if isinstance(r, Rule)] )
112+ self.settings = Settings( children=[r for r in self._all_rules if isinstance(r, Setting)] )
113+ assert len(all_rules) == len(self.parse_rules) + len(self.settings)
113114
114115
115116 class Group(Expression): pass # abstract -- Note: Do not Group for '(' ...')'; that's a Sequence!!
diff -r 969863cb6c95 -r 68702f02a7ef castle/readers/parser/visitor.py
--- a/castle/readers/parser/visitor.py Fri Feb 25 17:04:14 2022 +0100
+++ b/castle/readers/parser/visitor.py Fri Feb 25 23:39:53 2022 +0100
@@ -117,13 +117,7 @@
117117
118118
119119 def visit_peg_grammar(self, node, children):
120- rules=children[0]
121- logger.debug('visit_peg_grammar::' + self._logstr_node_children(node, children) + f'rules:: {rules}')
122- parse_rules = peg.ParseRules( children=[r for r in rules if isinstance(r, peg.Rule)] )
123- settings = peg.Settings( children=[r for r in rules if isinstance(r, peg.Setting)] )
124- assert len(rules) == len(parse_rules) + len(settings), f'Number of parse_rules ({len(parse_rules)}) and settings ({len(settings)}), does not match total: {len(rules)}'
125- return peg.Grammar(rules=parse_rules, settings=settings, parse_tree=node)
126-
120+ return peg.Grammar(all_rules=children[0], parse_tree=node)
127121
128122 def visit_setting_name(self, node, children):
129123 return peg.ID(name=str(node), parse_tree=node)
diff -r 969863cb6c95 -r 68702f02a7ef pytst/ast/XML_serialization/test_6_grammar.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/pytst/ast/XML_serialization/test_6_grammar.py Fri Feb 25 23:39:53 2022 +0100
@@ -0,0 +1,39 @@
1+import pytest
2+import logging; logger = logging.getLogger(__name__)
3+
4+from castle.ast import peg
5+
6+from . import xml_serialize #@pytest.fixture
7+from . import assert_xml_Element, StdSequence_withAsserts
8+
9+def test_a_Grammar_with_ParseRules_only(xml_serialize):
10+ name1, seq1 = 'aParseRule', StdSequence_withAsserts()
11+ name2, seq2 = 'rule_2', StdSequence_withAsserts()
12+ r1 = peg.Rule(name=peg.ID(name=name1), expr=seq1.seq)
13+ r2 = peg.Rule(name=peg.ID(name=name2), expr=seq2.seq)
14+
15+ g = peg.Grammar(all_rules=(r1,r2))
16+ txt = xml_serialize(g)
17+ logger.debug(f'XML:: {txt}')
18+
19+ assert_xml_Element(txt, tag='Grammar', child_count=2)
20+ assert_xml_Element(txt, tag='Grammar/Rule[1]', name=name1)
21+ assert_xml_Element(txt, tag='Grammar/Rule[2]', name=name2)
22+
23+def test_a_Grammar_with_MixedRules(xml_serialize):
24+ name1, seq1 = 'aParseRule', StdSequence_withAsserts()
25+ name2, val2 = 'setting2', "1965"
26+ name3, seq3 = 'rule_3', StdSequence_withAsserts()
27+ r1 = peg.Rule(name=peg.ID(name=name1), expr=seq1.seq)
28+ r2 = peg.Setting(name=peg.ID(name=name2), value=peg.Number(value=val2))
29+ r3 = peg.Rule(name=peg.ID(name=name3), expr=seq3.seq)
30+
31+ g = peg.Grammar(all_rules=(r1,r2,r3))
32+ txt = xml_serialize(g)
33+ logger.debug(f'XML:: {txt}')
34+
35+ assert_xml_Element(txt, tag='Grammar', child_count=3)
36+ assert_xml_Element(txt, tag='Grammar/Rule[1]', name=name1)
37+ assert_xml_Element(txt, tag='Grammar/Setting/ID', name=name2)
38+ assert_xml_Element(txt, tag='Grammar/Rule[2]', name=name3)
39+
diff -r 969863cb6c95 -r 68702f02a7ef pytst/readers/parser/d2_ast/__init__.py
--- a/pytst/readers/parser/d2_ast/__init__.py Fri Feb 25 17:04:14 2022 +0100
+++ b/pytst/readers/parser/d2_ast/__init__.py Fri Feb 25 23:39:53 2022 +0100
@@ -61,7 +61,7 @@
6161 def assert_PEG(ast, *, no_of_rules=None, no_of_settings=None):
6262 assert isinstance(ast, peg.Grammar)
6363
64- rules = ast.rules
64+ rules = ast.parse_rules
6565 assert isinstance(rules, peg.Rules)
6666 if no_of_rules:
6767 assert len(rules) == no_of_rules, f"The number of (parse_)rules ({len(rules)}) does not match the spec: {no_of_rules}"