Volksdata 1.0b7
RDF library
Loading...
Searching...
No Matches
parser_common.h
Go to the documentation of this file.
1#ifndef _VOLK_PARSER_COMMON_H
2#define _VOLK_PARSER_COMMON_H
3
4#include "volksdata/codec.h"
5
6
14#define YYCTYPE uint8_t
15#define YYCURSOR it->cur
16#define YYMARKER it->mar
17#define YYLIMIT it->lim
18#define YYFILL fill(it) == 0
19
20
21typedef struct {
22 FILE * fh;
23 const char * sh;
24 size_t buf_size;
25 YYCTYPE * buf,
26 * lim,
28 * cur,
29 * mar,
30 * tok,
31 * bol;
33 unsigned line;
34 unsigned ct;
35 bool eof;
38
39
40static int fill(ParseIterator *it);
41
42
52static void parse_init (ParseIterator *it, FILE *fh, const char *sh)
53{
54 if(fh) {
55 // Stream handling. It engages YYFILL and reads by chunks.
56 it->fh = fh;
57 it->sh = NULL;
58 it->buf_size = CHUNK_SIZE;
59 it->buf = malloc(it->buf_size);
60 if (!it->buf) log_error ("Error allocating lexer buffer.");
61 it->cur = it->mar = it->tok = it->lim = it->buf + it->buf_size - 1;
62 it->bol = it->buf;
63 it->eof = false;
64 it->lim[0] = 0;
65 } else {
66 // String handling. Uses the provided string as the buffer.
67 it->fh = NULL;
68 it->sh = sh;
69 it->buf_size = strlen(sh) + 1;
70 it->buf = NULL;
71 it->cur = it->tok = (YYCTYPE*)it->sh;
72 it->lim = it->mar = it->cur + it->buf_size - 1;
73 it->bol = it->cur;
74 it->eof = true;
75 }
76 it->line = 1;
77 it->ct = 0;
79}
80
81
82int
83fill(ParseIterator *it)
84{
85 log_debug ("Filling codec buffer @ %p.", it->buf);
86 if (it->eof) return 1;
87
88 size_t shift = it->tok - it->buf;
89 size_t used = it->lim - it->tok;
90
91 // If buffer is too small for the lexeme, double the capacity.
92 if (shift < 1) {
93 shift += it->buf_size;
94 it->buf_size *= 2;
95 // Store offsets to reapply to moved buffer.
96 size_t
97 cur_off = it->cur - it->buf,
98 tok_off = it->tok - it->buf,
99 lim_off = it->lim - it->buf,
100 mar_off = it->mar - it->buf;
101
102 LOG_DEBUG ("Reallocating buffer to %zu bytes.", it->buf_size);
103 YYCTYPE *tmp = realloc (it->buf, it->buf_size);
104 if (!tmp) {
105 log_error ("Memory allocation error.");
106 return -1;
107 }
108 it->buf = tmp;
109 // Move all relative points if address changed.
110 it->cur = it->buf + cur_off;
111 it->tok = it->buf + tok_off;
112 it->lim = it->buf + lim_off;
113 it->mar = it->buf + mar_off;
114 } else {
115 LOG_DEBUG("Shifting bytes: %zu", shift);
116 memmove (it->buf, it->tok, used);
117 LOG_TRACE ("Limit offset before reading data: %zu", it->lim - it->tok);
118 it->lim -= shift;
119 it->cur -= shift;
120 it->mar -= shift;
121 it->tok -= shift;
122 }
123 it->lim += fread (it->lim, 1, it->buf_size - used - 1, it->fh);
125 LOG_TRACE ("Cursor offset from last token: %zu", it->cur - it->tok);
126 LOG_TRACE ("Limit offset from last token: %zu", it->lim - it->tok);
127 it->lim[0] = 0;
128 it->eof = it->lim < it->buf + it->buf_size - 1;
129 return 0;
130}
131
132
133#endif // _VOLK_PARSER_COMMON_H
#define CHUNK_SIZE
Definition codec.h:16
#define LOG_TRACE(...)
Definition core.h:276
#define LOG_DEBUG(...)
Definition core.h:275
#define YYCTYPE
TTL is UTF-8 encoded.
Definition parser_nt.c:20
FILE * fh
Input file handle.
Definition parser_nt.c:28
size_t buf_size
Initial allocation for buffer.
Definition parser_nt.c:30
unsigned line
Current line no. (for debugging).
Definition parser_nt.c:39
YYCTYPE * cur
Next input character to be read (YYCURSOR).
Definition parser_nt.c:34
const char * sh
Input string. Exclusive with fh.
Definition parser_nt.c:29
bool eof
if we have reached EOF.
Definition parser_nt.c:41
YYCTYPE * buf
Start of buffer.
Definition parser_nt.c:31
YYCTYPE * bol
Definition parser_nt.c:37
YYCTYPE * lim
Definition parser_nt.c:33
unsigned ct
Number of statements parsed.
Definition parser_nt.c:40
YYCTYPE * mar
Most recent match (YYMARKER).
Definition parser_nt.c:35
YYCTYPE * tok
Start of current token.
Definition parser_nt.c:36