diff --git a/src/core/peg.c b/src/core/peg.c index 238facb8..877338b6 100644 --- a/src/core/peg.c +++ b/src/core/peg.c @@ -1028,6 +1028,14 @@ static uint32_t peg_compile1(Builder *b, Janet peg) { const Janet *tup = janet_unwrap_tuple(peg); int32_t len = janet_tuple_length(tup); if (len == 0) peg_panic(b, "tuple in grammar must have non-zero length"); + if (janet_checkint(tup[0])) { + int32_t n = janet_unwrap_integer(tup[0]); + if (n < 0) { + peg_panicf(b, "expected non-negative integer, got %d", n); + } + spec_repeat(b, len, tup); + break; + } if (!janet_checktype(tup[0], JANET_SYMBOL)) peg_panicf(b, "expected grammar command, found %v", tup[0]); const uint8_t *sym = janet_unwrap_symbol(tup[0]); diff --git a/test/suite8.janet b/test/suite8.janet index 93165489..71f1b0ff 100644 --- a/test/suite8.janet +++ b/test/suite8.janet @@ -301,4 +301,10 @@ neldb\0\0\0\xD8\x05printG\x01\0\xDE\xDE\xDE'\x03\0marshal_tes/\x02 (assert (= "abc" (0 (peg/match peg4 "(abc)"))) "peg thru/to 2") (assert (not (peg/match peg4 "123(abc")) "peg thru/to 3") +(def peg5 (peg/compile [3 "abc"])) + +(assert (:match peg5 "abcabcabc") "repeat alias 1") +(assert (:match peg5 "abcabcabcac") "repeat alias 2") +(assert (not (:match peg5 "abcabc")) "repeat alias 3") + (end-suite)