nt_lexer.re 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. #include "graph.h"
  2. #include "src/codec/nt_grammar.h"
  3. #include "nt_parser.h"
  4. #define YYCTYPE unsigned char
  5. #define YYCURSOR it->cur
  6. #define YYMARKER it->mar
  7. #define YYLIMIT it->lim
  8. #define YYFILL fill(it) == 0
  9. /**
  10. * Max chunk size passed to scanner at each iteration.
  11. */
  12. #ifdef LSUP_RDF_STREAM_CHUNK_SIZE
  13. #define CHUNK_SIZE LSUP_RDF_STREAM_CHUNK_SIZE
  14. #else
  15. #define CHUNK_SIZE 8192
  16. #endif
  17. typedef struct {
  18. FILE * fh; // Input file handle.
  19. YYCTYPE buf[CHUNK_SIZE + 1],// Start of buffer.
  20. * lim, // Position after the last available
  21. // input character (YYLIMIT).
  22. * cur, // Next input character to be read
  23. // (YYCURSOR)
  24. * mar, // Most recent match (YYMARKER)
  25. * tok, // Start of current token.
  26. * bol; // Address of the beginning of the
  27. // current line (for debugging).
  28. unsigned line; // Current line no. (for debugging).
  29. unsigned ct; // Number of parsed triples.
  30. bool eof; // if we have reached EOF.
  31. /*!stags:re2c format = "YYCTYPE *@@;"; */
  32. } ParseIterator;
  33. // TODO The opposite of this is in codec_nt.c. Find a better place for both.
  34. static inline char unescape_char(const char c) {
  35. switch (c) {
  36. case 't': return '\t';
  37. case 'b': return '\b';
  38. case 'n': return '\n';
  39. case 'r': return '\r';
  40. case 'f': return '\f';
  41. default: return c;
  42. }
  43. }
  44. static int fill(ParseIterator *it)
  45. {
  46. if (it->eof) {
  47. return 1;
  48. }
  49. const size_t shift = it->tok - it->buf;
  50. if (shift < 1) {
  51. return 2;
  52. }
  53. log_debug ("Shifting bytes: %lu", shift);
  54. memmove(it->buf, it->tok, it->lim - it->tok);
  55. it->lim -= shift;
  56. it->cur -= shift;
  57. it->mar -= shift;
  58. it->tok -= shift;
  59. it->lim += fread(it->lim, 1, shift, it->fh);
  60. /*!stags:re2c format = "if (it->@@) it->@@ -= shift; "; */
  61. it->lim[0] = 0;
  62. it->eof |= it->lim < it->buf + CHUNK_SIZE;
  63. return 0;
  64. }
  65. static void parse_init(ParseIterator *it, FILE *fh)
  66. {
  67. it->fh = fh;
  68. it->cur = it->mar = it->tok = it->lim = it->buf + CHUNK_SIZE;
  69. it->line = 1;
  70. it->bol = it->buf;
  71. it->ct = 0;
  72. it->eof = 0;
  73. /*!stags:re2c format = "it->@@ = NULL; "; */
  74. fill (it);
  75. }
  76. /** @brief Replace \uxxxx and \Uxxxxxxxx with Unicode bytes.
  77. */
  78. static YYCTYPE *unescape_unicode (const YYCTYPE *esc_str, size_t size)
  79. {
  80. YYCTYPE *uc_str = malloc (size + 1);
  81. size_t j = 0;
  82. YYCTYPE tmp_chr[5];
  83. for (size_t i = 0; i < size;) {
  84. if (esc_str[i] == '\\') {
  85. i++; // Skip over '\\'
  86. // 4-hex sequence.
  87. if (esc_str[i] == 'u') {
  88. i ++; // Skip over 'u'
  89. // Use tmp_chr to hold the hex string for the code point.
  90. memcpy(tmp_chr, esc_str + i, sizeof (tmp_chr) - 1);
  91. tmp_chr[4] = '\0';
  92. uint32_t tmp_val = strtol ((char*)tmp_chr, NULL, 16);
  93. log_debug ("tmp_val: %d", tmp_val);
  94. // Reuse tmp_chr to hold the byte values for the code point.
  95. int nbytes = utf8_encode (tmp_val, tmp_chr);
  96. // Copy bytes into destination.
  97. memcpy (uc_str + j, tmp_chr, nbytes);
  98. log_debug ("UC byte value: %x %x", uc_str[j], uc_str[j + 1]);
  99. j += nbytes;
  100. i += 4;
  101. // 8-hex sequence.
  102. } else if (esc_str[i] == 'U') {
  103. i ++; // Skip over 'U'
  104. log_error ("UTF-16 sequence unescaping not yet implemented.");
  105. return NULL; // TODO encode UTF-16
  106. // Unescape other escaped characters.
  107. } else uc_str[j++] = unescape_char(esc_str[i++]);
  108. } else {
  109. // Copy ASCII char verbatim.
  110. uc_str[j++] = esc_str[i++];
  111. }
  112. }
  113. YYCTYPE *tmp = realloc (uc_str, j + 1);
  114. if (UNLIKELY (!tmp)) return NULL;
  115. uc_str = tmp;
  116. uc_str[j] = '\0';
  117. return uc_str;
  118. }
  119. // Parser interface.
  120. void *ParseAlloc();
  121. void Parse();
  122. void ParseFree();
  123. // Lexer.
  124. static int lex (ParseIterator *it, LSUP_Term **term)
  125. {
  126. const YYCTYPE *lit_data_e, *dtype_s, *lang_s;
  127. loop:
  128. it->tok = it->cur;
  129. *term = NULL;
  130. /*!re2c
  131. re2c:eof = 0;
  132. re2c:flags:8 = 1;
  133. re2c:flags:tags = 1;
  134. re2c:tags:expression = "it->@@";
  135. re2c:api:style = functions;
  136. re2c:define:YYFILL:naked = 1;
  137. // For unresolved and partially resolved inconsistencies of the spec, see
  138. // https://lists.w3.org/Archives/Public/public-rdf-comments/2017Jun/0000.html
  139. _WS = [\x09\x20];
  140. WS = _WS+;
  141. EOL = [\x0D\x0A] (_WS | [\x0D\x0A])*;
  142. DOT = [.];
  143. HEX = [0-9A-Fa-f];
  144. ECHAR = [\\] [tbnrf"'\\];
  145. UCHAR = "\\u" HEX{4} | "\\U" HEX{8};
  146. PN_CHARS_BASE = [A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\U00010000-\U000EFFFF];
  147. PN_CHARS_U = PN_CHARS_BASE | '_' | ':';
  148. PN_CHARS = PN_CHARS_U | '-' | [0-9\u00B7\u0300-\u036F\u203F-\u2040];
  149. IRI_CHARS = ([^\x00-\x20<>"{}|^`\\] | UCHAR)*;
  150. LITERAL_QUOTE = ["] ([^\x22\x5C\x0A\x0D] | ECHAR|UCHAR)* ["];
  151. LANGTAG = [@] [a-zA-Z]+ ("-" [a-zA-Z0-9]+)*;
  152. IRIREF = [<] IRI_CHARS [>];
  153. LITERAL = LITERAL_QUOTE @lit_data_e _WS* ("^^" _WS* @dtype_s IRIREF | @lang_s LANGTAG)?;
  154. BNODE = "_:" ((PN_CHARS_U | [0-9]) ((PN_CHARS | ".")* PN_CHARS)?);
  155. COMMENT = "#" .*;
  156. EOL {
  157. it->line ++;
  158. it->bol = YYCURSOR;
  159. log_debug ("New line: #%u.", it->line);
  160. return T_EOL;
  161. }
  162. $ {
  163. log_debug ("End of buffer.");
  164. return T_EOF;
  165. }
  166. IRIREF {
  167. YYCTYPE *data = unescape_unicode (it->tok + 1, YYCURSOR - it->tok - 2);
  168. log_debug ("URI data: %s", data);
  169. *term = LSUP_uri_new ((char*)data);
  170. free (data);
  171. return T_IRIREF;
  172. }
  173. LITERAL {
  174. // Only unescape Unicode from data.
  175. size_t size = lit_data_e - it->tok - 2;
  176. YYCTYPE *data = unescape_unicode (it->tok + 1, size);
  177. log_debug ("Literal data: %s", data);
  178. YYCTYPE *datatype = NULL, *lang = NULL;
  179. if (dtype_s) {
  180. size = YYCURSOR - dtype_s - 1;
  181. datatype = malloc (size);
  182. memcpy (datatype, dtype_s + 1, size);
  183. datatype [size - 1] = '\0';
  184. log_debug ("datatype: %s", datatype);
  185. }
  186. if (lang_s) {
  187. size = YYCURSOR - lang_s;
  188. lang = malloc (size);
  189. memcpy (lang, lang_s + 1, size);
  190. lang [size - 1] = '\0';
  191. log_debug ("lang: %s", lang);
  192. }
  193. *term = LSUP_term_new (LSUP_TERM_LITERAL, (char*)data, (char*)datatype, (char*)lang);
  194. free (data);
  195. free (datatype);
  196. free (lang);
  197. return T_LITERAL;
  198. }
  199. BNODE {
  200. YYCTYPE *data = unescape_unicode (it->tok + 2, YYCURSOR - it->tok - 2);
  201. log_debug ("BNode data: %s", data);
  202. *term = LSUP_term_new (LSUP_TERM_BNODE, (char*)data, NULL, NULL);
  203. free (data);
  204. return T_BNODE;
  205. }
  206. DOT {
  207. log_debug ("End of triple.");
  208. it->ct ++;
  209. return T_DOT;
  210. }
  211. WS {
  212. log_debug ("Separator.");
  213. return T_WS;
  214. }
  215. COMMENT {
  216. size_t size = YYCURSOR - it->tok + 1;
  217. YYCTYPE *data = malloc (size);
  218. memcpy (data, it->tok, size);
  219. data [size - 1] = '\0';
  220. log_debug ("Comment: `%s`", data);
  221. free (data);
  222. goto loop;
  223. }
  224. * {
  225. log_debug (
  226. "Invalid token @ %lu: %s (\\x%x)",
  227. YYCURSOR - it->buf - 1, it->tok, *it->tok);
  228. return -1;
  229. }
  230. */
  231. }
  232. LSUP_rc
  233. LSUP_nt_parse_term (const char *rep, const LSUP_NSMap *map, LSUP_Term **term)
  234. {
  235. FILE *fh = fmemopen ((void *)rep, strlen (rep), "r");
  236. ParseIterator it;
  237. parse_init (&it, fh);
  238. int ttype = lex (&it, term);
  239. fclose (fh);
  240. switch (ttype) {
  241. case T_IRIREF:
  242. case T_LITERAL:
  243. case T_BNODE:
  244. return LSUP_OK;
  245. default:
  246. return LSUP_VALUE_ERR;
  247. }
  248. }
  249. LSUP_rc
  250. LSUP_nt_parse_doc (FILE *fh, LSUP_Graph **gr_p, size_t *ct, char **err_p)
  251. {
  252. *err_p = NULL;
  253. *gr_p = NULL;
  254. ParseIterator parse_it;
  255. parse_init (&parse_it, fh);
  256. void *parser = ParseAlloc (malloc);
  257. LSUP_rc rc;
  258. LSUP_Graph *gr = LSUP_graph_new (LSUP_STORE_MEM);
  259. if (UNLIKELY (!gr)) return LSUP_MEM_ERR;
  260. LSUP_GraphIterator *it = LSUP_graph_add_init (gr);
  261. if (UNLIKELY (!it)) {
  262. LSUP_graph_free (gr);
  263. return LSUP_MEM_ERR;
  264. }
  265. LSUP_Term *term = NULL;
  266. for (;;) {
  267. int ttype = lex (&parse_it, &term);
  268. if (ttype == -1) {
  269. char token[16] = {'\0'};
  270. strncpy (token, (const char *)parse_it.tok, 15);
  271. char *err_start = "Parse error near token `";
  272. char err_info [64];
  273. sprintf(
  274. err_info, "[...]' at line %u, character %ld.\n",
  275. parse_it.line, parse_it.cur - parse_it.bol);
  276. size_t err_size = strlen (err_start) + 16 + strlen(err_info);
  277. char *err_str = malloc (err_size);
  278. sprintf (err_str, "%s%s%s", err_start, token, err_info);
  279. rc = LSUP_VALUE_ERR;
  280. *err_p = err_str;
  281. goto finally;
  282. }
  283. Parse (parser, ttype, term, it);
  284. if (ttype == T_EOF) break;
  285. };
  286. if (ct) *ct = parse_it.ct;
  287. log_info ("Parsed %u triples.", parse_it.ct);
  288. log_debug ("Graph size: %lu", LSUP_graph_size (gr));
  289. rc = parse_it.ct > 0 ? LSUP_OK : LSUP_NORESULT;
  290. *gr_p = gr;
  291. finally:
  292. Parse (parser, 0, NULL, it);
  293. ParseFree (parser, free);
  294. LSUP_graph_add_done (it);
  295. LSUP_term_free (term);
  296. if (rc < 0) LSUP_graph_free (gr);
  297. return rc;
  298. }