int type;
const char *pat;
int plen;
- int (*parse)(const char *buf, struct jp_opcode *op);
+ int (*parse)(const char *buf, struct jp_opcode *op, struct jp_state *s);
};
#define dec(o) \
*/
static int
-parse_string(const char *buf, struct jp_opcode *op)
+parse_string(const char *buf, struct jp_opcode *op, struct jp_state *s)
{
char q = *(buf++);
char str[128] = { 0 };
hex(in[2]) * 16 * 16 +
hex(in[3]) * 16 +
hex(in[4])))
+ {
+ s->error_pos = s->off + (in - buf);
return -3;
+ }
in += 5;
}
else
{
+ s->error_pos = s->off + (in - buf);
return -2;
}
}
if (isxdigit(in[1]) && isxdigit(in[2]))
{
if (!utf8enc(&out, &rem, hex(in[1]) * 16 + hex(in[2])))
+ {
+ s->error_pos = s->off + (in - buf);
return -3;
+ }
in += 3;
}
else
{
+ s->error_pos = s->off + (in - buf);
return -2;
}
}
dec(in[2]);
if (code > 255)
+ {
+ s->error_pos = s->off + (in - buf);
return -2;
+ }
if (!utf8enc(&out, &rem, code))
+ {
+ s->error_pos = s->off + (in - buf);
return -3;
+ }
in += 3;
}
else if (in[1] >= '0' && in[1] <= '7')
{
if (!utf8enc(&out, &rem, dec(in[0]) * 8 + dec(in[1])))
+ {
+ s->error_pos = s->off + (in - buf);
return -3;
+ }
in += 2;
}
else
{
if (!utf8enc(&out, &rem, dec(in[0])))
+ {
+ s->error_pos = s->off + (in - buf);
return -3;
+ }
in += 1;
}
else
{
if (rem-- < 1)
+ {
+ s->error_pos = s->off + (in - buf);
return -3;
+ }
switch (in[0])
{
else
{
if (rem-- < 1)
+ {
+ s->error_pos = s->off + (in - buf);
return -3;
+ }
*out++ = *in++;
}
*/
static int
-parse_label(const char *buf, struct jp_opcode *op)
+parse_label(const char *buf, struct jp_opcode *op, struct jp_state *s)
{
char str[128] = { 0 };
char *out = str;
while (*in == '_' || isalnum(*in))
{
if (rem-- < 1)
+ {
+ s->error_pos = s->off + (in - buf);
return -3;
+ }
*out++ = *in++;
}
*/
static int
-parse_number(const char *buf, struct jp_opcode *op)
+parse_number(const char *buf, struct jp_opcode *op, struct jp_state *s)
{
char *e;
int n = strtol(buf, &e, 10);
if (e == buf)
+ {
+ s->error_pos = s->off;
return -2;
+ }
op->num = n;
static int
-match_token(const char *ptr, struct jp_opcode *op)
+match_token(const char *ptr, struct jp_opcode *op, struct jp_state *s)
{
int i;
const struct token *tok;
op->type = tok->type;
if (tok->parse)
- return tok->parse(ptr, op);
+ return tok->parse(ptr, op, s);
return tok->plen;
}
}
- return -1;
+ s->error_pos = s->off;
+ return -4;
}
struct jp_opcode *
{
struct jp_opcode op = { 0 };
- *mlen = match_token(input, &op);
+ *mlen = match_token(input, &op, s);
- if (*mlen < 0 || op.type == 0)
+ if (*mlen < 0)
+ {
+ s->error_code = *mlen;
return NULL;
+ }
+ else if (op.type == 0)
+ {
+ return NULL;
+ }
return jp_alloc_op(s, op.type, op.num, op.str, NULL);
}
}
}
+static void
+print_error(struct jp_state *state, char *expr)
+{
+ int i;
+ bool first = true;
+
+ fprintf(stderr, "Syntax error: ");
+
+ switch (state->error_code)
+ {
+ case -4:
+ fprintf(stderr, "Unexpected character\n");
+ break;
+
+ case -3:
+ fprintf(stderr, "String or label literal too long\n");
+ break;
+
+ case -2:
+ fprintf(stderr, "Invalid escape sequence\n");
+ break;
+
+ case -1:
+ fprintf(stderr, "Unterminated string\n");
+ break;
+
+ default:
+ for (i = 0; i < sizeof(state->error_code) * 8; i++)
+ {
+ if (state->error_code & (1 << i))
+ {
+ fprintf(stderr,
+ first ? "Expecting %s" : " or %s", tokennames[i]);
+
+ first = false;
+ }
+ }
+
+ fprintf(stderr, "\n");
+ break;
+ }
+
+ fprintf(stderr, "In expression %s\n", expr);
+ fprintf(stderr, "Near here ----");
+
+ for (i = 0; i < state->error_pos; i++)
+ fprintf(stderr, "-");
+
+ fprintf(stderr, "^\n");
+}
+
static bool
filter_json(int opt, struct json_object *jsobj, char *expr)
{
state = jp_parse(expr);
- if (!state || state->error)
+ if (!state)
{
- fprintf(stderr, "Syntax error near {%s}: %s\n",
- state ? expr + state->erroff : expr,
- state ? state->error : "Out of memory");
-
+ fprintf(stderr, "Out of memory\n");
+ goto out;
+ }
+ else if (state->error_code)
+ {
+ print_error(state, expr);
goto out;
}
}
%syntax_error {
- int n = sizeof(tokennames) / sizeof(tokennames[0]);
- int l = strlen("Expecting ");
- int c = 0;
int i;
- for (i = 0; i < n; i++)
- {
+ for (i = 0; i < sizeof(tokennames) / sizeof(tokennames[0]); i++)
if (yy_find_shift_action(yypParser, (YYCODETYPE)i) < YYNSTATE + YYNRULE)
- l += strlen(tokennames[i]) + 4;
- }
-
- s->error = malloc(l);
-
- if (s->error)
- {
- s->erroff = s->off;
- strcpy(s->error, "Expecting ");
-
- for (i = 0; i < n; i++)
- {
- if (yy_find_shift_action(yypParser, (YYCODETYPE)i) < YYNSTATE + YYNRULE)
- {
- if (c++)
- strcat(s->error, " or ");
-
- strcat(s->error, tokennames[i]);
- }
- }
- }
+ s->error_code |= (1 << i);
+
+ s->error_pos = s->off;
}