#include "lex.h"

using namespace empathy;
using namespace empathy::lex;

bool Lexer::eos() const
{
    return m_input.eof() && m_lookAheadCache.empty();
}

optional< Term > Lexer::consume()
{
    if( m_lookAheadCache.empty() )
        return readToken();

    auto tok = move( m_lookAheadCache.front() );
    m_lookAheadCache.pop_front();
    return tok;
}

optional< Term > Lexer::lookAhead( size_t distance )
{
    while( m_lookAheadCache.size() < ( distance + 1 ) )
    {
        auto tok = readToken();
        if( !tok )
            return nullopt;
        m_lookAheadCache.emplace_back( move( *tok ) );
    }

    return m_lookAheadCache[distance];
}
