Castle: The best Real-Time/Embedded/HighTech language EVER. Attempt 2
Revision | cd3f73efabaf2d7867cceab8974c70f5c575cb4a (tree) |
---|---|
Zeit | 2021-12-21 06:14:58 |
Autor | Albert Mietus < albert AT mietus DOT nl > |
Commiter | Albert Mietus < albert AT mietus DOT nl > |
Added RegEx visitor (and tests)
@@ -29,3 +29,32 @@ | ||
29 | 29 | assert isinstance(ast, peg.Terminal), "It should be a term ..." |
30 | 30 | assert isinstance(ast, peg.StrTerm), "... and a str" |
31 | 31 | assert ast.value == "triple string", "It's correct value should be without quotes" |
32 | + | |
33 | +def test_regex_RE(): | |
34 | + txt='/a reg.ex/' | |
35 | + ast = parse(txt, grammar.term) | |
36 | + assert isinstance(ast, peg.Terminal), "It should be a term ..." | |
37 | + assert isinstance(ast, peg.RegExpTerm), "... and a RegExp" | |
38 | + assert ast.value == 'a reg.ex', "It's correct value should be without slahes -- note: the regex itself is a string" | |
39 | + | |
40 | +def regex_variants(txt, expect): | |
41 | + ast = parse(txt, grammar.term) | |
42 | + assert isinstance(ast, peg.Terminal), "It should be a term ..." | |
43 | + assert isinstance(ast, peg.RegExpTerm), "... and a RegExp" | |
44 | + assert ast.value == expect, "And the regex-pre/postfix should be removed from the value" | |
45 | + | |
46 | +def testregex_variants(): | |
47 | + regex_variants(txt:="""/a reg.ex/""", expect=txt[1:-1]) # Same a test_regex_RE | |
48 | + regex_variants(txt:="""/re_slash/""", expect=txt[1:-1]) | |
49 | + | |
50 | + regex_variants(txt:="""R're__Rstr_s1'""", expect=txt[2:-1]) | |
51 | + regex_variants(txt:="""r're__rstr_s1'""", expect=txt[2:-1]) | |
52 | + regex_variants(txt:='''R"re__Rstr_d1"''', expect=txt[2:-1]) | |
53 | + regex_variants(txt:='''r"re__rstr_d1"''', expect=txt[2:-1]) | |
54 | + | |
55 | + regex_variants(txt:="""R'''re__Rstr_s3'''""", expect=txt[4:-3]) | |
56 | + regex_variants(txt:="""r'''re__rstr_s3'''""", expect=txt[4:-3]) | |
57 | + regex_variants(txt:='''R"""re__Rstr_d3"""''', expect=txt[4:-3]) | |
58 | + regex_variants(txt:='''r"""re__rstr_d3"""''', expect=txt[4:-3]) | |
59 | + | |
60 | + |
@@ -7,3 +7,6 @@ | ||
7 | 7 | def visit_str_term(self, node, children): |
8 | 8 | ast = peg.StrTerm(value=node[1], parse_tree=node) |
9 | 9 | return ast |
10 | + def visit_regex_term(self, node, children): | |
11 | + ast = peg.RegExpTerm(value=node[1], parse_tree=node) | |
12 | + return ast |