diff options
Diffstat (limited to 'lib/jsonlex.l')
-rw-r--r-- | lib/jsonlex.l | 183 |
1 files changed, 0 insertions, 183 deletions
diff --git a/lib/jsonlex.l b/lib/jsonlex.l deleted file mode 100644 index 322b05d..0000000 --- a/lib/jsonlex.l +++ /dev/null @@ -1,183 +0,0 @@ -%{ -/* This file is part of Eclat. - Copyright (C) 2012-2015 Sergey Poznyakoff. - - Eclat is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3, or (at your option) - any later version. - - Eclat is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with Eclat. If not, see <http://www.gnu.org/licenses/>. */ - -#include "libeclat.h" -#include "jsongrm.h" -#include "json.h" - -static char const *input_ptr; -static size_t input_size; - -char const *json_err_diag; -char const *json_err_ptr; - -#undef YY_INPUT -#define YY_INPUT(buf,result,max_size) \ - do { \ - size_t n = (max_size > input_size) ? input_size : max_size; \ - if (n) { \ - memcpy(buf, input_ptr, n); \ - input_ptr += n; \ - input_size -= n; \ - } \ - result = n; \ - } while(0) - -void -jsonlex_setup(char const *s, size_t l) -{ - input_ptr = s; - input_size = l; - json_err_diag = NULL; - json_err_ptr = NULL; - yy_flex_debug = 0; -} - -void -jsonlex_diag(const char *s, size_t off) -{ - if (!json_err_diag) { - json_err_diag = s; - json_err_ptr = input_ptr - off; - } -} - -static int -utf8_wctomb(char *u) -{ - unsigned int wc = strtoul(u, NULL, 16); - int count; - char r[6]; - - if (wc < 0x80) - count = 1; - else if (wc < 0x800) - count = 2; - else if (wc < 0x10000) - count = 3; - else if (wc < 0x200000) - count = 4; - else if (wc < 0x4000000) - count = 5; - else if (wc <= 0x7fffffff) - count = 6; - else - return -1; - - switch (count) { - /* Note: code falls through cases! */ - case 6: - r[5] = 0x80 | (wc & 0x3f); - wc = wc >> 6; - wc |= 0x4000000; - case 5: - r[4] = 0x80 | (wc & 0x3f); - wc = wc >> 6; - wc |= 0x200000; - case 4: - r[3] = 0x80 | (wc & 0x3f); - wc = wc >> 6; - wc |= 0x10000; - case 3: - r[2] = 0x80 | (wc & 0x3f); - wc = wc >> 6; - wc |= 0x800; - case 2: - r[1] = 0x80 | (wc & 0x3f); - wc = wc >> 6; - wc |= 0xc0; - case 1: - r[0] = wc; - } - grecs_line_acc_grow(r, count); - return count; -} - -static int -unescape(int c, int *o) -{ - static char transtab[] = "\\\\\"\"//b\bf\fn\nr\rt\t"; - char *p; - - for (p = transtab; *p; p += 2) { - if (*p == c) { - *o = p[1]; - return 0; - } - } - return -1; -} - -#define YY_SKIP_YYWRAP 1 -static int -yywrap() -{ - return 1; -} -%} -D [0-9] -X [0-9a-fA-F] -%x STR -%% -"-"?{D}{D}*(.{D}{D}*)?([eE][-+]?{D}{D}*)? { - yylval.n = strtod(yytext, NULL); - return T_NUMBER; - } -\"[^\\\"]*\" { grecs_line_begin(); - grecs_line_add(yytext + 1, yyleng - 2); - yylval.s = grecs_line_finish(); - return T_STRING; } -\"[^\\\"]*\\{X}{4} { BEGIN(STR); - grecs_line_begin(); - grecs_line_add(yytext + 1, yyleng - 5); - utf8_wctomb(yytext + yyleng - 4); - } -\"[^\\\"]*\\. { int c; - BEGIN(STR); - grecs_line_begin(); - grecs_line_acc_grow(yytext + 1, yyleng - 2); - if (unescape(yytext[yyleng - 1], &c)) { - jsonlex_diag("invalid UTF-8 codepoint", 5); - return T_ERR; - } - grecs_line_acc_grow_char(c); - } -<STR>[^\\\"]*\" { BEGIN(INITIAL); - if (yyleng > 1) - grecs_line_acc_grow(yytext, yyleng - 1); - yylval.s = grecs_line_finish(); - return T_STRING; } -<STR>[^\\\"]*\\{X}{4} { - grecs_line_add(yytext, yyleng - 5); - utf8_wctomb(yytext + yyleng - 4); -} -<STR>[^\\\"]*\\. { - int c; - grecs_line_acc_grow(yytext, yyleng - 2); - if (unescape(yytext[yyleng - 1], &c)) { - jsonlex_diag("invalid UTF-8 codepoint", 5); - return T_ERR; - } - grecs_line_acc_grow_char(c); } - -null { return T_NULL; } -true { yylval.b = 1; return T_BOOL; } -false { yylval.b = 0; return T_BOOL; } -"{"|"}"|"["|"]"|":"|"," return yytext[0]; -[ \t\n]* ; -. { jsonlex_diag("bogus character", 0); - return T_ERR; } |