Castle: The best Real-Time/Embedded/HighTech language EVER. Attempt 2
Revision | d89d215a0dd2cabf316ed1f1a49a2120a338833e (tree) |
---|---|
Time | 2021-12-20 06:03:53 |
Author | Albert Mietus < albert AT mietus DOT nl > |
Commiter | Albert Mietus < albert AT mietus DOT nl > |
Another peg.StrTerm test
@@ -1,13 +1,15 @@ | ||
1 | 1 | class AST_BASE: |
2 | 2 | """Base class for all Castle ATS nodes""" |
3 | 3 | |
4 | - def __init__(self, *, parse_tree=None): | |
4 | + def __init__(self, *, parse_tree=None, **kwargs): | |
5 | + super().__init__(**kwargs) | |
5 | 6 | self._parse_tree = parse_tree |
6 | 7 | |
8 | + | |
7 | 9 | @property |
8 | - def position(self): return self.parse_tree.position | |
10 | + def position(self): return self._parse_tree.position | |
9 | 11 | @property |
10 | - def position_end(self): return self.parse_tree.position_end | |
12 | + def position_end(self): return self._parse_tree.position_end | |
11 | 13 | |
12 | 14 | |
13 | 15 | class IDError(ValueError): |
@@ -2,6 +2,9 @@ | ||
2 | 2 | |
3 | 3 | class PEG (AST_BASE): # abstract |
4 | 4 | """Base class of all PEG classes""" |
5 | + def __init__(self, **kwargs): | |
6 | + super().__init__(**kwargs) | |
7 | + | |
5 | 8 | |
6 | 9 | ## |
7 | 10 | ## Note: When using TypeHints with PEG-classes; the clases |
@@ -13,6 +16,8 @@ | ||
13 | 16 | def __init__(self, *, value=None, **kwargs): |
14 | 17 | super().__init__(**kwargs) |
15 | 18 | self.value=value |
19 | + | |
20 | + | |
16 | 21 | class NonTerminal(PEG): pass # abstract |
17 | 22 | class Markers(PEG): pass # abstract |
18 | 23 |
@@ -24,7 +29,6 @@ | ||
24 | 29 | class Expression(NonTerminal): pass # abstract |
25 | 30 | |
26 | 31 | |
27 | - | |
28 | 32 | class Setting(PEG): |
29 | 33 | def __init__(self, *, |
30 | 34 | name: ID, |
@@ -36,7 +40,6 @@ | ||
36 | 40 | self.value = value |
37 | 41 | |
38 | 42 | |
39 | - | |
40 | 43 | class Rule(NonTerminal): |
41 | 44 | def __init__(self, *, |
42 | 45 | name: ID, |
@@ -21,7 +21,20 @@ | ||
21 | 21 | return ast |
22 | 22 | |
23 | 23 | def test_simple_str(): |
24 | - ast = parse("'a string'", grammar.term) | |
24 | + txt="'a string'" | |
25 | + ast = parse(txt, grammar.term) | |
25 | 26 | assert isinstance(ast, peg.Terminal), "It should be a term ..." |
26 | 27 | assert isinstance(ast, peg.StrTerm), "... and a str" |
27 | 28 | assert ast.value == "a string", "It's correct value should be without quotes" |
29 | + assert ast.position == 0, "The term's position includes the quotes ..." | |
30 | + assert ast.position_end == len(txt), " ... on both ends." | |
31 | + | |
32 | +def test_simple_str_2(): | |
33 | + txt='"""triple string"""' | |
34 | + ast = parse(txt, grammar.term) | |
35 | + assert isinstance(ast, peg.Terminal), "It should be a term ..." | |
36 | + assert isinstance(ast, peg.StrTerm), "... and a str" | |
37 | + assert ast.value == "triple string", "It's correct value should be without quotes" | |
38 | + assert ast.position == 0, "The term's position includes the quotes ..." | |
39 | + assert ast.position_end == len(txt), " ... on both ends." | |
40 | + |