// File: /workspace/github_cpp_makefile_star2_O2/AgathokakologicalBit[P]wit/src/parsing/lexing.cpp
// Function: akbit::system::parsing::get_next_token(akbit::system::parsing::LexerState&)
parsing::Token get_next_token(parsing::LexerState &state)
  {
    while (std::isspace(state.peek()))
      state.move();

    if (state.is_eof())
    {
      return parsing::Token{
        state.index, state.line, state.column,
        "##EOF##",
        TokenType::t_eof, TokenSubType::t_eof
      };
    }

    auto sub_type = get_character_type(state.peek());
    if (sub_type != TokenSubType::t_unknown)
    {
      state.move();

      // skipping comments
      if (sub_type == TokenSubType::t_slash && state.peek() == '/')
      {
        do state.move();
        while (state.peek() != '\n');

        // TODO: remove the recursion
        return get_next_token(state);
      }

      return parsing::Token{
        state.index - 1, state.line, state.column - 1,
        state.source.substr(state.index - 1, 1),
        TokenType::t_operator,
        sub_type
      };
    }

    if (state.peek() == '"')
    {
      std::uint32_t length = 1;
      do
      {
        state.move(), ++length;

        if (state.peek() == '\\')
          state.move(), ++length;
        
        if (state.peek() == '\0')
        {
          state.error.code = error_t::t_unexpected_eof;
          state.error.message = "String was not closed";
          break;
        }
      } while (state.peek() != '"');
      state.move();

      return parsing::Token{
        state.index - length, state.line, state.column - length,

        state.source.substr(state.index - length, length),

        TokenType::t_string,
        TokenSubType::t_string
      };
    }

    if (state.peek() == '\'')
    {
      state.move();
      std::uint32_t length = 1;

      if (std::isspace(state.peek()) || std::iscntrl(state.peek()))
      {
        state.error.code = error_t::t_misleading_character;
        state.error.message =
          "Character definition is misleading\n"
          "Whitespace symbols should be written using escape sequences:\n"
          "  \\s - space\n"
          "  \\t - tabulation\n"
          "  \\n - new line\n"
          "  \\r - carriage return";
      }

      if (state.peek() == '\\')
      {
        state.move(), ++length;
        switch(state.peek())
        {
          case 'r': case 'n': case 't': case 's': break;
          default:
          state.error.code = error_t::t_special_character_does_not_exist;
          state.error.message = std::string("Special character identifier '")
            + state.peek() + "' does not exists";
        }
      }

      state.move();

      return parsing::Token{
        state.index - length, state.line, state.column - length,

        state.source.substr(state.index - length, length),

        TokenType::t_character, TokenSubType::t_character,
      };
    }

    if ('0' <= state.peek() && state.peek() <= '9')
    {
      std::uint32_t length = 0;
      bool is_decimal = false;
      do state.move(), ++length;
      while (false
             || std::isdigit(state.peek())
             || (state.peek() == '.' && !is_decimal && (is_decimal = true)));

      return parsing::Token{
        state.index - length, state.line, state.column - length,

        state.source.substr(state.index - length, length),

        TokenType::t_number,
        (is_decimal
         ? TokenSubType::t_decimal
         : TokenSubType::t_integer)
      };
    }

    if (false
        || std::isalpha(state.peek())
        || state.peek() == '_' || state.peek() == '$')
    {
      std::uint32_t length = 0;
      do state.move(), ++length;
      while (false
             || std::isalnum(state.peek())
             || state.peek() == '_' || state.peek() == '$');

      return parsing::Token{
        state.index - length, state.line, state.column - length,
        state.source.substr(state.index - length, length),

        TokenType::t_identifier, TokenSubType::t_identifier
      };
    }

    auto ret = parsing::Token{
      state.index, state.line, state.column,
      state.source.substr(state.index, 1),

      TokenType::t_unknown, TokenSubType::t_unknown
    };

    state.move();
    return ret;
  }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movq 0x10(%r14), %r12
movl (%r14), %r15d
movq (%r12), %rax
movsbl (%rax,%r15), %r13d
movl %r13d, %edi
callq 0x4050
testl %eax, %eax
je 0x7878
movq %r14, %rdi
callq 0x7760
jmp 0x7852
cmpq %r15, 0x8(%r12)
jbe 0x78f8
movsbl %r13b, %edi
callq 0x7788
testl %eax, %eax
je 0x7927
movl %eax, %ebp
movq %r14, %rdi
callq 0x7760
movl (%r14), %edx
movq 0x10(%r14), %rsi
cmpl $0x15, %ebp
jne 0x78c9
movq (%rsi), %rax
cmpb $0x2f, (%rax,%rdx)
jne 0x78c9
movq %r14, %rdi
callq 0x7760
movq 0x10(%r14), %rax
movl (%r14), %ecx
movq (%rax), %rax
cmpb $0xa, (%rax,%rcx)
jne 0x78af
jmp 0x7852
decl %edx
movl %edx, (%rbx)
movl 0x4(%r14), %eax
movl %eax, 0x4(%rbx)
movl 0x8(%r14), %eax
decl %eax
movl %eax, 0x8(%rbx)
leaq 0x10(%rbx), %rdi
pushq $0x1
popq %rcx
callq 0x4240
movl $0x6, 0x30(%rbx)
movl %ebp, 0x34(%rbx)
jmp 0x7c24
movl %r15d, (%rbx)
movq 0x4(%r14), %rax
movq %rax, 0x4(%rbx)
leaq 0x10(%rbx), %rdi
leaq 0x9b28(%rip), %rsi      # 0x11436
leaq 0x48(%rsp), %rdx
callq 0x6728
movabsq $0x2200000007, %rax     # imm = 0x2200000007
jmp 0x7c20
movzbl %r13b, %eax
cmpl $0x27, %eax
je 0x79d5
cmpl $0x22, %eax
jne 0x7ae6
pushq $0x1
popq %rbp
movq %r14, %rdi
callq 0x7760
movq 0x10(%r14), %rax
movl (%r14), %ecx
movq (%rax), %rax
movb (%rax,%rcx), %al
cmpb $0x5c, %al
jne 0x7973
movq %r14, %rdi
callq 0x7760
addl $0x2, %ebp
movq 0x10(%r14), %rax
movl (%r14), %ecx
movq (%rax), %rax
movb (%rax,%rcx), %al
jmp 0x7975
incl %ebp
cmpb $0x22, %al
je 0x7998
movzbl %al, %eax
testl %eax, %eax
jne 0x7940
movl $0x3f2, 0x18(%r14)      # imm = 0x3F2
leaq 0x20(%r14), %rdi
leaq 0x9d19(%rip), %rsi      # 0x116ac
callq 0x4410
movq %r14, %rdi
callq 0x7760
movl (%r14), %edx
subl %ebp, %edx
movl %edx, (%rbx)
movl 0x4(%r14), %eax
movl %eax, 0x4(%rbx)
movl 0x8(%r14), %eax
subl %ebp, %eax
movl %eax, 0x8(%rbx)
leaq 0x10(%rbx), %rdi
movq 0x10(%r14), %rsi
movl %ebp, %ecx
callq 0x4240
movabsq $0x600000004, %rax      # imm = 0x600000004
jmp 0x7c20
movq %r14, %rdi
callq 0x7760
movq 0x10(%r14), %rax
movl (%r14), %ecx
movq (%rax), %rax
movb (%rax,%rcx), %r15b
movsbl %r15b, %ebp
movl %ebp, %edi
callq 0x4050
testl %eax, %eax
jne 0x7a05
movl %ebp, %edi
callq 0x4300
testl %eax, %eax
je 0x7a2b
movl $0x407, 0x18(%r14)      # imm = 0x407
leaq 0x20(%r14), %rdi
leaq 0x9caa(%rip), %rsi      # 0x116c2
callq 0x4410
movq 0x10(%r14), %rax
movl (%r14), %ecx
movq (%rax), %rax
movb (%rax,%rcx), %r15b
cmpb $0x5c, %r15b
jne 0x7b67
movq %r14, %rdi
callq 0x7760
movq 0x10(%r14), %rax
movl (%r14), %ecx
movq (%rax), %rax
movb (%rax,%rcx), %al
addb $-0x6e, %al
pushq $0x2
popq %rbp
cmpb $0x7, %al
jae 0x7a62
movzbl %al, %eax
pushq $0x71
popq %rcx
btl %eax, %ecx
jb 0x7b6a
movl $0x406, 0x18(%r14)      # imm = 0x406
leaq 0x9cf7(%rip), %rsi      # 0x11768
leaq 0x28(%rsp), %r15
leaq 0x7(%rsp), %rdx
movq %r15, %rdi
callq 0x6728
movq 0x10(%r14), %rax
movl (%r14), %ecx
movq (%rax), %rax
movsbl (%rax,%rcx), %edx
leaq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x7d85
leaq 0x9ce2(%rip), %rdx      # 0x11787
leaq 0x48(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x7d66
leaq 0x20(%r14), %rdi
leaq 0x48(%rsp), %r15
movq %r15, %rsi
callq 0x42a0
movq %r15, %rdi
callq 0x4120
leaq 0x8(%rsp), %rdi
callq 0x4120
leaq 0x28(%rsp), %rdi
callq 0x4120
jmp 0x7b6a
leal -0x30(%r13), %eax
cmpb $0x9, %al
ja 0x7ba4
xorl %r15d, %r15d
xorl %eax, %eax
movl %eax, %ebp
movq %r14, %rdi
callq 0x7760
incl %r15d
movq 0x10(%r14), %rsi
movl (%r14), %edx
movq (%rsi), %rax
movsbl (%rax,%rdx), %eax
leal -0x30(%rax), %ecx
cmpl $0xa, %ecx
jb 0x7af9
cmpb $0x2e, %al
sete %cl
movl %ebp, %edi
xorb $0x1, %dil
movb $0x1, %al
testb %dil, %cl
jne 0x7af7
subl %r15d, %edx
movl %edx, (%rbx)
movl 0x4(%r14), %eax
movl %eax, 0x4(%rbx)
movl 0x8(%r14), %eax
subl %r15d, %eax
movl %eax, 0x8(%rbx)
leaq 0x10(%rbx), %rdi
movl %r15d, %ecx
callq 0x4240
movl $0x3, 0x30(%rbx)
movzbl %bpl, %eax
andl $0x1, %eax
orl $0x4, %eax
movl %eax, 0x34(%rbx)
jmp 0x7c24
pushq $0x1
popq %rbp
movq %r14, %rdi
callq 0x7760
movl (%r14), %edx
subl %ebp, %edx
movl %edx, (%rbx)
movl 0x4(%r14), %eax
movl %eax, 0x4(%rbx)
movl 0x8(%r14), %eax
subl %ebp, %eax
movl %eax, 0x8(%rbx)
leaq 0x10(%rbx), %rdi
movq 0x10(%r14), %rsi
movl %ebp, %ecx
callq 0x4240
movabsq $0x700000005, %rax      # imm = 0x700000005
jmp 0x7c20
movl %r13d, %edi
callq 0x4370
testl %eax, %eax
je 0x7c36
pushq $0x1
popq %r13
movq %r14, %rdi
callq 0x7760
movq 0x10(%r14), %r15
movl (%r14), %r12d
movq (%r15), %rax
movsbl (%rax,%r12), %ebp
movl %ebp, %edi
callq 0x4230
testl %eax, %eax
jne 0x7be8
movzbl %bpl, %eax
cmpl $0x24, %eax
je 0x7be8
cmpl $0x5f, %eax
jne 0x7bed
incl %r13d
jmp 0x7bb8
subl %r13d, %r12d
movl %r12d, (%rbx)
movl 0x4(%r14), %eax
movl %eax, 0x4(%rbx)
movl 0x8(%r14), %eax
subl %r13d, %eax
movl %eax, 0x8(%rbx)
leaq 0x10(%rbx), %rdi
movl %r13d, %ecx
movq %r15, %rsi
movq %r12, %rdx
callq 0x4240
movabsq $0x300000002, %rax      # imm = 0x300000002
movq %rax, 0x30(%rbx)
movq %rbx, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x5f, %r13b
je 0x7bb4
movzbl %r13b, %eax
cmpl $0x24, %eax
je 0x7bb4
movl %r15d, (%rbx)
movq 0x4(%r14), %rax
movq %rax, 0x4(%rbx)
leaq 0x10(%rbx), %rdi
pushq $0x1
popq %rcx
movq %r12, %rsi
movq %r15, %rdx
callq 0x4240
andq $0x0, 0x30(%rbx)
movq %r14, %rdi
callq 0x7760
jmp 0x7c24
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x4120
jmp 0x7c8b
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x4120
movq %rbx, %rdi
callq 0x43c0