| 1 |
|
| #include "kdl/parser.h" |
| 2 |
|
| #include "kdl/common.h" |
| 3 |
|
| #include "kdl/tokenizer.h" |
| 4 |
|
| |
| 5 |
|
| #include "bigint.h" |
| 6 |
|
| #include "compat.h" |
| 7 |
|
| #include "grammar.h" |
| 8 |
|
| #include "str.h" |
| 9 |
|
| #include "utf8.h" |
| 10 |
|
| |
| 11 |
|
| #include <math.h> |
| 12 |
|
| #include <stdio.h> |
| 13 |
|
| #include <stdlib.h> |
| 14 |
|
| #include <string.h> |
| 15 |
|
| |
| 16 |
|
| #define _str_equals_literal(k, l) \ |
| 17 |
|
| ((k).len == (sizeof(l "") - 1) && 0 == memcmp(("" l), (k).data, (sizeof(l) - 1))) |
| 18 |
|
| |
| 19 |
|
| #define KDL_PARSE_OPT_VERSION_BITS KDL_DETECT_VERSION |
| 20 |
|
| |
| 21 |
|
| #define _v1_only(self) ((self->opt & KDL_PARSE_OPT_VERSION_BITS) == KDL_READ_VERSION_1) |
| 22 |
|
| #define _v1_allowed(self) ((self->opt & KDL_READ_VERSION_1) == KDL_READ_VERSION_1) |
| 23 |
|
| #define _v2_only(self) ((self->opt & KDL_PARSE_OPT_VERSION_BITS) == KDL_READ_VERSION_2) |
| 24 |
|
| #define _v2_allowed(self) ((self->opt & KDL_READ_VERSION_2) == KDL_READ_VERSION_2) |
| 25 |
|
| |
| 26 |
|
| enum _kdl_parser_state { |
| 27 |
|
| // Basic states |
| 28 |
|
| PARSER_OUTSIDE_NODE, |
| 29 |
|
| PARSER_IN_NODE, |
| 30 |
|
| // Flags modifying the state |
| 31 |
|
| PARSER_FLAG_LINE_CONT = 0x100, |
| 32 |
|
| PARSER_FLAG_TYPE_ANNOTATION_START = 0x200, |
| 33 |
|
| PARSER_FLAG_TYPE_ANNOTATION_END = 0x400, |
| 34 |
|
| PARSER_FLAG_TYPE_ANNOTATION_ENDED = 0x800, |
| 35 |
|
| PARSER_FLAG_IN_PROPERTY = 0x1000, |
| 36 |
|
| PARSER_FLAG_MAYBE_IN_PROPERTY = 0x2000, |
| 37 |
|
| PARSER_FLAG_BARE_PROPERTY_NAME = 0x4000, |
| 38 |
|
| PARSER_FLAG_NEWLINES_ARE_WHITESPACE = 0x8000, |
| 39 |
|
| PARSER_FLAG_END_OF_NODE = 0x10000, |
| 40 |
|
| PARSER_FLAG_END_OF_NODE_OR_CHILD_BLOCK = 0x20000, |
| 41 |
|
| PARSER_FLAG_WHITESPACE_REQUIRED = 0x40000, |
| 42 |
|
| PARSER_FLAG_CONTEXTUALLY_ILLEGAL_WHITESPACE = 0x100000, |
| 43 |
|
| // Bitmask for testing the state |
| 44 |
|
| PARSER_MASK_WHITESPACE_CONTEXTUALLY_BANNED = // |
| 45 |
|
| PARSER_FLAG_MAYBE_IN_PROPERTY, |
| 46 |
|
| PARSER_MASK_WHITESPACE_BANNED_V1 = // |
| 47 |
|
| PARSER_FLAG_TYPE_ANNOTATION_START // |
| 48 |
|
| | PARSER_FLAG_TYPE_ANNOTATION_END // |
| 49 |
|
| | PARSER_FLAG_TYPE_ANNOTATION_ENDED // |
| 50 |
|
| | PARSER_FLAG_IN_PROPERTY, // |
| 51 |
|
| PARSER_MASK_NODE_CANNOT_END_HERE = // |
| 52 |
|
| PARSER_MASK_WHITESPACE_BANNED_V1 // |
| 53 |
|
| | PARSER_MASK_WHITESPACE_CONTEXTUALLY_BANNED, // |
| 54 |
|
| }; |
| 55 |
|
| |
| 56 |
|
| struct _kdl_parser { |
| 57 |
|
| kdl_tokenizer* tokenizer; |
| 58 |
|
| kdl_parse_option opt; |
| 59 |
|
| int depth; |
| 60 |
|
| int slashdash_depth; |
| 61 |
|
| int child_block_at_depth; |
| 62 |
|
| enum _kdl_parser_state state; |
| 63 |
|
| kdl_event_data event; |
| 64 |
|
| kdl_owned_string tmp_string_type; |
| 65 |
|
| kdl_owned_string tmp_string_key; |
| 66 |
|
| kdl_owned_string tmp_string_value; |
| 67 |
|
| kdl_str waiting_type_annotation; |
| 68 |
|
| kdl_owned_string waiting_prop_name; |
| 69 |
|
| kdl_token next_token; |
| 70 |
|
| bool have_next_token; |
| 71 |
|
| }; |
| 72 |
|
| |
| 73 |
166 |
| static void _init_kdl_parser(kdl_parser* self, kdl_parse_option opt) |
| 74 |
|
| { |
| 75 |
166 |
| self->depth = 0; |
| 76 |
166 |
| self->slashdash_depth = -1; |
| 77 |
166 |
| self->child_block_at_depth = -1; |
| 78 |
166 |
| self->state = PARSER_OUTSIDE_NODE; |
| 79 |
166 |
| self->tmp_string_type = (kdl_owned_string){NULL, 0}; |
| 80 |
166 |
| self->tmp_string_key = (kdl_owned_string){NULL, 0}; |
| 81 |
166 |
| self->tmp_string_value = (kdl_owned_string){NULL, 0}; |
| 82 |
166 |
| self->waiting_type_annotation = (kdl_str){NULL, 0}; |
| 83 |
166 |
| self->waiting_prop_name = (kdl_owned_string){NULL, 0}; |
| 84 |
166 |
| self->have_next_token = false; |
| 85 |
|
| |
| 86 |
|
| // Fallback: use KDLv1 only |
| 87 |
166 |
▶ | if ((opt & KDL_PARSE_OPT_VERSION_BITS) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((opt & KDL_PARSE_OPT_VERSION_BITS) == 0) { |
0 |
166 |
166 |
|
| 88 |
0 |
| opt |= KDL_DETECT_VERSION; |
| 89 |
|
| } |
| 90 |
|
| |
| 91 |
166 |
| self->opt = opt; |
| 92 |
166 |
| } |
| 93 |
|
| |
| 94 |
477 |
| inline static kdl_character_set _default_character_set(kdl_parse_option opt) |
| 95 |
|
| { |
| 96 |
477 |
▶ | if (opt & KDL_READ_VERSION_2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (opt & KDL_READ_VERSION_2) { |
381 |
96 |
477 |
|
| 97 |
381 |
| return KDL_CHARACTER_SET_V2; |
| 98 |
|
| } else { |
| 99 |
96 |
| return KDL_CHARACTER_SET_V1; |
| 100 |
|
| } |
| 101 |
|
| } |
| 102 |
|
| |
| 103 |
128 |
| kdl_parser* kdl_create_string_parser(kdl_str doc, kdl_parse_option opt) |
| 104 |
|
| { |
| 105 |
128 |
| kdl_parser* self = malloc(sizeof(kdl_parser)); |
| 106 |
128 |
▶ | if (self != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self != NULL) { |
128 |
0 |
128 |
|
| 107 |
128 |
| _init_kdl_parser(self, opt); |
| 108 |
128 |
| self->tokenizer = kdl_create_string_tokenizer(doc); |
| 109 |
128 |
| kdl_tokenizer_set_character_set(self->tokenizer, _default_character_set(self->opt)); |
| 110 |
|
| } |
| 111 |
128 |
| return self; |
| 112 |
|
| } |
| 113 |
|
| |
| 114 |
38 |
| kdl_parser* kdl_create_stream_parser(kdl_read_func read_func, void* user_data, kdl_parse_option opt) |
| 115 |
|
| { |
| 116 |
38 |
| kdl_parser* self = malloc(sizeof(kdl_parser)); |
| 117 |
38 |
▶ | if (self != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self != NULL) { |
38 |
0 |
38 |
|
| 118 |
38 |
| _init_kdl_parser(self, opt); |
| 119 |
38 |
| self->tokenizer = kdl_create_stream_tokenizer(read_func, user_data); |
| 120 |
38 |
| kdl_tokenizer_set_character_set(self->tokenizer, _default_character_set(self->opt)); |
| 121 |
|
| } |
| 122 |
38 |
| return self; |
| 123 |
|
| } |
| 124 |
|
| |
| 125 |
166 |
| void kdl_destroy_parser(kdl_parser* self) |
| 126 |
|
| { |
| 127 |
166 |
| kdl_destroy_tokenizer(self->tokenizer); |
| 128 |
166 |
| kdl_free_string(&self->tmp_string_type); |
| 129 |
166 |
| kdl_free_string(&self->tmp_string_key); |
| 130 |
166 |
| kdl_free_string(&self->tmp_string_value); |
| 131 |
166 |
| kdl_free_string(&self->waiting_prop_name); |
| 132 |
166 |
| free(self); |
| 133 |
166 |
| } |
| 134 |
|
| |
| 135 |
311 |
| static void _set_version(kdl_parser* self, kdl_version version) |
| 136 |
|
| { |
| 137 |
311 |
▶ | kdl_parse_option version_flag = version == KDL_VERSION_1 ? KDL_READ_VERSION_1 : KDL_READ_VERSION_2; |
Branch: 1 branch
| Branch | True | False | Total |
| kdl_parse_option version_flag = version == KDL_VERSION_1 ? KDL_READ_VERSION_1 : KDL_READ_VERSION_2; |
78 |
233 |
311 |
|
| 138 |
|
| |
| 139 |
311 |
| self->opt = (self->opt & ~KDL_PARSE_OPT_VERSION_BITS) | version_flag; |
| 140 |
311 |
| kdl_tokenizer_set_character_set(self->tokenizer, _default_character_set(self->opt)); |
| 141 |
311 |
| } |
| 142 |
|
| |
| 143 |
34802 |
| static void _reset_event(kdl_parser* self) |
| 144 |
|
| { |
| 145 |
34802 |
| self->event.name = (kdl_str){NULL, 0}; |
| 146 |
34802 |
| self->event.value.type = KDL_TYPE_NULL; |
| 147 |
34802 |
| self->event.value.type_annotation = (kdl_str){NULL, 0}; |
| 148 |
34802 |
| } |
| 149 |
|
| |
| 150 |
25 |
| static void _set_parse_error(kdl_parser* self, char const* message) |
| 151 |
|
| { |
| 152 |
25 |
| self->event.event = KDL_EVENT_PARSE_ERROR; |
| 153 |
25 |
| self->event.value.type = KDL_TYPE_STRING; |
| 154 |
25 |
| self->event.value.string = (kdl_str){message, strlen(message)}; |
| 155 |
25 |
| } |
| 156 |
|
| |
| 157 |
4 |
| static void _set_comment_event(kdl_parser* self, kdl_token const* token) |
| 158 |
|
| { |
| 159 |
4 |
| self->event.event = KDL_EVENT_COMMENT; |
| 160 |
4 |
| self->event.value.type = KDL_TYPE_STRING; |
| 161 |
4 |
| self->event.value.string = token->value; |
| 162 |
4 |
| } |
| 163 |
|
| |
| 164 |
|
| static kdl_event_data* _next_node(kdl_parser* self, kdl_token* token); |
| 165 |
|
| static kdl_event_data* _next_event_in_node(kdl_parser* self, kdl_token* token); |
| 166 |
|
| static kdl_event_data* _apply_slashdash(kdl_parser* self); |
| 167 |
|
| static bool _parse_value(kdl_parser* self, kdl_token const* token, kdl_value* val, kdl_owned_string* s); |
| 168 |
|
| static bool _parse_number(kdl_str number, kdl_value* val, kdl_owned_string* s); |
| 169 |
|
| static bool _parse_decimal_number(kdl_str number, kdl_value* val, kdl_owned_string* s); |
| 170 |
|
| static bool _parse_decimal_integer(kdl_str number, kdl_value* val, kdl_owned_string* s); |
| 171 |
|
| static bool _parse_decimal_float(kdl_str number, kdl_value* val, kdl_owned_string* s); |
| 172 |
|
| static bool _parse_hex_number(kdl_str number, kdl_value* val, kdl_owned_string* s); |
| 173 |
|
| static bool _parse_octal_number(kdl_str number, kdl_value* val, kdl_owned_string* s); |
| 174 |
|
| static bool _parse_binary_number(kdl_str number, kdl_value* val, kdl_owned_string* s); |
| 175 |
|
| static bool _identifier_is_valid_v1(kdl_str value); |
| 176 |
|
| static bool _identifier_is_valid_v2(kdl_str value); |
| 177 |
|
| |
| 178 |
23861 |
| kdl_event_data* kdl_parser_next_event(kdl_parser* self) |
| 179 |
|
| { |
| 180 |
|
| kdl_token token; |
| 181 |
|
| kdl_event_data* ev; |
| 182 |
|
| |
| 183 |
23861 |
| _reset_event(self); |
| 184 |
|
| |
| 185 |
|
| while (true) { |
| 186 |
|
| |
| 187 |
|
| // get the next token (if available) |
| 188 |
48744 |
▶ | if (self->have_next_token) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->have_next_token) { |
49 |
48695 |
48744 |
|
| 189 |
49 |
| token = self->next_token; |
| 190 |
49 |
| self->have_next_token = false; |
| 191 |
|
| } else { |
| 192 |
48695 |
| switch (kdl_pop_token(self->tokenizer, &token)) { |
| 193 |
134 |
| case KDL_TOKENIZER_EOF: |
| 194 |
134 |
▶ | if ((self->state & 0xff) == PARSER_IN_NODE) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((self->state & 0xff) == PARSER_IN_NODE) { |
1 |
133 |
134 |
|
| 195 |
|
| // EOF may be ok, but we have to close the node first |
| 196 |
1 |
| self->state &= ~PARSER_FLAG_NEWLINES_ARE_WHITESPACE; |
| 197 |
1 |
| token.type = KDL_TOKEN_NEWLINE; |
| 198 |
1 |
| token.value = (kdl_str){NULL, 0}; |
| 199 |
1 |
| break; |
| 200 |
133 |
▶ | } else if (self->depth > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (self->depth > 0) { |
5 |
128 |
133 |
|
| 201 |
5 |
| _set_parse_error(self, "Unexpected end of data (unclosed lists of children)"); |
| 202 |
5 |
| return &self->event; |
| 203 |
128 |
▶ | } else if (self->slashdash_depth > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (self->slashdash_depth > 0) { |
1 |
127 |
128 |
|
| 204 |
1 |
| _set_parse_error(self, "Dangling slashdash (/-)"); |
| 205 |
1 |
| return &self->event; |
| 206 |
127 |
▶ | } else if (self->state != PARSER_OUTSIDE_NODE) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (self->state != PARSER_OUTSIDE_NODE) { |
0 |
127 |
127 |
|
| 207 |
|
| // Some flags are active, this is not allowed |
| 208 |
0 |
| _set_parse_error(self, "Unexpected end of data"); |
| 209 |
0 |
| return &self->event; |
| 210 |
|
| } else { |
| 211 |
|
| // state == PARSER_OUTYSIDE_NODE, at top level => EOF ok |
| 212 |
127 |
| _reset_event(self); |
| 213 |
127 |
| self->event.event = KDL_EVENT_EOF; |
| 214 |
127 |
| return &self->event; |
| 215 |
|
| } |
| 216 |
48545 |
| case KDL_TOKENIZER_OK: |
| 217 |
48545 |
| break; |
| 218 |
16 |
| default: |
| 219 |
|
| case KDL_TOKENIZER_ERROR: |
| 220 |
16 |
| _set_parse_error(self, "Parse error"); |
| 221 |
16 |
| return &self->event; |
| 222 |
|
| } |
| 223 |
|
| } |
| 224 |
|
| |
| 225 |
48595 |
▶ | if (token.type == KDL_TOKEN_NEWLINE && self->state & PARSER_FLAG_NEWLINES_ARE_WHITESPACE) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (token.type == KDL_TOKEN_NEWLINE && self->state & PARSER_FLAG_NEWLINES_ARE_WHITESPACE) { |
8308 |
40287 |
48595 |
| if (token.type == KDL_TOKEN_NEWLINE && self->state & PARSER_FLAG_NEWLINES_ARE_WHITESPACE) { |
1 |
8307 |
8308 |
|
| 226 |
1 |
| token.type = KDL_TOKEN_WHITESPACE; |
| 227 |
|
| } |
| 228 |
|
| |
| 229 |
48595 |
| switch (token.type) { |
| 230 |
15853 |
| case KDL_TOKEN_WHITESPACE: |
| 231 |
15853 |
▶ | if (self->state & PARSER_FLAG_WHITESPACE_REQUIRED) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->state & PARSER_FLAG_WHITESPACE_REQUIRED) { |
15807 |
46 |
15853 |
|
| 232 |
15807 |
| self->state &= ~PARSER_FLAG_WHITESPACE_REQUIRED; |
| 233 |
|
| } |
| 234 |
15853 |
▶ | if (self->state & PARSER_MASK_WHITESPACE_BANNED_V1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->state & PARSER_MASK_WHITESPACE_BANNED_V1) { |
0 |
15853 |
15853 |
|
| 235 |
0 |
▶ | if (_v1_only(self)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (_v1_only(self)) { |
0 |
0 |
0 |
|
| 236 |
0 |
| _set_parse_error(self, "Whitespace not allowed here"); |
| 237 |
0 |
| return &self->event; |
| 238 |
|
| } else { |
| 239 |
0 |
| _set_version(self, KDL_VERSION_2); |
| 240 |
|
| } |
| 241 |
15853 |
▶ | } else if (self->state & PARSER_MASK_WHITESPACE_CONTEXTUALLY_BANNED) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (self->state & PARSER_MASK_WHITESPACE_CONTEXTUALLY_BANNED) { |
9 |
15844 |
15853 |
|
| 242 |
9 |
| self->state |= PARSER_FLAG_CONTEXTUALLY_ILLEGAL_WHITESPACE; |
| 243 |
|
| } |
| 244 |
15853 |
| break; // ignore whitespace |
| 245 |
|
| |
| 246 |
6 |
| case KDL_TOKEN_MULTI_LINE_COMMENT: |
| 247 |
|
| case KDL_TOKEN_SINGLE_LINE_COMMENT: |
| 248 |
6 |
▶ | if (self->state & PARSER_FLAG_WHITESPACE_REQUIRED) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->state & PARSER_FLAG_WHITESPACE_REQUIRED) { |
0 |
6 |
6 |
|
| 249 |
0 |
| self->state &= ~PARSER_FLAG_WHITESPACE_REQUIRED; |
| 250 |
|
| } |
| 251 |
6 |
▶ | if (self->state & PARSER_MASK_WHITESPACE_BANNED_V1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->state & PARSER_MASK_WHITESPACE_BANNED_V1) { |
0 |
6 |
6 |
|
| 252 |
0 |
▶ | if (_v1_only(self)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (_v1_only(self)) { |
0 |
0 |
0 |
|
| 253 |
0 |
| _set_parse_error(self, "Comment not allowed here"); |
| 254 |
0 |
| return &self->event; |
| 255 |
|
| } else { |
| 256 |
0 |
| _set_version(self, KDL_VERSION_2); |
| 257 |
|
| } |
| 258 |
6 |
▶ | } else if (self->state & PARSER_MASK_WHITESPACE_CONTEXTUALLY_BANNED) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (self->state & PARSER_MASK_WHITESPACE_CONTEXTUALLY_BANNED) { |
0 |
6 |
6 |
|
| 259 |
0 |
| self->state |= PARSER_FLAG_CONTEXTUALLY_ILLEGAL_WHITESPACE; |
| 260 |
|
| } |
| 261 |
|
| // Comments may or may not be emitted |
| 262 |
6 |
▶ | if (self->opt & KDL_EMIT_COMMENTS) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->opt & KDL_EMIT_COMMENTS) { |
4 |
2 |
6 |
|
| 263 |
4 |
| _set_comment_event(self, &token); |
| 264 |
4 |
| return &self->event; |
| 265 |
|
| } |
| 266 |
2 |
| break; |
| 267 |
20 |
| case KDL_TOKEN_SLASHDASH: |
| 268 |
20 |
▶ | if (self->state & PARSER_FLAG_WHITESPACE_REQUIRED) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->state & PARSER_FLAG_WHITESPACE_REQUIRED) { |
0 |
20 |
20 |
|
| 269 |
0 |
| _set_parse_error(self, "Whitespace required before /-"); |
| 270 |
0 |
| return &self->event; |
| 271 |
|
| } |
| 272 |
|
| // slashdash comments out the next node or argument or property |
| 273 |
20 |
▶ | if (self->state & PARSER_MASK_WHITESPACE_CONTEXTUALLY_BANNED) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->state & PARSER_MASK_WHITESPACE_CONTEXTUALLY_BANNED) { |
20 |
0 |
20 |
|
| 274 |
|
| // fall through to _next_event_in_node() - this event should be |
| 275 |
|
| // bubbled back up |
| 276 |
20 |
▶ | } else if (self->state & PARSER_MASK_NODE_CANNOT_END_HERE) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (self->state & PARSER_MASK_NODE_CANNOT_END_HERE) { |
0 |
20 |
20 |
|
| 277 |
0 |
| _set_parse_error(self, "/- not allowed here"); |
| 278 |
0 |
| return &self->event; |
| 279 |
|
| } else { |
| 280 |
20 |
▶ | if (self->slashdash_depth < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->slashdash_depth < 0) { |
20 |
0 |
20 |
|
| 281 |
20 |
| self->slashdash_depth = self->depth + 1; |
| 282 |
|
| } |
| 283 |
20 |
| self->state |= PARSER_FLAG_NEWLINES_ARE_WHITESPACE; |
| 284 |
20 |
| break; |
| 285 |
|
| } |
| 286 |
|
| _fallthrough_; |
| 287 |
|
| default: |
| 288 |
32716 |
| self->state &= ~PARSER_FLAG_NEWLINES_ARE_WHITESPACE; |
| 289 |
|
| |
| 290 |
32716 |
| switch (self->state & 0xff) { |
| 291 |
11449 |
| case PARSER_OUTSIDE_NODE: |
| 292 |
11449 |
| ev = _next_node(self, &token); |
| 293 |
11449 |
▶ | if (ev) return ev; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ev) return ev; |
5760 |
5689 |
11449 |
|
| 294 |
5689 |
| else continue; |
| 295 |
21267 |
| case PARSER_IN_NODE: |
| 296 |
21267 |
| ev = _next_event_in_node(self, &token); |
| 297 |
21267 |
▶ | if (ev) return ev; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ev) return ev; |
17948 |
3319 |
21267 |
|
| 298 |
3319 |
| else continue; |
| 299 |
0 |
| default: |
| 300 |
0 |
| _set_parse_error(self, "Inconsistent state"); |
| 301 |
0 |
| return &self->event; |
| 302 |
|
| } |
| 303 |
|
| } |
| 304 |
|
| } |
| 305 |
|
| } |
| 306 |
|
| |
| 307 |
23724 |
| static kdl_event_data* _apply_slashdash(kdl_parser* self) |
| 308 |
|
| { |
| 309 |
23724 |
▶ | if (self->slashdash_depth >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->slashdash_depth >= 0) { |
48 |
23676 |
23724 |
|
| 310 |
|
| // slashdash is active |
| 311 |
48 |
▶ | if (self->slashdash_depth == self->depth + 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->slashdash_depth == self->depth + 1) { |
15 |
33 |
48 |
|
| 312 |
|
| // slashdash is ending here |
| 313 |
15 |
| self->slashdash_depth = -1; |
| 314 |
|
| } |
| 315 |
48 |
▶ | if (self->opt & KDL_EMIT_COMMENTS) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->opt & KDL_EMIT_COMMENTS) { |
29 |
19 |
48 |
|
| 316 |
29 |
| self->event.event |= KDL_EVENT_COMMENT; |
| 317 |
29 |
| return &self->event; |
| 318 |
|
| } else { |
| 319 |
|
| // eat this event and all its attributes |
| 320 |
19 |
| _reset_event(self); |
| 321 |
19 |
| return NULL; |
| 322 |
|
| } |
| 323 |
|
| } else { |
| 324 |
23676 |
| return &self->event; |
| 325 |
|
| } |
| 326 |
|
| } |
| 327 |
|
| |
| 328 |
11449 |
| static kdl_event_data* _next_node(kdl_parser* self, kdl_token* token) |
| 329 |
|
| { |
| 330 |
|
| kdl_value tmp_val; |
| 331 |
|
| kdl_event_data* ev; |
| 332 |
|
| |
| 333 |
11449 |
▶ | if (self->state & PARSER_FLAG_TYPE_ANNOTATION_START) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->state & PARSER_FLAG_TYPE_ANNOTATION_START) { |
34 |
11415 |
11449 |
|
| 334 |
34 |
▶ | switch (token->type) { |
Branch: 1 branch
| Branch | True | False | Total |
| switch (token->type) { |
34 |
0 |
34 |
|
| 335 |
34 |
| case KDL_TOKEN_WORD: |
| 336 |
|
| case KDL_TOKEN_STRING: |
| 337 |
|
| case KDL_TOKEN_MULTILINE_STRING: |
| 338 |
|
| case KDL_TOKEN_RAW_STRING_V1: |
| 339 |
|
| case KDL_TOKEN_RAW_STRING_V2: |
| 340 |
|
| case KDL_TOKEN_RAW_MULTILINE_STRING: |
| 341 |
34 |
▶ | if (!_parse_value(self, token, &tmp_val, &self->tmp_string_type)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!_parse_value(self, token, &tmp_val, &self->tmp_string_type)) { |
0 |
34 |
34 |
|
| 342 |
0 |
| _set_parse_error(self, "Error parsing type annotation"); |
| 343 |
0 |
| return &self->event; |
| 344 |
|
| } |
| 345 |
34 |
▶ | if (tmp_val.type == KDL_TYPE_STRING) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tmp_val.type == KDL_TYPE_STRING) { |
34 |
0 |
34 |
|
| 346 |
|
| // We're good, this is an identifier |
| 347 |
|
| self->state |
| 348 |
34 |
| = (self->state & ~PARSER_FLAG_TYPE_ANNOTATION_START) | PARSER_FLAG_TYPE_ANNOTATION_END; |
| 349 |
34 |
| self->waiting_type_annotation = tmp_val.string; |
| 350 |
34 |
| return NULL; |
| 351 |
|
| } else { |
| 352 |
0 |
| _set_parse_error(self, "Expected identifier or string"); |
| 353 |
0 |
| return &self->event; |
| 354 |
|
| } |
| 355 |
0 |
| default: |
| 356 |
0 |
| _set_parse_error(self, "Unexpected token, expected type"); |
| 357 |
0 |
| return &self->event; |
| 358 |
|
| } |
| 359 |
11415 |
▶ | } else if (self->state & PARSER_FLAG_TYPE_ANNOTATION_END) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (self->state & PARSER_FLAG_TYPE_ANNOTATION_END) { |
34 |
11381 |
11415 |
|
| 360 |
34 |
▶ | switch (token->type) { |
Branch: 1 branch
| Branch | True | False | Total |
| switch (token->type) { |
34 |
0 |
34 |
|
| 361 |
34 |
| case KDL_TOKEN_END_TYPE: |
| 362 |
|
| self->state |
| 363 |
34 |
| = (self->state & ~PARSER_FLAG_TYPE_ANNOTATION_END) | PARSER_FLAG_TYPE_ANNOTATION_ENDED; |
| 364 |
34 |
| return NULL; |
| 365 |
0 |
| default: |
| 366 |
0 |
| _set_parse_error(self, "Unexpected token, expected ')'"); |
| 367 |
0 |
| return &self->event; |
| 368 |
|
| } |
| 369 |
|
| } else { |
| 370 |
11381 |
| switch (token->type) { |
| 371 |
3052 |
| case KDL_TOKEN_NEWLINE: |
| 372 |
|
| case KDL_TOKEN_SEMICOLON: |
| 373 |
3052 |
▶ | if (self->state & PARSER_FLAG_WHITESPACE_REQUIRED) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->state & PARSER_FLAG_WHITESPACE_REQUIRED) { |
0 |
3052 |
3052 |
|
| 374 |
0 |
| self->state &= ~PARSER_FLAG_WHITESPACE_REQUIRED; |
| 375 |
|
| } |
| 376 |
3052 |
▶ | if (self->state & PARSER_MASK_NODE_CANNOT_END_HERE) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->state & PARSER_MASK_NODE_CANNOT_END_HERE) { |
0 |
3052 |
3052 |
|
| 377 |
0 |
| _set_parse_error(self, "Unexpected end of node (incomplete node name?)"); |
| 378 |
0 |
| return &self->event; |
| 379 |
|
| } else { |
| 380 |
|
| // When there is no open type annotation, additional newlines are allowed |
| 381 |
3052 |
| return NULL; |
| 382 |
|
| } |
| 383 |
34 |
| case KDL_TOKEN_START_TYPE: |
| 384 |
|
| // only one type allowed! |
| 385 |
34 |
▶ | if (self->event.value.type_annotation.data != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->event.value.type_annotation.data != NULL) { |
0 |
34 |
34 |
|
| 386 |
0 |
| _set_parse_error(self, "Unexpected second type annotation"); |
| 387 |
0 |
| return &self->event; |
| 388 |
|
| } else { |
| 389 |
34 |
| self->state |= PARSER_FLAG_TYPE_ANNOTATION_START; |
| 390 |
34 |
| return NULL; |
| 391 |
|
| } |
| 392 |
5765 |
| case KDL_TOKEN_WORD: |
| 393 |
|
| case KDL_TOKEN_STRING: |
| 394 |
|
| case KDL_TOKEN_MULTILINE_STRING: |
| 395 |
|
| case KDL_TOKEN_RAW_STRING_V1: |
| 396 |
|
| case KDL_TOKEN_RAW_STRING_V2: |
| 397 |
|
| case KDL_TOKEN_RAW_MULTILINE_STRING: |
| 398 |
5765 |
▶ | if (!_parse_value(self, token, &tmp_val, &self->tmp_string_key)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!_parse_value(self, token, &tmp_val, &self->tmp_string_key)) { |
0 |
5765 |
5765 |
|
| 399 |
0 |
| _set_parse_error(self, "Error parsing node name"); |
| 400 |
0 |
| return &self->event; |
| 401 |
|
| } |
| 402 |
5765 |
▶ | if (tmp_val.type == KDL_TYPE_STRING) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tmp_val.type == KDL_TYPE_STRING) { |
5765 |
0 |
5765 |
|
| 403 |
|
| // We're good, this is an identifier |
| 404 |
5765 |
| self->state = PARSER_IN_NODE | PARSER_FLAG_WHITESPACE_REQUIRED; |
| 405 |
5765 |
| self->event.event = KDL_EVENT_START_NODE; |
| 406 |
5765 |
| self->event.name = tmp_val.string; |
| 407 |
5765 |
▶ | if (self->waiting_type_annotation.data != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->waiting_type_annotation.data != NULL) { |
34 |
5731 |
5765 |
|
| 408 |
34 |
| self->event.value.type_annotation = self->waiting_type_annotation; |
| 409 |
34 |
| self->waiting_type_annotation = (kdl_str){NULL, 0}; |
| 410 |
|
| } |
| 411 |
5765 |
| ++self->depth; |
| 412 |
5765 |
| ev = _apply_slashdash(self); |
| 413 |
5765 |
| return ev; |
| 414 |
|
| } else { |
| 415 |
0 |
| _set_parse_error(self, "Expected identifier or string"); |
| 416 |
0 |
| return &self->event; |
| 417 |
|
| } |
| 418 |
2529 |
| case KDL_TOKEN_END_CHILDREN: |
| 419 |
|
| // end the parent node |
| 420 |
2529 |
▶ | if (self->depth == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->depth == 0) { |
0 |
2529 |
2529 |
|
| 421 |
0 |
| _set_parse_error(self, "Unexpected '}'"); |
| 422 |
0 |
| return &self->event; |
| 423 |
|
| } else { |
| 424 |
2529 |
| self->state = PARSER_IN_NODE; |
| 425 |
2529 |
| --self->depth; |
| 426 |
2529 |
| _reset_event(self); |
| 427 |
2529 |
▶ | if (self->slashdash_depth < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->slashdash_depth < 0) { |
2521 |
8 |
2529 |
|
| 428 |
|
| // slashdash is not active, this child block can't be |
| 429 |
|
| // followed by another child block. |
| 430 |
2521 |
| self->state |= PARSER_FLAG_END_OF_NODE; |
| 431 |
2521 |
| self->child_block_at_depth = self->depth; |
| 432 |
|
| } else { |
| 433 |
|
| // slashdash applies to this child block |
| 434 |
8 |
▶ | if (self->child_block_at_depth == self->depth) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->child_block_at_depth == self->depth) { |
0 |
8 |
8 |
|
| 435 |
|
| // we already had a child block here before |
| 436 |
0 |
| self->state |= PARSER_FLAG_END_OF_NODE; |
| 437 |
|
| } else { |
| 438 |
|
| // all child blocks seen so far in this node have been |
| 439 |
|
| // slashdashed |
| 440 |
8 |
| self->state |= PARSER_FLAG_END_OF_NODE_OR_CHILD_BLOCK; |
| 441 |
|
| } |
| 442 |
|
| } |
| 443 |
2529 |
▶ | if (self->slashdash_depth == self->depth + 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->slashdash_depth == self->depth + 1) { |
4 |
2525 |
2529 |
|
| 444 |
|
| // slashdash is ending here |
| 445 |
4 |
| self->slashdash_depth = -1; |
| 446 |
|
| } |
| 447 |
2529 |
| return NULL; |
| 448 |
|
| } |
| 449 |
0 |
| case KDL_TOKEN_LINE_CONTINUATION: |
| 450 |
|
| // line continuations do nothing outside of nodes in v2, but are illegal here in v1 |
| 451 |
0 |
▶ | if (_v2_allowed(self)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (_v2_allowed(self)) { |
0 |
0 |
0 |
|
| 452 |
0 |
| _set_version(self, KDL_VERSION_2); |
| 453 |
0 |
| return NULL; |
| 454 |
|
| } |
| 455 |
|
| _fallthrough_; |
| 456 |
|
| default: |
| 457 |
1 |
| _set_parse_error(self, "Unexpected token, expected node"); |
| 458 |
1 |
| return &self->event; |
| 459 |
|
| } |
| 460 |
|
| } |
| 461 |
|
| } |
| 462 |
|
| |
| 463 |
21267 |
| static kdl_event_data* _next_event_in_node(kdl_parser* self, kdl_token* token) |
| 464 |
|
| { |
| 465 |
|
| kdl_event_data* ev; |
| 466 |
|
| |
| 467 |
21267 |
▶ | if (self->state & PARSER_FLAG_LINE_CONT) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->state & PARSER_FLAG_LINE_CONT) { |
0 |
21267 |
21267 |
|
| 468 |
0 |
| switch (token->type) { |
| 469 |
0 |
| case KDL_TOKEN_SINGLE_LINE_COMMENT: |
| 470 |
0 |
| break; // fine |
| 471 |
0 |
| case KDL_TOKEN_NEWLINE: |
| 472 |
0 |
| self->state &= ~PARSER_FLAG_LINE_CONT; |
| 473 |
0 |
| return NULL; // Get next token - \ and newline cancel out |
| 474 |
0 |
| default: |
| 475 |
0 |
| _set_parse_error(self, "Illegal token after line continuation"); |
| 476 |
0 |
| return &self->event; |
| 477 |
|
| } |
| 478 |
|
| } |
| 479 |
|
| |
| 480 |
21267 |
▶ | if (token->type == KDL_TOKEN_LINE_CONTINUATION) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (token->type == KDL_TOKEN_LINE_CONTINUATION) { |
0 |
21267 |
21267 |
|
| 481 |
0 |
▶ | if (self->state & PARSER_FLAG_WHITESPACE_REQUIRED) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->state & PARSER_FLAG_WHITESPACE_REQUIRED) { |
0 |
0 |
0 |
|
| 482 |
0 |
| self->state &= ~PARSER_FLAG_WHITESPACE_REQUIRED; |
| 483 |
|
| } |
| 484 |
0 |
▶ | if (self->state & PARSER_MASK_WHITESPACE_BANNED_V1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->state & PARSER_MASK_WHITESPACE_BANNED_V1) { |
0 |
0 |
0 |
|
| 485 |
0 |
▶ | if (_v1_only(self)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (_v1_only(self)) { |
0 |
0 |
0 |
|
| 486 |
0 |
| _set_parse_error(self, "Line continuation not allowed here"); |
| 487 |
0 |
| return &self->event; |
| 488 |
|
| } else { |
| 489 |
0 |
| _set_version(self, KDL_VERSION_2); |
| 490 |
|
| } |
| 491 |
0 |
▶ | } else if (self->state & PARSER_MASK_WHITESPACE_CONTEXTUALLY_BANNED) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (self->state & PARSER_MASK_WHITESPACE_CONTEXTUALLY_BANNED) { |
0 |
0 |
0 |
|
| 492 |
0 |
| self->state |= PARSER_FLAG_CONTEXTUALLY_ILLEGAL_WHITESPACE; |
| 493 |
|
| } |
| 494 |
0 |
| self->state |= PARSER_FLAG_LINE_CONT; |
| 495 |
0 |
| return NULL; |
| 496 |
21267 |
▶ | } else if (self->state & PARSER_FLAG_MAYBE_IN_PROPERTY) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (self->state & PARSER_FLAG_MAYBE_IN_PROPERTY) { |
100 |
21167 |
21267 |
|
| 497 |
100 |
▶ | if (token->type == KDL_TOKEN_EQUALS) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (token->type == KDL_TOKEN_EQUALS) { |
50 |
50 |
100 |
|
| 498 |
|
| // Property value comes next |
| 499 |
50 |
| self->state = (self->state & ~PARSER_FLAG_MAYBE_IN_PROPERTY) | PARSER_FLAG_IN_PROPERTY; |
| 500 |
|
| |
| 501 |
|
| // Whitespace before the equals sign? |
| 502 |
50 |
▶ | if (self->state & PARSER_FLAG_CONTEXTUALLY_ILLEGAL_WHITESPACE) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->state & PARSER_FLAG_CONTEXTUALLY_ILLEGAL_WHITESPACE) { |
0 |
50 |
50 |
|
| 503 |
0 |
| self->state &= ~PARSER_FLAG_CONTEXTUALLY_ILLEGAL_WHITESPACE; |
| 504 |
0 |
▶ | if (_v1_only(self)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (_v1_only(self)) { |
0 |
0 |
0 |
|
| 505 |
0 |
| _set_parse_error(self, "Whitespace not allowed before = sign"); |
| 506 |
0 |
| return &self->event; |
| 507 |
|
| } else { |
| 508 |
0 |
| _set_version(self, KDL_VERSION_2); |
| 509 |
|
| } |
| 510 |
|
| } |
| 511 |
|
| |
| 512 |
50 |
| return NULL; |
| 513 |
|
| } else { |
| 514 |
|
| // We'll need that token again |
| 515 |
50 |
| self->next_token = *token; |
| 516 |
50 |
| self->have_next_token = true; |
| 517 |
|
| |
| 518 |
|
| // Not in property; emit as argument (if legal) |
| 519 |
50 |
▶ | if (self->state & PARSER_FLAG_BARE_PROPERTY_NAME) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->state & PARSER_FLAG_BARE_PROPERTY_NAME) { |
23 |
27 |
50 |
|
| 520 |
23 |
▶ | if (_v1_only(self)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (_v1_only(self)) { |
0 |
23 |
23 |
|
| 521 |
0 |
| _set_parse_error(self, "Bare identifier not allowed here"); |
| 522 |
0 |
| return &self->event; |
| 523 |
|
| } else { |
| 524 |
23 |
| _set_version(self, KDL_VERSION_2); |
| 525 |
|
| } |
| 526 |
|
| } |
| 527 |
|
| |
| 528 |
50 |
| self->event.event = KDL_EVENT_ARGUMENT; |
| 529 |
50 |
| self->event.value.type = KDL_TYPE_STRING; |
| 530 |
50 |
| self->tmp_string_value = self->waiting_prop_name; |
| 531 |
50 |
| self->waiting_prop_name = (kdl_owned_string){NULL, 0}; |
| 532 |
50 |
| self->event.value.string = kdl_borrow_str(&self->tmp_string_value); |
| 533 |
50 |
| ev = _apply_slashdash(self); |
| 534 |
50 |
| enum _kdl_parser_state new_state = PARSER_IN_NODE; |
| 535 |
50 |
▶ | if ((self->state & PARSER_FLAG_CONTEXTUALLY_ILLEGAL_WHITESPACE) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((self->state & PARSER_FLAG_CONTEXTUALLY_ILLEGAL_WHITESPACE) == 0) { |
41 |
9 |
50 |
|
| 536 |
41 |
| new_state |= PARSER_FLAG_WHITESPACE_REQUIRED; |
| 537 |
|
| } |
| 538 |
50 |
| self->state = new_state; |
| 539 |
50 |
| return ev; |
| 540 |
|
| } |
| 541 |
21167 |
▶ | } else if (self->state & PARSER_FLAG_TYPE_ANNOTATION_START) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (self->state & PARSER_FLAG_TYPE_ANNOTATION_START) { |
35 |
21132 |
21167 |
|
| 542 |
|
| kdl_value tmp_val; |
| 543 |
35 |
▶ | switch (token->type) { |
Branch: 1 branch
| Branch | True | False | Total |
| switch (token->type) { |
35 |
0 |
35 |
|
| 544 |
35 |
| case KDL_TOKEN_WORD: |
| 545 |
|
| case KDL_TOKEN_STRING: |
| 546 |
|
| case KDL_TOKEN_MULTILINE_STRING: |
| 547 |
|
| case KDL_TOKEN_RAW_STRING_V1: |
| 548 |
|
| case KDL_TOKEN_RAW_STRING_V2: |
| 549 |
|
| case KDL_TOKEN_RAW_MULTILINE_STRING: |
| 550 |
35 |
▶ | if (!_parse_value(self, token, &tmp_val, &self->tmp_string_type)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!_parse_value(self, token, &tmp_val, &self->tmp_string_type)) { |
0 |
35 |
35 |
|
| 551 |
0 |
| _set_parse_error(self, "Error parsing type annotation"); |
| 552 |
0 |
| return &self->event; |
| 553 |
|
| } |
| 554 |
35 |
▶ | if (tmp_val.type == KDL_TYPE_STRING) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tmp_val.type == KDL_TYPE_STRING) { |
35 |
0 |
35 |
|
| 555 |
|
| // We're good, this is an identifier |
| 556 |
|
| self->state |
| 557 |
35 |
| = (self->state & ~PARSER_FLAG_TYPE_ANNOTATION_START) | PARSER_FLAG_TYPE_ANNOTATION_END; |
| 558 |
35 |
| self->waiting_type_annotation = tmp_val.string; |
| 559 |
35 |
| return NULL; |
| 560 |
|
| } else { |
| 561 |
0 |
| _set_parse_error(self, "Expected identifier or string"); |
| 562 |
0 |
| return &self->event; |
| 563 |
|
| } |
| 564 |
0 |
| default: |
| 565 |
0 |
| _set_parse_error(self, "Unexpected token, expected type"); |
| 566 |
0 |
| return &self->event; |
| 567 |
|
| } |
| 568 |
21132 |
▶ | } else if (self->state & PARSER_FLAG_TYPE_ANNOTATION_END) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (self->state & PARSER_FLAG_TYPE_ANNOTATION_END) { |
35 |
21097 |
21132 |
|
| 569 |
35 |
▶ | switch (token->type) { |
Branch: 1 branch
| Branch | True | False | Total |
| switch (token->type) { |
35 |
0 |
35 |
|
| 570 |
35 |
| case KDL_TOKEN_END_TYPE: |
| 571 |
|
| self->state |
| 572 |
35 |
| = (self->state & ~PARSER_FLAG_TYPE_ANNOTATION_END) | PARSER_FLAG_TYPE_ANNOTATION_ENDED; |
| 573 |
35 |
| return NULL; |
| 574 |
0 |
| default: |
| 575 |
0 |
| _set_parse_error(self, "Unexpected token, expected ')'"); |
| 576 |
0 |
| return &self->event; |
| 577 |
|
| } |
| 578 |
|
| } else { |
| 579 |
|
| // End of node cases: always valid |
| 580 |
21097 |
| switch (token->type) { |
| 581 |
1 |
| case KDL_TOKEN_END_CHILDREN: |
| 582 |
|
| // end this node, and process the token again |
| 583 |
1 |
| self->next_token = *token; |
| 584 |
1 |
| self->have_next_token = true; |
| 585 |
|
| _fallthrough_; |
| 586 |
5215 |
| case KDL_TOKEN_NEWLINE: |
| 587 |
|
| case KDL_TOKEN_SEMICOLON: |
| 588 |
5215 |
▶ | if (self->state & PARSER_MASK_NODE_CANNOT_END_HERE) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->state & PARSER_MASK_NODE_CANNOT_END_HERE) { |
0 |
5215 |
5215 |
|
| 589 |
0 |
| _set_parse_error(self, "Unexpected end of node (incomplete argument or property?)"); |
| 590 |
0 |
| return &self->event; |
| 591 |
|
| } else { |
| 592 |
|
| // end the node |
| 593 |
5215 |
| self->state = PARSER_OUTSIDE_NODE; |
| 594 |
5215 |
| --self->depth; |
| 595 |
5215 |
▶ | if (self->child_block_at_depth > self->depth) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->child_block_at_depth > self->depth) { |
2521 |
2694 |
5215 |
|
| 596 |
2521 |
| self->child_block_at_depth = -1; |
| 597 |
|
| } |
| 598 |
5215 |
| _reset_event(self); |
| 599 |
5215 |
| self->event.event = KDL_EVENT_END_NODE; |
| 600 |
5215 |
| ev = _apply_slashdash(self); |
| 601 |
5215 |
| return ev; |
| 602 |
|
| } |
| 603 |
15882 |
| default: |
| 604 |
15882 |
| break; |
| 605 |
|
| } |
| 606 |
|
| |
| 607 |
|
| // Child block: valid unless we've had a non-slashdashed child block |
| 608 |
|
| // already or slashdash is active now |
| 609 |
15882 |
▶ | if (self->state & PARSER_FLAG_END_OF_NODE && self->slashdash_depth < 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->state & PARSER_FLAG_END_OF_NODE && self->slashdash_depth < 0) { |
0 |
15882 |
15882 |
| if (self->state & PARSER_FLAG_END_OF_NODE && self->slashdash_depth < 0) { |
0 |
0 |
0 |
|
| 610 |
0 |
| _set_parse_error(self, "Expected end of node"); |
| 611 |
0 |
| return &self->event; |
| 612 |
|
| } |
| 613 |
15882 |
▶ | if (token->type == KDL_TOKEN_START_CHILDREN) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (token->type == KDL_TOKEN_START_CHILDREN) { |
3051 |
12831 |
15882 |
|
| 614 |
|
| // Next event will probably be a new node |
| 615 |
3051 |
| self->state = PARSER_OUTSIDE_NODE; |
| 616 |
3051 |
| ++self->depth; |
| 617 |
3051 |
| _reset_event(self); |
| 618 |
3051 |
| return NULL; |
| 619 |
|
| } |
| 620 |
|
| |
| 621 |
|
| // Args, props: only valid at the start |
| 622 |
12831 |
▶ | if (self->state & (PARSER_FLAG_END_OF_NODE | PARSER_FLAG_END_OF_NODE_OR_CHILD_BLOCK)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->state & (PARSER_FLAG_END_OF_NODE | PARSER_FLAG_END_OF_NODE_OR_CHILD_BLOCK)) { |
0 |
12831 |
12831 |
|
| 623 |
0 |
| _set_parse_error(self, "Expected end of node or child block"); |
| 624 |
0 |
| return &self->event; |
| 625 |
|
| } |
| 626 |
|
| |
| 627 |
12831 |
| switch (token->type) { |
| 628 |
12795 |
| case KDL_TOKEN_WORD: |
| 629 |
|
| case KDL_TOKEN_STRING: |
| 630 |
|
| case KDL_TOKEN_MULTILINE_STRING: |
| 631 |
|
| case KDL_TOKEN_RAW_STRING_V1: |
| 632 |
|
| case KDL_TOKEN_RAW_STRING_V2: |
| 633 |
|
| case KDL_TOKEN_RAW_MULTILINE_STRING: { |
| 634 |
12795 |
▶ | if (self->state & PARSER_FLAG_WHITESPACE_REQUIRED) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->state & PARSER_FLAG_WHITESPACE_REQUIRED) { |
0 |
12795 |
12795 |
|
| 635 |
0 |
| _set_parse_error(self, "Whitespace required before argument or property"); |
| 636 |
0 |
| return &self->event; |
| 637 |
|
| } |
| 638 |
|
| // Either a property key, or a property value, or an argument. |
| 639 |
12795 |
▶ | if (!_parse_value(self, token, &self->event.value, &self->tmp_string_value)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!_parse_value(self, token, &self->event.value, &self->tmp_string_value)) { |
1 |
12794 |
12795 |
|
| 640 |
1 |
| _set_parse_error(self, "Error parsing property or argument"); |
| 641 |
1 |
| return &self->event; |
| 642 |
|
| } |
| 643 |
|
| |
| 644 |
|
| // Can this be a property key? |
| 645 |
12794 |
▶ | if (self->waiting_type_annotation.data == NULL && (self->state & PARSER_FLAG_IN_PROPERTY) == 0 |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->waiting_type_annotation.data == NULL && (self->state & PARSER_FLAG_IN_PROPERTY) == 0 |
12759 |
35 |
12794 |
| if (self->waiting_type_annotation.data == NULL && (self->state & PARSER_FLAG_IN_PROPERTY) == 0 |
12710 |
49 |
12759 |
|
| 646 |
12710 |
▶ | && self->event.value.type == KDL_TYPE_STRING) { |
Branch: 1 branch
| Branch | True | False | Total |
| && self->event.value.type == KDL_TYPE_STRING) { |
100 |
12610 |
12710 |
|
| 647 |
|
| // carry over the potential name in waiting_prop_name |
| 648 |
100 |
| self->waiting_prop_name = self->tmp_string_value; |
| 649 |
100 |
| self->tmp_string_value = (kdl_owned_string){NULL, 0}; |
| 650 |
100 |
| self->event.value.type = KDL_TYPE_NULL; |
| 651 |
100 |
| self->state |= PARSER_FLAG_MAYBE_IN_PROPERTY; |
| 652 |
|
| |
| 653 |
100 |
▶ | if (token->type == KDL_TOKEN_WORD) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (token->type == KDL_TOKEN_WORD) { |
43 |
57 |
100 |
|
| 654 |
|
| // bare identifiers can't be arguments in KDLv1 |
| 655 |
43 |
| self->state |= PARSER_FLAG_BARE_PROPERTY_NAME; |
| 656 |
|
| } |
| 657 |
|
| |
| 658 |
100 |
| return NULL; |
| 659 |
|
| } |
| 660 |
|
| |
| 661 |
|
| // This is an argument or a property value. |
| 662 |
|
| // KDLv1 bans bare identifiers in this position |
| 663 |
12694 |
▶ | if (token->type == KDL_TOKEN_WORD && self->event.value.type == KDL_TYPE_STRING) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (token->type == KDL_TOKEN_WORD && self->event.value.type == KDL_TYPE_STRING) { |
12624 |
70 |
12694 |
| if (token->type == KDL_TOKEN_WORD && self->event.value.type == KDL_TYPE_STRING) { |
1 |
12623 |
12624 |
|
| 664 |
1 |
▶ | if (_v1_only(self)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (_v1_only(self)) { |
0 |
1 |
1 |
|
| 665 |
0 |
| _set_parse_error(self, "Bare identifier not allowed here"); |
| 666 |
0 |
| return &self->event; |
| 667 |
|
| } else { |
| 668 |
1 |
| _set_version(self, KDL_VERSION_2); |
| 669 |
|
| } |
| 670 |
|
| } |
| 671 |
|
| |
| 672 |
|
| // We can return the event |
| 673 |
12694 |
▶ | if (self->waiting_type_annotation.data != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->waiting_type_annotation.data != NULL) { |
35 |
12659 |
12694 |
|
| 674 |
35 |
| self->event.value.type_annotation = self->waiting_type_annotation; |
| 675 |
35 |
| self->waiting_type_annotation = (kdl_str){NULL, 0}; |
| 676 |
|
| } |
| 677 |
12694 |
▶ | if (self->state & PARSER_FLAG_IN_PROPERTY) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->state & PARSER_FLAG_IN_PROPERTY) { |
50 |
12644 |
12694 |
|
| 678 |
50 |
| self->tmp_string_key = self->waiting_prop_name; |
| 679 |
50 |
| self->waiting_prop_name = (kdl_owned_string){NULL, 0}; |
| 680 |
50 |
| self->event.name = kdl_borrow_str(&self->tmp_string_key); |
| 681 |
50 |
| self->event.event = KDL_EVENT_PROPERTY; |
| 682 |
|
| } else { |
| 683 |
12644 |
| self->event.event = KDL_EVENT_ARGUMENT; |
| 684 |
|
| } |
| 685 |
12694 |
| ev = _apply_slashdash(self); |
| 686 |
|
| // be sure to reset the parser state |
| 687 |
12694 |
| self->state = PARSER_IN_NODE | PARSER_FLAG_WHITESPACE_REQUIRED; |
| 688 |
12694 |
| return ev; |
| 689 |
|
| } |
| 690 |
35 |
| case KDL_TOKEN_START_TYPE: |
| 691 |
35 |
▶ | if (self->state & PARSER_FLAG_WHITESPACE_REQUIRED) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->state & PARSER_FLAG_WHITESPACE_REQUIRED) { |
0 |
35 |
35 |
|
| 692 |
0 |
| _set_parse_error(self, "Whitespace required before type"); |
| 693 |
0 |
| return &self->event; |
| 694 |
|
| } |
| 695 |
|
| // only one type allowed! |
| 696 |
35 |
▶ | if (self->event.value.type_annotation.data != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->event.value.type_annotation.data != NULL) { |
0 |
35 |
35 |
|
| 697 |
0 |
| _set_parse_error(self, "Unexpected second type annotation"); |
| 698 |
0 |
| return &self->event; |
| 699 |
|
| } else { |
| 700 |
35 |
| self->state |= PARSER_FLAG_TYPE_ANNOTATION_START; |
| 701 |
35 |
| return NULL; |
| 702 |
|
| } |
| 703 |
1 |
| default: |
| 704 |
1 |
| _set_parse_error(self, "Unexpected token"); |
| 705 |
1 |
| return &self->event; |
| 706 |
|
| } |
| 707 |
|
| } |
| 708 |
|
| } |
| 709 |
|
| |
| 710 |
18629 |
| static bool _parse_value(kdl_parser* self, kdl_token const* token, kdl_value* val, kdl_owned_string* s) |
| 711 |
|
| { |
| 712 |
18629 |
| kdl_free_string(s); |
| 713 |
|
| |
| 714 |
18629 |
| switch (token->type) { |
| 715 |
0 |
| case KDL_TOKEN_RAW_STRING_V1: |
| 716 |
0 |
▶ | if (_v1_allowed(self)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (_v1_allowed(self)) { |
0 |
0 |
0 |
|
| 717 |
0 |
| _set_version(self, KDL_VERSION_1); |
| 718 |
|
| // no parsing necessary |
| 719 |
0 |
| *s = kdl_clone_str(&token->value); |
| 720 |
0 |
| val->type = KDL_TYPE_STRING; |
| 721 |
0 |
| val->string = kdl_borrow_str(s); |
| 722 |
0 |
| return true; |
| 723 |
|
| } else { |
| 724 |
0 |
| return false; |
| 725 |
|
| } |
| 726 |
0 |
| case KDL_TOKEN_RAW_STRING_V2: |
| 727 |
0 |
▶ | if (_v2_allowed(self)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (_v2_allowed(self)) { |
0 |
0 |
0 |
|
| 728 |
0 |
| _set_version(self, KDL_VERSION_2); |
| 729 |
|
| // newlines are not allowed in plain raw strings in v2 |
| 730 |
0 |
▶ | if (_kdl_str_contains_newline(&token->value)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (_kdl_str_contains_newline(&token->value)) { |
0 |
0 |
0 |
|
| 731 |
0 |
| return false; |
| 732 |
|
| } |
| 733 |
|
| // no parsing necessary |
| 734 |
0 |
| *s = kdl_clone_str(&token->value); |
| 735 |
0 |
| val->type = KDL_TYPE_STRING; |
| 736 |
0 |
| val->string = kdl_borrow_str(s); |
| 737 |
0 |
| return true; |
| 738 |
|
| } else { |
| 739 |
0 |
| return false; |
| 740 |
|
| } |
| 741 |
0 |
| case KDL_TOKEN_RAW_MULTILINE_STRING: |
| 742 |
0 |
▶ | if (_v2_allowed(self)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (_v2_allowed(self)) { |
0 |
0 |
0 |
|
| 743 |
0 |
| _set_version(self, KDL_VERSION_2); |
| 744 |
|
| // dedent multi-line string |
| 745 |
0 |
| *s = _kdl_dedent_multiline_string(&token->value); |
| 746 |
0 |
▶ | if (s->data == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->data == NULL) { |
0 |
0 |
0 |
|
| 747 |
0 |
| return false; |
| 748 |
|
| } |
| 749 |
0 |
| val->type = KDL_TYPE_STRING; |
| 750 |
0 |
| val->string = kdl_borrow_str(s); |
| 751 |
0 |
| return true; |
| 752 |
|
| } else { |
| 753 |
0 |
| return false; |
| 754 |
|
| } |
| 755 |
224 |
| case KDL_TOKEN_STRING: { |
| 756 |
|
| // parse escapes |
| 757 |
224 |
| kdl_owned_string v1_str = (kdl_owned_string){NULL, 0}; |
| 758 |
224 |
| kdl_owned_string v2_str = (kdl_owned_string){NULL, 0}; |
| 759 |
|
| |
| 760 |
224 |
▶ | if (_v1_allowed(self)) v1_str = kdl_unescape_v1(&token->value); |
Branch: 1 branch
| Branch | True | False | Total |
| if (_v1_allowed(self)) v1_str = kdl_unescape_v1(&token->value); |
77 |
147 |
224 |
|
| 761 |
224 |
▶ | if (_v2_allowed(self)) v2_str = kdl_unescape_v2_single_line(&token->value); |
Branch: 1 branch
| Branch | True | False | Total |
| if (_v2_allowed(self)) v2_str = kdl_unescape_v2_single_line(&token->value); |
165 |
59 |
224 |
|
| 762 |
|
| |
| 763 |
224 |
▶ | if (v1_str.data == NULL && v2_str.data != NULL) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (v1_str.data == NULL && v2_str.data != NULL) { |
147 |
77 |
224 |
| if (v1_str.data == NULL && v2_str.data != NULL) { |
146 |
1 |
147 |
|
| 764 |
146 |
| _set_version(self, KDL_VERSION_2); |
| 765 |
146 |
| *s = v2_str; |
| 766 |
78 |
▶ | } else if (v1_str.data != NULL && v2_str.data == NULL) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (v1_str.data != NULL && v2_str.data == NULL) { |
77 |
1 |
78 |
| } else if (v1_str.data != NULL && v2_str.data == NULL) { |
60 |
17 |
77 |
|
| 767 |
60 |
| _set_version(self, KDL_VERSION_1); |
| 768 |
60 |
| *s = v1_str; |
| 769 |
18 |
▶ | } else if (v1_str.data != NULL && v2_str.data != NULL) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (v1_str.data != NULL && v2_str.data != NULL) { |
17 |
1 |
18 |
| } else if (v1_str.data != NULL && v2_str.data != NULL) { |
17 |
0 |
17 |
|
| 770 |
|
| // Could be either version, used KDLv2 value |
| 771 |
17 |
| *s = v2_str; |
| 772 |
17 |
| kdl_free_string(&v1_str); |
| 773 |
|
| } |
| 774 |
|
| |
| 775 |
224 |
▶ | if (s->data == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->data == NULL) { |
1 |
223 |
224 |
|
| 776 |
1 |
| return false; |
| 777 |
|
| } else { |
| 778 |
223 |
| val->type = KDL_TYPE_STRING; |
| 779 |
223 |
| val->string = kdl_borrow_str(s); |
| 780 |
223 |
| return true; |
| 781 |
|
| } |
| 782 |
|
| } |
| 783 |
0 |
| case KDL_TOKEN_MULTILINE_STRING: { |
| 784 |
0 |
▶ | if (_v2_allowed(self)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (_v2_allowed(self)) { |
0 |
0 |
0 |
|
| 785 |
0 |
| *s = kdl_unescape_v2_multi_line(&token->value); |
| 786 |
0 |
▶ | if (s->data == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->data == NULL) { |
0 |
0 |
0 |
|
| 787 |
0 |
| return false; |
| 788 |
|
| } else { |
| 789 |
0 |
| val->type = KDL_TYPE_STRING; |
| 790 |
0 |
| val->string = kdl_borrow_str(s); |
| 791 |
0 |
| return true; |
| 792 |
|
| } |
| 793 |
|
| } else { |
| 794 |
0 |
| return false; |
| 795 |
|
| } |
| 796 |
|
| } |
| 797 |
18405 |
| case KDL_TOKEN_WORD: { |
| 798 |
18405 |
▶ | if (_str_equals_literal(token->value, "null")) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (_str_equals_literal(token->value, "null")) { |
926 |
17479 |
18405 |
| if (_str_equals_literal(token->value, "null")) { |
0 |
926 |
926 |
|
| 799 |
0 |
▶ | if (_v1_allowed(self)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (_v1_allowed(self)) { |
0 |
0 |
0 |
|
| 800 |
0 |
| _set_version(self, KDL_VERSION_1); |
| 801 |
0 |
| val->type = KDL_TYPE_NULL; |
| 802 |
0 |
| return true; |
| 803 |
|
| } else { |
| 804 |
0 |
| return false; |
| 805 |
|
| } |
| 806 |
18405 |
▶ | } else if (_str_equals_literal(token->value, "true")) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (_str_equals_literal(token->value, "true")) { |
926 |
17479 |
18405 |
| } else if (_str_equals_literal(token->value, "true")) { |
0 |
926 |
926 |
|
| 807 |
0 |
▶ | if (_v1_allowed(self)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (_v1_allowed(self)) { |
0 |
0 |
0 |
|
| 808 |
0 |
| _set_version(self, KDL_VERSION_1); |
| 809 |
0 |
| val->type = KDL_TYPE_BOOLEAN; |
| 810 |
0 |
| val->boolean = true; |
| 811 |
0 |
| return true; |
| 812 |
|
| } else { |
| 813 |
0 |
| return false; |
| 814 |
|
| } |
| 815 |
18405 |
▶ | } else if (_str_equals_literal(token->value, "false")) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (_str_equals_literal(token->value, "false")) { |
1030 |
17375 |
18405 |
| } else if (_str_equals_literal(token->value, "false")) { |
0 |
1030 |
1030 |
|
| 816 |
0 |
▶ | if (_v1_allowed(self)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (_v1_allowed(self)) { |
0 |
0 |
0 |
|
| 817 |
0 |
| _set_version(self, KDL_VERSION_1); |
| 818 |
0 |
| val->type = KDL_TYPE_BOOLEAN; |
| 819 |
0 |
| val->boolean = false; |
| 820 |
0 |
| return true; |
| 821 |
|
| } else { |
| 822 |
0 |
| return false; |
| 823 |
|
| } |
| 824 |
18405 |
▶ | } else if (_v2_allowed(self) && _str_equals_literal(token->value, "#null")) { |
Branch: 3 branches
| Branch | True | False | Total |
| } else if (_v2_allowed(self) && _str_equals_literal(token->value, "#null")) { |
18387 |
18 |
18405 |
| } else if (_v2_allowed(self) && _str_equals_literal(token->value, "#null")) { |
1030 |
17357 |
18387 |
| } else if (_v2_allowed(self) && _str_equals_literal(token->value, "#null")) { |
3 |
1027 |
1030 |
|
| 825 |
3 |
| _set_version(self, KDL_VERSION_2); |
| 826 |
3 |
| val->type = KDL_TYPE_NULL; |
| 827 |
3 |
| return true; |
| 828 |
18402 |
▶ | } else if (_v2_allowed(self) && _str_equals_literal(token->value, "#true")) { |
Branch: 3 branches
| Branch | True | False | Total |
| } else if (_v2_allowed(self) && _str_equals_literal(token->value, "#true")) { |
18384 |
18 |
18402 |
| } else if (_v2_allowed(self) && _str_equals_literal(token->value, "#true")) { |
1027 |
17357 |
18384 |
| } else if (_v2_allowed(self) && _str_equals_literal(token->value, "#true")) { |
2 |
1025 |
1027 |
|
| 829 |
2 |
| _set_version(self, KDL_VERSION_2); |
| 830 |
2 |
| val->type = KDL_TYPE_BOOLEAN; |
| 831 |
2 |
| val->boolean = true; |
| 832 |
2 |
| return true; |
| 833 |
18400 |
▶ | } else if (_v2_allowed(self) && _str_equals_literal(token->value, "#false")) { |
Branch: 3 branches
| Branch | True | False | Total |
| } else if (_v2_allowed(self) && _str_equals_literal(token->value, "#false")) { |
18382 |
18 |
18400 |
| } else if (_v2_allowed(self) && _str_equals_literal(token->value, "#false")) { |
11 |
18371 |
18382 |
| } else if (_v2_allowed(self) && _str_equals_literal(token->value, "#false")) { |
2 |
9 |
11 |
|
| 834 |
2 |
| _set_version(self, KDL_VERSION_2); |
| 835 |
2 |
| val->type = KDL_TYPE_BOOLEAN; |
| 836 |
2 |
| val->boolean = false; |
| 837 |
2 |
| return true; |
| 838 |
18398 |
▶ | } else if (_v2_allowed(self) && _str_equals_literal(token->value, "#inf")) { |
Branch: 3 branches
| Branch | True | False | Total |
| } else if (_v2_allowed(self) && _str_equals_literal(token->value, "#inf")) { |
18380 |
18 |
18398 |
| } else if (_v2_allowed(self) && _str_equals_literal(token->value, "#inf")) { |
922 |
17458 |
18380 |
| } else if (_v2_allowed(self) && _str_equals_literal(token->value, "#inf")) { |
1 |
921 |
922 |
|
| 839 |
1 |
| _set_version(self, KDL_VERSION_2); |
| 840 |
1 |
| val->type = KDL_TYPE_NUMBER; |
| 841 |
1 |
| val->number.type = KDL_NUMBER_TYPE_FLOATING_POINT; |
| 842 |
1 |
| val->number.floating_point = INFINITY; |
| 843 |
1 |
| return true; |
| 844 |
18397 |
▶ | } else if (_v2_allowed(self) && _str_equals_literal(token->value, "#-inf")) { |
Branch: 3 branches
| Branch | True | False | Total |
| } else if (_v2_allowed(self) && _str_equals_literal(token->value, "#-inf")) { |
18379 |
18 |
18397 |
| } else if (_v2_allowed(self) && _str_equals_literal(token->value, "#-inf")) { |
1025 |
17354 |
18379 |
| } else if (_v2_allowed(self) && _str_equals_literal(token->value, "#-inf")) { |
1 |
1024 |
1025 |
|
| 845 |
1 |
| _set_version(self, KDL_VERSION_2); |
| 846 |
1 |
| val->type = KDL_TYPE_NUMBER; |
| 847 |
1 |
| val->number.type = KDL_NUMBER_TYPE_FLOATING_POINT; |
| 848 |
1 |
| val->number.floating_point = -INFINITY; |
| 849 |
1 |
| return true; |
| 850 |
18396 |
▶ | } else if (_v2_allowed(self) && _str_equals_literal(token->value, "#nan")) { |
Branch: 3 branches
| Branch | True | False | Total |
| } else if (_v2_allowed(self) && _str_equals_literal(token->value, "#nan")) { |
18378 |
18 |
18396 |
| } else if (_v2_allowed(self) && _str_equals_literal(token->value, "#nan")) { |
921 |
17457 |
18378 |
| } else if (_v2_allowed(self) && _str_equals_literal(token->value, "#nan")) { |
1 |
920 |
921 |
|
| 851 |
1 |
| _set_version(self, KDL_VERSION_2); |
| 852 |
1 |
| val->type = KDL_TYPE_NUMBER; |
| 853 |
1 |
| val->number.type = KDL_NUMBER_TYPE_FLOATING_POINT; |
| 854 |
1 |
| val->number.floating_point = NAN; |
| 855 |
1 |
| return true; |
| 856 |
|
| } |
| 857 |
|
| // either a number or an identifier |
| 858 |
18395 |
▶ | if (token->value.len >= 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (token->value.len >= 1) { |
18395 |
0 |
18395 |
|
| 859 |
18395 |
| char first_char = token->value.data[0]; |
| 860 |
18395 |
| int offset = 0; |
| 861 |
|
| // skip sign if present |
| 862 |
18395 |
▶ | if ((first_char == '+' || first_char == '-') && token->value.len >= 2) { |
Branch: 3 branches
| Branch | True | False | Total |
| if ((first_char == '+' || first_char == '-') && token->value.len >= 2) { |
18395 |
0 |
18395 |
| if ((first_char == '+' || first_char == '-') && token->value.len >= 2) { |
5059 |
13336 |
18395 |
| if ((first_char == '+' || first_char == '-') && token->value.len >= 2) { |
5054 |
5 |
5059 |
|
| 863 |
5054 |
| first_char = token->value.data[1]; |
| 864 |
5054 |
| offset = 1; |
| 865 |
|
| } |
| 866 |
18395 |
▶ | if (first_char >= '0' && first_char <= '9') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (first_char >= '0' && first_char <= '9') { |
18385 |
10 |
18395 |
| if (first_char >= '0' && first_char <= '9') { |
12613 |
5772 |
18385 |
|
| 867 |
|
| // first character after sign is a digit, this value should be interpreted as a number |
| 868 |
12613 |
| return _parse_number(token->value, val, s); |
| 869 |
5782 |
▶ | } else if (_v2_only(self) && first_char == '.' && token->value.len - offset >= 2) { |
Branch: 3 branches
| Branch | True | False | Total |
| } else if (_v2_only(self) && first_char == '.' && token->value.len - offset >= 2) { |
53 |
5729 |
5782 |
| } else if (_v2_only(self) && first_char == '.' && token->value.len - offset >= 2) { |
0 |
53 |
53 |
| } else if (_v2_only(self) && first_char == '.' && token->value.len - offset >= 2) { |
0 |
0 |
0 |
|
| 870 |
|
| // check for v2 rule of banned "almost numbers" |
| 871 |
0 |
| char second_char = token->value.data[offset + 1]; |
| 872 |
0 |
▶ | if (second_char >= '0' && second_char <= '9') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (second_char >= '0' && second_char <= '9') { |
0 |
0 |
0 |
| if (second_char >= '0' && second_char <= '9') { |
0 |
0 |
0 |
|
| 873 |
0 |
| return false; |
| 874 |
|
| } |
| 875 |
|
| } |
| 876 |
|
| } |
| 877 |
|
| // this is a regular identifier (or a syntax error) |
| 878 |
5782 |
▶ | bool is_v1_identifier = _v1_allowed(self) && _identifier_is_valid_v1(token->value); |
Branch: 2 branches
| Branch | True | False | Total |
| bool is_v1_identifier = _v1_allowed(self) && _identifier_is_valid_v1(token->value); |
5729 |
53 |
5782 |
| bool is_v1_identifier = _v1_allowed(self) && _identifier_is_valid_v1(token->value); |
5729 |
0 |
5729 |
|
| 879 |
5782 |
▶ | bool is_v2_identifier = _v2_allowed(self) && _identifier_is_valid_v2(token->value); |
Branch: 2 branches
| Branch | True | False | Total |
| bool is_v2_identifier = _v2_allowed(self) && _identifier_is_valid_v2(token->value); |
5764 |
18 |
5782 |
| bool is_v2_identifier = _v2_allowed(self) && _identifier_is_valid_v2(token->value); |
5764 |
0 |
5764 |
|
| 880 |
5782 |
▶ | bool is_identifier = is_v1_identifier || is_v2_identifier; |
Branch: 2 branches
| Branch | True | False | Total |
| bool is_identifier = is_v1_identifier || is_v2_identifier; |
53 |
5729 |
5782 |
| bool is_identifier = is_v1_identifier || is_v2_identifier; |
53 |
0 |
53 |
|
| 881 |
5782 |
▶ | if (is_v1_identifier && !is_v2_identifier) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (is_v1_identifier && !is_v2_identifier) { |
5729 |
53 |
5782 |
| if (is_v1_identifier && !is_v2_identifier) { |
18 |
5711 |
5729 |
|
| 882 |
18 |
| _set_version(self, KDL_VERSION_1); |
| 883 |
5764 |
▶ | } else if (is_v2_identifier && !is_v1_identifier) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (is_v2_identifier && !is_v1_identifier) { |
5764 |
0 |
5764 |
| } else if (is_v2_identifier && !is_v1_identifier) { |
53 |
5711 |
5764 |
|
| 884 |
53 |
| _set_version(self, KDL_VERSION_2); |
| 885 |
|
| } |
| 886 |
5782 |
▶ | if (is_identifier) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_identifier) { |
5782 |
0 |
5782 |
|
| 887 |
5782 |
| *s = kdl_clone_str(&token->value); |
| 888 |
5782 |
| val->type = KDL_TYPE_STRING; |
| 889 |
5782 |
| val->string = kdl_borrow_str(s); |
| 890 |
5782 |
| return true; |
| 891 |
|
| } |
| 892 |
|
| _fallthrough_; |
| 893 |
|
| } |
| 894 |
|
| default: |
| 895 |
0 |
| return false; |
| 896 |
|
| } |
| 897 |
|
| } |
| 898 |
|
| |
| 899 |
12613 |
| static bool _parse_number(kdl_str number, kdl_value* val, kdl_owned_string* s) |
| 900 |
|
| { |
| 901 |
12613 |
| kdl_str orig_number = number; |
| 902 |
12613 |
▶ | if (number.len >= 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (number.len >= 1) { |
12613 |
0 |
12613 |
|
| 903 |
12613 |
▶ | switch (number.data[0]) { |
Branch: 1 branch
| Branch | True | False | Total |
| switch (number.data[0]) { |
5050 |
7563 |
12613 |
|
| 904 |
5050 |
| case '-': |
| 905 |
|
| case '+': |
| 906 |
5050 |
| ++number.data; |
| 907 |
5050 |
| --number.len; |
| 908 |
12613 |
| default: |
| 909 |
12613 |
| break; |
| 910 |
|
| } |
| 911 |
|
| } |
| 912 |
|
| |
| 913 |
12613 |
▶ | if (number.len > 2 && number.data[0] == '0') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (number.len > 2 && number.data[0] == '0') { |
10034 |
2579 |
12613 |
| if (number.len > 2 && number.data[0] == '0') { |
70 |
9964 |
10034 |
|
| 914 |
70 |
| switch (number.data[1]) { |
| 915 |
1 |
| case 'x': |
| 916 |
1 |
| return _parse_hex_number(orig_number, val, s); |
| 917 |
0 |
| case 'o': |
| 918 |
0 |
| return _parse_octal_number(orig_number, val, s); |
| 919 |
0 |
| case 'b': |
| 920 |
0 |
| return _parse_binary_number(orig_number, val, s); |
| 921 |
69 |
| default: |
| 922 |
69 |
| break; |
| 923 |
|
| } |
| 924 |
|
| } |
| 925 |
12612 |
| return _parse_decimal_number(orig_number, val, s); |
| 926 |
|
| } |
| 927 |
|
| |
| 928 |
12612 |
| static bool _parse_decimal_number(kdl_str number, kdl_value* val, kdl_owned_string* s) |
| 929 |
|
| { |
| 930 |
|
| // Check this is an integer or a decimal |
| 931 |
32489 |
▶ | for (size_t i = 0; i < number.len; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t i = 0; i < number.len; ++i) { |
29896 |
2593 |
32489 |
|
| 932 |
29896 |
▶ | switch (number.data[i]) { |
Branch: 1 branch
| Branch | True | False | Total |
| switch (number.data[i]) { |
10019 |
19877 |
29896 |
|
| 933 |
10019 |
| case '.': |
| 934 |
|
| case 'e': |
| 935 |
|
| case 'E': |
| 936 |
10019 |
| return _parse_decimal_float(number, val, s); |
| 937 |
|
| } |
| 938 |
|
| } |
| 939 |
2593 |
| return _parse_decimal_integer(number, val, s); |
| 940 |
|
| } |
| 941 |
|
| |
| 942 |
2593 |
| static bool _parse_decimal_integer(kdl_str number, kdl_value* val, kdl_owned_string* s) |
| 943 |
|
| { |
| 944 |
2593 |
| bool negative = false; |
| 945 |
2593 |
| _kdl_ubigint* n = _kdl_ubigint_new(0); |
| 946 |
2593 |
▶ | if (n == NULL) return false; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == NULL) return false; |
0 |
2593 |
2593 |
|
| 947 |
|
| |
| 948 |
2593 |
| size_t i = 0; // index into number-string |
| 949 |
|
| |
| 950 |
|
| // handle sign |
| 951 |
2593 |
▶ | if (number.len > 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (number.len > 1) { |
17 |
2576 |
2593 |
|
| 952 |
17 |
| switch (number.data[0]) { |
| 953 |
4 |
| case '-': |
| 954 |
4 |
| negative = true; |
| 955 |
|
| _fallthrough_; |
| 956 |
4 |
| case '+': |
| 957 |
4 |
| i = 1; |
| 958 |
|
| } |
| 959 |
|
| } |
| 960 |
|
| |
| 961 |
2593 |
▶ | if (number.len - i > 0 && number.data[i] == '_') goto error; |
Branch: 2 branches
| Branch | True | False | Total |
| if (number.len - i > 0 && number.data[i] == '_') goto error; |
2593 |
0 |
2593 |
| if (number.len - i > 0 && number.data[i] == '_') goto error; |
0 |
2593 |
2593 |
|
| 962 |
|
| |
| 963 |
|
| // scan through entire number |
| 964 |
5411 |
▶ | for (; i < number.len; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; i < number.len; ++i) { |
2818 |
2593 |
5411 |
|
| 965 |
2818 |
| char c = number.data[i]; |
| 966 |
2818 |
▶ | if (c == '_') continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '_') continue; |
0 |
2818 |
2818 |
|
| 967 |
2818 |
▶ | else if (c < '0' || c > '9') goto error; |
Branch: 2 branches
| Branch | True | False | Total |
| else if (c < '0' || c > '9') goto error; |
2818 |
0 |
2818 |
| else if (c < '0' || c > '9') goto error; |
0 |
2818 |
2818 |
|
| 968 |
|
| |
| 969 |
2818 |
| int digit = c - '0'; |
| 970 |
2818 |
| n = _kdl_ubigint_multiply_inplace(n, 10); |
| 971 |
2818 |
| n = _kdl_ubigint_add_inplace(n, digit); |
| 972 |
|
| } |
| 973 |
|
| |
| 974 |
|
| // try to reduce down to long long |
| 975 |
|
| long long n_ll; |
| 976 |
2593 |
▶ | if (_kdl_ubigint_as_long_long(n, &n_ll)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (_kdl_ubigint_as_long_long(n, &n_ll)) { |
2581 |
12 |
2593 |
|
| 977 |
|
| // number is representable as long long |
| 978 |
2581 |
▶ | if (negative) n_ll = -n_ll; |
Branch: 1 branch
| Branch | True | False | Total |
| if (negative) n_ll = -n_ll; |
1 |
2580 |
2581 |
|
| 979 |
2581 |
| val->type = KDL_TYPE_NUMBER; |
| 980 |
2581 |
| val->number.type = KDL_NUMBER_TYPE_INTEGER; |
| 981 |
2581 |
| val->number.integer = n_ll; |
| 982 |
|
| } else { |
| 983 |
|
| // represent number as string |
| 984 |
12 |
▶ | *s = _kdl_ubigint_as_string_sgn(negative ? -1 : +1, n); |
Branch: 1 branch
| Branch | True | False | Total |
| *s = _kdl_ubigint_as_string_sgn(negative ? -1 : +1, n); |
3 |
9 |
12 |
|
| 985 |
12 |
| val->type = KDL_TYPE_NUMBER; |
| 986 |
12 |
| val->number.type = KDL_NUMBER_TYPE_STRING_ENCODED; |
| 987 |
12 |
| val->number.string = kdl_borrow_str(s); |
| 988 |
|
| } |
| 989 |
2593 |
| _kdl_ubigint_free(n); |
| 990 |
2593 |
| return true; |
| 991 |
0 |
| error: |
| 992 |
0 |
| _kdl_ubigint_free(n); |
| 993 |
0 |
| return false; |
| 994 |
|
| } |
| 995 |
|
| |
| 996 |
10019 |
| static bool _parse_decimal_float(kdl_str number, kdl_value* val, kdl_owned_string* s) |
| 997 |
|
| { |
| 998 |
10019 |
| bool negative = false; |
| 999 |
10019 |
| int digits_before_decimal = 0; |
| 1000 |
10019 |
| int digits_after_decimal = 0; |
| 1001 |
10019 |
| unsigned long long decimal_mantissa = 0; |
| 1002 |
10019 |
| int explicit_exponent = 0; |
| 1003 |
10019 |
| bool exponent_negative = false; |
| 1004 |
|
| enum { |
| 1005 |
|
| before_decimal, |
| 1006 |
|
| after_decimal_nodigit, |
| 1007 |
|
| after_decimal, |
| 1008 |
|
| exponent_nodigit, |
| 1009 |
|
| exponent |
| 1010 |
10019 |
| } state = before_decimal; |
| 1011 |
|
| |
| 1012 |
10019 |
| size_t i = 0; // index into number-string |
| 1013 |
|
| |
| 1014 |
|
| // handle sign |
| 1015 |
10019 |
▶ | if (number.len > 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (number.len > 1) { |
10019 |
0 |
10019 |
|
| 1016 |
10019 |
| switch (number.data[0]) { |
| 1017 |
5046 |
| case '-': |
| 1018 |
5046 |
| negative = true; |
| 1019 |
|
| _fallthrough_; |
| 1020 |
5046 |
| case '+': |
| 1021 |
5046 |
| i = 1; |
| 1022 |
|
| } |
| 1023 |
|
| } |
| 1024 |
|
| |
| 1025 |
10019 |
▶ | if (number.len - i > 0 && number.data[i] == '_') return false; |
Branch: 2 branches
| Branch | True | False | Total |
| if (number.len - i > 0 && number.data[i] == '_') return false; |
10019 |
0 |
10019 |
| if (number.len - i > 0 && number.data[i] == '_') return false; |
0 |
10019 |
10019 |
|
| 1026 |
|
| |
| 1027 |
|
| // scan through entire number |
| 1028 |
220067 |
▶ | for (; i < number.len; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; i < number.len; ++i) { |
210048 |
10019 |
220067 |
|
| 1029 |
210048 |
| char c = number.data[i]; |
| 1030 |
210048 |
▶ | if (c == '.' && state == before_decimal) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == '.' && state == before_decimal) { |
10014 |
200034 |
210048 |
| if (c == '.' && state == before_decimal) { |
10014 |
0 |
10014 |
|
| 1031 |
10014 |
| state = after_decimal_nodigit; |
| 1032 |
10014 |
▶ | if (number.len - i <= 1 || number.data[i + 1] == '_') return false; |
Branch: 2 branches
| Branch | True | False | Total |
| if (number.len - i <= 1 || number.data[i + 1] == '_') return false; |
10014 |
0 |
10014 |
| if (number.len - i <= 1 || number.data[i + 1] == '_') return false; |
0 |
10014 |
10014 |
|
| 1033 |
200034 |
▶ | } else if ((c == 'e' || c == 'E') && state != exponent && state != after_decimal_nodigit) { |
Branch: 4 branches
| Branch | True | False | Total |
| } else if ((c == 'e' || c == 'E') && state != exponent && state != after_decimal_nodigit) { |
190343 |
9691 |
200034 |
| } else if ((c == 'e' || c == 'E') && state != exponent && state != after_decimal_nodigit) { |
0 |
190343 |
190343 |
| } else if ((c == 'e' || c == 'E') && state != exponent && state != after_decimal_nodigit) { |
9691 |
0 |
9691 |
| } else if ((c == 'e' || c == 'E') && state != exponent && state != after_decimal_nodigit) { |
9691 |
0 |
9691 |
|
| 1034 |
9691 |
| state = exponent_nodigit; |
| 1035 |
9691 |
▶ | if (i + 1 < number.len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i + 1 < number.len) { |
9691 |
0 |
9691 |
|
| 1036 |
|
| // handle exponent sign |
| 1037 |
9691 |
| switch (number.data[i + 1]) { |
| 1038 |
4907 |
| case '-': |
| 1039 |
4907 |
| exponent_negative = true; |
| 1040 |
|
| _fallthrough_; |
| 1041 |
9687 |
| case '+': |
| 1042 |
9687 |
| ++i; |
| 1043 |
|
| } |
| 1044 |
9691 |
▶ | if (number.len - i <= 1 || number.data[i + 1] == '_') return false; |
Branch: 2 branches
| Branch | True | False | Total |
| if (number.len - i <= 1 || number.data[i + 1] == '_') return false; |
9691 |
0 |
9691 |
| if (number.len - i <= 1 || number.data[i + 1] == '_') return false; |
0 |
9691 |
9691 |
|
| 1045 |
|
| } |
| 1046 |
380686 |
▶ | } else if (c >= '0' && c <= '9') { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (c >= '0' && c <= '9') { |
190343 |
0 |
190343 |
| } else if (c >= '0' && c <= '9') { |
190343 |
0 |
190343 |
|
| 1047 |
|
| // digit! |
| 1048 |
190343 |
| int digit = c - '0'; |
| 1049 |
190343 |
▶ | if (state == exponent || state == exponent_nodigit) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (state == exponent || state == exponent_nodigit) { |
173884 |
16459 |
190343 |
| if (state == exponent || state == exponent_nodigit) { |
9691 |
164193 |
173884 |
|
| 1050 |
26150 |
| state = exponent; |
| 1051 |
|
| // stop parsing beyond rough heuristic, avoids overflow |
| 1052 |
26150 |
▶ | if (explicit_exponent < 285) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (explicit_exponent < 285) { |
26150 |
0 |
26150 |
|
| 1053 |
26150 |
| explicit_exponent = explicit_exponent * 10 + digit; |
| 1054 |
|
| } |
| 1055 |
|
| } else { |
| 1056 |
164193 |
| decimal_mantissa = decimal_mantissa * 10 + digit; |
| 1057 |
164193 |
▶ | if (state == before_decimal) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (state == before_decimal) { |
12009 |
152184 |
164193 |
|
| 1058 |
12009 |
| ++digits_before_decimal; |
| 1059 |
|
| } else { |
| 1060 |
152184 |
| state = after_decimal; |
| 1061 |
152184 |
| ++digits_after_decimal; |
| 1062 |
|
| } |
| 1063 |
|
| } |
| 1064 |
0 |
▶ | } else if (c == '_') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (c == '_') { |
0 |
0 |
0 |
|
| 1065 |
|
| // underscores are allowed |
| 1066 |
|
| } else { |
| 1067 |
|
| // invalid |
| 1068 |
0 |
| return false; |
| 1069 |
|
| } |
| 1070 |
|
| } |
| 1071 |
|
| |
| 1072 |
10019 |
▶ | if (state == after_decimal_nodigit || state == exponent_nodigit) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (state == after_decimal_nodigit || state == exponent_nodigit) { |
10019 |
0 |
10019 |
| if (state == after_decimal_nodigit || state == exponent_nodigit) { |
0 |
10019 |
10019 |
|
| 1073 |
|
| // invalid |
| 1074 |
0 |
| return false; |
| 1075 |
|
| } |
| 1076 |
|
| |
| 1077 |
|
| // rough heuristic for numbers that fit into a double exactly |
| 1078 |
10019 |
▶ | if (digits_before_decimal + digits_after_decimal <= 15 && explicit_exponent < 285 |
Branch: 2 branches
| Branch | True | False | Total |
| if (digits_before_decimal + digits_after_decimal <= 15 && explicit_exponent < 285 |
627 |
9392 |
10019 |
| if (digits_before_decimal + digits_after_decimal <= 15 && explicit_exponent < 285 |
565 |
62 |
627 |
|
| 1079 |
565 |
▶ | && explicit_exponent > -285) { |
Branch: 1 branch
| Branch | True | False | Total |
| && explicit_exponent > -285) { |
565 |
0 |
565 |
|
| 1080 |
|
| // "strip zeros" |
| 1081 |
567 |
▶ | while (decimal_mantissa % 10 == 0 && digits_after_decimal > 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (decimal_mantissa % 10 == 0 && digits_after_decimal > 0) { |
3 |
564 |
567 |
| while (decimal_mantissa % 10 == 0 && digits_after_decimal > 0) { |
2 |
1 |
3 |
|
| 1082 |
2 |
| decimal_mantissa /= 10; |
| 1083 |
2 |
| --digits_after_decimal; |
| 1084 |
|
| } |
| 1085 |
565 |
| double n = (double)decimal_mantissa; |
| 1086 |
565 |
▶ | if (negative) n = -n; |
Branch: 1 branch
| Branch | True | False | Total |
| if (negative) n = -n; |
272 |
293 |
565 |
|
| 1087 |
565 |
▶ | if (exponent_negative) explicit_exponent = -explicit_exponent; |
Branch: 1 branch
| Branch | True | False | Total |
| if (exponent_negative) explicit_exponent = -explicit_exponent; |
249 |
316 |
565 |
|
| 1088 |
565 |
| int net_exponent = explicit_exponent - digits_after_decimal; |
| 1089 |
565 |
▶ | if (net_exponent < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (net_exponent < 0) { |
263 |
302 |
565 |
|
| 1090 |
263 |
| n /= pow(10.0, -net_exponent); |
| 1091 |
|
| } |
| 1092 |
565 |
▶ | if (net_exponent > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (net_exponent > 0) { |
300 |
265 |
565 |
|
| 1093 |
300 |
| n *= pow(10.0, net_exponent); |
| 1094 |
|
| } |
| 1095 |
|
| |
| 1096 |
565 |
| val->type = KDL_TYPE_NUMBER; |
| 1097 |
565 |
| val->number.type = KDL_NUMBER_TYPE_FLOATING_POINT; |
| 1098 |
565 |
| val->number.floating_point = n; |
| 1099 |
565 |
| return true; |
| 1100 |
|
| } else { |
| 1101 |
|
| // Remove all underscores and the initial plus |
| 1102 |
9454 |
| *s = kdl_clone_str(&number); |
| 1103 |
9454 |
| char const* p1 = number.data; |
| 1104 |
9454 |
| char const* end = number.data + number.len; |
| 1105 |
9454 |
| char* p2 = s->data; |
| 1106 |
9454 |
| s->len = 0; |
| 1107 |
9454 |
▶ | if (p1 != end && *p1 == '+') ++p1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (p1 != end && *p1 == '+') ++p1; |
9454 |
0 |
9454 |
| if (p1 != end && *p1 == '+') ++p1; |
0 |
9454 |
9454 |
|
| 1108 |
222529 |
▶ | while (p1 != end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p1 != end) { |
213075 |
9454 |
222529 |
|
| 1109 |
213075 |
| int c = *(p1++); |
| 1110 |
213075 |
▶ | if (c != '_') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c != '_') { |
213075 |
0 |
213075 |
|
| 1111 |
|
| // copy character |
| 1112 |
213075 |
| *(p2++) = (char)c; |
| 1113 |
213075 |
| ++s->len; |
| 1114 |
|
| } |
| 1115 |
|
| } |
| 1116 |
9454 |
| *p2 = 0; |
| 1117 |
|
| |
| 1118 |
9454 |
| val->type = KDL_TYPE_NUMBER; |
| 1119 |
9454 |
| val->number.type = KDL_NUMBER_TYPE_STRING_ENCODED; |
| 1120 |
9454 |
| val->number.string = kdl_borrow_str(s); |
| 1121 |
9454 |
| return true; |
| 1122 |
|
| } |
| 1123 |
|
| } |
| 1124 |
|
| |
| 1125 |
1 |
| static bool _parse_hex_number(kdl_str number, kdl_value* val, kdl_owned_string* s) |
| 1126 |
|
| { |
| 1127 |
1 |
| bool negative = false; |
| 1128 |
1 |
| _kdl_ubigint* n = _kdl_ubigint_new(0); |
| 1129 |
1 |
▶ | if (n == NULL) return false; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == NULL) return false; |
0 |
1 |
1 |
|
| 1130 |
|
| |
| 1131 |
1 |
| size_t i = 0; // index into number-string |
| 1132 |
|
| |
| 1133 |
|
| // handle sign |
| 1134 |
1 |
▶ | if (number.len > 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (number.len > 1) { |
1 |
0 |
1 |
|
| 1135 |
1 |
| switch (number.data[0]) { |
| 1136 |
0 |
| case '-': |
| 1137 |
0 |
| negative = true; |
| 1138 |
|
| _fallthrough_; |
| 1139 |
0 |
| case '+': |
| 1140 |
0 |
| i = 1; |
| 1141 |
|
| } |
| 1142 |
|
| } |
| 1143 |
|
| |
| 1144 |
1 |
▶ | if (number.len - i < 3 || number.data[i] != '0' || number.data[i + 1] != 'x' || number.data[i + 2] == '_') |
Branch: 4 branches
| Branch | True | False | Total |
| if (number.len - i < 3 || number.data[i] != '0' || number.data[i + 1] != 'x' || number.data[i + 2] == '_') |
1 |
0 |
1 |
| if (number.len - i < 3 || number.data[i] != '0' || number.data[i + 1] != 'x' || number.data[i + 2] == '_') |
1 |
0 |
1 |
| if (number.len - i < 3 || number.data[i] != '0' || number.data[i + 1] != 'x' || number.data[i + 2] == '_') |
1 |
0 |
1 |
| if (number.len - i < 3 || number.data[i] != '0' || number.data[i + 1] != 'x' || number.data[i + 2] == '_') |
0 |
1 |
1 |
|
| 1145 |
0 |
| goto error; |
| 1146 |
1 |
| i += 2; |
| 1147 |
|
| |
| 1148 |
|
| // scan through entire number |
| 1149 |
9 |
▶ | for (; i < number.len; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; i < number.len; ++i) { |
8 |
1 |
9 |
|
| 1150 |
8 |
| char c = number.data[i]; |
| 1151 |
|
| int digit; |
| 1152 |
|
| |
| 1153 |
8 |
▶ | if (c == '_') continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '_') continue; |
0 |
8 |
8 |
|
| 1154 |
8 |
▶ | else if (c >= '0' && c <= '9') digit = c - '0'; |
Branch: 2 branches
| Branch | True | False | Total |
| else if (c >= '0' && c <= '9') digit = c - '0'; |
8 |
0 |
8 |
| else if (c >= '0' && c <= '9') digit = c - '0'; |
0 |
8 |
8 |
|
| 1155 |
8 |
▶ | else if (c >= 'a' && c <= 'f') digit = c - 'a' + 0xa; |
Branch: 2 branches
| Branch | True | False | Total |
| else if (c >= 'a' && c <= 'f') digit = c - 'a' + 0xa; |
0 |
8 |
8 |
| else if (c >= 'a' && c <= 'f') digit = c - 'a' + 0xa; |
0 |
0 |
0 |
|
| 1156 |
8 |
▶ | else if (c >= 'A' && c <= 'F') digit = c - 'A' + 0xa; |
Branch: 2 branches
| Branch | True | False | Total |
| else if (c >= 'A' && c <= 'F') digit = c - 'A' + 0xa; |
8 |
0 |
8 |
| else if (c >= 'A' && c <= 'F') digit = c - 'A' + 0xa; |
8 |
0 |
8 |
|
| 1157 |
0 |
| else goto error; |
| 1158 |
|
| |
| 1159 |
8 |
| n = _kdl_ubigint_multiply_inplace(n, 16); |
| 1160 |
8 |
| n = _kdl_ubigint_add_inplace(n, digit); |
| 1161 |
|
| } |
| 1162 |
|
| |
| 1163 |
|
| // try to reduce down to long long |
| 1164 |
|
| long long n_ll; |
| 1165 |
1 |
▶ | if (_kdl_ubigint_as_long_long(n, &n_ll)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (_kdl_ubigint_as_long_long(n, &n_ll)) { |
0 |
1 |
1 |
|
| 1166 |
|
| // number is representable as long long |
| 1167 |
0 |
▶ | if (negative) n_ll = -n_ll; |
Branch: 1 branch
| Branch | True | False | Total |
| if (negative) n_ll = -n_ll; |
0 |
0 |
0 |
|
| 1168 |
0 |
| val->type = KDL_TYPE_NUMBER; |
| 1169 |
0 |
| val->number.type = KDL_NUMBER_TYPE_INTEGER; |
| 1170 |
0 |
| val->number.integer = n_ll; |
| 1171 |
|
| } else { |
| 1172 |
|
| // represent number as string |
| 1173 |
1 |
▶ | *s = _kdl_ubigint_as_string_sgn(negative ? -1 : +1, n); |
Branch: 1 branch
| Branch | True | False | Total |
| *s = _kdl_ubigint_as_string_sgn(negative ? -1 : +1, n); |
0 |
1 |
1 |
|
| 1174 |
1 |
| val->type = KDL_TYPE_NUMBER; |
| 1175 |
1 |
| val->number.type = KDL_NUMBER_TYPE_STRING_ENCODED; |
| 1176 |
1 |
| val->number.string = kdl_borrow_str(s); |
| 1177 |
|
| } |
| 1178 |
1 |
| _kdl_ubigint_free(n); |
| 1179 |
1 |
| return true; |
| 1180 |
0 |
| error: |
| 1181 |
0 |
| _kdl_ubigint_free(n); |
| 1182 |
0 |
| return false; |
| 1183 |
|
| } |
| 1184 |
|
| |
| 1185 |
0 |
| static bool _parse_octal_number(kdl_str number, kdl_value* val, kdl_owned_string* s) |
| 1186 |
|
| { |
| 1187 |
0 |
| bool negative = false; |
| 1188 |
0 |
| _kdl_ubigint* n = _kdl_ubigint_new(0); |
| 1189 |
0 |
▶ | if (n == NULL) return false; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == NULL) return false; |
0 |
0 |
0 |
|
| 1190 |
|
| |
| 1191 |
0 |
| size_t i = 0; // index into number-string |
| 1192 |
|
| |
| 1193 |
|
| // handle sign |
| 1194 |
0 |
▶ | if (number.len > 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (number.len > 1) { |
0 |
0 |
0 |
|
| 1195 |
0 |
| switch (number.data[0]) { |
| 1196 |
0 |
| case '-': |
| 1197 |
0 |
| negative = true; |
| 1198 |
|
| _fallthrough_; |
| 1199 |
0 |
| case '+': |
| 1200 |
0 |
| i = 1; |
| 1201 |
|
| } |
| 1202 |
|
| } |
| 1203 |
|
| |
| 1204 |
0 |
▶ | if (number.len - i < 3 || number.data[i] != '0' || number.data[i + 1] != 'o' || number.data[i + 2] == '_') |
Branch: 4 branches
| Branch | True | False | Total |
| if (number.len - i < 3 || number.data[i] != '0' || number.data[i + 1] != 'o' || number.data[i + 2] == '_') |
0 |
0 |
0 |
| if (number.len - i < 3 || number.data[i] != '0' || number.data[i + 1] != 'o' || number.data[i + 2] == '_') |
0 |
0 |
0 |
| if (number.len - i < 3 || number.data[i] != '0' || number.data[i + 1] != 'o' || number.data[i + 2] == '_') |
0 |
0 |
0 |
| if (number.len - i < 3 || number.data[i] != '0' || number.data[i + 1] != 'o' || number.data[i + 2] == '_') |
0 |
0 |
0 |
|
| 1205 |
0 |
| goto error; |
| 1206 |
0 |
| i += 2; |
| 1207 |
|
| |
| 1208 |
|
| // scan through entire number |
| 1209 |
0 |
▶ | for (; i < number.len; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; i < number.len; ++i) { |
0 |
0 |
0 |
|
| 1210 |
0 |
| char c = number.data[i]; |
| 1211 |
|
| |
| 1212 |
0 |
▶ | if (c == '_') continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '_') continue; |
0 |
0 |
0 |
|
| 1213 |
0 |
▶ | else if (c < '0' || c > '7') goto error; |
Branch: 2 branches
| Branch | True | False | Total |
| else if (c < '0' || c > '7') goto error; |
0 |
0 |
0 |
| else if (c < '0' || c > '7') goto error; |
0 |
0 |
0 |
|
| 1214 |
|
| |
| 1215 |
0 |
| int digit = c - '0'; |
| 1216 |
0 |
| n = _kdl_ubigint_multiply_inplace(n, 8); |
| 1217 |
0 |
| n = _kdl_ubigint_add_inplace(n, digit); |
| 1218 |
|
| } |
| 1219 |
|
| |
| 1220 |
|
| // try to reduce down to long long |
| 1221 |
|
| long long n_ll; |
| 1222 |
0 |
▶ | if (_kdl_ubigint_as_long_long(n, &n_ll)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (_kdl_ubigint_as_long_long(n, &n_ll)) { |
0 |
0 |
0 |
|
| 1223 |
|
| // number is representable as long long |
| 1224 |
0 |
▶ | if (negative) n_ll = -n_ll; |
Branch: 1 branch
| Branch | True | False | Total |
| if (negative) n_ll = -n_ll; |
0 |
0 |
0 |
|
| 1225 |
0 |
| val->type = KDL_TYPE_NUMBER; |
| 1226 |
0 |
| val->number.type = KDL_NUMBER_TYPE_INTEGER; |
| 1227 |
0 |
| val->number.integer = n_ll; |
| 1228 |
|
| } else { |
| 1229 |
|
| // represent number as string |
| 1230 |
0 |
▶ | *s = _kdl_ubigint_as_string_sgn(negative ? -1 : +1, n); |
Branch: 1 branch
| Branch | True | False | Total |
| *s = _kdl_ubigint_as_string_sgn(negative ? -1 : +1, n); |
0 |
0 |
0 |
|
| 1231 |
0 |
| val->type = KDL_TYPE_NUMBER; |
| 1232 |
0 |
| val->number.type = KDL_NUMBER_TYPE_STRING_ENCODED; |
| 1233 |
0 |
| val->number.string = kdl_borrow_str(s); |
| 1234 |
|
| } |
| 1235 |
0 |
| _kdl_ubigint_free(n); |
| 1236 |
0 |
| return true; |
| 1237 |
0 |
| error: |
| 1238 |
0 |
| _kdl_ubigint_free(n); |
| 1239 |
0 |
| return false; |
| 1240 |
|
| } |
| 1241 |
|
| |
| 1242 |
0 |
| static bool _parse_binary_number(kdl_str number, kdl_value* val, kdl_owned_string* s) |
| 1243 |
|
| { |
| 1244 |
0 |
| bool negative = false; |
| 1245 |
0 |
| _kdl_ubigint* n = _kdl_ubigint_new(0); |
| 1246 |
0 |
▶ | if (n == NULL) return false; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == NULL) return false; |
0 |
0 |
0 |
|
| 1247 |
|
| |
| 1248 |
0 |
| size_t i = 0; // index into number-string |
| 1249 |
|
| |
| 1250 |
|
| // handle sign |
| 1251 |
0 |
▶ | if (number.len > 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (number.len > 1) { |
0 |
0 |
0 |
|
| 1252 |
0 |
| switch (number.data[0]) { |
| 1253 |
0 |
| case '-': |
| 1254 |
0 |
| negative = true; |
| 1255 |
|
| _fallthrough_; |
| 1256 |
0 |
| case '+': |
| 1257 |
0 |
| i = 1; |
| 1258 |
|
| } |
| 1259 |
|
| } |
| 1260 |
|
| |
| 1261 |
0 |
▶ | if (number.len - i < 3 || number.data[i] != '0' || number.data[i + 1] != 'b' || number.data[i + 2] == '_') |
Branch: 4 branches
| Branch | True | False | Total |
| if (number.len - i < 3 || number.data[i] != '0' || number.data[i + 1] != 'b' || number.data[i + 2] == '_') |
0 |
0 |
0 |
| if (number.len - i < 3 || number.data[i] != '0' || number.data[i + 1] != 'b' || number.data[i + 2] == '_') |
0 |
0 |
0 |
| if (number.len - i < 3 || number.data[i] != '0' || number.data[i + 1] != 'b' || number.data[i + 2] == '_') |
0 |
0 |
0 |
| if (number.len - i < 3 || number.data[i] != '0' || number.data[i + 1] != 'b' || number.data[i + 2] == '_') |
0 |
0 |
0 |
|
| 1262 |
0 |
| goto error; |
| 1263 |
0 |
| i += 2; |
| 1264 |
|
| |
| 1265 |
|
| // scan through entire number |
| 1266 |
0 |
▶ | for (; i < number.len; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; i < number.len; ++i) { |
0 |
0 |
0 |
|
| 1267 |
0 |
| char c = number.data[i]; |
| 1268 |
|
| |
| 1269 |
0 |
▶ | if (c == '_') continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '_') continue; |
0 |
0 |
0 |
|
| 1270 |
0 |
▶ | else if (c < '0' || c > '1') goto error; |
Branch: 2 branches
| Branch | True | False | Total |
| else if (c < '0' || c > '1') goto error; |
0 |
0 |
0 |
| else if (c < '0' || c > '1') goto error; |
0 |
0 |
0 |
|
| 1271 |
|
| |
| 1272 |
0 |
| int digit = c - '0'; |
| 1273 |
0 |
| n = _kdl_ubigint_multiply_inplace(n, 2); |
| 1274 |
0 |
| n = _kdl_ubigint_add_inplace(n, digit); |
| 1275 |
|
| } |
| 1276 |
|
| |
| 1277 |
|
| // try to reduce down to long long |
| 1278 |
|
| long long n_ll; |
| 1279 |
0 |
▶ | if (_kdl_ubigint_as_long_long(n, &n_ll)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (_kdl_ubigint_as_long_long(n, &n_ll)) { |
0 |
0 |
0 |
|
| 1280 |
|
| // number is representable as long long |
| 1281 |
0 |
▶ | if (negative) n_ll = -n_ll; |
Branch: 1 branch
| Branch | True | False | Total |
| if (negative) n_ll = -n_ll; |
0 |
0 |
0 |
|
| 1282 |
0 |
| val->type = KDL_TYPE_NUMBER; |
| 1283 |
0 |
| val->number.type = KDL_NUMBER_TYPE_INTEGER; |
| 1284 |
0 |
| val->number.integer = n_ll; |
| 1285 |
|
| } else { |
| 1286 |
|
| // represent number as string |
| 1287 |
0 |
▶ | *s = _kdl_ubigint_as_string_sgn(negative ? -1 : +1, n); |
Branch: 1 branch
| Branch | True | False | Total |
| *s = _kdl_ubigint_as_string_sgn(negative ? -1 : +1, n); |
0 |
0 |
0 |
|
| 1288 |
0 |
| val->type = KDL_TYPE_NUMBER; |
| 1289 |
0 |
| val->number.type = KDL_NUMBER_TYPE_STRING_ENCODED; |
| 1290 |
0 |
| val->number.string = kdl_borrow_str(s); |
| 1291 |
|
| } |
| 1292 |
0 |
| _kdl_ubigint_free(n); |
| 1293 |
0 |
| return true; |
| 1294 |
0 |
| error: |
| 1295 |
0 |
| _kdl_ubigint_free(n); |
| 1296 |
0 |
| return false; |
| 1297 |
|
| } |
| 1298 |
|
| |
| 1299 |
5729 |
| static bool _identifier_is_valid_v1(kdl_str value) |
| 1300 |
|
| { |
| 1301 |
5729 |
| uint32_t c = 0; |
| 1302 |
5729 |
▶ | if (_kdl_pop_codepoint(&value, &c) != KDL_UTF8_OK || !_kdl_is_id_start(KDL_CHARACTER_SET_V1, c)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (_kdl_pop_codepoint(&value, &c) != KDL_UTF8_OK || !_kdl_is_id_start(KDL_CHARACTER_SET_V1, c)) { |
5729 |
0 |
5729 |
| if (_kdl_pop_codepoint(&value, &c) != KDL_UTF8_OK || !_kdl_is_id_start(KDL_CHARACTER_SET_V1, c)) { |
5729 |
0 |
5729 |
|
| 1303 |
0 |
| return false; |
| 1304 |
|
| } |
| 1305 |
|
| |
| 1306 |
|
| while (true) { |
| 1307 |
12867 |
| switch (_kdl_pop_codepoint(&value, &c)) { |
| 1308 |
7138 |
| case KDL_UTF8_OK: |
| 1309 |
7138 |
▶ | if (!_kdl_is_id(KDL_CHARACTER_SET_V1, c)) return false; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!_kdl_is_id(KDL_CHARACTER_SET_V1, c)) return false; |
0 |
7138 |
7138 |
|
| 1310 |
7138 |
| break; |
| 1311 |
5729 |
| case KDL_UTF8_EOF: |
| 1312 |
5729 |
| return true; |
| 1313 |
0 |
| default: |
| 1314 |
0 |
| return false; |
| 1315 |
|
| } |
| 1316 |
|
| } |
| 1317 |
|
| } |
| 1318 |
|
| |
| 1319 |
5764 |
| static bool _identifier_is_valid_v2(kdl_str value) |
| 1320 |
|
| { |
| 1321 |
5764 |
▶ | if (_str_equals_literal(value, "inf") || _str_equals_literal(value, "-inf") |
Branch: 4 branches
| Branch | True | False | Total |
| if (_str_equals_literal(value, "inf") || _str_equals_literal(value, "-inf") |
115 |
5649 |
5764 |
| if (_str_equals_literal(value, "inf") || _str_equals_literal(value, "-inf") |
115 |
0 |
115 |
| if (_str_equals_literal(value, "inf") || _str_equals_literal(value, "-inf") |
915 |
4849 |
5764 |
| if (_str_equals_literal(value, "inf") || _str_equals_literal(value, "-inf") |
915 |
0 |
915 |
|
| 1322 |
5764 |
▶ | || _str_equals_literal(value, "nan") || _str_equals_literal(value, "null") |
Branch: 4 branches
| Branch | True | False | Total |
| || _str_equals_literal(value, "nan") || _str_equals_literal(value, "null") |
115 |
5649 |
5764 |
| || _str_equals_literal(value, "nan") || _str_equals_literal(value, "null") |
115 |
0 |
115 |
| || _str_equals_literal(value, "nan") || _str_equals_literal(value, "null") |
915 |
4849 |
5764 |
| || _str_equals_literal(value, "nan") || _str_equals_literal(value, "null") |
915 |
0 |
915 |
|
| 1323 |
5764 |
▶ | || _str_equals_literal(value, "true") || _str_equals_literal(value, "false")) { |
Branch: 4 branches
| Branch | True | False | Total |
| || _str_equals_literal(value, "true") || _str_equals_literal(value, "false")) { |
915 |
4849 |
5764 |
| || _str_equals_literal(value, "true") || _str_equals_literal(value, "false")) { |
915 |
0 |
915 |
| || _str_equals_literal(value, "true") || _str_equals_literal(value, "false")) { |
1022 |
4742 |
5764 |
| || _str_equals_literal(value, "true") || _str_equals_literal(value, "false")) { |
0 |
1022 |
1022 |
|
| 1324 |
0 |
| return false; |
| 1325 |
|
| } |
| 1326 |
|
| |
| 1327 |
5764 |
| uint32_t c = 0; |
| 1328 |
5764 |
▶ | if (_kdl_pop_codepoint(&value, &c) != KDL_UTF8_OK || !_kdl_is_id_start(KDL_CHARACTER_SET_V2, c)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (_kdl_pop_codepoint(&value, &c) != KDL_UTF8_OK || !_kdl_is_id_start(KDL_CHARACTER_SET_V2, c)) { |
5764 |
0 |
5764 |
| if (_kdl_pop_codepoint(&value, &c) != KDL_UTF8_OK || !_kdl_is_id_start(KDL_CHARACTER_SET_V2, c)) { |
0 |
5764 |
5764 |
|
| 1329 |
0 |
| return false; |
| 1330 |
|
| } |
| 1331 |
|
| |
| 1332 |
|
| while (true) { |
| 1333 |
13016 |
| switch (_kdl_pop_codepoint(&value, &c)) { |
| 1334 |
7252 |
| case KDL_UTF8_OK: |
| 1335 |
7252 |
▶ | if (!_kdl_is_id(KDL_CHARACTER_SET_V2, c)) return false; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!_kdl_is_id(KDL_CHARACTER_SET_V2, c)) return false; |
0 |
7252 |
7252 |
|
| 1336 |
7252 |
| break; |
| 1337 |
5764 |
| case KDL_UTF8_EOF: |
| 1338 |
5764 |
| return true; |
| 1339 |
0 |
| default: |
| 1340 |
0 |
| return false; |
| 1341 |
|
| } |
| 1342 |
|
| } |
| 1343 |
|
| } |