<commit_before>
#include <synext.hpp>
#include "../ast/expr.hpp"
#include "../ast/ast.hpp"
#include "../parse/common.hpp"
#include "macro_rules.hpp"

class CMacroRulesExpander:
    public ExpandProcMacro
{
    bool    expand_early() const override { return true; }
    
    ::std::unique_ptr<TokenStream> expand(Span sp, const ::AST::Crate& crate, const ::std::string& ident, const TokenTree& tt, AST::Module& mod) override
    {
        if( ident == "" )
            ERROR(sp, E0000, "macro_rules! requires an identifier" );
        
        TTStream    lex(tt);
        auto mac = Parse_MacroRules(lex);
        mod.add_macro( false, ident, mac );
        
        return box$( TTStreamO(TokenTree()) );
    }
};

class CMacroUseHandler:
    public ExpandDecorator
{
    AttrStage stage() const override { return AttrStage::EarlyPost; }
    
    void handle(const AST::MetaItem& mi, ::AST::Crate& crate, const AST::Path& path, AST::Module& mod, AST::Item& i) const override
    {
        TRACE_FUNCTION_F("path=" << path);
        if( !i.is_Module() )
            throw ::std::runtime_error("ERROR: Use of #[macro_use] on non-module");
        
        const auto& submod = i.as_Module().e;
        
        if( mi.has_sub_items() )
        {
            throw ::std::runtime_error("TODO: #[macro_use]");
        }
        else
        {
            for( const auto& mr : submod.macros() )
            {
                DEBUG("Imported " << mr.name);
                mod.add_macro_import( mr.name, mr.data );
            }
            for( const auto& mri : submod.macro_imports_res() )
            {
                DEBUG("Imported " << mri.name << " (propagate)");
                mod.add_macro_import( mri.name, *mri.data );
            }
        }
    }
    
};

::std::unique_ptr<TokenStream>  Macro_Invoke(const char* name, const MacroRules& rules, const TokenTree& tt, AST::Module& mod)
{
    return Macro_InvokeRules(name, rules, tt);
}


STATIC_MACRO("macro_rules", CMacroRulesExpander);
STATIC_DECORATOR("macro_use", CMacroUseHandler);
<commit_msg>Expand - macro_use with ident list<commit_after>
#include <synext.hpp>
#include "../ast/expr.hpp"
#include "../ast/ast.hpp"
#include "../parse/common.hpp"
#include "macro_rules.hpp"

class CMacroRulesExpander:
    public ExpandProcMacro
{
    bool    expand_early() const override { return true; }
    
    ::std::unique_ptr<TokenStream> expand(Span sp, const ::AST::Crate& crate, const ::std::string& ident, const TokenTree& tt, AST::Module& mod) override
    {
        if( ident == "" )
            ERROR(sp, E0000, "macro_rules! requires an identifier" );
        
        TTStream    lex(tt);
        auto mac = Parse_MacroRules(lex);
        mod.add_macro( false, ident, mac );
        
        return box$( TTStreamO(TokenTree()) );
    }
};

class CMacroUseHandler:
    public ExpandDecorator
{
    AttrStage stage() const override { return AttrStage::EarlyPost; }
    
    void handle(const AST::MetaItem& mi, ::AST::Crate& crate, const AST::Path& path, AST::Module& mod, AST::Item& i) const override
    {
        TRACE_FUNCTION_F("path=" << path);
        if( !i.is_Module() )
            throw ::std::runtime_error("ERROR: Use of #[macro_use] on non-module");
        
        const auto& submod = i.as_Module().e;
        
        if( mi.has_sub_items() )
        {
            for( const auto& si : mi.items() )
            {
                const auto& name = si.name();
                for( const auto& mr : submod.macros() )
                {
                    if( mr.name == name ) {
                        DEBUG("Imported " << mr.name);
                        mod.add_macro_import( mr.name, mr.data );
                        goto _good;
                    }
                }
                for( const auto& mri : submod.macro_imports_res() )
                {
                    if( mri.name == name ) {
                        DEBUG("Imported " << mri.name << " (propagate)");
                        mod.add_macro_import( mri.name, *mri.data );
                        goto _good;
                    }
                }
                ERROR(Span(), E0000, "Couldn't find macro " << name);
            _good:
                (void)0;
            }
        }
        else
        {
            for( const auto& mr : submod.macros() )
            {
                DEBUG("Imported " << mr.name);
                mod.add_macro_import( mr.name, mr.data );
            }
            for( const auto& mri : submod.macro_imports_res() )
            {
                DEBUG("Imported " << mri.name << " (propagate)");
                mod.add_macro_import( mri.name, *mri.data );
            }
        }
    }
    
};

::std::unique_ptr<TokenStream>  Macro_Invoke(const char* name, const MacroRules& rules, const TokenTree& tt, AST::Module& mod)
{
    return Macro_InvokeRules(name, rules, tt);
}


STATIC_MACRO("macro_rules", CMacroRulesExpander);
STATIC_DECORATOR("macro_use", CMacroUseHandler);

{'fraction_non_alphanumeric': 0.13496686081542478, 'fraction_numerical': 0.0026109660574412533, 'mean_word_length': 1.8869565217391304, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5365522', 'n_tokens_mistral': 1620, 'n_tokens_neox': 1530, 'n_words': 335}