ckdl/src/tokenizer.c

filter: Statementstmt 81.1% 323 / 398 Branchbran 62.5% 205 / 328 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 72.7% 528 / 726 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« ckdl/src/compat.c ↑ Summary ckdl/src/utf8.c »
1 #include "kdl/tokenizer.h"
2 #include "compat.h"
3 #include "grammar.h"
4 #include "utf8.h"
5  
6 #include <stdbool.h>
7 #include <stdlib.h>
8 #include <string.h>
9  
10 // No buffering in Debug mode to find more bugs
11 #ifdef KDL_DEBUG
12 # define MIN_BUFFER_SIZE 1
13 # define BUFFER_SIZE_INCREMENT 1
14 #else
15 # define MIN_BUFFER_SIZE 1024
16 # define BUFFER_SIZE_INCREMENT 4096
17 #endif
18  
19 struct _kdl_tokenizer {
20 kdl_str document;
21 kdl_character_set charset;
22 kdl_read_func read_func;
23 void* read_user_data;
24 char* buffer;
25 size_t buffer_size;
26 };
27  
28 static inline void _remove_initial_bom(kdl_tokenizer* self);
29 static inline void _update_doc_ptr(kdl_tokenizer* self, char const* new_ptr);
30 static inline kdl_utf8_status _tok_get_char(
31 kdl_tokenizer* self, char const** cur, char const** next, uint32_t* codepoint);
32  
33 128 kdl_tokenizer* kdl_create_string_tokenizer(kdl_str doc)
34 {
35 128 kdl_tokenizer* self = malloc(sizeof(kdl_tokenizer));
36 128 ▶ if (self != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (self != NULL) { 128 0 128
37 128 self->document = doc;
38 128 self->charset = KDL_CHARACTER_SET_DEFAULT;
39 128 self->read_func = NULL;
40 128 self->read_user_data = NULL;
41 128 self->buffer = NULL;
42 128 self->buffer_size = 0;
43 }
44 128 _remove_initial_bom(self);
45 128 return self;
46 }
47  
48 38 kdl_tokenizer* kdl_create_stream_tokenizer(kdl_read_func read_func, void* user_data)
49 {
50 38 kdl_tokenizer* self = malloc(sizeof(kdl_tokenizer));
51 38 ▶ if (self != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (self != NULL) { 38 0 38
52 38 self->document = (kdl_str){.data = NULL, .len = 0};
53 38 self->charset = KDL_CHARACTER_SET_DEFAULT;
54 38 self->read_func = read_func;
55 38 self->read_user_data = user_data;
56 38 self->buffer = NULL;
57 38 self->buffer_size = 0;
58 }
59 38 _remove_initial_bom(self);
60 38 return self;
61 }
62  
63 166 void kdl_destroy_tokenizer(kdl_tokenizer* tokenizer)
64 {
65 166 ▶ if (tokenizer->buffer != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (tokenizer->buffer != NULL) { 38 128 166
66 38 free(tokenizer->buffer);
67 }
68 166 free(tokenizer);
69 166 }
70  
71 166 static inline void _remove_initial_bom(kdl_tokenizer* self)
72 {
73 166 uint32_t c = 0;
74 166 char const* cur = self->document.data;
75 166 char const* next = NULL;
76  
77 166 ▶ if (_tok_get_char(self, &cur, &next, &c) == KDL_UTF8_OK && c == 0xFEFF) {
Branch: 2 branches
BranchTrueFalseTotal
if (_tok_get_char(self, &cur, &next, &c) == KDL_UTF8_OK && c == 0xFEFF) { 162 4 166
if (_tok_get_char(self, &cur, &next, &c) == KDL_UTF8_OK && c == 0xFEFF) { 0 162 162
78 // skip initial BOM
79 0 _update_doc_ptr(self, next);
80 }
81 166 }
82  
83 477 void kdl_tokenizer_set_character_set(kdl_tokenizer* self, kdl_character_set cs) { self->charset = cs; }
84  
85 301 static size_t _refill_tokenizer(kdl_tokenizer* self)
86 {
87 301 ▶ if (self->read_func == NULL) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (self->read_func == NULL) return 0; 116 185 301
88  
89 185 ▶ if (self->buffer == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->buffer == NULL) { 38 147 185
90 38 self->buffer = malloc(BUFFER_SIZE_INCREMENT);
91 38 ▶ if (self->buffer == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->buffer == NULL) { 0 38 38
92 // memory allocation failed
93 0 return 0;
94 }
95 38 self->buffer_size = BUFFER_SIZE_INCREMENT;
96 38 self->document.len = 0;
97 }
98 // Move whatever data is left unparsed to the top of the buffer
99 185 ▶ if (self->document.len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->document.len > 0) { 123 62 185
100 123 memmove(self->buffer, self->document.data, self->document.len);
101 }
102 185 self->document.data = self->buffer;
103 185 size_t len_available = self->buffer_size - self->document.len;
104 185 ▶ if (len_available < MIN_BUFFER_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
if (len_available < MIN_BUFFER_SIZE) { 0 185 185
105 // Need more room
106 0 size_t new_buf_size = self->buffer_size + BUFFER_SIZE_INCREMENT;
107 0 char* new_buffer = realloc(self->buffer, new_buf_size);
108 0 ▶ if (new_buffer == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (new_buffer == NULL) { 0 0 0
109 0 return 0;
110 } else {
111 0 self->buffer = new_buffer;
112 0 self->buffer_size = new_buf_size;
113 0 self->document.data = new_buffer;
114 0 len_available = self->buffer_size - self->document.len;
115 }
116 }
117 185 char* end = self->buffer + self->document.len;
118  
119 185 size_t read_count = self->read_func(self->read_user_data, end, len_available);
120 185 self->document.len += read_count;
121 185 return read_count;
122 }
123  
124 610063 bool _kdl_is_whitespace(kdl_character_set charset, uint32_t c)
125 {
126 610063 ▶ return c == 0x0009 || // Character Tabulation
Branch: 1 branch
BranchTrueFalseTotal
return c == 0x0009 || // Character Tabulation 578369 31694 610063
127 578369 ▶ c == 0x0020 || // Space
Branch: 1 branch
BranchTrueFalseTotal
c == 0x0020 || // Space 578369 0 578369
128 578369 ▶ c == 0x00A0 || // No-Break Space
Branch: 1 branch
BranchTrueFalseTotal
c == 0x00A0 || // No-Break Space 578369 0 578369
129 578369 ▶ c == 0x1680 || // Ogham Space Mark
Branch: 1 branch
BranchTrueFalseTotal
c == 0x1680 || // Ogham Space Mark 578369 0 578369
130 578369 ▶ c == 0x2000 || // En Quad
Branch: 1 branch
BranchTrueFalseTotal
c == 0x2000 || // En Quad 578369 0 578369
131 578369 ▶ c == 0x2001 || // Em Quad
Branch: 1 branch
BranchTrueFalseTotal
c == 0x2001 || // Em Quad 578369 0 578369
132 578369 ▶ c == 0x2002 || // En Space
Branch: 1 branch
BranchTrueFalseTotal
c == 0x2002 || // En Space 578369 0 578369
133 578369 ▶ c == 0x2003 || // Em Space
Branch: 1 branch
BranchTrueFalseTotal
c == 0x2003 || // Em Space 578369 0 578369
134 578369 ▶ c == 0x2004 || // Three-Per-Em Space
Branch: 1 branch
BranchTrueFalseTotal
c == 0x2004 || // Three-Per-Em Space 578369 0 578369
135 578369 ▶ c == 0x2005 || // Four-Per-Em Space
Branch: 1 branch
BranchTrueFalseTotal
c == 0x2005 || // Four-Per-Em Space 578369 0 578369
136 578369 ▶ c == 0x2006 || // Six-Per-Em Space
Branch: 1 branch
BranchTrueFalseTotal
c == 0x2006 || // Six-Per-Em Space 578369 0 578369
137 578369 ▶ c == 0x2007 || // Figure Space
Branch: 1 branch
BranchTrueFalseTotal
c == 0x2007 || // Figure Space 578369 0 578369
138 578369 ▶ c == 0x2008 || // Punctuation Space
Branch: 1 branch
BranchTrueFalseTotal
c == 0x2008 || // Punctuation Space 578369 0 578369
139 578369 ▶ c == 0x2009 || // Thin Space
Branch: 1 branch
BranchTrueFalseTotal
c == 0x2009 || // Thin Space 578369 0 578369
140 578369 ▶ c == 0x200A || // Hair Space
Branch: 1 branch
BranchTrueFalseTotal
c == 0x200A || // Hair Space 578369 0 578369
141 578369 ▶ c == 0x202F || // Narrow No-Break Space
Branch: 1 branch
BranchTrueFalseTotal
c == 0x202F || // Narrow No-Break Space 578369 0 578369
142 578369 ▶ c == 0x205F || // Medium Mathematical Space
Branch: 1 branch
BranchTrueFalseTotal
c == 0x205F || // Medium Mathematical Space 578369 0 578369
143 578369 ▶ c == 0x3000 || // Ideographic Space
Branch: 1 branch
BranchTrueFalseTotal
c == 0x3000 || // Ideographic Space 13376 564993 578369
144  
145 1785119 ▶ (charset == KDL_CHARACTER_SET_V1 && c == 0xFEFF) || // Byte-order mark
Branch: 3 branches
BranchTrueFalseTotal
(charset == KDL_CHARACTER_SET_V1 && c == 0xFEFF) || // Byte-order mark 610063 0 610063
(charset == KDL_CHARACTER_SET_V1 && c == 0xFEFF) || // Byte-order mark 13376 0 13376
(charset == KDL_CHARACTER_SET_V1 && c == 0xFEFF) || // Byte-order mark 564993 13376 578369
146 564993 ▶ (charset == KDL_CHARACTER_SET_V2 && c == 0x000B) // Vertical Tab
Branch: 1 branch
BranchTrueFalseTotal
(charset == KDL_CHARACTER_SET_V2 && c == 0x000B) // Vertical Tab 0 564993 564993
147 ;
148 }
149  
150 563058 bool _kdl_is_newline(uint32_t c)
151 {
152 563058 ▶ return c == 0x000D || // CR Carriage Return
Branch: 1 branch
BranchTrueFalseTotal
return c == 0x000D || // CR Carriage Return 552157 10901 563058
153 552157 ▶ c == 0x000A || // LF Line Feed
Branch: 1 branch
BranchTrueFalseTotal
c == 0x000A || // LF Line Feed 552157 0 552157
154 552157 ▶ c == 0x0085 || // NEL Next Line
Branch: 1 branch
BranchTrueFalseTotal
c == 0x0085 || // NEL Next Line 552157 0 552157
155 552157 ▶ c == 0x000C || // FF Form Feed
Branch: 1 branch
BranchTrueFalseTotal
c == 0x000C || // FF Form Feed 552157 0 552157
156 1126116 ▶ c == 0x2028 || // LS Line Separator
Branch: 2 branches
BranchTrueFalseTotal
c == 0x2028 || // LS Line Separator 563058 0 563058
c == 0x2028 || // LS Line Separator 0 552157 552157
157 c == 0x2029; // PS Paragraph Separator
158 }
159  
160 27294 bool _kdl_is_id(kdl_character_set charset, uint32_t c)
161 {
162 27294 ▶ return _kdl_is_word_char(charset, c) && !(charset == KDL_CHARACTER_SET_V2 && c == '#');
Branch: 3 branches
BranchTrueFalseTotal
return _kdl_is_word_char(charset, c) && !(charset == KDL_CHARACTER_SET_V2 && c == '#'); 27291 3 27294
return _kdl_is_word_char(charset, c) && !(charset == KDL_CHARACTER_SET_V2 && c == '#'); 14216 13075 27291
return _kdl_is_word_char(charset, c) && !(charset == KDL_CHARACTER_SET_V2 && c == '#'); 14216 0 14216
163 }
164  
165 11876 bool _kdl_is_id_start(kdl_character_set charset, uint32_t c)
166 {
167 11876 ▶ return _kdl_is_word_start(charset, c) && !(charset == KDL_CHARACTER_SET_V2 && c == '#');
Branch: 3 branches
BranchTrueFalseTotal
return _kdl_is_word_start(charset, c) && !(charset == KDL_CHARACTER_SET_V2 && c == '#'); 11867 9 11876
return _kdl_is_word_start(charset, c) && !(charset == KDL_CHARACTER_SET_V2 && c == '#'); 6086 5781 11867
return _kdl_is_word_start(charset, c) && !(charset == KDL_CHARACTER_SET_V2 && c == '#'); 6086 0 6086
168 }
169  
170 286424 bool _kdl_is_word_char(kdl_character_set charset, uint32_t c)
171 {
172 286421 ▶ return c > 0x20 && c <= 0x10FFFF && c != '\\' && c != '/' && c != '(' && c != ')' && c != '{' && c != '}'
Branch: 7 branches
BranchTrueFalseTotal
return c > 0x20 && c <= 0x10FFFF && c != '\\' && c != '/' && c != '(' && c != ')' && c != '{' && c != '}' 286421 0 286421
return c > 0x20 && c <= 0x10FFFF && c != '\\' && c != '/' && c != '(' && c != ')' && c != '{' && c != '}' 286421 0 286421
return c > 0x20 && c <= 0x10FFFF && c != '\\' && c != '/' && c != '(' && c != ')' && c != '{' && c != '}' 286421 0 286421
return c > 0x20 && c <= 0x10FFFF && c != '\\' && c != '/' && c != '(' && c != ')' && c != '{' && c != '}' 286421 0 286421
return c > 0x20 && c <= 0x10FFFF && c != '\\' && c != '/' && c != '(' && c != ')' && c != '{' && c != '}' 286421 0 286421
return c > 0x20 && c <= 0x10FFFF && c != '\\' && c != '/' && c != '(' && c != ')' && c != '{' && c != '}' 286421 0 286421
return c > 0x20 && c <= 0x10FFFF && c != '\\' && c != '/' && c != '(' && c != ')' && c != '{' && c != '}' 286421 0 286421
173 286421 ▶ && c != ';' && c != '[' && c != ']' && c != '"' && c != '='
Branch: 5 branches
BranchTrueFalseTotal
&& c != ';' && c != '[' && c != ']' && c != '"' && c != '=' 286421 0 286421
&& c != ';' && c != '[' && c != ']' && c != '"' && c != '=' 286421 0 286421
&& c != ';' && c != '[' && c != ']' && c != '"' && c != '=' 286421 0 286421
&& c != ';' && c != '[' && c != ']' && c != '"' && c != '=' 286421 0 286421
&& c != ';' && c != '[' && c != ']' && c != '"' && c != '=' 286421 0 286421
174 286421 ▶ && !(charset == KDL_CHARACTER_SET_V1 && (c == '<' || c == '>' || c == ','))
Branch: 4 branches
BranchTrueFalseTotal
&& !(charset == KDL_CHARACTER_SET_V1 && (c == '<' || c == '>' || c == ',')) 13139 273282 286421
&& !(charset == KDL_CHARACTER_SET_V1 && (c == '<' || c == '>' || c == ',')) 13139 0 13139
&& !(charset == KDL_CHARACTER_SET_V1 && (c == '<' || c == '>' || c == ',')) 13139 0 13139
&& !(charset == KDL_CHARACTER_SET_V1 && (c == '<' || c == '>' || c == ',')) 13139 0 13139
175 572845 ▶ && !_kdl_is_whitespace(charset, c) && !_kdl_is_newline(c) && !_kdl_is_illegal_char(charset, c);
Branch: 4 branches
BranchTrueFalseTotal
&& !_kdl_is_whitespace(charset, c) && !_kdl_is_newline(c) && !_kdl_is_illegal_char(charset, c); 286421 3 286424
&& !_kdl_is_whitespace(charset, c) && !_kdl_is_newline(c) && !_kdl_is_illegal_char(charset, c); 286421 0 286421
&& !_kdl_is_whitespace(charset, c) && !_kdl_is_newline(c) && !_kdl_is_illegal_char(charset, c); 286421 0 286421
&& !_kdl_is_whitespace(charset, c) && !_kdl_is_newline(c) && !_kdl_is_illegal_char(charset, c); 286421 0 286421
176 }
177  
178 11876 bool _kdl_is_word_start(kdl_character_set charset, uint32_t c)
179 {
180 11876 ▶ return _kdl_is_id(charset, c) && (c < '0' || c > '9');
Branch: 3 branches
BranchTrueFalseTotal
return _kdl_is_id(charset, c) && (c < '0' || c > '9'); 11875 1 11876
return _kdl_is_id(charset, c) && (c < '0' || c > '9'); 11773 102 11875
return _kdl_is_id(charset, c) && (c < '0' || c > '9'); 11765 8 11773
181 }
182  
183 259130 bool _kdl_is_end_of_word(kdl_character_set charset, uint32_t c)
184 {
185 // is this character something that could terminate an identifier (or number) in some situation?
186 502514 ▶ return _kdl_is_whitespace(charset, c) || _kdl_is_newline(c) //
Branch: 1 branch
BranchTrueFalseTotal
return _kdl_is_whitespace(charset, c) || _kdl_is_newline(c) // 240754 2630 243384
187 502514 ▶ || c == ';' || c == ')' || c == '}' || c == '/' || c == '\\' || c == '=';
Branch: 7 branches
BranchTrueFalseTotal
|| c == ';' || c == ')' || c == '}' || c == '/' || c == '\\' || c == '='; 243384 15746 259130
|| c == ';' || c == ')' || c == '}' || c == '/' || c == '\\' || c == '='; 240754 0 240754
|| c == ';' || c == ')' || c == '}' || c == '/' || c == '\\' || c == '='; 240745 9 240754
|| c == ';' || c == ')' || c == '}' || c == '/' || c == '\\' || c == '='; 240745 0 240745
|| c == ';' || c == ')' || c == '}' || c == '/' || c == '\\' || c == '='; 240745 0 240745
|| c == ';' || c == ')' || c == '}' || c == '/' || c == '\\' || c == '='; 240745 0 240745
|| c == ';' || c == ')' || c == '}' || c == '/' || c == '\\' || c == '='; 20 240725 240745
188 }
189  
190 337423 bool _kdl_is_illegal_char(kdl_character_set charset, uint32_t c)
191 {
192 return charset == KDL_CHARACTER_SET_V2
193 661295 ▶ && (c <= 0x0008 || // control characters
Branch: 3 branches
BranchTrueFalseTotal
&& (c <= 0x0008 || // control characters 323872 13551 337423
&& (c <= 0x0008 || // control characters 323872 0 323872
&& (c <= 0x0008 || // control characters 315615 8257 323872
194 323872 ▶ (0x000E <= c && c <= 0x001F) || // ''
Branch: 2 branches
BranchTrueFalseTotal
(0x000E <= c && c <= 0x001F) || // '' 315615 0 315615
(0x000E <= c && c <= 0x001F) || // '' 323872 0 323872
195 323872 ▶ c == 0x007F || // delete
Branch: 1 branch
BranchTrueFalseTotal
c == 0x007F || // delete 11 323861 323872
196 323872 ▶ (0xD800 <= c && c <= 0xDFFF) || // UTF-16 surrogates
Branch: 2 branches
BranchTrueFalseTotal
(0xD800 <= c && c <= 0xDFFF) || // UTF-16 surrogates 11 0 11
(0xD800 <= c && c <= 0xDFFF) || // UTF-16 surrogates 323872 0 323872
197 323872 ▶ c == 0x200E || c == 0x200F || // directional control characters
Branch: 2 branches
BranchTrueFalseTotal
c == 0x200E || c == 0x200F || // directional control characters 323872 0 323872
c == 0x200E || c == 0x200F || // directional control characters 51 323821 323872
198 323872 ▶ (0x202A <= c && c <= 0x202E) || // ''
Branch: 2 branches
BranchTrueFalseTotal
(0x202A <= c && c <= 0x202E) || // '' 51 0 51
(0x202A <= c && c <= 0x202E) || // '' 51 323821 323872
199 323872 ▶ (0x2066 <= c && c <= 0x2069) || // ''
Branch: 2 branches
BranchTrueFalseTotal
(0x2066 <= c && c <= 0x2069) || // '' 51 0 51
(0x2066 <= c && c <= 0x2069) || // '' 323872 0 323872
200 323872 ▶ c == 0xFEFF || // ZWNBSP = BOM
Branch: 1 branch
BranchTrueFalseTotal
c == 0xFEFF || // ZWNBSP = BOM 0 323872 323872
201 c > 0x10FFFF // not a codepoint
202 );
203 }
204  
205 325731 static inline kdl_utf8_status _tok_get_char(
206 kdl_tokenizer* self, char const** cur, char const** next, uint32_t* codepoint)
207 {
208 147 while (true) {
209 ptrdiff_t offset;
210 325878 kdl_str s = {*cur, self->document.len - (*cur - self->document.data)};
211 325878 kdl_utf8_status status = _kdl_pop_codepoint(&s, codepoint);
212 325878 switch (status) {
213 325577 case KDL_UTF8_OK:
214 325577 *next = s.data;
215 325731 return KDL_UTF8_OK;
216 301 case KDL_UTF8_EOF:
217 case KDL_UTF8_INCOMPLETE:
218 301 offset = *cur - self->document.data;
219 301 size_t bytes_read = _refill_tokenizer(self);
220 // make sure *cur is still valid despite possible realloc call
221 301 *cur = self->document.data + offset;
222 301 ▶ if (bytes_read == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (bytes_read == 0) { 154 147 301
223 // actual EOF
224 154 return status;
225 } else {
226 // more data, try again
227 147 break; // out of switch
228 }
229 0 default: // error
230 0 return status;
231 }
232 }
233 }
234  
235 48545 static inline void _update_doc_ptr(kdl_tokenizer* self, char const* new_ptr)
236 {
237 48545 self->document.len -= (new_ptr - self->document.data);
238 48545 self->document.data = new_ptr;
239 48545 }
240  
241 static kdl_tokenizer_status _pop_word(kdl_tokenizer* self, kdl_token* dest);
242 static kdl_tokenizer_status _pop_comment(kdl_tokenizer* self, kdl_token* dest);
243 static kdl_tokenizer_status _pop_string(kdl_tokenizer* self, kdl_token* dest);
244  
245 48695 kdl_tokenizer_status kdl_pop_token(kdl_tokenizer* self, kdl_token* dest)
246 {
247 48695 uint32_t c = 0;
248 48695 char const* cur = self->document.data;
249 48695 char const* next = NULL;
250  
251 while (true) {
252 48695 switch (_tok_get_char(self, &cur, &next, &c)) {
253 48561 case KDL_UTF8_OK:
254 48561 break;
255 134 case KDL_UTF8_EOF:
256 134 return KDL_TOKENIZER_EOF;
257 0 default: // error
258 0 return KDL_TOKENIZER_ERROR;
259 }
260  
261 // Could be the start of a new token
262 48561 ▶ if (_kdl_is_illegal_char(self->charset, c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_kdl_is_illegal_char(self->charset, c)) { 0 48561 48561
263 0 return KDL_TOKENIZER_ERROR;
264 48561 ▶ } else if (_kdl_is_whitespace(self->charset, c)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (_kdl_is_whitespace(self->charset, c)) { 15852 32709 48561
265 // find whitespace run
266 15852 size_t ws_start_offset = cur - self->document.data;
267 15852 cur = next;
268 15852 while (
269 15948 ▶ _tok_get_char(self, &cur, &next, &c) == KDL_UTF8_OK && _kdl_is_whitespace(self->charset, c)) {
Branch: 2 branches
BranchTrueFalseTotal
_tok_get_char(self, &cur, &next, &c) == KDL_UTF8_OK && _kdl_is_whitespace(self->charset, c)) { 15948 0 15948
_tok_get_char(self, &cur, &next, &c) == KDL_UTF8_OK && _kdl_is_whitespace(self->charset, c)) { 96 15852 15948
270 // accept whitespace character
271 96 cur = next;
272 }
273 // cur is now the first non-whitespace character.
274 15852 dest->type = KDL_TOKEN_WHITESPACE;
275 15852 dest->value.data = self->document.data + ws_start_offset;
276 15852 dest->value.len = cur - dest->value.data;
277 15852 _update_doc_ptr(self, cur);
278 15852 return KDL_TOKENIZER_OK;
279 32709 ▶ } else if (_kdl_is_newline(c)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (_kdl_is_newline(c)) { 8268 24441 32709
280 // end of line
281 // special treatment for CRLF
282 8268 ▶ if (c == '\r') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '\r') { 0 8268 8268
283 char const* nnext;
284 uint32_t c2;
285 0 ▶ bool is_crlf = _tok_get_char(self, &next, &nnext, &c2) == KDL_UTF8_OK && c2 == '\n';
Branch: 2 branches
BranchTrueFalseTotal
bool is_crlf = _tok_get_char(self, &next, &nnext, &c2) == KDL_UTF8_OK && c2 == '\n'; 0 0 0
bool is_crlf = _tok_get_char(self, &next, &nnext, &c2) == KDL_UTF8_OK && c2 == '\n'; 0 0 0
286 // cur has been invalidated by _tok_get_char
287 // but we know '\r' takes 1 byte
288 0 cur = next - 1;
289 0 ▶ if (is_crlf) next = nnext;
Branch: 1 branch
BranchTrueFalseTotal
if (is_crlf) next = nnext; 0 0 0
290 }
291 8268 dest->type = KDL_TOKEN_NEWLINE;
292 8268 dest->value.data = cur;
293 8268 dest->value.len = (size_t)(next - cur);
294 8268 _update_doc_ptr(self, next);
295 8268 return KDL_TOKENIZER_OK;
296 24441 ▶ } else if (c == ';') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c == ';') { 0 24441 24441
297 // semicolon (end of node)
298 0 dest->type = KDL_TOKEN_SEMICOLON;
299 0 dest->value.data = self->document.data;
300 0 dest->value.len = (size_t)(cur - self->document.data);
301 0 _update_doc_ptr(self, next);
302 0 return KDL_TOKENIZER_OK;
303 24441 ▶ } else if (c == '\\') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c == '\\') { 0 24441 24441
304 // line continuation
305 0 dest->type = KDL_TOKEN_LINE_CONTINUATION;
306 0 dest->value.data = cur;
307 0 dest->value.len = (size_t)(next - cur);
308 0 _update_doc_ptr(self, next);
309 0 return KDL_TOKENIZER_OK;
310 24441 ▶ } else if (c == '(') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c == '(') { 69 24372 24441
311 // start of type annotation
312 69 dest->type = KDL_TOKEN_START_TYPE;
313 69 dest->value.data = cur;
314 69 dest->value.len = (size_t)(next - cur);
315 69 _update_doc_ptr(self, next);
316 69 return KDL_TOKENIZER_OK;
317 24372 ▶ } else if (c == ')') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c == ')') { 70 24302 24372
318 // end of type annotation
319 70 dest->type = KDL_TOKEN_END_TYPE;
320 70 dest->value.data = cur;
321 70 dest->value.len = (size_t)(next - cur);
322 70 _update_doc_ptr(self, next);
323 70 return KDL_TOKENIZER_OK;
324 24302 ▶ } else if (c == '{') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c == '{') { 3051 21251 24302
325 // start of list of children
326 3051 dest->type = KDL_TOKEN_START_CHILDREN;
327 3051 dest->value.data = cur;
328 3051 dest->value.len = (size_t)(next - cur);
329 3051 _update_doc_ptr(self, next);
330 3051 return KDL_TOKENIZER_OK;
331 21251 ▶ } else if (c == '}') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c == '}') { 2529 18722 21251
332 // end of list of children
333 2529 dest->type = KDL_TOKEN_END_CHILDREN;
334 2529 dest->value.data = cur;
335 2529 dest->value.len = (size_t)(next - cur);
336 2529 _update_doc_ptr(self, next);
337 2529 return KDL_TOKENIZER_OK;
338 18722 ▶ } else if (c == '=') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c == '=') { 51 18671 18722
339 // attribute assignment
340 51 dest->type = KDL_TOKEN_EQUALS;
341 51 dest->value.data = cur;
342 51 dest->value.len = (size_t)(next - cur);
343 51 _update_doc_ptr(self, next);
344 51 return KDL_TOKENIZER_OK;
345 18671 ▶ } else if (c == '/') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c == '/') { 26 18645 18671
346 // could be a comment
347 26 return _pop_comment(self, dest);
348 18645 ▶ } else if (c == '"') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c == '"') { 240 18405 18645
349 // string
350 240 return _pop_string(self, dest);
351 18405 ▶ } else if (_kdl_is_word_char(self->charset, c)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (_kdl_is_word_char(self->charset, c)) { 18405 0 18405
352 18405 ▶ if (c == 'r' || c == '#') {
Branch: 2 branches
BranchTrueFalseTotal
if (c == 'r' || c == '#') { 18405 0 18405
if (c == 'r' || c == '#') { 10 18395 18405
353 // this *could* be a raw string
354 10 kdl_tokenizer_status rstring_status = _pop_string(self, dest);
355 10 ▶ if (rstring_status == KDL_TOKENIZER_OK) return KDL_TOKENIZER_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (rstring_status == KDL_TOKENIZER_OK) return KDL_TOKENIZER_OK; 0 10 10
356 // else: parse this as an identifier instead (which may also fail)
357 }
358 // start of an identfier, number, bool, or null
359 18405 return _pop_word(self, dest);
360 } else {
361 0 return KDL_TOKENIZER_ERROR;
362 }
363 }
364 }
365  
366 18405 static kdl_tokenizer_status _pop_word(kdl_tokenizer* self, kdl_token* dest)
367 {
368 18405 uint32_t c = 0;
369 18405 char const* cur = self->document.data;
370 18405 char const* next = NULL;
371  
372 while (true) {
373 259130 switch (_tok_get_char(self, &cur, &next, &c)) {
374 259130 case KDL_UTF8_OK:
375 259130 break;
376 0 case KDL_UTF8_EOF:
377 0 goto end_of_word;
378 0 default: // error
379 0 return KDL_TOKENIZER_ERROR;
380 }
381  
382 259130 ▶ if (_kdl_is_end_of_word(self->charset, c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_kdl_is_end_of_word(self->charset, c)) { 18405 240725 259130
383 // end the word
384 18405 goto end_of_word;
385 240725 ▶ } else if (!_kdl_is_word_char(self->charset, c)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!_kdl_is_word_char(self->charset, c)) { 0 240725 240725
386 // invalid character
387 0 return KDL_TOKENIZER_ERROR;
388 }
389 // Accept this character
390 240725 cur = next;
391 }
392 18405 end_of_word:
393 18405 dest->value = self->document;
394 18405 dest->value.len = (size_t)(cur - dest->value.data);
395 18405 _update_doc_ptr(self, cur);
396 18405 dest->type = KDL_TOKEN_WORD;
397 18405 return KDL_TOKENIZER_OK;
398 }
399  
400 26 static kdl_tokenizer_status _pop_comment(kdl_tokenizer* self, kdl_token* dest)
401 {
402 26 uint32_t c1 = 0;
403 26 uint32_t c2 = 0;
404 26 char const* cur = self->document.data;
405 26 char const* next = NULL;
406 26 ▶ if (_tok_get_char(self, &cur, &next, &c1) != KDL_UTF8_OK) return KDL_TOKENIZER_ERROR;
Branch: 1 branch
BranchTrueFalseTotal
if (_tok_get_char(self, &cur, &next, &c1) != KDL_UTF8_OK) return KDL_TOKENIZER_ERROR; 0 26 26
407 26 cur = next;
408 26 ▶ if (_tok_get_char(self, &cur, &next, &c2) != KDL_UTF8_OK) return KDL_TOKENIZER_ERROR;
Branch: 1 branch
BranchTrueFalseTotal
if (_tok_get_char(self, &cur, &next, &c2) != KDL_UTF8_OK) return KDL_TOKENIZER_ERROR; 0 26 26
409 26 ▶ if (c1 != '/') return KDL_TOKENIZER_ERROR;
Branch: 1 branch
BranchTrueFalseTotal
if (c1 != '/') return KDL_TOKENIZER_ERROR; 0 26 26
410  
411 26 ▶ if (c2 == '-') {
Branch: 1 branch
BranchTrueFalseTotal
if (c2 == '-') { 20 6 26
412 // slashdash /-
413 20 dest->value.data = self->document.data;
414 20 dest->value.len = (size_t)2;
415 20 dest->type = KDL_TOKEN_SLASHDASH;
416 20 _update_doc_ptr(self, next);
417 20 return KDL_TOKENIZER_OK;
418 6 ▶ } else if (c2 == '/') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c2 == '/') { 3 3 6
419 // C++ style comment
420 // scan until a newline or EOF
421 3 uint32_t c = 0;
422 3 cur = next;
423 while (true) {
424 22 switch (_tok_get_char(self, &cur, &next, &c)) {
425 22 case KDL_UTF8_OK:
426 22 break;
427 0 case KDL_UTF8_EOF:
428 0 goto end_of_line;
429 0 default: // error
430 0 return KDL_TOKENIZER_ERROR;
431 }
432 22 ▶ if (_kdl_is_illegal_char(self->charset, c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_kdl_is_illegal_char(self->charset, c)) { 0 22 22
433 0 return KDL_TOKENIZER_ERROR;
434 22 ▶ } else if (_kdl_is_newline(c)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (_kdl_is_newline(c)) { 3 19 22
435 3 goto end_of_line;
436 }
437 // Accept this character
438 19 cur = next;
439 }
440 3 end_of_line:
441 3 dest->value = self->document;
442 3 dest->value.len = (size_t)(cur - dest->value.data);
443 3 _update_doc_ptr(self, cur);
444 3 dest->type = KDL_TOKEN_SINGLE_LINE_COMMENT;
445 3 return KDL_TOKENIZER_OK;
446 3 ▶ } else if (c2 == '*') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c2 == '*') { 3 0 3
447 // C style multi line comment (with nesting)
448 3 int depth = 1;
449 3 uint32_t c = 0;
450 3 uint32_t prev_char = 0;
451 3 cur = next;
452 30 ▶ while (depth > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (depth > 0) { 27 3 30
453 27 ▶ switch (_tok_get_char(self, &cur, &next, &c)) {
Branch: 1 branch
BranchTrueFalseTotal
switch (_tok_get_char(self, &cur, &next, &c)) { 27 0 27
454 27 case KDL_UTF8_OK:
455 27 break;
456 0 case KDL_UTF8_EOF: // EOF in a comment is an error
457 default: // error
458 0 return KDL_TOKENIZER_ERROR;
459 }
460  
461 27 ▶ if (_kdl_is_illegal_char(self->charset, c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_kdl_is_illegal_char(self->charset, c)) { 0 27 27
462 0 return KDL_TOKENIZER_ERROR;
463 27 ▶ } else if (c == '*' && prev_char == '/') {
Branch: 2 branches
BranchTrueFalseTotal
} else if (c == '*' && prev_char == '/') { 3 24 27
} else if (c == '*' && prev_char == '/') { 0 3 3
464 // another level of nesting
465 0 ++depth;
466 0 c = 0; // "/*/" doesn't count as self-closing
467 27 ▶ } else if (c == '/' && prev_char == '*') {
Branch: 2 branches
BranchTrueFalseTotal
} else if (c == '/' && prev_char == '*') { 3 24 27
} else if (c == '/' && prev_char == '*') { 3 0 3
468 3 --depth;
469 3 c = 0; // "*/*" doesn't count as reopening
470 }
471  
472 // Accept this character
473 27 cur = next;
474 27 prev_char = c;
475 }
476 // end of comment reached
477 3 dest->value = self->document;
478 3 dest->value.len = (size_t)(cur - dest->value.data);
479 3 _update_doc_ptr(self, cur);
480 3 dest->type = KDL_TOKEN_MULTI_LINE_COMMENT;
481 3 return KDL_TOKENIZER_OK;
482 } else {
483 0 return KDL_TOKENIZER_ERROR;
484 }
485 }
486  
487 250 static kdl_tokenizer_status _pop_string(kdl_tokenizer* self, kdl_token* dest)
488 {
489 250 uint32_t c = 0;
490 250 char const* cur = self->document.data;
491 250 char const* next = NULL;
492 250 bool is_raw = false;
493 250 bool is_v1 = false;
494  
495 250 ▶ if (_tok_get_char(self, &cur, &next, &c) != KDL_UTF8_OK) return KDL_TOKENIZER_ERROR;
Branch: 1 branch
BranchTrueFalseTotal
if (_tok_get_char(self, &cur, &next, &c) != KDL_UTF8_OK) return KDL_TOKENIZER_ERROR; 0 250 250
496 250 switch (c) {
497 0 case 'r':
498 0 is_raw = true;
499 0 is_v1 = true;
500 0 cur = next;
501 0 break;
502 10 case '#':
503 10 is_raw = true;
504 10 break;
505 240 case '"':
506 240 break;
507 0 default:
508 0 return KDL_TOKENIZER_ERROR;
509 }
510  
511 // Count the hashes
512 250 int hashes = 0;
513 260 ▶ while (is_raw) {
Branch: 1 branch
BranchTrueFalseTotal
while (is_raw) { 20 240 260
514 20 ▶ switch (_tok_get_char(self, &cur, &next, &c)) {
Branch: 1 branch
BranchTrueFalseTotal
switch (_tok_get_char(self, &cur, &next, &c)) { 20 0 20
515 20 case KDL_UTF8_OK:
516 20 break;
517 0 case KDL_UTF8_EOF: // eof in a string is an error
518 default: // error
519 0 return KDL_TOKENIZER_ERROR;
520 }
521  
522 20 ▶ if (c == '#') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '#') { 10 10 20
523 10 ++hashes;
524 10 cur = next;
525 10 ▶ } else if (c == '"') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c == '"') { 0 10 10
526 0 break;
527 } else {
528 10 return KDL_TOKENIZER_ERROR;
529 }
530 }
531  
532 // Count the quotes
533 240 int initial_quote_count = 0;
534 240 ▶ int max_quote_count = is_v1 ? 1 : 3;
Branch: 1 branch
BranchTrueFalseTotal
int max_quote_count = is_v1 ? 1 : 3; 0 240 240
535 480 ▶ while (initial_quote_count < max_quote_count) {
Branch: 1 branch
BranchTrueFalseTotal
while (initial_quote_count < max_quote_count) { 480 0 480
536 480 switch (_tok_get_char(self, &cur, &next, &c)) {
537 480 case KDL_UTF8_OK:
538 480 break;
539 0 case KDL_UTF8_EOF:
540 // is this an empty string?
541 0 ▶ if (!is_raw && initial_quote_count == 2) {
Branch: 2 branches
BranchTrueFalseTotal
if (!is_raw && initial_quote_count == 2) { 0 0 0
if (!is_raw && initial_quote_count == 2) { 0 0 0
542 0 dest->value.data = self->document.data;
543 0 dest->value.len = 0;
544 0 _update_doc_ptr(self, cur);
545 0 dest->type = KDL_TOKEN_STRING;
546 0 return KDL_TOKENIZER_OK;
547 }
548 _fallthrough_;
549 default: // error
550 0 return KDL_TOKENIZER_ERROR;
551 }
552  
553 480 ▶ if (c == '"') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '"') { 240 240 480
554 240 ++initial_quote_count;
555 240 cur = next;
556 } else {
557 240 break;
558 }
559 }
560  
561 240 ▶ if (initial_quote_count == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (initial_quote_count == 2) { 0 240 240
562 // this is an empty string and we overshot - backtrack by one char
563 0 --cur;
564 0 --initial_quote_count;
565 }
566  
567 // cur is now the first character after the quote(s)
568 240 size_t string_start_offset = cur - self->document.data;
569  
570 // Scan the string itself
571 240 int hashes_found = 0;
572 240 int quotes_found = 0;
573 240 size_t end_quote_offset = 0;
574 240 uint32_t prev_char = 0;
575  
576 while (true) {
577 941 ▶ switch (_tok_get_char(self, &cur, &next, &c)) {
Branch: 1 branch
BranchTrueFalseTotal
switch (_tok_get_char(self, &cur, &next, &c)) { 925 16 941
578 925 case KDL_UTF8_OK:
579 925 break;
580 16 case KDL_UTF8_EOF: // eof in a string is an error
581 default: // error
582 16 return KDL_TOKENIZER_ERROR;
583 }
584  
585 925 ▶ if (_kdl_is_illegal_char(self->charset, c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_kdl_is_illegal_char(self->charset, c)) { 0 925 925
586 0 return KDL_TOKENIZER_ERROR;
587 925 ▶ } else if (!is_raw && c == '\\' && prev_char == '\\') {
Branch: 3 branches
BranchTrueFalseTotal
} else if (!is_raw && c == '\\' && prev_char == '\\') { 925 0 925
} else if (!is_raw && c == '\\' && prev_char == '\\') { 4 921 925
} else if (!is_raw && c == '\\' && prev_char == '\\') { 0 4 4
588 0 c = 0; // double backslash is no backslash
589 0 quotes_found = hashes_found = 0;
590 0 end_quote_offset = 0;
591 925 ▶ } else if (c == '"' && (is_raw || prev_char != '\\')) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (c == '"' && (is_raw || prev_char != '\\')) { 224 701 925
} else if (c == '"' && (is_raw || prev_char != '\\')) { 224 0 224
} else if (c == '"' && (is_raw || prev_char != '\\')) { 224 0 224
592 224 ▶ if (quotes_found == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (quotes_found == 0) { 224 0 224
593 224 end_quote_offset = cur - self->document.data;
594 }
595 224 ▶ if (quotes_found < initial_quote_count) {
Branch: 1 branch
BranchTrueFalseTotal
if (quotes_found < initial_quote_count) { 224 0 224
596 224 ++quotes_found;
597 } else {
598 // possibly extra quotes at the end of a raw string.
599 0 ++end_quote_offset;
600 }
601 224 hashes_found = 0;
602 701 ▶ } else if (c == '#' && (hashes_found != 0 || quotes_found == initial_quote_count)) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (c == '#' && (hashes_found != 0 || quotes_found == initial_quote_count)) { 0 701 701
} else if (c == '#' && (hashes_found != 0 || quotes_found == initial_quote_count)) { 0 0 0
} else if (c == '#' && (hashes_found != 0 || quotes_found == initial_quote_count)) { 0 0 0
603 0 ++hashes_found;
604 0 quotes_found = 0;
605 } else {
606 // this is neither a quote nor a hash
607 701 quotes_found = hashes_found = 0;
608 701 end_quote_offset = 0;
609 }
610  
611 925 ▶ if (end_quote_offset != 0
Branch: 1 branch
BranchTrueFalseTotal
if (end_quote_offset != 0 224 701 925
612 224 ▶ && ((hashes == 0 && quotes_found == initial_quote_count)
Branch: 2 branches
BranchTrueFalseTotal
&& ((hashes == 0 && quotes_found == initial_quote_count) 224 0 224
&& ((hashes == 0 && quotes_found == initial_quote_count) 0 224 224
613 0 ▶ || (hashes != 0 && hashes_found == hashes))) {
Branch: 2 branches
BranchTrueFalseTotal
|| (hashes != 0 && hashes_found == hashes))) { 0 0 0
|| (hashes != 0 && hashes_found == hashes))) { 0 0 0
614 // end of string!
615 break;
616 } else {
617 // Accept character
618 701 prev_char = c;
619 701 cur = next;
620 }
621 }
622  
623 // end_quote_ptr = the (first) end quote, next = the char after the quote and hashes
624 224 dest->value.data = self->document.data + string_start_offset;
625 224 dest->value.len = end_quote_offset - string_start_offset;
626 224 _update_doc_ptr(self, next);
627 224 ▶ if (initial_quote_count == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (initial_quote_count == 3) { 0 224 224
628 0 ▶ dest->type = is_raw ? KDL_TOKEN_RAW_MULTILINE_STRING : KDL_TOKEN_MULTILINE_STRING;
Branch: 1 branch
BranchTrueFalseTotal
dest->type = is_raw ? KDL_TOKEN_RAW_MULTILINE_STRING : KDL_TOKEN_MULTILINE_STRING; 0 0 0
629 224 ▶ } else if (is_raw) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (is_raw) { 0 224 224
630 0 ▶ dest->type = is_v1 ? KDL_TOKEN_RAW_STRING_V1 : KDL_TOKEN_RAW_STRING_V2;
Branch: 1 branch
BranchTrueFalseTotal
dest->type = is_v1 ? KDL_TOKEN_RAW_STRING_V1 : KDL_TOKEN_RAW_STRING_V2; 0 0 0
631 } else {
632 224 dest->type = KDL_TOKEN_STRING;
633 }
634 224 return KDL_TOKENIZER_OK;
635 }
« ckdl/src/compat.c ↑ Summary ckdl/src/utf8.c »