Goose  Artifact [b7b9e4cc3b]

Artifact b7b9e4cc3b5187bbabecd3edf974a05a75ae83e29f724924efe8d7cceda0d24a:

  • File bs/builtins/statements/using.cpp — part of check-in [972c1d8b47] at 2019-08-12 20:15:11 on branch trunk —
    • Implemented a system to temporarily silence error outputs.
    • Syntax errors now silence further errors encountered while parsing the same block and nested children blocks.
    • All errors are also silenced while speculatively attempting to evaluate invocations at compilation time.
    (user: achavasse size: 5853)

#include "builtins/builtins.h"
#include "parse/parse.h"
#include "precedence.h"

using namespace empathy;
using namespace empathy::ir;
using namespace empathy::parse;

namespace empathy::builtins
{
    void SetupUsingStmt( Env& e )
    {
        auto handleUsing = []( Parser& p, uint32_t locationId, uint32_t prec )
        {
            auto& dm = DiagnosticsManager::GetInstance();

            auto nameTerm = p.resolver()->consume();
            if( !nameTerm )
            {
                dm.emitSyntaxErrorMessage( p.resolver()->getCurrentLocation(), "expected an identifier.", 0 );
                return false;
            }

            const auto* name = get_if< StringId >( &nameTerm->first );
            if( !name )
            {
                dm.emitSyntaxErrorMessage( nameTerm->second, "expected an identifier.", 0 );
                return false;
            }

            auto eqTerm = p.resolver()->consumeUnresolved();
            if( !eqTerm )
            {
                dm.emitSyntaxErrorMessage( p.resolver()->getCurrentLocation(), "expected '='.", 0 );
                return false;
            }

            const auto* eq = get_if< StringId >( &eqTerm->first );
            if( !eq || *eq != "="_sid )
            {
                dm.emitSyntaxErrorMessage( eqTerm->second, "expected '='.", 0 );
                return false;
            }

            p.resolver()->consumeNewLines();

            // Store all the units following the equal sign until the next semicolon or newline.
            vector< TermLoc > toks;
            while( !p.resolver()->eos() )
            {
                const auto& tok = p.resolver()->lookAheadRaw();
                if( !tok )
                    break;

                const auto* delim = get_if< Delimiter >( &tok->first );
                if( delim && *delim == Delimiter::Newline )
                {
                    p.resolver()->consumeRaw();
                    break;
                }

                const auto* id = get_if< StringId >( &tok->first );
                if( id && *id == ";"_sid )
                    break;

                auto g = p.resolver()->consumeUnit();
                move( g.begin(), g.end(), back_inserter( toks ) );
            }

            if( toks.empty() )
            {
                // TODO maybe use the current location after consuming the equal sign as the location here
                dm.emitSyntaxErrorMessage( eqTerm->second, "expected an expression.", 0 );
                return false;
            }

            const auto& context = p.resolver()->context();

            // Create a local identity for the constant, from which the current identity will be visible.
            // This is to avoid anything declared from within the using expression to leak outside,
            // and also to make sure that whenever it will be parsed, it will have access to the current
            // context in which it has been declared.
            auto localIdentity = AppendToVectorTerm( context.identity(), nameTerm->first );
            context.env()->addVisibilityRule( context.identity(), localIdentity );

            variant< vector< TermLoc >, Term > content = move( toks );
            auto nameSid = *name;
            auto nameLoc = nameTerm->second;

            bool bInUse = false;

            auto UsingValProvider = [content, localIdentity, bInUse, nameSid, nameLoc]( Env& e, const Term& identity, const Term& contextId, Term& result ) mutable
            {
                auto& dm = DiagnosticsManager::GetInstance();

                if( holds_alternative< vector< TermLoc > >( content ) )
                {
                    if( bInUse )
                    {
                        dm.emitErrorMessage( nameLoc, "recursive using expression." );
                        return Env::Status::NoMatch;
                    }

                    bInUse = true;

                    Context localContext( e.shared_from_this(), localIdentity );
                    auto tokProvider = lex::MakeVectorAdapter( get< vector< TermLoc > >( content ) );
                    auto r = make_shared< parse::Resolver >( tokProvider, localContext );
                    Parser p( r );

                    if( !p.parseExpression() )
                    {
                        // TODO: Perhaps return a poison value instead of nothing?
                        dm.emitErrorMessage( nameLoc, "invalid expression." );
                        return Env::Status::NoMatch;
                    }

                    auto result = p.peekLastValue();
                    if( !result )
                    {
                        // TODO: Perhaps return a poison value instead of nothing?
                        dm.emitErrorMessage( nameLoc, "invalid expression." );
                        return Env::Status::NoMatch;
                    }

                    if( !result->isConstant() )
                    {
                        // TODO: Perhaps return a poison value instead of nothing?
                        dm.emitErrorMessage( nameLoc, "using expression doesn't evaluate to a constant." );
                        return Env::Status::NoMatch;
                    }

                    content = ValueToIRExpr( *result );
                    bInUse = false;
                }

                result = get< Term >( content );
                return Env::Status::Success;
            };

            context.env()->storeValue( localIdentity, ANYTERM( _ ),
                make_shared< Env::ValueProvider >( move( UsingValProvider ) ) );

            return true;
        };

        Rule r( handleUsing );
        auto ruleVal = ToValue( move( r ) );
        auto ruleTerm = ValueToIRExpr( ruleVal );
        e.storeValue( AppendToVectorTerm( RootIdentity(), TSID( using ) ), ANYTERM( _ ), ruleTerm );
    }
}