| 16318 | } |
| 16319 | |
| 16320 | tl::expected<std::vector<token>, errors> tokenize(std::string_view input, |
| 16321 | token_policy policy) { |
| 16322 | ada_log("tokenize input: ", input); |
| 16323 | // Let tokenizer be a new tokenizer. |
| 16324 | // Set tokenizer's input to input. |
| 16325 | // Set tokenizer's policy to policy. |
| 16326 | auto tokenizer = Tokenizer(input, policy); |
| 16327 | // While tokenizer's index is less than tokenizer's input's code point length: |
| 16328 | while (tokenizer.index < tokenizer.input.size()) { |
| 16329 | // Run seek and get the next code point given tokenizer and tokenizer's |
| 16330 | // index. |
| 16331 | tokenizer.seek_and_get_next_code_point(tokenizer.index); |
| 16332 | |
| 16333 | // If tokenizer's code point is U+002A (*): |
| 16334 | if (tokenizer.code_point == '*') { |
| 16335 | // Run add a token with default position and length given tokenizer and |
| 16336 | // "asterisk". |
| 16337 | tokenizer.add_token_with_defaults(token_type::ASTERISK); |
| 16338 | ada_log("add ASTERISK token"); |
| 16339 | // Continue. |
| 16340 | continue; |
| 16341 | } |
| 16342 | |
| 16343 | // If tokenizer's code point is U+002B (+) or U+003F (?): |
| 16344 | if (tokenizer.code_point == '+' || tokenizer.code_point == '?') { |
| 16345 | // Run add a token with default position and length given tokenizer and |
| 16346 | // "other-modifier". |
| 16347 | tokenizer.add_token_with_defaults(token_type::OTHER_MODIFIER); |
| 16348 | // Continue. |
| 16349 | continue; |
| 16350 | } |
| 16351 | |
| 16352 | // If tokenizer's code point is U+005C (\): |
| 16353 | if (tokenizer.code_point == '\\') { |
| 16354 | // If tokenizer's index is equal to tokenizer's input's code point length |
| 16355 | // - 1: |
| 16356 | if (tokenizer.index == tokenizer.input.size() - 1) { |
| 16357 | // Run process a tokenizing error given tokenizer, tokenizer's next |
| 16358 | // index, and tokenizer's index. |
| 16359 | if (auto error = tokenizer.process_tokenizing_error( |
| 16360 | tokenizer.next_index, tokenizer.index)) { |
| 16361 | ada_log("process_tokenizing_error failed"); |
| 16362 | return tl::unexpected(*error); |
| 16363 | } |
| 16364 | continue; |
| 16365 | } |
| 16366 | |
| 16367 | // Let escaped index be tokenizer's next index. |
| 16368 | auto escaped_index = tokenizer.next_index; |
| 16369 | // Run get the next code point given tokenizer. |
| 16370 | tokenizer.get_next_code_point(); |
| 16371 | // Run add a token with default length given tokenizer, "escaped-char", |
| 16372 | // tokenizer's next index, and escaped index. |
| 16373 | tokenizer.add_token_with_default_length( |
| 16374 | token_type::ESCAPED_CHAR, tokenizer.next_index, escaped_index); |
| 16375 | ada_log("add ESCAPED_CHAR token on next_index ", tokenizer.next_index, |
| 16376 | " with escaped index ", escaped_index); |
| 16377 | // Continue. |
no test coverage detected