summaryrefslogtreecommitdiff
path: root/src/macros.cpp
blob: c52f8836d1520dffa77d3f5ef0b82444af246b77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*
 */
#include "common.hpp"
#include "macros.hpp"
#include "parse/parseerror.hpp"
#include "parse/tokentree.hpp"
#include "parse/common.hpp"
#include "ast/ast.hpp"

typedef ::std::map< ::std::string, MacroRules>  t_macro_regs;

t_macro_regs g_macro_registrations;
const LList<AST::Module*>*  g_macro_module;

TokenTree   g_crate_path_tt = TokenTree({
    TokenTree(Token(TOK_DOUBLE_COLON)),
    TokenTree(Token(TOK_STRING, "--CRATE--")),
    });

void Macro_SetModule(const LList<AST::Module*>& mod)
{
    g_macro_module = &mod;
}

void Macro_InitDefaults()
{
    // try!() macro
    {
        MacroRule   rule;
        rule.m_pattern.push_back( MacroPatEnt("val", MacroPatEnt::PAT_EXPR) );
        // match $rule {
        rule.m_contents.push_back( MacroRuleEnt(Token(TOK_RWORD_MATCH)) );
        rule.m_contents.push_back( MacroRuleEnt("val") );
        rule.m_contents.push_back( MacroRuleEnt(Token(TOK_BRACE_OPEN)) );
        // Ok(v) => v,
        rule.m_contents.push_back( MacroRuleEnt(Token(TOK_IDENT, "Ok")) );
        rule.m_contents.push_back( MacroRuleEnt(Token(TOK_PAREN_OPEN)) );
        rule.m_contents.push_back( MacroRuleEnt(Token(TOK_IDENT, "v")) );
        rule.m_contents.push_back( MacroRuleEnt(Token(TOK_PAREN_CLOSE)) );
        rule.m_contents.push_back( MacroRuleEnt(Token(TOK_FATARROW)) );
        rule.m_contents.push_back( MacroRuleEnt(Token(TOK_IDENT, "v")) );
        rule.m_contents.push_back( MacroRuleEnt(Token(TOK_COMMA)) );
        // Err(e) => return Err(r),
        rule.m_contents.push_back( MacroRuleEnt(Token(TOK_IDENT, "Err")) );
        rule.m_contents.push_back( MacroRuleEnt(Token(TOK_PAREN_OPEN)) );
        rule.m_contents.push_back( MacroRuleEnt(Token(TOK_IDENT, "e")) );
        rule.m_contents.push_back( MacroRuleEnt(Token(TOK_PAREN_CLOSE)) );
        rule.m_contents.push_back( MacroRuleEnt(Token(TOK_FATARROW)) );
        rule.m_contents.push_back( MacroRuleEnt(Token(TOK_RWORD_RETURN)) );
        rule.m_contents.push_back( MacroRuleEnt(Token(TOK_IDENT, "Err")) );
        rule.m_contents.push_back( MacroRuleEnt(Token(TOK_PAREN_OPEN)) );
        rule.m_contents.push_back( MacroRuleEnt(Token(TOK_IDENT, "e")) );
        rule.m_contents.push_back( MacroRuleEnt(Token(TOK_PAREN_CLOSE)) );
        rule.m_contents.push_back( MacroRuleEnt(Token(TOK_COMMA)) );
        // }
        rule.m_contents.push_back( MacroRuleEnt(Token(TOK_BRACE_CLOSE)) );
        MacroRules  rules;
        rules.push_back(rule);
        g_macro_registrations.insert( make_pair(::std::string("try"), rules));
    }
    
    // panic!() "macro"
    {
        MacroRule   rule;
        rule.m_pattern.push_back( MacroPatEnt(Token(TOK_NULL), false, {
            MacroPatEnt("tt", MacroPatEnt::PAT_TT), 
            } ) );
        rule.m_contents.push_back( MacroRuleEnt(Token(TOK_PAREN_OPEN)) );
        rule.m_contents.push_back( MacroRuleEnt(Token(TOK_PAREN_CLOSE)) );
        
        
        MacroRules  rules;
        rules.push_back(rule);
        g_macro_registrations.insert( make_pair(::std::string("panic"), rules));
    }
}

void Macro_HandlePattern(TTStream& lex, const MacroPatEnt& pat, unsigned int layer, MacroExpander::t_mappings& bound_tts)
{
    Token   tok;
    TokenTree   val;
    switch(pat.type)
    {
    case MacroPatEnt::PAT_TOKEN:
        DEBUG("Token " << pat.tok);
        GET_CHECK_TOK(tok, lex, pat.tok.type());
        break;
    case MacroPatEnt::PAT_LOOP:
    //case MacroPatEnt::PAT_OPTLOOP:
        if( layer )
        {
            throw ParseError::BugCheck("Nested macro loop");
        }
        else
        {
            DEBUG("Loop");
            for(;;)
            {
                DEBUG("Try");
                TTStream    saved = lex;
                try {
                    Macro_HandlePattern(lex, pat.subpats[0], layer+1, bound_tts);
                }
                catch(const ParseError::Base& e) {
                    DEBUG("Breakout");
                    lex = saved;
                    break;
                }
                for( unsigned int i = 1; i < pat.subpats.size(); i ++ )
                {
                    Macro_HandlePattern(lex, pat.subpats[i], layer+1, bound_tts);
                }
                DEBUG("succ");
                if( pat.tok.type() != TOK_NULL )
                {
                    if( GET_TOK(tok, lex) != pat.tok.type() )
                    {
                        lex.putback(tok);
                        break;
                    }
                }
            }
            DEBUG("Done");
        }
        break;
    
    case MacroPatEnt::PAT_TT:
        DEBUG("TT");
        if( GET_TOK(tok, lex) == TOK_EOF )
            throw ParseError::Unexpected(lex, TOK_EOF);
        else
            lex.putback(tok);
        val = Parse_TT(lex, false);
        if(0)
    case MacroPatEnt::PAT_TYPE:
        val = Parse_TT_Type(lex);
        if(0)
    case MacroPatEnt::PAT_EXPR:
        val = Parse_TT_Expr(lex);
        if(0)
    case MacroPatEnt::PAT_STMT:
        val = Parse_TT_Stmt(lex);
        if(0)
    case MacroPatEnt::PAT_PATH:
        val = Parse_TT_Path(lex, false);    // non-expr mode
        if(0)
    case MacroPatEnt::PAT_BLOCK:
        val = Parse_TT_Block(lex);
        if(0)
    case MacroPatEnt::PAT_IDENT:
        {
            GET_CHECK_TOK(tok, lex, TOK_IDENT);
            val = TokenTree(tok);
        }
        bound_tts.insert( ::std::make_pair( ::std::make_pair(layer, pat.name.c_str()), ::std::move(val) ) );
        break;
    
    //default:
    //    throw ParseError::Todo("full macro pattern matching");
    }

}

MacroExpander Macro_InvokeInt(const char *name, const MacroRules& rules, TokenTree input)
{
    TRACE_FUNCTION;
    
    // 2. Check input token tree against possible variants
    // 3. Bind names
    // 4. Return expander
    int i = 0;
    for(const auto& rule : rules)
    {
        Token   tok;
        // Create token stream for input tree
        TTStream    lex(input);
        /*
        enum eTokenType close;
        switch( GET_TOK(tok, lex) )
        {
        case TOK_PAREN_OPEN:    close = TOK_PAREN_CLOSE;    break;
        case TOK_BRACE_OPEN:    close = TOK_BRACE_CLOSE;    break;
        default:
            throw ParseError::Unexpected(lex, tok);
        }
        */
        MacroExpander::t_mappings   bound_tts;
        // Parse according to rules
        try
        {
            for(const auto& pat : rule.m_pattern)
            {
                Macro_HandlePattern(lex, pat, 0, bound_tts);
            }
            
            //GET_CHECK_TOK(tok, lex, close);
            GET_CHECK_TOK(tok, lex, TOK_EOF);
            DEBUG( rule.m_contents.size() << " rule contents bound to " << bound_tts.size() << " values - " << name );
            return MacroExpander(rule.m_contents, bound_tts, g_crate_path_tt);
        }
        catch(const ParseError::Base& e)
        {
            DEBUG("Parse of rule " << i << " of " << name <<" failed - " << e.what());
        }
        i ++;
    }
    DEBUG("");
    throw ParseError::Todo("Error when macro fails to match");
}

MacroExpander Macro_Invoke(const char* name, TokenTree input)
{
    // XXX: EVIL HACK! - This should be removed when std loading is implemented
    if( g_macro_registrations.size() == 0 ) {
        Macro_InitDefaults();
    }
    
    // Look for macro in builtins
    t_macro_regs::iterator macro_reg = g_macro_registrations.find(name);
    if( macro_reg != g_macro_registrations.end() )
    {
        return Macro_InvokeInt(macro_reg->first.c_str(), macro_reg->second, input);
    }
    
    // Search import list
    for( auto ent = g_macro_module; ent; ent = ent->m_prev )
    {
        const AST::Module& mm = *ent->m_item;
        for( const auto &m : mm.macros() )
        {
            DEBUG("" << m.name);
            if( m.name == name )
            {
                return Macro_InvokeInt(m.name.c_str(), m.data, input);
            }
        }
        
        for( const auto& mi : mm.macro_imports_res() )
        {
            DEBUG("" << mi.name);
            if( mi.name == name )
            {
                return Macro_InvokeInt(mi.name.c_str(), *mi.data, input);
            }
        }
    }

    throw ParseError::Generic( ::std::string("Macro '") + name + "' was not found" );
}

Position MacroExpander::getPosition() const
{
    return Position("Macro", 0);
}
Token MacroExpander::realGetToken()
{
    if( m_ttstream.get() )
    {
        DEBUG("TTStream set");
        Token rv = m_ttstream->getToken();
        if( rv.type() != TOK_EOF )
            return rv;
        m_ttstream.reset();
    }
    //DEBUG("ofs " << m_offsets << " < " << m_root_contents.size());
    
    // Check offset of lowest layer
    while(m_offsets.size() > 0)
    {
        assert(m_offsets.size() > 0);
        unsigned int layer = m_offsets.size() - 1;
        // - If that layer has hit its limit
        const auto& ents = *m_cur_ents;
        size_t idx = m_offsets.back().first;
        m_offsets.back().first ++;
    
        //DEBUG("ents = " << ents);
        
        // Check if limit has been reached
        if( idx < ents.size() )
        {
            const auto& ent = ents[idx];
            // - If not, just handle the next entry
            // Check type of entry
            if( ent.name != "" )
            {
                // - Name
                // HACK: Handle $crate with a special name
                if( ent.name == "*crate" ) {
                    m_ttstream.reset( new TTStream(m_crate_path) );
                    return m_ttstream->getToken();
                }
                else {
                    const size_t iter_idx = m_offsets.back().second;
                    const auto tt_i = m_mappings.equal_range( ::std::make_pair(layer, ent.name.c_str()) );
                    if( tt_i.first == tt_i.second )
                        throw ParseError::Generic( FMT("Cannot find mapping name: " << ent.name << " for layer " << layer) );
                    
                    size_t i = 0;
                    for( auto it = tt_i.first; it != tt_i.second; it ++ )
                    {
                        if( i == iter_idx )
                        {
                            m_ttstream.reset( new TTStream(it->second) );
                            return m_ttstream->getToken();
                        }
                        i ++;
                    }
                    throw ParseError::Generic( FMT("Cannot find mapping name: " << ent.name << " for layer " << layer) );
                }
            }
            else if( ent.subpats.size() != 0 )
            {
                // New layer
                // - Push an offset
                m_offsets.push_back( ::std::make_pair(0, 0) );
                // - Save the current layer
                m_cur_ents = getCurLayer();
                // - Restart loop for new layer
            }
            else
            {
                // Raw token
                return ent.tok;
            }
            // Fall through for loop
        }
        else
        {
            // - Otherwise, restart/end loop and fall through
            unsigned int layer_max = m_layer_counts.at(layer);
            if( m_offsets.back().second + 1 < layer_max )
            {
                DEBUG("Restart layer");
                m_offsets.back().first = 0;
                m_offsets.back().second ++;
                // Fall through and restart layer
            }
            else
            {
                DEBUG("Terminate layer");
                // Terminate loop, fall through to lower layers
                m_offsets.pop_back();
                // - Special case: End of macro, avoid issues
                if( m_offsets.size() == 0 )
                    break;
                m_cur_ents = getCurLayer();
            }
        }
    } // while( m_offsets NONEMPTY )
    
    DEBUG("EOF");
    return Token(TOK_EOF);
}

/// Count the number of names at each layer
void MacroExpander::prep_counts()
{
    struct TMP {
        size_t  count;
        const char *first_name;
    };
    ::std::vector<TMP>  counts;
    
    for( const auto& ent : m_mappings )
    {
        unsigned int layer = ent.first.first;
        const char *name = ent.first.second;
    
        if( layer >= counts.size() )
        {
            counts.resize(layer+1);
        }
        auto& l = counts[layer];
        if( l.first_name == NULL || ::std::strcmp(l.first_name, name) == 0 )
        {
            l.first_name = name;
            l.count += 1;
        }
    }
    
    for( const auto& l : counts )
    {
        m_layer_counts.push_back(l.count);
    }
}
const ::std::vector<MacroRuleEnt>* MacroExpander::getCurLayer() const
{
    assert( m_offsets.size() > 0 );
    const ::std::vector<MacroRuleEnt>* ents = &m_root_contents;
    for( unsigned int i = 0; i < m_offsets.size()-1; i ++ )
    {
        unsigned int ofs = m_offsets[i].first;
        //DEBUG(i << " ofs=" << ofs << " / " << ents->size());
        assert( ofs > 0 && ofs <= ents->size() );
        ents = &(*ents)[ofs-1].subpats;
        //DEBUG("ents = " << ents);
    }
    return ents;
}

SERIALISE_TYPE_S(MacroRule, {
    s.item(m_pattern);
    s.item(m_contents);
});


void operator%(Serialiser& s, MacroPatEnt::Type c) {
    switch(c) {
    #define _(v) case MacroPatEnt::v: s << #v; return
    _(PAT_TOKEN);
    _(PAT_TT);
    _(PAT_EXPR);
    _(PAT_LOOP);
    //_(PAT_OPTLOOP);
    _(PAT_STMT);
    _(PAT_PATH);
    _(PAT_BLOCK);
    _(PAT_IDENT);
    #undef _
    }
}
void operator%(::Deserialiser& s, MacroPatEnt::Type& c) {
    ::std::string   n;
    s.item(n);
    #define _(v) else if(n == #v) c = MacroPatEnt::v
    if(0) ;
    _(PAT_TOKEN);
    _(PAT_TT);
    _(PAT_EXPR);
    _(PAT_LOOP);
    //_(PAT_OPTLOOP);
    _(PAT_STMT);
    _(PAT_PATH);
    _(PAT_BLOCK);
    _(PAT_IDENT);
    else
        throw ::std::runtime_error( FMT("No conversion for '" << n << "'") );
    #undef _
}
SERIALISE_TYPE_S(MacroPatEnt, {
    s % type;
    s.item(name);
    s.item(tok);
    s.item(subpats);
});

SERIALISE_TYPE_S(MacroRuleEnt, {
    s.item(name);
    s.item(tok);
    s.item(subpats);
});