// File: cphinton42[P]hinton-language/src/parse.cpp
// Function: report_error(Parsing_Context*, Token*, Token*, char const*)
void report_error(Parsing_Context *ctx, Token *start_section, Token *current,const byte *error_text)
{
    byte *start_highlight = start_section->contents.data;
    byte *start = start_highlight;
    byte *start_program = ctx->program_text.data;
    while(start > start_program)
    {
        if(*start == '\n' || *start == '\r')
        {
            ++start;
            break;
        }
        else
        {
            --start;
        }
    }
    
    bool error_at_eof = (current == &ctx->tokens[ctx->tokens.count - 1]);
    byte *end_program = ctx->program_text.data + ctx->program_text.count;
    
    byte *start_error;
    byte *end_highlight;
    byte *end;
    
    if(error_at_eof)
    {
        start_error = end_program;
        end_highlight = end_program;
        end = end_program;
    }
    else
    {
        start_error = current->contents.data;
        end_highlight = start_error + current->contents.count;
        end = end_highlight;
        
        while(end < end_program)
        {
            if(*end == '\r')
            {
                ++end;
                if(end < end_program && *end == '\n')
                {
                    ++end;
                }
                break;
            }
            else if(*end == '\n')
            {
                ++end;
                break;
            }
            else
            {
                ++end;
            }
        }
    }
    
    print_err("\x1B[1;31mError\x1B[0m: %d:%d:\n    %s\n", current->line_number, current->line_offset, error_text);
    
    print_err_indented(start, start_highlight);
    
    print_err("\x1B[1;33m");
    print_err_indented(start_highlight, start_error, false);
    
    if(error_at_eof)
    {
        print_err("\x1B[1;31mEOF\x1B[0m\n");
    }
    else
    {
        print_err("\x1B[1;31m");
        print_err_indented(start_error, end_highlight, false);
        
        print_err("\x1B[0m");
        print_err_indented(end_highlight, end, false);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x38(%rbp), %rax
jbe 0xc6c6
movq -0x30(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xa, %eax
je 0xc6aa
movq -0x30(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xd, %eax
jne 0xc6b8
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0xc6c6
movq -0x30(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0xc688
movq -0x18(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rdi
addq $0x10, %rdi
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rsi
subq $0x1, %rsi
callq 0x173a0
movq %rax, %rcx
movq -0x68(%rbp), %rax
cmpq %rcx, %rax
sete %al
andb $0x1, %al
movb %al, -0x39(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
addq (%rcx), %rax
movq %rax, -0x48(%rbp)
testb $0x1, -0x39(%rbp)
je 0xc72f
movq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x60(%rbp)
jmp 0xc7c5
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq -0x18(%rbp), %rcx
addq 0x10(%rcx), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x48(%rbp), %rax
jae 0xc7c3
movq -0x60(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xd, %eax
jne 0xc799
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x48(%rbp), %rax
jae 0xc797
movq -0x60(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xa, %eax
jne 0xc797
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0xc7c3
movq -0x60(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xa, %eax
jne 0xc7b3
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0xc7c3
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0xc7c1
jmp 0xc753
jmp 0xc7c5
movq -0x18(%rbp), %rax
movl 0x8(%rax), %esi
movq -0x18(%rbp), %rax
movl 0xc(%rax), %edx
movq -0x20(%rbp), %rcx
leaq 0xdbc6(%rip), %rdi      # 0x1a3a4
movb $0x0, %al
callq 0x4990
movq -0x30(%rbp), %rdi
movq -0x28(%rbp), %rsi
movl $0x1, %edx
callq 0xa210
leaq 0xdbc7(%rip), %rdi      # 0x1a3c5
movb $0x0, %al
callq 0x4990
movq -0x28(%rbp), %rdi
movq -0x50(%rbp), %rsi
xorl %edx, %edx
callq 0xa210
testb $0x1, -0x39(%rbp)
je 0xc82a
leaq 0xdbac(%rip), %rdi      # 0x1a3cd
movb $0x0, %al
callq 0x4990
jmp 0xc864
leaq 0xdbac(%rip), %rdi      # 0x1a3dd
movb $0x0, %al
callq 0x4990
movq -0x50(%rbp), %rdi
movq -0x58(%rbp), %rsi
xorl %edx, %edx
callq 0xa210
leaq 0xe57c(%rip), %rdi      # 0x1adca
movb $0x0, %al
callq 0x4990
movq -0x58(%rbp), %rdi
movq -0x60(%rbp), %rsi
xorl %edx, %edx
callq 0xa210
addq $0x70, %rsp
popq %rbp
retq
nopw (%rax,%rax)