// File: /workspace/github_cpp_makefile_star2_O2/AutomataLab[P]JSONSki/src/JSONPathParser.cpp
// Function: JSONPathParser::updateQueryAutomaton(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, QueryAutomaton&)
void JSONPathParser::updateQueryAutomaton(string query, QueryAutomaton &qa) {
    int length = query.size();
    int lexer_state = 0;
    int query_state = START_STATE;
    char buffer[MAX_KEY_LENGTH];
    for (int i = 0; i < length; ++i) {
        char ch = query[i];
        switch (lexer_state) {
            case 0: { // begin of the path
                if (ch == '.') {
                    lexer_state = 1;
                } else if (ch == '[') {
                    lexer_state = 2;
                    qa.updateStateTransInfo(query_state, false, NONE, ARRAY, NULL, query_state + 1);
                    // cout<<"("<<query_state<<", false, NONE, ARRAY, NULL, "<<(query_state + 1)<<")"<<endl;
                    ++query_state;
                }
                break;
            }
            case 1: { // in object
                int key_end = 0;
                while (ch != '.' && ch != '[') {
                    buffer[key_end++] = ch;
                    if (i + 1 == length) break;
                    ch = query[++i];
                }
                buffer[key_end] = '\0';
                if (i + 1 < length) {
                    if (ch == '[') {
                        lexer_state = 2;
                        // current query state -> expected key-array pair
                        qa.updateStateTransInfo(query_state, false, ARRAY, NONE, buffer, query_state + 1);
                        // cout<<"("<<query_state<<", false, ARRAY, NONE, "<<buffer<<", "<<(query_state + 1)<<")"<<endl;
                        // state transition for [
                        qa.updateStateTransInfo(query_state + 1, false, NONE, NONE, NULL, query_state + 2);
                        // cout<<"("<<(query_state + 1)<<", false, NONE, NONE, NULL, "<<(query_state + 2)<<")"<<endl;
                        query_state += 2;
                        // break;
                    } else if (ch == '.') {
                        lexer_state = 1;
                        qa.updateStateTransInfo(query_state, false, OBJECT, NONE, buffer, query_state + 1);
                        // cout<<"("<<query_state<<", false, OBJECT, NONE, "<<buffer<<", "<<(query_state + 1)<<")"<<endl;
                        ++query_state;
                        // break;
                    } 
                } else {
                    // output info
                    qa.updateStateTransInfo(query_state, false, PRIMITIVE, NONE, buffer, query_state + 1);
                    // cout<<"("<<query_state<<", false, PRIMITIVE, NONE, "<<buffer<<", "<<(query_state + 1)<<")"<<endl;
                    qa.updateStateTransInfo(query_state + 1, true, NONE, NONE, NULL, query_state + 1);
                    // cout<<"("<<(query_state + 1)<<", true, NONE, NONE, NULL, "<<(query_state + 1)<<")"<<endl;
                }
                break;
            }
            case 2: { // in array
                int start_idx = 0;
                int end_idx = -1;
                int index_end = 0;
                bool has_colon = false;
                while (ch != ']') {
                    if (ch == ':') {
                        buffer[index_end] = '\0';
                        start_idx = atoi(buffer);
                        end_idx = INT_MAX;
                        index_end = 0;
                        has_colon = true;
                    }
                    else if (ch >= '0' && ch <= '9') {
                        buffer[index_end++] = ch;
                    }
                    if (i + 1 == length) break;
                    ch = query[++i];
                }
                if (has_colon == false && index_end > 0) {
                    buffer[index_end] = '\0';
                    start_idx = atoi(buffer);
                    end_idx = start_idx + 1;
                } else if (index_end > 0) {
                    buffer[index_end] = '\0';
                    end_idx = atoi(buffer);
                }
                if (end_idx > -1) {
                    qa.addIndexConstraints(query_state, start_idx, end_idx);
                    // cout<<"index constraints "<<start_idx<<" "<<end_idx<<" current state "<<query_state<<endl;
                }
                if (i + 1 < length) {
                    ch = query[++i];
                    if (ch == '.') {
                        lexer_state = 1;
                        qa.updateStateTransInfo(query_state, false, NONE, OBJECT, NULL, query_state + 1);
                        // cout<<"("<<query_state<<", false, NONE, OBJECT, NULL, "<<(query_state + 1)<<")"<<endl;
                    } else if (ch == '[') {
                        cout<<"additional ["<<endl;
                        lexer_state = 2;
                        qa.updateStateTransInfo(query_state, false, NONE, ARRAY, NULL, query_state + 1);
                        // cout<<"("<<query_state<<", false, NONE, ARRAY, NULL, "<<(query_state + 1)<<")"<<endl;
                        ++query_state;
                    }
                } else {
                    qa.updateStateTransInfo(query_state, true, NONE, PRIMITIVE, NULL, query_state);
                    // cout<<"("<<query_state<<", false, NONE, PRIMITIVE, NULL, "<<(query_state + 1)<<")"<<endl;
                }
                break;
            }
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdi, %rbp
movl 0x8(%rdi), %eax
movq %rsi, 0x10(%rsp)
leaq 0x4(%rsi), %rcx
movq %rcx, 0x8(%rsp)
leal -0x1(%rax), %r14d
pushq $0x1
popq %rcx
movl %ecx, %edx
movq %rax, 0x20(%rsp)
subl %eax, %edx
movq %rdx, 0x28(%rsp)
xorl %eax, %eax
movl %ecx, %r13d
xorl %r15d, %r15d
movq %rdi, 0x18(%rsp)
movq 0x20(%rsp), %rcx
movl %ecx, %edx
subl %r15d, %edx
jle 0x166a4
movslq %r15d, %rsi
movq (%rbp), %r12
movb (%r12,%rsi), %cl
testl %eax, %eax
je 0x163aa
cmpl $0x1, %eax
jne 0x163f4
addq %r12, %rsi
incq %rsi
movl %r15d, %edi
addq 0x28(%rsp), %rdi
xorl %r8d, %r8d
movzbl %cl, %eax
cmpb $0x2e, %al
je 0x16453
cmpl $0x5b, %eax
je 0x16453
movb %cl, 0x30(%rsp,%r8)
movl %edi, %r9d
addl %r8d, %r9d
je 0x1656e
movb (%rsi,%r8), %cl
incq %r8
jmp 0x1637c
movzbl %cl, %eax
cmpl $0x2e, %eax
je 0x1661a
cmpl $0x5b, %eax
jne 0x16567
leal 0x1(%r13), %ecx
decl %r13d
movslq %r13d, %rax
imulq $0x49c, %rax, %rax      # imm = 0x49C
movq 0x8(%rsp), %rdx
movl $0x66, 0x6c(%rdx,%rax)
movl %ecx, 0x70(%rdx,%rax)
movb $0x0, 0x48c(%rdx,%rax)
pushq $0x2
popq %rax
movl %ecx, %r13d
jmp 0x1669c
leaq (%r12,%rsi), %rbx
incq %rbx
xorl %ebp, %ebp
pushq $-0x1
popq %rax
xorl %edx, %edx
xorl %esi, %esi
movslq %edx, %rdi
cmpb $0x3a, %cl
je 0x16429
movzbl %cl, %r8d
cmpl $0x5d, %r8d
je 0x16461
leal -0x30(%rcx), %r8d
cmpb $0x9, %r8b
ja 0x16444
leal 0x1(%rdi), %edx
movb %cl, 0x30(%rsp,%rdi)
jmp 0x16444
movb $0x0, 0x30(%rsp,%rdi)
leaq 0x30(%rsp), %rdi
callq 0x134c0
movl %eax, %ebp
movb $0x1, %sil
xorl %edx, %edx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl %r15d, %r14d
je 0x1645e
movb (%rbx), %cl
incl %r15d
incq %rbx
jmp 0x16404
addl %r8d, %r15d
movl %r8d, %edx
jmp 0x16571
movl %r14d, %r15d
testb $0x1, %sil
sete %cl
testl %edx, %edx
setg %sil
andb %cl, %sil
movl %edx, %ecx
cmpb $0x1, %sil
jne 0x1648e
movb $0x0, 0x30(%rsp,%rcx)
leaq 0x30(%rsp), %rdi
callq 0x134c0
movl %eax, %ebp
incl %eax
jmp 0x164a1
testl %edx, %edx
jle 0x164a1
movb $0x0, 0x30(%rsp,%rcx)
leaq 0x30(%rsp), %rdi
callq 0x134c0
testl %eax, %eax
sets %cl
testl %r13d, %r13d
sete %dl
orb %cl, %dl
jne 0x164d9
leal -0x1(%r13), %ecx
movslq %ecx, %rcx
imulq $0x49c, %rcx, %rcx      # imm = 0x49C
movq 0x8(%rsp), %rdx
movb $0x1, 0x498(%rdx,%rcx)
movl %ebp, 0x490(%rdx,%rcx)
movl %eax, 0x494(%rdx,%rcx)
movslq %r15d, %rbx
incq %rbx
cmpl 0x20(%rsp), %ebx
jge 0x16533
movzbl (%r12,%rbx), %eax
cmpl $0x5b, %eax
movq 0x18(%rsp), %rbp
je 0x16649
cmpl $0x2e, %eax
jne 0x16696
leal 0x1(%r13), %eax
leal -0x1(%r13), %ecx
movslq %ecx, %rcx
imulq $0x49c, %rcx, %rcx      # imm = 0x49C
movq 0x8(%rsp), %rdx
movl $0x65, 0x6c(%rdx,%rcx)
movl %eax, 0x70(%rdx,%rcx)
movb $0x0, 0x48c(%rdx,%rcx)
pushq $0x1
jmp 0x16698
leal -0x1(%r13), %eax
cltq
imulq $0x49c, %rax, %rax      # imm = 0x49C
movq 0x8(%rsp), %rcx
movl $0x67, 0x6c(%rcx,%rax)
movl %r13d, 0x70(%rcx,%rax)
movb $0x1, 0x48c(%rcx,%rax)
pushq $0x2
popq %rax
movq 0x18(%rsp), %rbp
jmp 0x1669c
xorl %eax, %eax
jmp 0x1669c
movl %r14d, %r15d
movslq %edx, %rdx
movb $0x0, 0x30(%rsp,%rdx)
leal 0x1(%r15), %edx
movslq %r13d, %rbx
cmpl 0x20(%rsp), %edx
jge 0x165db
cmpb $0x2e, %cl
je 0x1661f
cmpl $0x5b, %eax
jne 0x1661a
leal 0x1(%r13), %eax
movl %eax, (%rsp)
movq 0x10(%rsp), %rdi
movl %r13d, %esi
xorl %edx, %edx
pushq $0x66
popq %rcx
pushq $0x6c
popq %r8
leaq 0x30(%rsp), %r9
callq 0x166b6
addl $0x2, %r13d
imulq $0x49c, %rbx, %rax      # imm = 0x49C
movq 0x8(%rsp), %rcx
movl %r13d, 0x70(%rcx,%rax)
movb $0x0, 0x48c(%rcx,%rax)
pushq $0x2
jmp 0x1661c
leal 0x1(%r13), %ebp
movl %ebp, (%rsp)
movq 0x10(%rsp), %rdi
movl %r13d, %esi
xorl %edx, %edx
pushq $0x67
popq %rcx
pushq $0x6c
popq %r8
leaq 0x30(%rsp), %r9
callq 0x166b6
imulq $0x49c, %rbx, %rax      # imm = 0x49C
movq 0x8(%rsp), %rcx
movl %ebp, 0x70(%rcx,%rax)
movq 0x18(%rsp), %rbp
movb $0x1, 0x48c(%rcx,%rax)
pushq $0x1
popq %rax
jmp 0x1669c
leal 0x1(%r13), %ebx
movl %ebx, (%rsp)
movq 0x10(%rsp), %rdi
movl %r13d, %esi
xorl %edx, %edx
pushq $0x65
popq %rcx
pushq $0x6c
popq %r8
leaq 0x30(%rsp), %r9
callq 0x166b6
pushq $0x1
popq %rax
movl %ebx, %r13d
jmp 0x1669c
leaq 0xe8e30(%rip), %rdi     # 0xff480
leaq 0xad9ad(%rip), %rsi     # 0xc4004
callq 0x8e220
movq %rax, %rdi
callq 0x8db00
leal 0x1(%r13), %ecx
decl %r13d
movslq %r13d, %rax
imulq $0x49c, %rax, %rax      # imm = 0x49C
movq 0x8(%rsp), %rdx
movl $0x66, 0x6c(%rdx,%rax)
movl %ecx, 0x70(%rdx,%rax)
movb $0x0, 0x48c(%rdx,%rax)
pushq $0x2
popq %rax
movl %ecx, %r13d
jmp 0x16699
pushq $0x2
popq %rax
movl %ebx, %r15d
incl %r15d
jmp 0x16343
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq