From: Stephen Dolan Date: Mon, 3 Sep 2012 12:36:12 +0000 (+0100) Subject: Literal strings and better literal numbers. X-Git-Tag: jq-1.1~88 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6041fd76613b287ccda3de58829a9e0f98b91f17;p=jq Literal strings and better literal numbers. --- diff --git a/c/jv.h b/c/jv.h index c03967b..8fbc77a 100644 --- a/c/jv.h +++ b/c/jv.h @@ -86,7 +86,7 @@ int jv_get_refcnt(jv); void jv_dump(jv); jv jv_parse(const char* string); - +jv jv_parse_sized(const char* string, int length); diff --git a/c/jv_parse.c b/c/jv_parse.c index 2c2da39..1f6a20a 100644 --- a/c/jv_parse.c +++ b/c/jv_parse.c @@ -1,5 +1,6 @@ #include #include +#include #include "jv.h" #include "jv_dtoa.h" #include "jv_parse.h" @@ -345,25 +346,31 @@ static pfunc finish(struct jv_parser* p) { return 0; } -jv jv_parse(const char* string) { +jv jv_parse_sized(const char* string, int length) { + printf("'%s' [%d]", string, length); struct jv_parser parser; jv_parser_init(&parser); const char* p = string; char ch; - while ((ch = *p++)) { + while (p < string + length) { + ch = *p++; presult msg = scan(&parser, ch); if (msg){ - printf("ERROR: %s (parsing [%s])\n", msg, string); + printf("ERROR: %s (parsing '%s')\n", msg, string); return jv_null(); } } presult msg = finish(&parser); if (msg) { - printf("ERROR: %s (parsing [%s])\n", msg, string); + printf("ERROR: %s (parsing '%s')\n", msg, string); return jv_null(); } jv value = jv_copy(parser.next); jv_parser_free(&parser); return value; } + +jv jv_parse(const char* string) { + return jv_parse_sized(string, strlen(string)); +} diff --git a/c/lexer.l b/c/lexer.l index 34253b0..550ad85 100644 --- a/c/lexer.l +++ b/c/lexer.l @@ -15,9 +15,15 @@ "|=" { return SETPIPE; } "."|"="|";"|"["|"]"|","|":"|"("|")"|"{"|"}"|"|"|"+"|"\$" { return yytext[0];} -[[:digit:]]+ { yylval->literal = jv_number((double)atoi(yytext)); return LITERAL;} +\"(\\.|[^\\"])*\" | +[+-]?[0-9.]+([eE][+-]?[0-9]+)? { + yylval->literal = jv_parse_sized(yytext, yyleng); return LITERAL; +} + [[:alnum:]]+ { yylval->literal = jv_string(yytext); return IDENT;} + [ \n\t]+ {} + %% /* perhaps these should be calls... */ /* diff --git a/c/parser.y b/c/parser.y index 14a338a..3629245 100644 --- a/c/parser.y +++ b/c/parser.y @@ -189,6 +189,10 @@ MkDictPair : IDENT ':' ExpD { $$ = gen_dictpair(gen_op_const(LOADK, $1), $3); } +| LITERAL ':' ExpD { + assert(jv_get_kind($1) == JV_KIND_STRING); + $$ = gen_dictpair(gen_op_const(LOADK, $1), $3); + } | IDENT { $$ = gen_dictpair(gen_op_const(LOADK, jv_copy($1)), gen_index(gen_noop(), gen_op_const(LOADK, $1)));