// File: sumanishs[P]boost-edif-parser2/edifreader.cpp
// Function: EdifReader::Read(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&)
bool 
EdifReader::Read(std::string& filename){

    typedef std::string::iterator base_iterator_type;
    typedef lex::lexertl::token< base_iterator_type, boost::mpl::vector<unsigned int, std::string> > token_type;
    typedef lex::lexertl::actor_lexer<token_type> lexer_type;
    typedef edif_tokens<lexer_type> edif_tokens;
    typedef edif_tokens::iterator_type iterator_type;
    typedef edif_grammar<iterator_type, edif_tokens::lexer_def> edif_grammar;

    EdifParseTreeBuilder* ptb = new EdifParseTreeBuilder();

    edif_tokens tokens;                                 // Our lexer
    edif_grammar grammar(tokens, ptb);                  // Our parser

    std::ifstream inFile;
    inFile.open(filename.c_str());
    if(inFile.fail()){
        std::cout << "Cannot open file to read...." << std::endl;
        return false;
    }

    std::stringstream strStream;
    strStream << inFile.rdbuf();
    std::string str(strStream.str());
    //str.pop_back();
    //std::cout << "File content:\n" << str << "!" << std::endl;
    inFile.close();

    std::string::iterator it = str.begin();
    iterator_type iter = tokens.begin(it, str.end());
    iterator_type end = tokens.end();

    //iterator_type itr = iter;
    //while (itr != end && token_is_valid(*itr))
    //{   
    //    std::cout << "Token: " << itr->id() << ": " /*<< tokens.nameof(itr)*/ << " ('" << itr->value() << "')\n";
    //    ++itr;
    //}   
    ////return 0;


    std::string ws("WS");
    bool r = qi::phrase_parse(iter, end, grammar, qi::in_state(ws)[tokens.self]);

    if (r && iter == end)
    {
        std::cout << "\n-------------------------\n";
        std::cout << "Parsing succeeded\n";
        std::cout << "-------------------------\n";
        return true;
    }
    else
    {
        std::cout << "\n-------------------------\n";
        std::cout << "Parsing failed\n";
        std::cout << "-------------------------\n";
        return false;
    }

    std::cout << "Bye... :-) \n\n";
    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2d00, %rsp           # imm = 0x2D00
movq %rdi, -0x50(%rbp)
movq %rsi, -0x58(%rbp)
movl $0x1, %edi
callq 0x6360
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x2ca0(%rbp)
callq 0x8d50
jmp 0x7bf1
movq -0x2ca0(%rbp), %rax
movq %rax, -0x60(%rbp)
leaq -0x1610(%rbp), %rdi
movq %rdi, -0x2ca8(%rbp)
callq 0x8d60
movq -0x2ca8(%rbp), %rsi
movq -0x60(%rbp), %rdx
leaq -0x2840(%rbp), %rdi
callq 0xc740
jmp 0x7c28
leaq -0x2a48(%rbp), %rdi
callq 0x6410
jmp 0x7c36
movq -0x58(%rbp), %rdi
callq 0x6130
movq %rax, %rsi
leaq -0x2a48(%rbp), %rdi
movl $0x8, %edx
callq 0x6210
jmp 0x7c55
movq -0x2a48(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x2a48(%rbp,%rax), %rdi
callq 0x6030
movb %al, -0x2ca9(%rbp)
jmp 0x7c75
movb -0x2ca9(%rbp), %al
testb $0x1, %al
jne 0x7c84
jmp 0x7d1d
movq 0x10f34d(%rip), %rdi    # 0x116fd8
leaq 0xb739e(%rip), %rsi     # 0xbf030
callq 0x6350
movq %rax, -0x2cb8(%rbp)
jmp 0x7ca0
movq -0x2cb8(%rbp), %rdi
movq 0x10f312(%rip), %rsi    # 0x116fc0
callq 0x63b0
jmp 0x7cb5
movb $0x0, -0x41(%rbp)
movl $0x1, -0x2a4c(%rbp)
jmp 0x8135
movq -0x2ca0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
movl $0x1, %esi
callq 0x6370
jmp 0x818b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x817f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x8173
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x8167
leaq -0x2bd8(%rbp), %rdi
callq 0x6280
jmp 0x7d2b
leaq -0x2bc8(%rbp), %rax
movq %rax, -0x2cc8(%rbp)
leaq -0x2a48(%rbp), %rdi
callq 0x6550
movq %rax, -0x2cc0(%rbp)
jmp 0x7d4e
movq -0x2cc0(%rbp), %rsi
movq -0x2cc8(%rbp), %rdi
callq 0x60a0
jmp 0x7d63
leaq -0x2bf8(%rbp), %rdi
leaq -0x2bd8(%rbp), %rsi
callq 0x6340
jmp 0x7d78
leaq -0x2a48(%rbp), %rdi
callq 0x6090
jmp 0x7d86
leaq -0x2bf8(%rbp), %rdi
movq %rdi, -0x2cd0(%rbp)
callq 0x6310
movq -0x2cd0(%rbp), %rdi
movq %rax, -0x2c00(%rbp)
callq 0x61b0
movq %rax, -0x2c18(%rbp)
xorl %eax, %eax
movl %eax, %r8d
leaq -0x2c10(%rbp), %rdi
leaq -0x1610(%rbp), %rsi
leaq -0x2c00(%rbp), %rdx
leaq -0x2c18(%rbp), %rcx
callq 0x2b270
jmp 0x7ddb
leaq -0x2c28(%rbp), %rdi
leaq -0x1610(%rbp), %rsi
callq 0x2b2c0
jmp 0x7df0
leaq -0x2c49(%rbp), %rdi
movq %rdi, -0x2cd8(%rbp)
callq 0x65f0
movq -0x2cd8(%rbp), %rdx
leaq 0xb723c(%rip), %rsi     # 0xbf04d
leaq -0x2c48(%rbp), %rdi
callq 0x2b2f0
jmp 0x7e1f
leaq -0x2c49(%rbp), %rdi
callq 0x63d0
leaq -0x2c60(%rbp), %rdi
leaq -0x2c28(%rbp), %rsi
callq 0x2b430
jmp 0x7e40
leaq 0x10f619(%rip), %rsi    # 0x117460
xorl %eax, %eax
movl %eax, %ecx
leaq -0x2c98(%rbp), %rdi
leaq -0x2c48(%rbp), %rdx
callq 0x2b460
jmp 0x7e60
leaq -0x1610(%rbp), %rax
addq $0x2f8, %rax            # imm = 0x2F8
leaq -0x2c98(%rbp), %rcx
movq %rcx, -0x38(%rbp)
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x30(%rbp)
leaq -0x30(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x2ce8(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
leaq -0x19(%rbp), %rcx
movq %rcx, -0x8(%rbp)
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x2ce0(%rbp)
movq -0x2ce8(%rbp), %rax
movq -0x2ce0(%rbp), %rcx
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
movq %rax, -0x2cf8(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x2cf0(%rbp)
movq -0x2cf0(%rbp), %rax
movq -0x2cf8(%rbp), %rcx
movq %rcx, -0x2c70(%rbp)
movq %rax, -0x2c68(%rbp)
leaq -0x2c10(%rbp), %rdi
leaq -0x2c60(%rbp), %rsi
leaq -0x2840(%rbp), %rdx
leaq -0x2c70(%rbp), %rcx
xorl %r8d, %r8d
callq 0x2b3a0
movb %al, -0x2cf9(%rbp)
jmp 0x7f25
leaq -0x2c98(%rbp), %rdi
callq 0x2b5b0
leaq -0x2c60(%rbp), %rdi
callq 0x2b5d0
movb -0x2cf9(%rbp), %al
andb $0x1, %al
movb %al, -0x2c4a(%rbp)
testb $0x1, -0x2c4a(%rbp)
je 0x806c
leaq -0x2c10(%rbp), %rdi
leaq -0x2c28(%rbp), %rsi
callq 0x2b5f0
movb %al, -0x2cfa(%rbp)
jmp 0x7f73
movb -0x2cfa(%rbp), %al
testb $0x1, %al
jne 0x7f82
jmp 0x806c
movq 0x10f04f(%rip), %rdi    # 0x116fd8
leaq 0xb70c0(%rip), %rsi     # 0xbf050
callq 0x6350
jmp 0x7f97
movq 0x10f03a(%rip), %rdi    # 0x116fd8
leaq 0xb70c7(%rip), %rsi     # 0xbf06c
callq 0x6350
jmp 0x7fac
movq 0x10f025(%rip), %rdi    # 0x116fd8
leaq 0xb7097(%rip), %rsi     # 0xbf051
callq 0x6350
jmp 0x7fc1
movb $0x1, -0x41(%rbp)
movl $0x1, -0x2a4c(%rbp)
jmp 0x80b9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x8127
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x811b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x810f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x2c49(%rbp), %rdi
callq 0x63d0
jmp 0x8103
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x80f7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x805b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x2c98(%rbp), %rdi
callq 0x2b5b0
leaq -0x2c60(%rbp), %rdi
callq 0x2b5d0
jmp 0x80f7
movq 0x10ef65(%rip), %rdi    # 0x116fd8
leaq 0xb6fd6(%rip), %rsi     # 0xbf050
callq 0x6350
jmp 0x8081
movq 0x10ef50(%rip), %rdi    # 0x116fd8
leaq 0xb6ff0(%rip), %rsi     # 0xbf07f
callq 0x6350
jmp 0x8096
movq 0x10ef3b(%rip), %rdi    # 0x116fd8
leaq 0xb6fad(%rip), %rsi     # 0xbf051
callq 0x6350
jmp 0x80ab
movb $0x0, -0x41(%rbp)
movl $0x1, -0x2a4c(%rbp)
leaq -0x2c48(%rbp), %rdi
callq 0x61e0
leaq -0x2c28(%rbp), %rdi
callq 0x2b5d0
leaq -0x2c10(%rbp), %rdi
callq 0x2b5d0
leaq -0x2bf8(%rbp), %rdi
callq 0x61e0
leaq -0x2bd8(%rbp), %rdi
callq 0x62c0
jmp 0x8135
leaq -0x2c48(%rbp), %rdi
callq 0x61e0
leaq -0x2c28(%rbp), %rdi
callq 0x2b5d0
leaq -0x2c10(%rbp), %rdi
callq 0x2b5d0
leaq -0x2bf8(%rbp), %rdi
callq 0x61e0
leaq -0x2bd8(%rbp), %rdi
callq 0x62c0
jmp 0x8167
leaq -0x2a48(%rbp), %rdi
callq 0x60e0
leaq -0x2840(%rbp), %rdi
callq 0x2b660
leaq -0x1610(%rbp), %rdi
callq 0x2ba80
movb -0x41(%rbp), %al
andb $0x1, %al
addq $0x2d00, %rsp           # imm = 0x2D00
popq %rbp
retq
leaq -0x2a48(%rbp), %rdi
callq 0x60e0
leaq -0x2840(%rbp), %rdi
callq 0x2b660
leaq -0x1610(%rbp), %rdi
callq 0x2ba80
movq -0x68(%rbp), %rdi
callq 0x65e0
nopw %cs:(%rax,%rax)
nop