// File: /workspace/github_cpp_makefile_star2_O2/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 %r15
pushq %r14
pushq %rbx
subq $0x2c10, %rsp           # imm = 0x2C10
movq %rsi, %rbx
pushq $0x1
popq %rdi
callq 0x5270
movq %rax, %r14
leaq 0x440(%rsp), %r15
movq %r15, %rdi
callq 0x5d06
leaq 0x19e0(%rsp), %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x7cd2
leaq 0xb0(%rsp), %rdi
callq 0x52b0
movq (%rbx), %rsi
leaq 0xb0(%rsp), %rdi
pushq $0x8
popq %rdx
callq 0x5170
movq 0xb0(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0xd0(%rsp,%rax)
je 0x5a71
movq 0x4c582(%rip), %rdi     # 0x51fd8
leaq 0x29357(%rip), %rsi     # 0x2edb4
callq 0x5260
movq %rax, %rdi
callq 0x54f8
xorl %ebx, %ebx
jmp 0x5c28
leaq 0x2b8(%rsp), %rdi
callq 0x51c0
leaq 0x2c8(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x5070
leaq 0x2d0(%rsp), %rsi
leaq 0x30(%rsp), %rdi
callq 0x53e0
leaq 0xb0(%rsp), %rdi
callq 0x5060
movq 0x30(%rsp), %rax
leaq 0x98(%rsp), %rdx
movq %rax, (%rdx)
addq 0x38(%rsp), %rax
leaq 0x50(%rsp), %rcx
movq %rax, (%rcx)
movq %rsp, %rdi
leaq 0x440(%rsp), %rsi
xorl %r8d, %r8d
callq 0x22134
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
leaq 0x292e0(%rip), %rsi     # 0x2edd1
leaq 0x78(%rsp), %rdi
leaq 0x50(%rsp), %rdx
callq 0xbdf6
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
leaq 0x4c871(%rip), %rsi     # 0x52380
leaq 0x50(%rsp), %rbx
leaq 0x78(%rsp), %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0xbe7c
leaq 0x738(%rsp), %rax
leaq 0xa0(%rsp), %rcx
movq %rbx, (%rcx)
movq %rax, 0x8(%rcx)
movq %rsp, %rdi
leaq 0x10(%rsp), %rsi
leaq 0x19e0(%rsp), %rdx
xorl %r8d, %r8d
callq 0xbe31
movl %eax, %ebx
leaq 0x50(%rsp), %rdi
callq 0x5110
leaq 0x10(%rsp), %rdi
callq 0x15514
testb %bl, %bl
je 0x5bba
movq %rsp, %rdi
leaq 0x20(%rsp), %rsi
callq 0xbeec
testb %al, %al
je 0x5bba
movq 0x4c454(%rip), %rdi     # 0x51fd8
leaq 0x29249(%rip), %rsi     # 0x2edd4
callq 0x5260
movq 0x4c441(%rip), %rdi     # 0x51fd8
leaq 0x29252(%rip), %rsi     # 0x2edf0
callq 0x5260
movb $0x1, %bl
movq 0x4c42c(%rip), %rdi     # 0x51fd8
leaq 0x29222(%rip), %rsi     # 0x2edd5
callq 0x5260
jmp 0x5bf5
movq 0x4c417(%rip), %rdi     # 0x51fd8
leaq 0x2920c(%rip), %rsi     # 0x2edd4
callq 0x5260
movq 0x4c404(%rip), %rdi     # 0x51fd8
leaq 0x29228(%rip), %rsi     # 0x2ee03
callq 0x5260
movq 0x4c3f1(%rip), %rdi     # 0x51fd8
leaq 0x291e7(%rip), %rsi     # 0x2edd5
callq 0x5260
xorl %ebx, %ebx
leaq 0x78(%rsp), %rdi
callq 0x5110
leaq 0x20(%rsp), %rdi
callq 0x15514
movq %rsp, %rdi
callq 0x15514
leaq 0x30(%rsp), %rdi
callq 0x5110
leaq 0x2b8(%rsp), %rdi
callq 0x5200
leaq 0xb0(%rsp), %rdi
callq 0x50a0
leaq 0x19e0(%rsp), %rdi
callq 0xbf24
leaq 0x440(%rsp), %rdi
callq 0xc230
movl %ebx, %eax
addq $0x2c10, %rsp           # imm = 0x2C10
popq %rbx
popq %r14
popq %r15
retq
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x5110
jmp 0x5c70
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x15514
jmp 0x5c9e
movq %rax, %rbx
jmp 0x5ca8
jmp 0x5c83
movq %rax, %rbx
jmp 0x5cba
jmp 0x5c8a
movq %rax, %rbx
jmp 0x5cc4
jmp 0x5cd3
movq %rax, %rbx
jmp 0x5ce3
movq %rax, %rbx
jmp 0x5cf0
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x5110
leaq 0x20(%rsp), %rdi
callq 0x15514
movq %rsp, %rdi
callq 0x15514
leaq 0x30(%rsp), %rdi
callq 0x5110
leaq 0x2b8(%rsp), %rdi
callq 0x5200
jmp 0x5cd6
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x50a0
leaq 0x19e0(%rsp), %rdi
callq 0xbf24
leaq 0x440(%rsp), %rdi
callq 0xc230
movq %rbx, %rdi
callq 0x5460
nop