Merge pull request #1187 from CosmicToast/peg-boolean

peg: add support for "true" and "false" primitives to always/never match
This commit is contained in:
Calvin Rose 2023-06-18 09:40:46 -05:00 committed by GitHub
commit 1ef5c038db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 0 deletions

View File

@ -735,6 +735,12 @@ static const uint8_t *peg_getrange(Builder *b, Janet x) {
return str;
}
static int32_t peg_getboolean(Builder *b, Janet x) {
if (!janet_checktype(x, JANET_BOOLEAN))
peg_panicf(b, "expected boolean, got %v", x);
return janet_unwrap_boolean(x);
}
static int32_t peg_getinteger(Builder *b, Janet x) {
if (!janet_checkint(x))
peg_panicf(b, "expected integer, got %v", x);
@ -1261,6 +1267,13 @@ static uint32_t peg_compile1(Builder *b, Janet peg) {
default:
peg_panic(b, "unexpected peg source");
return 0;
case JANET_BOOLEAN: {
int n = peg_getboolean(b, peg);
Reserve r = reserve(b, 2);
emit_1(r, n ? RULE_NCHAR : RULE_NOTNCHAR, 0);
break;
}
case JANET_NUMBER: {
int32_t n = peg_getinteger(b, peg);
Reserve r = reserve(b, 2);