#! begin and end.\r
[ syntax ] 2dip syntax pack ;\r
\r
-: replace-escapes ( string -- string )\r
+#! Don't want to use 'replace' in an action since replace doesn't infer.\r
+#! Do the compilation of the peg at parse time and call (replace).\r
+PEG: escaper ( string -- ast )\r
[\r
"\\t" token [ drop "\t" ] action ,\r
"\\n" token [ drop "\n" ] action ,\r
"\\r" token [ drop "\r" ] action ,\r
- ] choice* replace ;\r
+ ] choice* any-char-parser 2array choice repeat0 ;\r
+\r
+: replace-escapes ( string -- string )\r
+ escaper sift [ [ tree-write ] each ] with-string-writer ;\r
\r
: insert-escapes ( string -- string )\r
[\r
M: ebnf-rule (transform) ( ast -- parser )\r
dup elements>> \r
(transform) [\r
- swap symbol>> dup get [ "Rule '" over append "' defined more than once" append throw ] [ set ] if\r
+ swap symbol>> dup get { [ tuple? ] [ delegate parser? ] } 1&& [ \r
+ "Rule '" over append "' defined more than once" append throw \r
+ ] [ \r
+ set \r
+ ] if\r
] keep ;\r
\r
M: ebnf-sequence (transform) ( ast -- parser )\r