src/pdfmake_tokenizer.c

filter: Statementstmt 85.1% 223 / 262 Branchbran 73.0% 146 / 200 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 79.8% 369 / 462 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/writer.xs ↑ Summary src/pdfmake_markup_style.c »
1 /*
2 * pdfmake_tokenizer.c — PDF lexical tokenizer per §7.2
3 */
4  
5 #include "pdfmake_tokenizer.h"
6 #include <string.h>
7 #include <stdlib.h>
8 #include <ctype.h>
9  
10 /*============================================================================
11 * Character classification table (§7.2.2)
12 *
13 * PDF whitespace: NUL(0), TAB(9), LF(10), FF(12), CR(13), SPACE(32)
14 * PDF delimiters: ( ) < > [ ] { } / %
15 *==========================================================================*/
16  
17 const uint8_t pdfmake_char_class[256] = {
18 /* 0x00-0x0F */
19 PDFMAKE_CC_WHITESPACE, /* NUL */
20 PDFMAKE_CC_REGULAR, /* SOH */
21 PDFMAKE_CC_REGULAR, /* STX */
22 PDFMAKE_CC_REGULAR, /* ETX */
23 PDFMAKE_CC_REGULAR, /* EOT */
24 PDFMAKE_CC_REGULAR, /* ENQ */
25 PDFMAKE_CC_REGULAR, /* ACK */
26 PDFMAKE_CC_REGULAR, /* BEL */
27 PDFMAKE_CC_REGULAR, /* BS */
28 PDFMAKE_CC_WHITESPACE, /* TAB */
29 PDFMAKE_CC_WHITESPACE, /* LF */
30 PDFMAKE_CC_REGULAR, /* VT */
31 PDFMAKE_CC_WHITESPACE, /* FF */
32 PDFMAKE_CC_WHITESPACE, /* CR */
33 PDFMAKE_CC_REGULAR, /* SO */
34 PDFMAKE_CC_REGULAR, /* SI */
35  
36 /* 0x10-0x1F */
37 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
38 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
39 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
40 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
41  
42 /* 0x20-0x2F: SPACE ! " # $ % & ' ( ) * + , - . / */
43 PDFMAKE_CC_WHITESPACE, /* SPACE */
44 PDFMAKE_CC_REGULAR, /* ! */
45 PDFMAKE_CC_REGULAR, /* " */
46 PDFMAKE_CC_REGULAR, /* # */
47 PDFMAKE_CC_REGULAR, /* $ */
48 PDFMAKE_CC_DELIMITER, /* % comment start */
49 PDFMAKE_CC_REGULAR, /* & */
50 PDFMAKE_CC_REGULAR, /* ' */
51 PDFMAKE_CC_DELIMITER, /* ( */
52 PDFMAKE_CC_DELIMITER, /* ) */
53 PDFMAKE_CC_REGULAR, /* * */
54 PDFMAKE_CC_SIGN, /* + */
55 PDFMAKE_CC_REGULAR, /* , */
56 PDFMAKE_CC_SIGN, /* - */
57 PDFMAKE_CC_REGULAR, /* . */
58 PDFMAKE_CC_DELIMITER, /* / name start */
59  
60 /* 0x30-0x3F: 0-9 : ; < = > ? */
61 PDFMAKE_CC_DIGIT | PDFMAKE_CC_HEX, /* 0 */
62 PDFMAKE_CC_DIGIT | PDFMAKE_CC_HEX, /* 1 */
63 PDFMAKE_CC_DIGIT | PDFMAKE_CC_HEX, /* 2 */
64 PDFMAKE_CC_DIGIT | PDFMAKE_CC_HEX, /* 3 */
65 PDFMAKE_CC_DIGIT | PDFMAKE_CC_HEX, /* 4 */
66 PDFMAKE_CC_DIGIT | PDFMAKE_CC_HEX, /* 5 */
67 PDFMAKE_CC_DIGIT | PDFMAKE_CC_HEX, /* 6 */
68 PDFMAKE_CC_DIGIT | PDFMAKE_CC_HEX, /* 7 */
69 PDFMAKE_CC_DIGIT | PDFMAKE_CC_HEX, /* 8 */
70 PDFMAKE_CC_DIGIT | PDFMAKE_CC_HEX, /* 9 */
71 PDFMAKE_CC_REGULAR, /* : */
72 PDFMAKE_CC_REGULAR, /* ; */
73 PDFMAKE_CC_DELIMITER, /* < */
74 PDFMAKE_CC_REGULAR, /* = */
75 PDFMAKE_CC_DELIMITER, /* > */
76 PDFMAKE_CC_REGULAR, /* ? */
77  
78 /* 0x40-0x4F: @ A-O */
79 PDFMAKE_CC_REGULAR, /* @ */
80 PDFMAKE_CC_ALPHA | PDFMAKE_CC_HEX, /* A */
81 PDFMAKE_CC_ALPHA | PDFMAKE_CC_HEX, /* B */
82 PDFMAKE_CC_ALPHA | PDFMAKE_CC_HEX, /* C */
83 PDFMAKE_CC_ALPHA | PDFMAKE_CC_HEX, /* D */
84 PDFMAKE_CC_ALPHA | PDFMAKE_CC_HEX, /* E */
85 PDFMAKE_CC_ALPHA | PDFMAKE_CC_HEX, /* F */
86 PDFMAKE_CC_ALPHA, /* G */
87 PDFMAKE_CC_ALPHA, /* H */
88 PDFMAKE_CC_ALPHA, /* I */
89 PDFMAKE_CC_ALPHA, /* J */
90 PDFMAKE_CC_ALPHA, /* K */
91 PDFMAKE_CC_ALPHA, /* L */
92 PDFMAKE_CC_ALPHA, /* M */
93 PDFMAKE_CC_ALPHA, /* N */
94 PDFMAKE_CC_ALPHA, /* O */
95  
96 /* 0x50-0x5F: P-Z [ \ ] ^ _ */
97 PDFMAKE_CC_ALPHA, /* P */
98 PDFMAKE_CC_ALPHA, /* Q */
99 PDFMAKE_CC_ALPHA, /* R */
100 PDFMAKE_CC_ALPHA, /* S */
101 PDFMAKE_CC_ALPHA, /* T */
102 PDFMAKE_CC_ALPHA, /* U */
103 PDFMAKE_CC_ALPHA, /* V */
104 PDFMAKE_CC_ALPHA, /* W */
105 PDFMAKE_CC_ALPHA, /* X */
106 PDFMAKE_CC_ALPHA, /* Y */
107 PDFMAKE_CC_ALPHA, /* Z */
108 PDFMAKE_CC_DELIMITER, /* [ */
109 PDFMAKE_CC_REGULAR, /* \ */
110 PDFMAKE_CC_DELIMITER, /* ] */
111 PDFMAKE_CC_REGULAR, /* ^ */
112 PDFMAKE_CC_REGULAR, /* _ */
113  
114 /* 0x60-0x6F: ` a-o */
115 PDFMAKE_CC_REGULAR, /* ` */
116 PDFMAKE_CC_ALPHA | PDFMAKE_CC_HEX, /* a */
117 PDFMAKE_CC_ALPHA | PDFMAKE_CC_HEX, /* b */
118 PDFMAKE_CC_ALPHA | PDFMAKE_CC_HEX, /* c */
119 PDFMAKE_CC_ALPHA | PDFMAKE_CC_HEX, /* d */
120 PDFMAKE_CC_ALPHA | PDFMAKE_CC_HEX, /* e */
121 PDFMAKE_CC_ALPHA | PDFMAKE_CC_HEX, /* f */
122 PDFMAKE_CC_ALPHA, /* g */
123 PDFMAKE_CC_ALPHA, /* h */
124 PDFMAKE_CC_ALPHA, /* i */
125 PDFMAKE_CC_ALPHA, /* j */
126 PDFMAKE_CC_ALPHA, /* k */
127 PDFMAKE_CC_ALPHA, /* l */
128 PDFMAKE_CC_ALPHA, /* m */
129 PDFMAKE_CC_ALPHA, /* n */
130 PDFMAKE_CC_ALPHA, /* o */
131  
132 /* 0x70-0x7F: p-z { | } ~ DEL */
133 PDFMAKE_CC_ALPHA, /* p */
134 PDFMAKE_CC_ALPHA, /* q */
135 PDFMAKE_CC_ALPHA, /* r */
136 PDFMAKE_CC_ALPHA, /* s */
137 PDFMAKE_CC_ALPHA, /* t */
138 PDFMAKE_CC_ALPHA, /* u */
139 PDFMAKE_CC_ALPHA, /* v */
140 PDFMAKE_CC_ALPHA, /* w */
141 PDFMAKE_CC_ALPHA, /* x */
142 PDFMAKE_CC_ALPHA, /* y */
143 PDFMAKE_CC_ALPHA, /* z */
144 PDFMAKE_CC_DELIMITER, /* { */
145 PDFMAKE_CC_REGULAR, /* | */
146 PDFMAKE_CC_DELIMITER, /* } */
147 PDFMAKE_CC_REGULAR, /* ~ */
148 PDFMAKE_CC_REGULAR, /* DEL */
149  
150 /* 0x80-0xFF: High bytes - all regular in PDF */
151 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
152 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
153 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
154 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
155 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
156 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
157 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
158 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
159 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
160 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
161 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
162 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
163 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
164 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
165 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
166 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
167 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
168 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
169 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
170 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
171 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
172 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
173 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
174 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
175 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
176 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
177 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
178 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
179 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
180 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
181 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
182 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
183 };
184  
185 /*============================================================================
186 * Token kind names (for debugging)
187 *==========================================================================*/
188  
189 static const char *tok_kind_names[] = {
190 "EOF", "WS", "COMMENT",
191 "INT", "REAL", "NAME", "LSTR", "HSTR",
192 "ARR_OPEN", "ARR_CLOSE", "DICT_OPEN", "DICT_CLOSE",
193 "KW_NULL", "KW_TRUE", "KW_FALSE",
194 "KW_OBJ", "KW_ENDOBJ", "KW_STREAM", "KW_ENDSTREAM",
195 "KW_R", "KW_XREF", "KW_TRAILER", "KW_STARTXREF",
196 "ERROR"
197 };
198  
199 0 const char *pdfmake_tok_kind_name(pdfmake_tok_kind_t kind) {
200 0 ▶ if (kind >= 0 && kind <= PDFMAKE_TOK_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if (kind >= 0 && kind <= PDFMAKE_TOK_ERROR) { 0 0 0
201 0 return tok_kind_names[kind];
202 }
203 0 return "UNKNOWN";
204 }
205  
206 /*============================================================================
207 * Tokenizer initialization
208 *==========================================================================*/
209  
210 2160 void pdfmake_tokenizer_init(pdfmake_tokenizer_t *t,
211 const uint8_t *buf, size_t len) {
212 2160 t->buf = buf;
213 2160 t->pos = 0;
214 2160 t->len = len;
215 2160 t->has_peeked = 0;
216 2160 }
217  
218 1 void pdfmake_tokenizer_seek(pdfmake_tokenizer_t *t, size_t pos) {
219 1 t->pos = pos < t->len ? pos : t->len;
220 1 t->has_peeked = 0; /* Invalidate peek cache */
221 1 }
222  
223 /*============================================================================
224 * Whitespace lexer (§7.2.2)
225 *==========================================================================*/
226  
227 55608 static pdfmake_tok_t lex_whitespace(pdfmake_tokenizer_t *t) {
228 pdfmake_tok_t tok;
229 55608 tok.kind = PDFMAKE_TOK_WS;
230 55608 tok.offset = t->pos;
231  
232 117004 ▶ while (t->pos < t->len && pdfmake_is_whitespace(t->buf[t->pos])) {
Branch: 2 branches
BranchTrueFalseTotal
while (t->pos < t->len && pdfmake_is_whitespace(t->buf[t->pos])) { 116912 92 117004
while (t->pos < t->len && pdfmake_is_whitespace(t->buf[t->pos])) { 61396 55516 116912
233 61396 t->pos++;
234 }
235  
236 55608 tok.length = t->pos - tok.offset;
237 55608 return tok;
238 }
239  
240 /*============================================================================
241 * Comment lexer (§7.2.3)
242 * % through end of line (LF or CR or CRLF)
243 *==========================================================================*/
244  
245 73 static pdfmake_tok_t lex_comment(pdfmake_tokenizer_t *t) {
246 pdfmake_tok_t tok;
247 73 tok.kind = PDFMAKE_TOK_COMMENT;
248 73 tok.offset = t->pos;
249  
250 73 t->pos++; /* Skip % */
251  
252 550 ▶ while (t->pos < t->len) {
Branch: 1 branch
BranchTrueFalseTotal
while (t->pos < t->len) { 540 10 550
253 540 uint8_t c = t->buf[t->pos];
254 540 ▶ if (c == '\n') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '\n') { 63 477 540
255 63 t->pos++;
256 63 break;
257 }
258 477 ▶ if (c == '\r') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '\r') { 0 477 477
259 0 t->pos++;
260 0 ▶ if (t->pos < t->len && t->buf[t->pos] == '\n') {
Branch: 2 branches
BranchTrueFalseTotal
if (t->pos < t->len && t->buf[t->pos] == '\n') { 0 0 0
if (t->pos < t->len && t->buf[t->pos] == '\n') { 0 0 0
261 0 t->pos++;
262 }
263 0 break;
264 }
265 477 t->pos++;
266 }
267  
268 73 tok.length = t->pos - tok.offset;
269 73 return tok;
270 }
271  
272 /*============================================================================
273 * Number lexer (§7.3.3)
274 * Integers: [+-]?\d+
275 * Reals: [+-]?\d*\.\d* (at least one digit somewhere)
276 * No scientific notation in PDF.
277 *==========================================================================*/
278  
279 34364 static pdfmake_tok_t lex_number(pdfmake_tokenizer_t *t) {
280 pdfmake_tok_t tok;
281 34364 int is_real = 0;
282 34364 int has_digits = 0;
283 34364 int is_negative = 0;
284 34364 double val = 0.0;
285 34364 double frac = 0.0;
286 34364 double frac_div = 1.0;
287 34364 int in_frac = 0;
288 34364 int64_t ival = 0;
289 size_t i;
290  
291 34364 tok.offset = t->pos;
292  
293 /* Optional sign */
294 34364 ▶ if (t->pos < t->len && (t->buf[t->pos] == '+' || t->buf[t->pos] == '-')) {
Branch: 3 branches
BranchTrueFalseTotal
if (t->pos < t->len && (t->buf[t->pos] == '+' || t->buf[t->pos] == '-')) { 34364 0 34364
if (t->pos < t->len && (t->buf[t->pos] == '+' || t->buf[t->pos] == '-')) { 34364 0 34364
if (t->pos < t->len && (t->buf[t->pos] == '+' || t->buf[t->pos] == '-')) { 2518 31846 34364
295 2518 ▶ if (t->buf[t->pos] == '-') is_negative = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (t->buf[t->pos] == '-') is_negative = 1; 2518 0 2518
296 2518 t->pos++;
297 }
298  
299 /* Digits before decimal point */
300 120259 ▶ while (t->pos < t->len && pdfmake_is_digit(t->buf[t->pos])) {
Branch: 2 branches
BranchTrueFalseTotal
while (t->pos < t->len && pdfmake_is_digit(t->buf[t->pos])) { 120259 0 120259
while (t->pos < t->len && pdfmake_is_digit(t->buf[t->pos])) { 85895 34364 120259
301 85895 has_digits = 1;
302 85895 t->pos++;
303 }
304  
305 /* Decimal point */
306 34364 ▶ if (t->pos < t->len && t->buf[t->pos] == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (t->pos < t->len && t->buf[t->pos] == '.') { 34364 0 34364
if (t->pos < t->len && t->buf[t->pos] == '.') { 4983 29381 34364
307 4983 is_real = 1;
308 4983 t->pos++;
309  
310 /* Digits after decimal point */
311 16207 ▶ while (t->pos < t->len && pdfmake_is_digit(t->buf[t->pos])) {
Branch: 2 branches
BranchTrueFalseTotal
while (t->pos < t->len && pdfmake_is_digit(t->buf[t->pos])) { 16207 0 16207
while (t->pos < t->len && pdfmake_is_digit(t->buf[t->pos])) { 11224 4983 16207
312 11224 has_digits = 1;
313 11224 t->pos++;
314 }
315 }
316  
317 34364 tok.length = t->pos - tok.offset;
318  
319 34364 ▶ if (!has_digits) {
Branch: 1 branch
BranchTrueFalseTotal
if (!has_digits) { 0 34364 34364
320 /* Just a sign or just a dot — error */
321 0 tok.kind = PDFMAKE_TOK_ERROR;
322 0 return tok;
323 }
324  
325 /* Parse the number */
326 34364 ▶ if (is_real) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_real) { 4983 29381 34364
327 4983 tok.kind = PDFMAKE_TOK_REAL;
328 /* Parse directly — no need for strtod locale issues */
329 31583 ▶ for (i = tok.offset; i < tok.offset + tok.length; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = tok.offset; i < tok.offset + tok.length; i++) { 26600 4983 31583
330 26600 uint8_t c = t->buf[i];
331 26600 ▶ if (c == '+' || c == '-') continue;
Branch: 2 branches
BranchTrueFalseTotal
if (c == '+' || c == '-') continue; 26600 0 26600
if (c == '+' || c == '-') continue; 2124 24476 26600
332 24476 ▶ if (c == '.') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '.') { 4983 19493 24476
333 4983 in_frac = 1;
334 4983 continue;
335 }
336 19493 ▶ if (in_frac) {
Branch: 1 branch
BranchTrueFalseTotal
if (in_frac) { 11224 8269 19493
337 11224 frac_div *= 10.0;
338 11224 frac += (c - '0') / frac_div;
339 } else {
340 8269 val = val * 10.0 + (c - '0');
341 }
342 }
343 4983 ▶ tok.payload.real_val = is_negative ? -(val + frac) : (val + frac);
Branch: 1 branch
BranchTrueFalseTotal
tok.payload.real_val = is_negative ? -(val + frac) : (val + frac); 2124 2859 4983
344 } else {
345 29381 tok.kind = PDFMAKE_TOK_INT;
346 107401 ▶ for (i = tok.offset; i < tok.offset + tok.length; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = tok.offset; i < tok.offset + tok.length; i++) { 78020 29381 107401
347 78020 uint8_t c = t->buf[i];
348 78020 ▶ if (c == '+' || c == '-') continue;
Branch: 2 branches
BranchTrueFalseTotal
if (c == '+' || c == '-') continue; 78020 0 78020
if (c == '+' || c == '-') continue; 394 77626 78020
349 77626 ival = ival * 10 + (c - '0');
350 }
351 29381 ▶ tok.payload.int_val = is_negative ? -ival : ival;
Branch: 1 branch
BranchTrueFalseTotal
tok.payload.int_val = is_negative ? -ival : ival; 394 28987 29381
352 }
353  
354 34364 return tok;
355 }
356  
357 /*============================================================================
358 * Name lexer (§7.3.5)
359 * /Name with #XX hex escape
360 *==========================================================================*/
361  
362 10326 static pdfmake_tok_t lex_name(pdfmake_tokenizer_t *t) {
363 pdfmake_tok_t tok;
364 10326 tok.kind = PDFMAKE_TOK_NAME;
365 10326 tok.offset = t->pos;
366  
367 10326 t->pos++; /* Skip / */
368  
369 /* Name continues until whitespace or delimiter */
370 75942 ▶ while (t->pos < t->len) {
Branch: 1 branch
BranchTrueFalseTotal
while (t->pos < t->len) { 75942 0 75942
371 75942 uint8_t c = t->buf[t->pos];
372 75942 ▶ if (pdfmake_is_whitespace(c) || pdfmake_is_delimiter(c)) {
Branch: 2 branches
BranchTrueFalseTotal
if (pdfmake_is_whitespace(c) || pdfmake_is_delimiter(c)) { 66669 9273 75942
if (pdfmake_is_whitespace(c) || pdfmake_is_delimiter(c)) { 65616 1053 66669
373 break;
374 }
375 65616 ▶ if (c == '#') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '#') { 1 65615 65616
376 /* Hex escape: skip #XX */
377 2 if (t->pos + 2 < t->len &&
378 2 ▶ pdfmake_is_hex(t->buf[t->pos + 1]) &&
Branch: 1 branch
BranchTrueFalseTotal
pdfmake_is_hex(t->buf[t->pos + 1]) && 1 0 1
379 1 pdfmake_is_hex(t->buf[t->pos + 2])) {
380 1 t->pos += 3;
381 } else {
382 /* Invalid escape — treat # as regular char */
383 0 t->pos++;
384 }
385 } else {
386 65615 t->pos++;
387 }
388 }
389  
390 10326 tok.length = t->pos - tok.offset;
391 10326 return tok;
392 }
393  
394 /*============================================================================
395 * Literal string lexer (§7.3.4.2)
396 * (string) with balanced parens and escapes
397 *==========================================================================*/
398  
399 3703 static pdfmake_tok_t lex_literal_string(pdfmake_tokenizer_t *t) {
400 pdfmake_tok_t tok;
401 int depth;
402 3703 tok.kind = PDFMAKE_TOK_LSTR;
403 3703 tok.offset = t->pos;
404  
405 3703 t->pos++; /* Skip opening ( */
406 3703 depth = 1;
407  
408 28317 ▶ while (t->pos < t->len && depth > 0) {
Branch: 2 branches
BranchTrueFalseTotal
while (t->pos < t->len && depth > 0) { 28317 0 28317
while (t->pos < t->len && depth > 0) { 24614 3703 28317
409 24614 uint8_t c = t->buf[t->pos];
410  
411 24614 ▶ if (c == '(') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '(') { 0 24614 24614
412 0 depth++;
413 0 t->pos++;
414 24614 ▶ } else if (c == ')') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c == ')') { 3703 20911 24614
415 3703 depth--;
416 3703 t->pos++;
417 20911 ▶ } else if (c == '\\') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c == '\\') { 53 20858 20911
418 /* Escape sequence */
419 53 t->pos++;
420 53 ▶ if (t->pos < t->len) {
Branch: 1 branch
BranchTrueFalseTotal
if (t->pos < t->len) { 53 0 53
421 53 uint8_t esc = t->buf[t->pos];
422 53 ▶ if (esc >= '0' && esc <= '7') {
Branch: 2 branches
BranchTrueFalseTotal
if (esc >= '0' && esc <= '7') { 21 32 53
if (esc >= '0' && esc <= '7') { 20 1 21
423 /* Octal: 1-3 digits */
424 20 t->pos++;
425 20 ▶ if (t->pos < t->len && t->buf[t->pos] >= '0' && t->buf[t->pos] <= '7') {
Branch: 3 branches
BranchTrueFalseTotal
if (t->pos < t->len && t->buf[t->pos] >= '0' && t->buf[t->pos] <= '7') { 20 0 20
if (t->pos < t->len && t->buf[t->pos] >= '0' && t->buf[t->pos] <= '7') { 20 0 20
if (t->pos < t->len && t->buf[t->pos] >= '0' && t->buf[t->pos] <= '7') { 20 0 20
426 20 t->pos++;
427 20 ▶ if (t->pos < t->len && t->buf[t->pos] >= '0' && t->buf[t->pos] <= '7') {
Branch: 3 branches
BranchTrueFalseTotal
if (t->pos < t->len && t->buf[t->pos] >= '0' && t->buf[t->pos] <= '7') { 20 0 20
if (t->pos < t->len && t->buf[t->pos] >= '0' && t->buf[t->pos] <= '7') { 20 0 20
if (t->pos < t->len && t->buf[t->pos] >= '0' && t->buf[t->pos] <= '7') { 20 0 20
428 20 t->pos++;
429 }
430 }
431 33 ▶ } else if (esc == '\r') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (esc == '\r') { 0 33 33
432 /* Line continuation: \CR or \CRLF */
433 0 t->pos++;
434 0 ▶ if (t->pos < t->len && t->buf[t->pos] == '\n') {
Branch: 2 branches
BranchTrueFalseTotal
if (t->pos < t->len && t->buf[t->pos] == '\n') { 0 0 0
if (t->pos < t->len && t->buf[t->pos] == '\n') { 0 0 0
435 0 t->pos++;
436 }
437 33 ▶ } else if (esc == '\n') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (esc == '\n') { 0 33 33
438 /* Line continuation: \LF */
439 0 t->pos++;
440 } else {
441 /* Single-char escape: n r t b f ( ) \ or unknown */
442 33 t->pos++;
443 }
444 }
445 } else {
446 20858 t->pos++;
447 }
448 }
449  
450 3703 tok.length = t->pos - tok.offset;
451  
452 3703 ▶ if (depth != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (depth != 0) { 0 3703 3703
453 /* Unbalanced parens */
454 0 tok.kind = PDFMAKE_TOK_ERROR;
455 }
456  
457 3703 return tok;
458 }
459  
460 /*============================================================================
461 * Hex string lexer (§7.3.4.3)
462 * <hex> with whitespace ignored
463 *==========================================================================*/
464  
465 349 static pdfmake_tok_t lex_hex_string(pdfmake_tokenizer_t *t) {
466 pdfmake_tok_t tok;
467 349 tok.kind = PDFMAKE_TOK_HSTR;
468 349 tok.offset = t->pos;
469  
470 349 t->pos++; /* Skip < */
471  
472 12362 ▶ while (t->pos < t->len) {
Branch: 1 branch
BranchTrueFalseTotal
while (t->pos < t->len) { 12362 0 12362
473 12362 uint8_t c = t->buf[t->pos];
474  
475 12362 ▶ if (c == '>') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '>') { 349 12013 12362
476 349 t->pos++;
477 349 break;
478 }
479  
480 /* Skip whitespace inside hex string */
481 12013 ▶ if (pdfmake_is_whitespace(c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_is_whitespace(c)) { 0 12013 12013
482 0 t->pos++;
483 0 continue;
484 }
485  
486 /* Must be hex digit */
487 12013 ▶ if (!pdfmake_is_hex(c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pdfmake_is_hex(c)) { 0 12013 12013
488 /* Invalid character in hex string */
489 0 tok.kind = PDFMAKE_TOK_ERROR;
490 0 t->pos++;
491 0 break;
492 }
493  
494 12013 t->pos++;
495 }
496  
497 349 tok.length = t->pos - tok.offset;
498 349 return tok;
499 }
500  
501 /*============================================================================
502 * Keyword/identifier lexer
503 * Identifies: null, true, false, obj, endobj, stream, endstream, R,
504 * xref, trailer, startxref
505 *==========================================================================*/
506  
507 /* Keyword table for fast lookup */
508 typedef struct {
509 const char *text;
510 size_t len;
511 pdfmake_tok_kind_t kind;
512 } keyword_entry_t;
513  
514 static const keyword_entry_t keywords[] = {
515 {"null", 4, PDFMAKE_TOK_KW_NULL},
516 {"true", 4, PDFMAKE_TOK_KW_TRUE},
517 {"false", 5, PDFMAKE_TOK_KW_FALSE},
518 {"obj", 3, PDFMAKE_TOK_KW_OBJ},
519 {"endobj", 6, PDFMAKE_TOK_KW_ENDOBJ},
520 {"stream", 6, PDFMAKE_TOK_KW_STREAM},
521 {"endstream", 9, PDFMAKE_TOK_KW_ENDSTREAM},
522 {"R", 1, PDFMAKE_TOK_KW_R},
523 {"xref", 4, PDFMAKE_TOK_KW_XREF},
524 {"trailer", 7, PDFMAKE_TOK_KW_TRAILER},
525 {"startxref", 9, PDFMAKE_TOK_KW_STARTXREF},
526 {NULL, 0, PDFMAKE_TOK_ERROR}
527 };
528  
529 13676 static pdfmake_tok_t lex_keyword_or_error(pdfmake_tokenizer_t *t) {
530 pdfmake_tok_t tok;
531 const keyword_entry_t *kw;
532 13676 tok.offset = t->pos;
533  
534 /* Read until whitespace or delimiter */
535 48268 ▶ while (t->pos < t->len && pdfmake_is_regular(t->buf[t->pos])) {
Branch: 2 branches
BranchTrueFalseTotal
while (t->pos < t->len && pdfmake_is_regular(t->buf[t->pos])) { 48242 26 48268
while (t->pos < t->len && pdfmake_is_regular(t->buf[t->pos])) { 34592 13650 48242
536 34592 t->pos++;
537 }
538  
539 13676 tok.length = t->pos - tok.offset;
540  
541 /* Check against keyword table */
542 127754 ▶ for (kw = keywords; kw->text != NULL; kw++) {
Branch: 1 branch
BranchTrueFalseTotal
for (kw = keywords; kw->text != NULL; kw++) { 120719 7035 127754
543 120719 ▶ if (tok.length == kw->len &&
Branch: 1 branch
BranchTrueFalseTotal
if (tok.length == kw->len && 10867 109852 120719
544 10867 ▶ memcmp(t->buf + tok.offset, kw->text, kw->len) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
memcmp(t->buf + tok.offset, kw->text, kw->len) == 0) { 6641 4226 10867
545 6641 tok.kind = kw->kind;
546  
547 /* For 'stream', compute body offset */
548 6641 ▶ if (tok.kind == PDFMAKE_TOK_KW_STREAM) {
Branch: 1 branch
BranchTrueFalseTotal
if (tok.kind == PDFMAKE_TOK_KW_STREAM) { 339 6302 6641
549 /* §7.3.8.1: stream keyword followed by EOL (LF or CRLF)
550 * Body starts after the EOL */
551 339 size_t body_start = t->pos;
552  
553 /* Skip optional whitespace before EOL (lenient) */
554 339 ▶ while (body_start < t->len &&
Branch: 1 branch
BranchTrueFalseTotal
while (body_start < t->len && 339 0 339
555 339 ▶ (t->buf[body_start] == ' ' || t->buf[body_start] == '\t')) {
Branch: 2 branches
BranchTrueFalseTotal
(t->buf[body_start] == ' ' || t->buf[body_start] == '\t')) { 0 339 339
(t->buf[body_start] == ' ' || t->buf[body_start] == '\t')) { 0 339 339
556 0 body_start++;
557 }
558  
559 /* Must have EOL */
560 339 ▶ if (body_start < t->len && t->buf[body_start] == '\r') {
Branch: 2 branches
BranchTrueFalseTotal
if (body_start < t->len && t->buf[body_start] == '\r') { 339 0 339
if (body_start < t->len && t->buf[body_start] == '\r') { 160 179 339
561 160 body_start++;
562 160 ▶ if (body_start < t->len && t->buf[body_start] == '\n') {
Branch: 2 branches
BranchTrueFalseTotal
if (body_start < t->len && t->buf[body_start] == '\n') { 160 0 160
if (body_start < t->len && t->buf[body_start] == '\n') { 160 0 160
563 160 body_start++;
564 }
565 /* Note: CR alone is technically not spec-compliant but
566 * we accept it for Adobe compatibility */
567 179 ▶ } else if (body_start < t->len && t->buf[body_start] == '\n') {
Branch: 2 branches
BranchTrueFalseTotal
} else if (body_start < t->len && t->buf[body_start] == '\n') { 179 0 179
} else if (body_start < t->len && t->buf[body_start] == '\n') { 179 0 179
568 179 body_start++;
569 }
570  
571 339 tok.payload.body_offset = body_start;
572 }
573  
574 6641 return tok;
575 }
576 }
577  
578 /* Not a keyword — error (bare identifier not valid in PDF) */
579 7035 tok.kind = PDFMAKE_TOK_ERROR;
580 7035 return tok;
581 }
582  
583 /*============================================================================
584 * Main tokenizer
585 *==========================================================================*/
586  
587 176559 pdfmake_tok_t pdfmake_tok_next(pdfmake_tokenizer_t *t) {
588 pdfmake_tok_t tok;
589 uint8_t c;
590 /* Return peeked token if available */
591 176559 ▶ if (t->has_peeked) {
Branch: 1 branch
BranchTrueFalseTotal
if (t->has_peeked) { 50630 125929 176559
592 50630 t->has_peeked = 0;
593 50630 return t->peeked;
594 }
595  
596 /* EOF check */
597 125929 ▶ if (t->pos >= t->len) {
Branch: 1 branch
BranchTrueFalseTotal
if (t->pos >= t->len) { 164 125765 125929
598 164 tok.kind = PDFMAKE_TOK_EOF;
599 164 tok.offset = t->len;
600 164 tok.length = 0;
601 164 return tok;
602 }
603  
604 125765 c = t->buf[t->pos];
605  
606 /* Whitespace */
607 125765 ▶ if (pdfmake_is_whitespace(c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_is_whitespace(c)) { 55608 70157 125765
608 55608 return lex_whitespace(t);
609 }
610  
611 /* Comment */
612 70157 ▶ if (c == '%') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '%') { 73 70084 70157
613 73 return lex_comment(t);
614 }
615  
616 /* Number (starts with digit, +, -, or .) */
617 70084 ▶ if (pdfmake_is_digit(c) ||
Branch: 2 branches
BranchTrueFalseTotal
if (pdfmake_is_digit(c) || 38239 31845 70084
if (pdfmake_is_digit(c) || 38239 0 38239
618 38239 ▶ ((c == '+' || c == '-' || c == '.') &&
Branch: 2 branches
BranchTrueFalseTotal
((c == '+' || c == '-' || c == '.') && 35721 2518 38239
((c == '+' || c == '-' || c == '.') && 1 35720 35721
619 5038 t->pos + 1 < t->len &&
620 2519 ▶ (pdfmake_is_digit(t->buf[t->pos + 1]) || t->buf[t->pos + 1] == '.'))) {
Branch: 1 branch
BranchTrueFalseTotal
(pdfmake_is_digit(t->buf[t->pos + 1]) || t->buf[t->pos + 1] == '.'))) { 0 0 0
621 34364 return lex_number(t);
622 }
623  
624 /* Name */
625 35720 ▶ if (c == '/') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '/') { 10326 25394 35720
626 10326 return lex_name(t);
627 }
628  
629 /* Literal string */
630 25394 ▶ if (c == '(') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '(') { 3703 21691 25394
631 3703 return lex_literal_string(t);
632 }
633  
634 /* Hex string or dict delimiter */
635 21691 ▶ if (c == '<') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '<') { 2597 19094 21691
636 2597 ▶ if (t->pos + 1 < t->len && t->buf[t->pos + 1] == '<') {
Branch: 2 branches
BranchTrueFalseTotal
if (t->pos + 1 < t->len && t->buf[t->pos + 1] == '<') { 2597 0 2597
if (t->pos + 1 < t->len && t->buf[t->pos + 1] == '<') { 2248 349 2597
637 /* << dict open */
638 2248 tok.kind = PDFMAKE_TOK_DICT_OPEN;
639 2248 tok.offset = t->pos;
640 2248 tok.length = 2;
641 2248 t->pos += 2;
642 2248 return tok;
643 }
644 349 return lex_hex_string(t);
645 }
646  
647 /* Dict close */
648 19094 ▶ if (c == '>') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '>') { 2243 16851 19094
649 2243 ▶ if (t->pos + 1 < t->len && t->buf[t->pos + 1] == '>') {
Branch: 2 branches
BranchTrueFalseTotal
if (t->pos + 1 < t->len && t->buf[t->pos + 1] == '>') { 2243 0 2243
if (t->pos + 1 < t->len && t->buf[t->pos + 1] == '>') { 2243 0 2243
650 2243 tok.kind = PDFMAKE_TOK_DICT_CLOSE;
651 2243 tok.offset = t->pos;
652 2243 tok.length = 2;
653 2243 t->pos += 2;
654 2243 return tok;
655 }
656 /* Lone > is an error */
657 0 tok.kind = PDFMAKE_TOK_ERROR;
658 0 tok.offset = t->pos;
659 0 tok.length = 1;
660 0 t->pos++;
661 0 return tok;
662 }
663  
664 /* Array delimiters */
665 16851 ▶ if (c == '[') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '[') { 1362 15489 16851
666 1362 tok.kind = PDFMAKE_TOK_ARR_OPEN;
667 1362 tok.offset = t->pos;
668 1362 tok.length = 1;
669 1362 t->pos++;
670 1362 return tok;
671 }
672  
673 15489 ▶ if (c == ']') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == ']') { 1813 13676 15489
674 1813 tok.kind = PDFMAKE_TOK_ARR_CLOSE;
675 1813 tok.offset = t->pos;
676 1813 tok.length = 1;
677 1813 t->pos++;
678 1813 return tok;
679 }
680  
681 /* Braces (not used in PDF content, but are delimiters) */
682 13676 ▶ if (c == '{' || c == '}') {
Branch: 2 branches
BranchTrueFalseTotal
if (c == '{' || c == '}') { 13676 0 13676
if (c == '{' || c == '}') { 0 13676 13676
683 0 tok.kind = PDFMAKE_TOK_ERROR;
684 0 tok.offset = t->pos;
685 0 tok.length = 1;
686 0 t->pos++;
687 0 return tok;
688 }
689  
690 /* Close paren without open (error) */
691 13676 ▶ if (c == ')') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == ')') { 0 13676 13676
692 0 tok.kind = PDFMAKE_TOK_ERROR;
693 0 tok.offset = t->pos;
694 0 tok.length = 1;
695 0 t->pos++;
696 0 return tok;
697 }
698  
699 /* Keyword or unknown identifier */
700 13676 return lex_keyword_or_error(t);
701 }
702  
703 /*============================================================================
704 * Peek
705 *==========================================================================*/
706  
707 58859 void pdfmake_tok_peek(pdfmake_tokenizer_t *t, pdfmake_tok_t *out) {
708 58859 ▶ if (!t->has_peeked) {
Branch: 1 branch
BranchTrueFalseTotal
if (!t->has_peeked) { 51025 7834 58859
709 51025 t->peeked = pdfmake_tok_next(t);
710 51025 t->has_peeked = 1;
711 }
712 58859 *out = t->peeked;
713 58859 }
714  
715 /*============================================================================
716 * Skip whitespace/comments
717 *==========================================================================*/
718  
719 66207 pdfmake_tok_t pdfmake_tok_next_significant(pdfmake_tokenizer_t *t) {
720 pdfmake_tok_t tok;
721 do {
722 95642 tok = pdfmake_tok_next(t);
723 95642 ▶ } while (tok.kind == PDFMAKE_TOK_WS || tok.kind == PDFMAKE_TOK_COMMENT);
Branch: 2 branches
BranchTrueFalseTotal
} while (tok.kind == PDFMAKE_TOK_WS || tok.kind == PDFMAKE_TOK_COMMENT); 29393 66249 95642
} while (tok.kind == PDFMAKE_TOK_WS || tok.kind == PDFMAKE_TOK_COMMENT); 42 66207 66249
724 66207 return tok;
725 }
726  
727 36221 void pdfmake_tok_peek_significant(pdfmake_tokenizer_t *t, pdfmake_tok_t *out) {
728 /* Skip whitespace/comments, then peek */
729 while (1) {
730 58859 pdfmake_tok_peek(t, out);
731 58859 ▶ if (out->kind != PDFMAKE_TOK_WS && out->kind != PDFMAKE_TOK_COMMENT) {
Branch: 2 branches
BranchTrueFalseTotal
if (out->kind != PDFMAKE_TOK_WS && out->kind != PDFMAKE_TOK_COMMENT) { 36222 22637 58859
if (out->kind != PDFMAKE_TOK_WS && out->kind != PDFMAKE_TOK_COMMENT) { 36221 1 36222
732 36221 break;
733 }
734 22638 pdfmake_tok_next(t); /* Consume and continue */
735 }
736 36221 }
« xs/writer.xs ↑ Summary src/pdfmake_markup_style.c »