I'm looking at reducers.
There is a nice example in the Tutor for counting words:
(0 | it + 1 | /\w+/ := S)
where S
is some longer string with several words. The reducer returns the count of such words.
I was wondering how to capture the matched substring and use it in the accumulating expression, something like
("" | it + e | str e ... /\w+/ := S)
so that the result would be the concatenation of all matched substrings.
Any idea?
Yes, the capture syntax is with the <name:regex>
notation:
("" | it + e | /<e:\w+>/ := S)
rascal>S ="Jabberwocky by Lewis Carroll";
str: "Jabberwocky by Lewis Carroll"
rascal>("" | "<it>,<e>" | /<e:\w+>/ := S)[1..]
str: "Jabberwocky,by,Lewis,Carroll"
or use the for-template syntax instead of a reducer expression:
rascal>x = "<for (/<e:\w+>/ := S) {><e>;
>>>>>>> '<}>";
str: "Jabberwocky;\nby;\nLewis;\nCarroll;\n"
rascal>import IO;
ok
rascal>println(x)
Jabberwocky;
by;
Lewis;
Carroll;
ok
rascal>