189 explicit Lexer(std::string_view source,
190 bool trusted_debug_provenance =
false)
192 , trusted_debug_provenance_(trusted_debug_provenance)
208 pending_debug_provenance_ =
false;
209 if(pending_comment_error_) {
210 pending_comment_error_ =
false;
220 const std::size_t start = pos_;
221 const char c = peek();
223 return lex_word(token, start);
226 return lex_number(token, start);
228 if(c ==
'\'' || c ==
'"') {
229 return lex_string(token, start, c);
232 return lex_located_address(token, start);
234 return lex_punct(token, start);
240 return pos_ >= source_.size();
243 char peek(std::size_t ahead = 0)
const
245 const std::size_t index = pos_ + ahead;
246 return index < source_.size() ? source_[index] :
'\0';
254 if(source_[pos_] ==
'\n') {
266 const char c = peek();
267 if(c ==
' ' || c ==
'\t' || c ==
'\r' || c ==
'\n') {
271 if(c ==
'/' && peek(1) ==
'/') {
272 while(!at_end() && peek() !=
'\n') {
277 if(c ==
'(' && peek(1) ==
'*') {
278 if(trusted_debug_provenance_ && peek(2) ==
'@' &&
279 peek(3) ==
'D' && peek(4) ==
'B' &&
280 peek(5) ==
'G' && peek(6) ==
' ') {
281 parse_debug_directive();
287 while(!at_end() && depth > 0) {
288 if(peek() ==
'(' && peek(1) ==
'*') {
292 }
else if(peek() ==
'*' && peek(1) ==
')') {
301 pending_comment_error_ =
true;
310 void parse_debug_directive()
312 for(
unsigned i = 0; i < 7; ++i) advance();
313 const auto field = [&]() {
314 while(!at_end() && peek() ==
' ') advance();
315 const std::size_t begin = pos_;
316 while(!at_end() && peek() !=
' ' &&
317 !(peek() ==
'*' && peek(1) ==
')'))
319 return source_.substr(begin, pos_ - begin);
321 const auto number = [&](std::string_view text) {
322 std::uint32_t value = 0;
324 if(c <
'0' || c >
'9')
return std::uint32_t{0};
325 value = value * 10U +
static_cast<std::uint32_t
>(c -
'0');
329 pending_debug_pou_ =
field();
330 pending_debug_call_path_ =
field();
331 pending_debug_line_ =
static_cast<std::int32_t
>(number(
field()));
332 pending_debug_column_ =
static_cast<std::int32_t
>(number(
field()));
333 pending_debug_depth_ =
static_cast<std::uint16_t
>(number(
field()));
334 while(!at_end() && !(peek() ==
'*' && peek(1) ==
')')) advance();
335 if(!at_end()) { advance(); advance(); }
336 pending_debug_provenance_ = !pending_debug_pou_.empty() &&
337 pending_debug_line_ > 0 &&
338 pending_debug_column_ > 0 &&
339 pending_debug_depth_ > 0;
342 Token make_error(Token token,
DiagCode code, std::size_t start)
345 token.diag_payload =
static_cast<std::uint16_t
>(code);
346 token.text = source_.substr(start, pos_ - start);
351 Token lex_word(Token token, std::size_t start)
353 bool bad_underscore =
false;
354 char previous =
'\0';
356 if(peek() ==
'_' && previous ==
'_') {
357 bad_underscore =
true;
362 if(previous ==
'_') {
363 bad_underscore =
true;
365 token.text = source_.substr(start, pos_ - start);
381 return lex_date_time(token, start, literal);
390 return lex_time_literal(token, start);
395 static constexpr struct
397 std::string_view lower;
399 } kTypedPrefixes[] = {
409 for(
const auto &prefix : kTypedPrefixes) {
412 return lex_typed_literal(token, start, prefix.type);
419 token.unsigned_value = 1;
424 token.unsigned_value = 0;
429 token.kind = entry.kind;
436 token.diag_payload =
static_cast<std::uint16_t
>(entry.code);
447 Token lex_located_address(Token token, std::size_t start)
451 if(area !=
'i' && area !=
'q' && area !=
'm') {
456 if(width !=
'x' && width !=
'b' && width !=
'w' &&
457 width !=
'd' && width !=
'l') {
465 if(width ==
'x' && peek() ==
'.') {
474 token.text = source_.substr(start, pos_ - start);
480 bool read_digits(std::uint64_t
base, std::uint64_t &value,
bool &any,
486 const char c = peek();
491 std::uint64_t digit = 0;
493 digit =
static_cast<std::uint64_t
>(c -
'0');
496 if(lower <
'a' || lower >
'f') {
499 digit =
static_cast<std::uint64_t
>(lower) -
500 static_cast<std::uint64_t
>(
'a') + 10;
507 if(value > (~
static_cast<std::uint64_t
>(0) - digit) /
base) {
510 value = value *
base + digit;
517 Token lex_number(Token token, std::size_t start)
519 std::uint64_t value = 0;
521 bool overflow =
false;
522 if(!read_digits(10, value, any, overflow)) {
528 if(value != 2 && value != 8 && value != 16) {
533 const std::uint64_t
base = value;
535 if(!read_digits(
base, value, any, overflow) || !any) {
543 token.unsigned_value = value;
545 token.text = source_.substr(start, pos_ - start);
550 const bool has_fraction = peek() ==
'.' && peek(1) !=
'.';
551 bool is_real = has_fraction;
554 std::uint64_t ignored = 0;
555 bool frac_any =
false;
556 bool frac_overflow =
false;
557 if(!read_digits(10, ignored, frac_any, frac_overflow) || !frac_any) {
561 if(peek() ==
'e' || peek() ==
'E') {
562 const char sign = peek(1);
563 const std::size_t digit_at = (sign ==
'+' || sign ==
'-') ? 2 : 1;
567 if(sign ==
'+' || sign ==
'-') {
570 std::uint64_t ignored = 0;
571 bool exp_any =
false;
572 bool exp_overflow =
false;
573 read_digits(10, ignored, exp_any, exp_overflow);
576 token.text = source_.substr(start, pos_ - start);
579 token.real_value = parse_real(token.text);
590 token.unsigned_value = value;
594 void skip_ident_tail()
604 static double parse_real(std::string_view text)
612 if(
n + 1 >=
sizeof(buffer)) {
618 return text_to_double(buffer);
621 static double text_to_double(
const char *text)
623 return std::strtod(text,
nullptr);
626 Token lex_time_literal(Token token, std::size_t start)
632 }
else if(peek() ==
'+') {
638 static constexpr std::int64_t kUnitNs[] = {
647 std::int64_t total = 0;
649 bool any_segment =
false;
650 bool fraction_seen =
false;
651 bool overflow =
false;
663 return time_error(token, start);
665 std::uint64_t whole = 0;
667 bool digit_overflow =
false;
668 read_digits(10, whole, any, digit_overflow);
672 std::uint64_t frac_digits = 0;
673 std::uint64_t frac_scale = 1;
674 if(peek() ==
'.' && peek(1) !=
'.') {
676 fraction_seen =
true;
682 if(frac_scale <= 1000000000000000000ULL / 10ULL) {
683 frac_digits = frac_digits * 10 +
684 static_cast<std::uint64_t
>(peek() -
'0');
690 const int rank = read_time_unit();
691 if(rank < 0 || rank >= last_rank) {
692 return time_error(token, start);
697 const std::int64_t unit = kUnitNs[rank];
698 if(whole >
static_cast<std::uint64_t
>(
699 9223372036854775807LL / (unit > 0 ? unit : 1))) {
702 const std::int64_t segment =
703 static_cast<std::int64_t
>(whole) * unit;
704 if(total > 9223372036854775807LL - segment) {
710 if(fraction_seen && frac_scale > 1) {
713 const double fractional =
714 static_cast<double>(frac_digits) /
715 static_cast<double>(frac_scale);
716 const std::int64_t extra =
static_cast<std::int64_t
>(
717 fractional *
static_cast<double>(unit));
718 if(total > 9223372036854775807LL - extra) {
726 return time_error(token, start);
732 token.signed_value =
negative ? -total : total;
733 token.text = source_.substr(start, pos_ - start);
737 Token lex_typed_literal(Token token, std::size_t start,
Type type)
739 token.literal_type =
type;
741 const std::size_t word_start = pos_;
746 const std::string_view
word =
747 source_.substr(word_start, pos_ - word_start);
750 token.unsigned_value = 1;
753 token.unsigned_value = 0;
758 }
else if(peek() ==
'0' || peek() ==
'1') {
760 token.unsigned_value =
761 static_cast<std::uint64_t
>(peek() -
'0');
767 token.text = source_.substr(start, pos_ - start);
775 }
else if(peek() ==
'+') {
783 number.line = token.line;
784 number.column = token.column;
785 number = lex_number(number, pos_);
787 number.text = source_.substr(start, pos_ - start);
793 negative ? -number.real_value : number.real_value;
794 token.unsigned_value = number.unsigned_value;
795 token.based = number.based;
796 token.signed_value =
negative ? -1 : 0;
797 token.text = source_.substr(start, pos_ - start);
801 Token lex_string(Token token, std::size_t start,
char quote)
804 const std::size_t payload = pos_;
805 while(!at_end() && peek() != quote && peek() !=
'\n' && peek() !=
'\r') {
808 if(at_end() || peek() != quote) {
814 token.text = source_.substr(payload, pos_ - payload);
819 bool read_fixed(
unsigned digits,
unsigned &value)
822 for(
unsigned i = 0; i < digits; ++i) {
826 value = value * 10U +
static_cast<unsigned>(peek() -
'0');
832 static constexpr bool leap_year(
unsigned year)
834 return (year % 4U == 0U && year % 100U != 0U) || year % 400U == 0U;
837 static constexpr unsigned month_days(
unsigned year,
unsigned month)
839 constexpr unsigned days[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
840 return month == 2U && leap_year(year) ? 29U
841 : month <= 12U ? days[month] : 0U;
844 static constexpr std::int64_t civil_days(std::int64_t year,
845 unsigned month,
unsigned day)
847 year -= month <= 2U ? 1 : 0;
848 const std::int64_t era = (year >= 0 ? year : year - 399) / 400;
849 const unsigned yoe =
static_cast<unsigned>(year - era * 400);
850 const unsigned shifted =
static_cast<unsigned>(
851 static_cast<int>(month) + (month > 2U ? -3 : 9));
852 const unsigned doy = (153U * shifted + 2U) / 5U + day - 1U;
853 const unsigned doe = yoe * 365U + yoe / 4U - yoe / 100U + doy;
854 return era * 146097 +
static_cast<std::int64_t
>(doe) - 719468;
857 void consume_date_tail()
860 const char c = peek();
861 if(c ==
';' || c ==
',' || c ==
')' || c ==
']' ||
862 c ==
' ' || c ==
'\t' || c ==
'\r' || c ==
'\n') {
869 Token date_error(Token token, std::size_t start,
DiagCode code)
872 return make_error(token, code, start);
875 Token lex_date_time(Token token, std::size_t start,
TokenKind kind)
877 constexpr std::int64_t kSecondNs = 1000000000LL;
878 constexpr std::int64_t kDayNs = 86400LL * kSecondNs;
885 std::int64_t nanoseconds = 0;
887 if(!read_fixed(4, year) || peek() !=
'-') {
891 if(!read_fixed(2, month) || peek() !=
'-') {
895 if(!read_fixed(2, day) || month < 1U || month > 12U ||
896 day < 1U || day > month_days(year, month)) {
900 token.signed_value = civil_days(year, month, day);
909 if(!read_fixed(2, hour) || peek() !=
':') {
913 if(!read_fixed(2, minute) || peek() !=
':') {
917 if(!read_fixed(2, second) || hour > 23U || minute > 59U ||
921 unsigned fraction_digits = 0;
925 nanoseconds = nanoseconds * 10 + (peek() -
'0');
932 while(fraction_digits++ < 9U) {
936 const std::int64_t
tod =
937 (
static_cast<std::int64_t
>(hour) * 3600LL +
938 static_cast<std::int64_t
>(minute) * 60LL + second) *
939 kSecondNs + nanoseconds;
941 token.signed_value =
tod;
943 const std::int64_t days = civil_days(year, month, day);
945 const std::uint64_t
limit =
static_cast<std::uint64_t
>(
946 std::numeric_limits<std::int64_t>::max() -
tod);
947 if(
static_cast<std::uint64_t
>(days) >
948 limit /
static_cast<std::uint64_t
>(kDayNs)) {
956 const std::uint64_t magnitude =
957 static_cast<std::uint64_t
>(-days);
958 const std::uint64_t min_magnitude =
959 static_cast<std::uint64_t
>(
960 std::numeric_limits<std::int64_t>::max()) + 1U;
961 const std::uint64_t
limit =
962 min_magnitude +
static_cast<std::uint64_t
>(
tod);
964 limit /
static_cast<std::uint64_t
>(kDayNs)) {
969 const std::uint64_t delta =
970 magnitude *
static_cast<std::uint64_t
>(kDayNs) -
971 static_cast<std::uint64_t
>(
tod);
972 token.signed_value = delta == min_magnitude
973 ? std::numeric_limits<std::int64_t>::min()
974 : -static_cast<std::int64_t>(delta);
982 token.text = source_.substr(start, pos_ - start);
986 Token time_error(Token token, std::size_t start)
1004 if(a ==
'm' && b ==
's') {
1007 }
else if(a ==
'u' && b ==
's') {
1010 }
else if(a ==
'n' && b ==
's') {
1013 }
else if(a ==
'd') {
1016 }
else if(a ==
'h') {
1019 }
else if(a ==
'm') {
1022 }
else if(a ==
's') {
1028 const char after = peek(
static_cast<std::size_t
>(length));
1032 for(
int i = 0; i < length; ++i) {
1038 Token lex_punct(Token token, std::size_t start)
1040 const char c = peek();
1083 }
else if(peek() ==
'=') {
1099 token.text = source_.substr(start, pos_ - start);
1101 token.diag_payload =
1105 token.text = source_.substr(start, pos_ - start);
1109 std::string_view source_;
1110 bool trusted_debug_provenance_ =
false;
1111 std::size_t pos_ = 0;
1112 std::int32_t line_ = 1;
1113 std::int32_t column_ = 1;
1114 bool pending_comment_error_ =
false;
1115 bool pending_debug_provenance_ =
false;
1116 std::string_view pending_debug_pou_;
1117 std::string_view pending_debug_call_path_;
1118 std::int32_t pending_debug_line_ = 0;
1119 std::int32_t pending_debug_column_ = 0;
1120 std::uint16_t pending_debug_depth_ = 0;