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