Goose  Artifact [265bcd31c1]

Artifact 265bcd31c13ce53464e880c3c27cc6c257b6c784d29d28683b95714d74e319a9:

  • File bs/parse/parser.cpp — part of check-in [15b15050d1] at 2019-01-20 16:45:59 on branch trunk — Parser: when the resolver yields custom parsing rules wrapped as values, invoke them. (user: achavasse size: 3484)


#include "parse.h"

using namespace empathy;
using namespace empathy::parse;

optional< ir::Value > Parser::parse( uint32_t precedence )
{
    auto next = m_resolver.lookAhead();
    if( !next )
        return nullopt;

    auto leftVal = parsePrefix( *m_resolver.consume(), precedence );
    if( !leftVal )
        return nullopt;

    push( leftVal );

    while( next = m_resolver.lookAhead() )
    {
        auto prec = getPrecedence( *next );
        if( !prec || precedence > *prec )
            return result();

        push( *parseInfix( *m_resolver.consume(), *prec ) );
    }

    return result();
}

optional< ir::Value > Parser::result() const
{
    if( !m_seq )
        return m_lastValue;

    if( m_seq && m_lastValue )
    {
        const auto& llr = m_lastValue->llr();
        m_seq->emplace_back( move( *llr ) );
        return Value( m_lastValue->type(), m_seq );
    }

    return nullopt;
}

optional< uint32_t > Parser::getPrecedence( const Term& t )
{
    return visit( [&]( auto&& content )
    {
        return getPrecedence( t, content );
    }, t.content() );
}

optional< ir::Value > Parser::parsePrefix( const Term& t, uint32_t precedence )
{
    return visit( [&]( auto&& content )
    {
        return parsePrefix( t, content, precedence );
    }, t.content() );
}

optional< ir::Value > Parser::parseInfix( const Term& t, uint32_t precedence )
{
    return visit( [&]( auto&& content )
    {
        return parseInfix( t, content, precedence );
    }, t.content() );
}

optional< ir::Value > Parser::parsePrefix( const Term&, uint64_t intlit, uint32_t )
{
    static auto type = TSID( integer_literal );
    return ir::Value( type, TERM( intlit ) );
}

optional< ir::Value > Parser::parsePrefix( const Term&, const string& strlit, uint32_t )
{
    static auto type = TSID( string_literal );
    return ir::Value( type, TERM( strlit ) );
}

// Standalone (ie not part of a grammar construct that expects them such as a decl), unresolved identifiers
// end up here.
optional< ir::Value > Parser::parsePrefix( const Term&, const StringId& strid, uint32_t )
{
    // TODO location, poisoning
    cout << "undefined identifier '" << strid.c_str() << "'\n";
    return nullopt;
}

optional< uint32_t > Parser::getPrecedence( const Term& t, const pvec& vec )
{
    auto val = ValueFromIRExpr( t );
    if( !val )
        return nullopt;

    // If the term is an infix rule value, invoke its getPrecedence() function.
    auto rule = RuleFromValue( *val );
    if( !rule )
        return nullopt;

    if( !rule->isInfix() )
        return nullopt;

    return rule->getPrecedence( *this, t );
}

optional< ir::Value > Parser::parsePrefix( const Term& t, const pvec& vec, uint32_t prec )
{
    auto val = ValueFromIRExpr( t );
    if( !val )
        return nullopt;

    // If the term is a prefix rule value, invoke its parsePrefix() function.
    auto rule = RuleFromValue( *val );
    if( !rule )
        return nullopt;

    if( !rule->isPrefix() )
        return nullopt;

    return rule->parsePrefix( *this, t, prec );
}

optional< ir::Value > Parser::parseInfix( const Term& t, const pvec& vec, uint32_t prec )
{
    auto val = ValueFromIRExpr( t );
    if( !val )
        return nullopt;

    // If the term is an infix rule value, invoke its parseInfix() function.
    auto rule = RuleFromValue( *val );
    if( !rule )
        return nullopt;

    if( !rule->isInfix() )
        return nullopt;

    return rule->parseInfix( *this, t, prec );
}