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 inline void newline (ParseIterator *it) {
46 it->line ++;
47 it->bol = YYCURSOR;
48 log_trace ("New line: #%u.", it->line);
49}
50
51
52static int fill(ParseIterator *it);
53
54
64static void parse_init (ParseIterator *it, FILE *fh, const char *sh)
65{
66 if(fh) {
67 // Stream handling. It engages YYFILL and reads by chunks.
68 it->fh = fh;
69 it->sh = NULL;
70 it->buf_size = CHUNK_SIZE;
71 it->buf = malloc(it->buf_size);
72 if (!it->buf) log_error ("Error allocating lexer buffer.");
73 it->cur = it->mar = it->tok = it->lim = it->buf + it->buf_size - 1;
74 it->bol = it->buf;
75 it->eof = false;
76 it->lim[0] = 0;
77 } else {
78 // String handling. Uses the provided string as the buffer.
79 it->fh = NULL;
80 it->sh = sh;
81 it->buf_size = strlen(sh) + 1;
82 it->buf = NULL;
83 it->cur = it->tok = (YYCTYPE*)it->sh;
84 it->lim = it->mar = it->cur + it->buf_size - 1;
85 it->bol = it->cur;
86 it->eof = true;
87 }
88 it->line = 1;
89 it->ct = 0;
91}
92
93
94int
95fill(ParseIterator *it)
96{
97 log_debug ("Filling codec buffer @ %p.", it->buf);
98 if (it->eof) return 1;
99
100 size_t shift = it->tok - it->buf;
101 size_t used = it->lim - it->tok;
102
103 // If buffer is too small for the lexeme, double the capacity.
104 if (shift < 1) {
105 shift += it->buf_size;
106 it->buf_size *= 2;
107 // Store offsets to reapply to moved buffer.
108 size_t
109 cur_off = it->cur - it->buf,
110 tok_off = it->tok - it->buf,
111 lim_off = it->lim - it->buf,
112 mar_off = it->mar - it->buf;
113
114 log_debug ("Reallocating buffer to %zu bytes.", it->buf_size);
115 YYCTYPE *tmp = realloc (it->buf, it->buf_size);
116 if (!tmp) {
117 log_error ("Memory allocation error.");
118 return -1;
119 }
120 it->buf = tmp;
121 // Move all relative points if address changed.
122 it->cur = it->buf + cur_off;
123 it->tok = it->buf + tok_off;
124 it->lim = it->buf + lim_off;
125 it->mar = it->buf + mar_off;
126 } else {
127 log_debug ("Shifting bytes: %zu", shift);
128 memmove (it->buf, it->tok, used);
129 log_trace ("Limit offset before reading data: %zu", it->lim - it->tok);
130 it->lim -= shift;
131 it->cur -= shift;
132 it->mar -= shift;
133 it->tok -= shift;
134 }
135 it->lim += fread (it->lim, 1, it->buf_size - used - 1, it->fh);
137 log_trace ("Cursor offset from last token: %zu", it->cur - it->tok);
138 log_trace ("Limit offset from last token: %zu", it->lim - it->tok);
139 it->lim[0] = 0;
140 it->eof = it->lim < it->buf + it->buf_size - 1;
141 return 0;
142}
143
144
145#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 YYCURSOR
Definition parser_nt.c:26
#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