Skip to content
This repository has been archived by the owner on Mar 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #311 from picrin-scheme/expressive_read_error
Browse files Browse the repository at this point in the history
add irritant to read error, fixing #310
  • Loading branch information
nyuichi committed Oct 6, 2015
2 parents fa3488d + df1a7b5 commit 5e6ffec
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions extlib/benz/read.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ static pic_value read(pic_state *pic, struct pic_port *port, int c);
static pic_value read_nullable(pic_state *pic, struct pic_port *port, int c);

PIC_NORETURN static void
read_error(pic_state *pic, const char *msg)
read_error(pic_state *pic, const char *msg, pic_value irritant)
{
struct pic_error *e;

e = pic_make_error(pic, pic_intern(pic, "read"), msg, pic_nil_value());
e = pic_make_error(pic, pic_intern(pic, "read"), msg, irritant);

pic_raise(pic, pic_obj_value(e));
}
Expand Down Expand Up @@ -226,7 +226,7 @@ read_uinteger(pic_state *pic, struct pic_port *port, int c)
unsigned u = 0;

if (! isdigit(c)) {
read_error(pic, "expected one or more digits");
read_error(pic, "expected one or more digits", pic_list1(pic, pic_char_value(c)));
}

u = c - '0';
Expand Down Expand Up @@ -398,10 +398,10 @@ read_true(pic_state *pic, struct pic_port *port, int c)
{
if ((c = peek(pic, port)) == 'r') {
if (! expect(pic, port, "rue")) {
read_error(pic, "unexpected character while reading #true");
read_error(pic, "unexpected character while reading #true", pic_nil_value());
}
} else if (! isdelim(c)) {
read_error(pic, "non-delimiter character given after #t");
read_error(pic, "non-delimiter character given after #t", pic_list1(pic, pic_char_value(c)));
}

return pic_true_value();
Expand All @@ -412,10 +412,10 @@ read_false(pic_state *pic, struct pic_port *port, int c)
{
if ((c = peek(pic, port)) == 'a') {
if (! expect(pic, port, "alse")) {
read_error(pic, "unexpected character while reading #false");
read_error(pic, "unexpected character while reading #false", pic_nil_value());
}
} else if (! isdelim(c)) {
read_error(pic, "non-delimiter character given after #f");
read_error(pic, "non-delimiter character given after #f", pic_list1(pic, pic_char_value(c)));
}

return pic_false_value();
Expand All @@ -428,7 +428,7 @@ read_char(pic_state *pic, struct pic_port *port, int c)

if (! isdelim(peek(pic, port))) {
switch (c) {
default: read_error(pic, "unexpected character after char literal");
default: read_error(pic, "unexpected character after char literal", pic_list1(pic, pic_char_value(c)));
case 'a': c = '\a'; if (! expect(pic, port, "larm")) goto fail; break;
case 'b': c = '\b'; if (! expect(pic, port, "ackspace")) goto fail; break;
case 'd': c = 0x7F; if (! expect(pic, port, "elete")) goto fail; break;
Expand All @@ -453,7 +453,7 @@ read_char(pic_state *pic, struct pic_port *port, int c)
return pic_char_value((char)c);

fail:
read_error(pic, "unexpected character while reading character literal");
read_error(pic, "unexpected character while reading character literal", pic_list1(pic, pic_char_value(c)));
}

static pic_value
Expand Down Expand Up @@ -516,7 +516,7 @@ read_pipe(pic_state *pic, struct pic_port *port, int c)
i = 0;
while ((HEX_BUF[i++] = (char)next(pic, port)) != ';') {
if (i >= sizeof HEX_BUF)
read_error(pic, "expected ';'");
read_error(pic, "expected ';'", pic_list1(pic, pic_char_value(HEX_BUF[sizeof(HEX_BUF) - 1])));
}
c = (char)strtol(HEX_BUF, NULL, 16);
break;
Expand Down Expand Up @@ -550,11 +550,11 @@ read_blob(pic_state *pic, struct pic_port *port, int c)
}

if (nbits != 8) {
read_error(pic, "unsupported bytevector bit width");
read_error(pic, "unsupported bytevector bit width", pic_list1(pic, pic_int_value(nbits)));
}

if (c != '(') {
read_error(pic, "expected '(' character");
read_error(pic, "expected '(' character", pic_list1(pic, pic_char_value(c)));
}

len = 0;
Expand All @@ -563,7 +563,7 @@ read_blob(pic_state *pic, struct pic_port *port, int c)
while ((c = skip(pic, port, c)) != ')') {
n = read_uinteger(pic, port, c);
if (n < 0 || (1 << nbits) <= n) {
read_error(pic, "invalid element in bytevector literal");
read_error(pic, "invalid element in bytevector literal", pic_list1(pic, pic_int_value(n)));
}
len += 1;
dat = pic_realloc(pic, dat, len);
Expand All @@ -585,12 +585,12 @@ read_undef_or_blob(pic_state *pic, struct pic_port *port, int c)
{
if ((c = peek(pic, port)) == 'n') {
if (! expect(pic, port, "ndefined")) {
read_error(pic, "unexpected character while reading #undefined");
read_error(pic, "unexpected character while reading #undefined", pic_nil_value());
}
return pic_undef_value();
}
if (! isdigit(c)) {
read_error(pic, "expect #undefined or #u8(...), but illegal character given");
read_error(pic, "expect #undefined or #u8(...), but illegal character given", pic_list1(pic, pic_char_value(c)));
}
return read_blob(pic, port, 'u');
}
Expand All @@ -616,7 +616,7 @@ read_pair(pic_state *pic, struct pic_port *port, int c)
if (pic_invalid_p(read_nullable(pic, port, c))) {
goto closing;
}
read_error(pic, "unmatched parenthesis");
read_error(pic, "unmatched parenthesis", pic_nil_value());
}
return cdr;
}
Expand Down Expand Up @@ -714,7 +714,7 @@ read_label_ref(pic_state *pic, struct pic_port PIC_UNUSED(*port), int i)

it = kh_get(read, h, i);
if (it == kh_end(h)) {
read_error(pic, "label of given index not defined");
read_error(pic, "label of given index not defined", pic_list1(pic, pic_int_value(i)));
}
return kh_val(h, it);
}
Expand All @@ -735,13 +735,13 @@ read_label(pic_state *pic, struct pic_port *port, int c)
if (c == '#') {
return read_label_ref(pic, port, i);
}
read_error(pic, "broken label expression");
read_error(pic, "broken label expression", pic_nil_value());
}

static pic_value
read_unmatch(pic_state *pic, struct pic_port PIC_UNUSED(*port), int PIC_UNUSED(c))
{
read_error(pic, "unmatched parenthesis");
read_error(pic, "unmatched parenthesis", pic_nil_value());
}

static pic_value
Expand All @@ -750,11 +750,11 @@ read_dispatch(pic_state *pic, struct pic_port *port, int c)
c = next(pic, port);

if (c == EOF) {
read_error(pic, "unexpected EOF");
read_error(pic, "unexpected EOF", pic_nil_value());
}

if (pic->reader.dispatch[c] == NULL) {
read_error(pic, "invalid character at the seeker head");
read_error(pic, "invalid character at the seeker head", pic_list1(pic, pic_char_value(c)));
}

return pic->reader.dispatch[c](pic, port, c);
Expand All @@ -766,11 +766,11 @@ read_nullable(pic_state *pic, struct pic_port *port, int c)
c = skip(pic, port, c);

if (c == EOF) {
read_error(pic, "unexpected EOF");
read_error(pic, "unexpected EOF", pic_nil_value());
}

if (pic->reader.table[c] == NULL) {
read_error(pic, "invalid character at the seeker head");
read_error(pic, "invalid character at the seeker head", pic_list1(pic, pic_char_value(c)));
}

return pic->reader.table[c](pic, port, c);
Expand Down

0 comments on commit 5e6ffec

Please sign in to comment.