• 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

Revision7156fb339e3311903043a33ebd30c776a742d3a4 (tree)
Zeit2021-12-23 08:20:52
AutorAlbert Mietus < albert AT mietus DOT nl >
CommiterAlbert Mietus < albert AT mietus DOT nl >

Log Message

Added ID (rule_name & rule_crossref) to AST/vistor

Ändern Zusammenfassung

Diff

diff -r bea0d751eb50 -r 7156fb339e33 AST/castle/_base.py
--- a/AST/castle/_base.py Mon Dec 20 23:48:17 2021 +0100
+++ b/AST/castle/_base.py Thu Dec 23 00:20:52 2021 +0100
@@ -17,7 +17,7 @@
1717
1818 import re
1919
20-class ID(str):
20+class ID(AST_BASE):
2121 _pattern = re.compile(r'[A-Za-z_][A-Za-z0-9_]*')
2222
2323 @staticmethod
@@ -27,3 +27,8 @@
2727 if ID._pattern.fullmatch(value) is None:
2828 raise IDError("not a valid pattern")
2929
30+ def __init__(self, *, name, **kwargs):
31+ super().__init__(**kwargs)
32+ self.validate_or_raise(name)
33+ self.name=name
34+
diff -r bea0d751eb50 -r 7156fb339e33 AST/castle/peg.py
--- a/AST/castle/peg.py Mon Dec 20 23:48:17 2021 +0100
+++ b/AST/castle/peg.py Thu Dec 23 00:20:52 2021 +0100
@@ -1,4 +1,4 @@
1-from ._base import AST_BASE, ID
1+from ._base import AST_BASE, ID, IDError
22
33 class PEG (AST_BASE): # abstract
44 """Base class of all PEG classes"""
@@ -72,7 +72,7 @@
7272 super().__init__(**kwargs)
7373 self.value=value
7474
75-
75+
7676 class OrderedChoice(Expression):pass
7777 class Predicate(Expression): pass # abstract
7878
diff -r bea0d751eb50 -r 7156fb339e33 Arpeggio/pytst/d2_ast/__init__.py
--- a/Arpeggio/pytst/d2_ast/__init__.py Mon Dec 20 23:48:17 2021 +0100
+++ b/Arpeggio/pytst/d2_ast/__init__.py Thu Dec 23 00:20:52 2021 +0100
@@ -0,0 +1,10 @@
1+import visitor
2+import arpeggio
3+
4+def parse(txt, rule):
5+ parser = arpeggio.ParserPython(rule)
6+ pt = parser.parse(txt)
7+ assert pt.position_end == len(txt), f"Did not parse all input txt=>>{txt}<<len={len(txt)} ==> parse_tree: >>{pt}<<_end={pt.position_end}"
8+ ast = arpeggio.visit_parse_tree(pt, visitor.PegVisitor())
9+ assert ast.position == 0 and ast.position_end == len(txt), f"Also the AST (type={type(ast)}) should include all input"
10+ return ast
diff -r bea0d751eb50 -r 7156fb339e33 Arpeggio/pytst/d2_ast/test_1_term.py
--- a/Arpeggio/pytst/d2_ast/test_1_term.py Mon Dec 20 23:48:17 2021 +0100
+++ b/Arpeggio/pytst/d2_ast/test_1_term.py Thu Dec 23 00:20:52 2021 +0100
@@ -1,20 +1,11 @@
11 import pytest
22
33 import grammar
4-import visitor
5-import arpeggio
64
75 import sys; sys.path.append("./../AST/") ; sys.path.append("./../../AST/")
86 from castle import peg # has the AST clases
97
10-
11-def parse(txt, rule):
12- parser = arpeggio.ParserPython(rule)
13- pt = parser.parse(txt)
14- assert pt.position_end == len(txt), "Did not parse all input" # JTBS
15- ast = arpeggio.visit_parse_tree(pt, visitor.PegVisitor())
16- assert ast.position == 0 and ast.position_end == len(txt), "Also the AST should include all input"
17- return ast
8+from . import parse
189
1910 def test_simple_str():
2011 txt="'a string'"
diff -r bea0d751eb50 -r 7156fb339e33 Arpeggio/visitor.py
--- a/Arpeggio/visitor.py Mon Dec 20 23:48:17 2021 +0100
+++ b/Arpeggio/visitor.py Thu Dec 23 00:20:52 2021 +0100
@@ -4,16 +4,25 @@
44 from castle import peg # has the AST clases
55
66 class PegVisitor(arpeggio.PTNodeVisitor):
7+
78 def visit_str_term(self, node, children):
89 ast = peg.StrTerm(value=node[1], parse_tree=node)
910 return ast
1011 def visit_regex_term(self, node, children):
1112 ast = peg.RegExpTerm(value=node[1], parse_tree=node)
1213 return ast
14+
15+ def visit_rule_name(self, node, children):
16+ ast = peg.ID(name=str(node), parse_tree=node)
17+ return ast
18+ def visit_rule_crossref(self, node, children):
19+ ast = peg.ID(name=str(node), parse_tree=node)
20+ return ast
21+
1322 def visit_single_expr(self, node, children): # [ rule_crossref, term, group, predicate ] Optional([ '?' , '*' , '+' , '#' ]))
1423 if len(children) == 1: # No optional part
1524 ast = peg.Sequence(value=children, parse_tree=node)
1625 else:
1726 assert NotImplementedError("To Do: visit_single_expr with optional part")
27+ return ast
1828
19- return ast