1
0
mirror of https://github.com/janet-lang/janet synced 2025-11-08 11:33:02 +00:00

Add optional form to peg (shorthand for (between 0 1 patt)).

This commit is contained in:
Calvin Rose
2019-01-15 14:08:03 -05:00
parent f0fcdf6bc5
commit 4eeadd7463
3 changed files with 20 additions and 1 deletions

View File

@@ -713,7 +713,6 @@ static void spec_replace(Builder *b, int32_t argc, const Janet *argv) {
emit_2(r, RULE_REPLACE, subrule, constant);
}
/* For some and any, really just short-hand for (^ rule n) */
static void spec_repeater(Builder *b, int32_t argc, const Janet *argv, int32_t min) {
peg_fixarity(b, argc, 1);
Reserve r = reserve(b, 4);
@@ -744,6 +743,13 @@ static void spec_atmost(Builder *b, int32_t argc, const Janet *argv) {
emit_3(r, RULE_BETWEEN, 0, n, subrule);
}
static void spec_opt(Builder *b, int32_t argc, const Janet *argv) {
peg_fixarity(b, argc, 1);
Reserve r = reserve(b, 4);
uint32_t subrule = compile1(b, argv[0]);
emit_3(r, RULE_BETWEEN, 0, 1, subrule);
}
static void spec_matchtime(Builder *b, int32_t argc, const Janet *argv) {
peg_fixarity(b, argc, 2);
Reserve r = reserve(b, 3);
@@ -773,6 +779,7 @@ static const SpecialPair specials[] = {
{"/", spec_replace},
{"<-", spec_capture},
{">", spec_look},
{"?", spec_opt},
{"any", spec_any},
{"argument", spec_argument},
{"at-least", spec_atleast},
@@ -788,6 +795,7 @@ static const SpecialPair specials[] = {
{"if-not", spec_ifnot},
{"look", spec_look},
{"not", spec_not},
{"opt", spec_opt},
{"position", spec_position},
{"range", spec_range},
{"replace", spec_replace},