From 898bee15817573b5ab865a1dae7e52da310affa8 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sat, 9 Nov 2024 16:27:54 -0500 Subject: Introduce a `Match` struct to represent pattern matching results, which improves the usability of a lot of the APIs. Also bugfix some issues with ranges. --- test/ranges.tm | 9 +++++++++ test/text.tm | 22 +++++++++++----------- 2 files changed, 20 insertions(+), 11 deletions(-) (limited to 'test') diff --git a/test/ranges.tm b/test/ranges.tm index c611957f..0ff67c1a 100644 --- a/test/ranges.tm +++ b/test/ranges.tm @@ -1,5 +1,14 @@ func main(): + >> r := 5:to(10):by(2) + = Range(first=5, last=10, step=2) + >> r.first + = 5 + >> r.last + = 10 + >> r.step + = 2 + >> Range(1, 5) == 1:to(5) = yes diff --git a/test/text.tm b/test/text.tm index 6ecd211b..cf95878e 100644 --- a/test/text.tm +++ b/test/text.tm @@ -169,17 +169,17 @@ func main(): = [] !! Test text:find_all() - >> " one two three ":find_all($/{alpha}/) - = ["one", "two", "three"] + >> " #one #two #three ":find_all($/#{alpha}/) + = [Match(text="#one", index=2, captures=["one"]), Match(text="#two", index=8, captures=["two"]), Match(text="#three", index=13, captures=["three"])] - >> " one two three ":find_all($/{!space}/) - = ["one", "two", "three"] + >> " #one #two #three ":find_all($/#{!space}/) + = [Match(text="#one", index=2, captures=["one"]), Match(text="#two", index=8, captures=["two"]), Match(text="#three", index=13, captures=["three"])] >> " ":find_all($/{alpha}/) = [] >> " foo(baz(), 1) doop() ":find_all($/{id}(?)/) - = ["foo(baz(), 1)", "doop()"] + = [Match(text="foo(baz(), 1)", index=2, captures=["foo", "baz(), 1"]), Match(text="doop()", index=17, captures=["doop", ""])] >> "":find_all($Pattern'') = [] @@ -189,13 +189,13 @@ func main(): !! Test text:find() >> " one two three ":find($/{id}/, start=-999) - = !Int + = !Match >> " one two three ":find($/{id}/, start=999) - = !Int + = !Match >> " one two three ":find($/{id}/) - = 2? + = Match(text="one", index=2, captures=["one"])? >> " one two three ":find($/{id}/, start=5) - = 8? + = Match(text="two", index=8, captures=["two"])? !! Test text slicing: >> "abcdef":slice() @@ -225,7 +225,7 @@ func main(): = !Text >> "one two; three four":find_all($/; {..}/) - = ["; three four"] + = [Match(text="; three four", index=8, captures=["three four"])] >> malicious := "{xxx}" >> $/$malicious/ @@ -261,7 +261,7 @@ func main(): else: fail("Failed to match") - >> "hello world":map($/world/, Text.upper) + >> "hello world":map($/world/, func(m:Match): m.text:upper()) = "hello WORLD" >> "Abc":repeat(3) -- cgit v1.2.3