• R/O
  • SSH

Commit

Tags
Aucun tag

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

Révision033e1be89b60d1188a6bb7626202094a6470fc36 (tree)
l'heure2021-12-24 06:52:42
AuteurAlbert Mietus < albert AT mietus DOT nl >
CommiterAlbert Mietus < albert AT mietus DOT nl >

Message de Log

Trival 'rule' (two IDs) now works

Change Summary

Modification

diff -r dd3f259f732d -r 033e1be89b60 AST/castle/_base.py
--- a/AST/castle/_base.py Thu Dec 23 00:21:18 2021 +0100
+++ b/AST/castle/_base.py Thu Dec 23 22:52:42 2021 +0100
@@ -22,8 +22,9 @@
2222
2323 @staticmethod
2424 def validate_or_raise(value):
25- if not isinstance(value, (ID, str)):
26- raise IDError("not a str of ID")
25+ if isinstance(value, ID): return
26+ if not isinstance(value, str):
27+ raise IDError("not a str (or an ID)")
2728 if ID._pattern.fullmatch(value) is None:
2829 raise IDError("not a valid pattern")
2930
diff -r dd3f259f732d -r 033e1be89b60 AST/castle/peg.py
--- a/AST/castle/peg.py Thu Dec 23 00:21:18 2021 +0100
+++ b/AST/castle/peg.py Thu Dec 23 22:52:42 2021 +0100
@@ -51,7 +51,6 @@
5151 self.expr = expr
5252
5353
54-
5554 class Grammar(NonTerminal):
5655 def __init__(self, *,
5756 rules: list[Rule]=None,
@@ -71,6 +70,10 @@
7170 def __init__(self, *, value=None, **kwargs):
7271 super().__init__(**kwargs)
7372 self.value=value
73+ def __len__(self):
74+ return len(self.value)
75+ def __getitem__(self, n):
76+ return self.value[n]
7477
7578
7679 class OrderedChoice(Expression):pass
diff -r dd3f259f732d -r 033e1be89b60 Arpeggio/pytst/d2_ast/test_2_ID.py
--- a/Arpeggio/pytst/d2_ast/test_2_ID.py Thu Dec 23 00:21:18 2021 +0100
+++ b/Arpeggio/pytst/d2_ast/test_2_ID.py Thu Dec 23 22:52:42 2021 +0100
@@ -16,8 +16,23 @@
1616
1717 def test_rule_crossref():
1818 """The rule's expressions can also refer an ID"""
19- txt="aName"
19+ txt="aRef"
2020 ast = parse(txt, grammar.rule_crossref)
2121 assert isinstance(ast, peg.ID), "It should be an ID"
2222 assert ast.name == txt
2323
24+
25+def test_ID_as_single_expr():
26+ txt="aRef"
27+ ast = parse(txt, grammar.single_expr)
28+ assert isinstance(ast, peg.Expression), "A crossref is also an Expression"
29+ assert len(ast.value) == 1, "An expression with length==1"
30+ assert ast.value[0].name == txt, "The name of the (ID of the) Expression-value is still the same"
31+
32+def test_ID_as_expressions():
33+ txt="aRef"
34+ ast = parse(txt, grammar.expressions)
35+ assert isinstance(ast, peg.Expression), "A crossref is also an Expression"
36+ assert len(ast.value) == 1, "An expression with length==1"
37+ assert ast.value[0].name == txt, "The name of the (ID of the) Expression-value is still the same"
38+
diff -r dd3f259f732d -r 033e1be89b60 Arpeggio/pytst/d2_ast/test_3_rule.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Arpeggio/pytst/d2_ast/test_3_rule.py Thu Dec 23 22:52:42 2021 +0100
@@ -0,0 +1,22 @@
1+import pytest
2+
3+import grammar
4+
5+import sys; sys.path.append("./../AST/") ; sys.path.append("./../../AST/")
6+from castle import peg # has the AST clases
7+
8+from . import parse
9+
10+def test_trivial_rule_with_2IDS():
11+ """The most simple rule has only two IDs"""
12+ txt="trivial <- cross ;"
13+ ast = parse(txt, grammar.rule)
14+ assert isinstance(ast, peg.Rule), "It should be an ID"
15+
16+ name, expr = ast.name, ast.expr;
17+ assert isinstance(name, peg.ID)
18+ assert isinstance(expr, peg.Expression), "The expression is an Expression ..."
19+ assert isinstance(expr, peg.Sequence), " .. and a Sequence .."
20+ assert len(expr) ==1, " .. of length==1"
21+ assert name.name == txt.split()[0], "the name of the (ID of ) rule is the first ID"
22+ assert expr[0].name == txt.split()[2], "The single element of the expression is the 2nnd ID, which name os the 3 part of the txt"
diff -r dd3f259f732d -r 033e1be89b60 Arpeggio/visitor.py
--- a/Arpeggio/visitor.py Thu Dec 23 00:21:18 2021 +0100
+++ b/Arpeggio/visitor.py Thu Dec 23 22:52:42 2021 +0100
@@ -23,6 +23,9 @@
2323 if len(children) == 1: # No optional part
2424 ast = peg.Sequence(value=children, parse_tree=node)
2525 else:
26- assert NotImplementedError("To Do: visit_single_expr with optional part")
26+ assert NotImplementedError("To Do: visit_single_expr with optional part") # XXX
2727 return ast
2828
29+ def visit_rule(self, node, children): # Name '<-' expressions ';'
30+ ast = peg.Rule(name=children[0],expr=children[1], parse_tree=node)
31+ return ast