summaryrefslogtreecommitdiff
path: root/src/parse/pattern.cpp
blob: 53c0cfe8f7cdb31cb20dadc5f73fb7950e21300b (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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/*
 * MRustC - Rust Compiler
 * - By John Hodge (Mutabah/thePowersGang)
 *
 * parse/pattern.cpp
 * - Parsing for patterns
 */
#include "common.hpp"
#include "parseerror.hpp"
#include <ast/expr.hpp> // To convert :expr

// NEWNODE is needed for the Value pattern type
typedef ::std::unique_ptr<AST::ExprNode>    ExprNodeP;
#define NEWNODE(type, ...)  ExprNodeP(new type(__VA_ARGS__))
using AST::ExprNode;



::AST::Pattern::TuplePat Parse_PatternTuple(TokenStream& lex, bool is_refutable);
AST::Pattern Parse_PatternReal_Slice(TokenStream& lex, bool is_refutable);
AST::Pattern Parse_PatternReal_Path(TokenStream& lex, ProtoSpan ps, AST::Path path, bool is_refutable);
AST::Pattern Parse_PatternReal(TokenStream& lex, bool is_refutable);
AST::Pattern Parse_PatternStruct(TokenStream& lex, ProtoSpan ps, AST::Path path, bool is_refutable);

AST::Pattern Parse_PatternReal(TokenStream& lex, bool is_refutable);
AST::Pattern Parse_PatternReal1(TokenStream& lex, bool is_refutable);


/// Parse a pattern
///
/// Examples:
/// - `Enum::Variant(a)`
/// - `(1, a)`
/// - `1 ... 2`
/// - `"string"`
/// - `mut x`
/// - `mut x @ 1 ... 2`
AST::Pattern Parse_Pattern(TokenStream& lex, bool is_refutable)
{
    TRACE_FUNCTION;
    auto ps = lex.start_span();

    Token   tok;
    tok = lex.getToken();

    if( tok.type() == TOK_IDENT && lex.lookahead(0) == TOK_EXCLAM )
    {
        lex.getToken();
        return AST::Pattern( AST::Pattern::TagMacro(), lex.end_span(ps), box$(Parse_MacroInvocation(ps, tok.istr(), lex)));
    }
    if( tok.type() == TOK_INTERPOLATED_PATTERN )
    {
        return mv$(tok.frag_pattern());
    }

    bool expect_bind = false;
    auto bind_type = AST::PatternBinding::Type::MOVE;
    bool is_mut = false;
    // 1. Mutablity + Reference
    if( tok.type() == TOK_RWORD_REF )
    {
        expect_bind = true;
        tok = lex.getToken();
        if( tok.type() == TOK_RWORD_MUT )
        {
            bind_type = AST::PatternBinding::Type::MUTREF;
            GET_TOK(tok, lex);
        }
        else
        {
            bind_type = AST::PatternBinding::Type::REF;
        }
    }
    else if( tok.type() == TOK_RWORD_MUT )
    {
        is_mut = true;
        expect_bind = true;
        GET_TOK(tok, lex);
    }
    else
    {
        // Fall through
    }

    AST::PatternBinding binding;
    // If a 'ref' or 'mut' annotation was seen, the next name must be a binding name
    if( expect_bind )
    {
        CHECK_TOK(tok, TOK_IDENT);
        auto bind_name = lex.get_ident(mv$(tok));
        // If there's no '@' after it, it's a name binding only (_ pattern)
        if( GET_TOK(tok, lex) != TOK_AT )
        {
            PUTBACK(tok, lex);
            return AST::Pattern(AST::Pattern::TagBind(), lex.end_span(ps), mv$(bind_name), bind_type, is_mut);
        }
        binding = AST::PatternBinding( mv$(bind_name), bind_type, is_mut );

        // '@' consumed, move on to next token
        GET_TOK(tok, lex);
    }
    // Otherwise, handle MaybeBind
    else if( tok.type() == TOK_IDENT )
    {
        switch( LOOK_AHEAD(lex) )
        {
        // Known path `ident::`
        case TOK_DOUBLE_COLON:
            break;
        // Known struct `Ident {` or `Ident (`
        case TOK_BRACE_OPEN:
        case TOK_PAREN_OPEN:
            break;
        // Known value `IDENT ...`
        case TOK_TRIPLE_DOT:
        case TOK_DOUBLE_DOT_EQUAL:
            break;
        // Known binding `ident @`
        case TOK_AT:
            binding = AST::PatternBinding( lex.get_ident(mv$(tok)), bind_type/*MOVE*/, is_mut/*false*/ );
            GET_TOK(tok, lex);  // '@'
            GET_TOK(tok, lex);  // Match lex.putback() below
            break;
        default: {  // Maybe bind
            auto name = lex.get_ident(mv$(tok));
            // if the pattern can be refuted (i.e this could be an enum variant), return MaybeBind
            if( is_refutable ) {
                assert(bind_type == ::AST::PatternBinding::Type::MOVE);
                assert(is_mut == false);
                return AST::Pattern(AST::Pattern::TagMaybeBind(), lex.end_span(ps), mv$(name));
            }
            // Otherwise, it IS a binding
            else {
                return AST::Pattern(AST::Pattern::TagBind(), lex.end_span(ps), mv$(name), bind_type, is_mut);
            }
            break;}
        }
    }
    else
    {
        // Otherwise, fall through
    }

    PUTBACK(tok, lex);
    auto pat = Parse_PatternReal(lex, is_refutable);
    pat.binding() = mv$(binding);
    return pat;
}

AST::Pattern Parse_PatternReal(TokenStream& lex, bool is_refutable)
{
    Token   tok;
    if( LOOK_AHEAD(lex) == TOK_INTERPOLATED_PATTERN )
    {
        GET_TOK(tok, lex);
        return mv$(tok.frag_pattern());
    }
    auto ps = lex.start_span();
    AST::Pattern    ret = Parse_PatternReal1(lex, is_refutable);
    if( (GET_TOK(tok, lex) == TOK_TRIPLE_DOT)
     || (TARGETVER_1_29 && tok.type() == TOK_DOUBLE_DOT_EQUAL)
      )
    {
        if( !ret.data().is_Value() )
            throw ParseError::Generic(lex, "Using '...' with a non-value on left");
        auto& ret_v = ret.data().as_Value();

        auto    right_pat = Parse_PatternReal1(lex, is_refutable);
        if( !right_pat.data().is_Value() )
            throw ParseError::Generic(lex, "Using '...' with a non-value on right");
        auto    rightval = mv$( right_pat.data().as_Value().start );
        ret_v.end = mv$(rightval);
        // TODO: use `ps` here?

        return ret;
    }
    else
    {
        PUTBACK(tok, lex);
        return ret;
    }
}
AST::Pattern Parse_PatternReal1(TokenStream& lex, bool is_refutable)
{
    TRACE_FUNCTION;
    auto ps = lex.start_span();

    Token   tok;
    AST::Path   path;

    switch( GET_TOK(tok, lex) )
    {
    case TOK_UNDERSCORE:
        return AST::Pattern( lex.end_span(ps), AST::Pattern::Data() );
    //case TOK_DOUBLE_DOT:
    //    return AST::Pattern( AST::Pattern::TagWildcard() );
    case TOK_RWORD_BOX:
        return AST::Pattern( AST::Pattern::TagBox(), lex.end_span(ps), Parse_Pattern(lex, is_refutable) );
    case TOK_DOUBLE_AMP:
        lex.putback(TOK_AMP);
    case TOK_AMP: {
        DEBUG("Ref");
        // NOTE: Falls back into "Pattern" not "PatternReal" to handle MaybeBind again
        bool is_mut = false;
        if( GET_TOK(tok, lex) == TOK_RWORD_MUT )
            is_mut = true;
        else
            PUTBACK(tok, lex);
        return AST::Pattern( AST::Pattern::TagReference(), lex.end_span(ps), is_mut, Parse_Pattern(lex, is_refutable) );
        }
    case TOK_RWORD_SELF:
    case TOK_RWORD_SUPER:
    case TOK_IDENT:
    case TOK_LT:
    case TOK_DOUBLE_LT:
    case TOK_INTERPOLATED_PATH:
        PUTBACK(tok, lex);
        return Parse_PatternReal_Path( lex, ps, Parse_Path(lex, PATH_GENERIC_EXPR), is_refutable );
    case TOK_DOUBLE_COLON:
        // 2. Paths are enum/struct names
        return Parse_PatternReal_Path( lex, ps, Parse_Path(lex, true, PATH_GENERIC_EXPR), is_refutable );
    case TOK_DASH:
        if(GET_TOK(tok, lex) == TOK_INTEGER)
        {
            auto dt = tok.datatype();
            // TODO: Ensure that the type is ANY or a signed integer
            return AST::Pattern( AST::Pattern::TagValue(), lex.end_span(ps), AST::Pattern::Value::make_Integer({dt, ~tok.intval() + 1}) );
        }
        else if( tok.type() == TOK_FLOAT )
        {
            return AST::Pattern( AST::Pattern::TagValue(), lex.end_span(ps), AST::Pattern::Value::make_Float({tok.datatype(), -tok.floatval()}) );
        }
        else
        {
            throw ParseError::Unexpected(lex, tok, {TOK_INTEGER, TOK_FLOAT});
        }
    case TOK_FLOAT:
        return AST::Pattern( AST::Pattern::TagValue(), lex.end_span(ps), AST::Pattern::Value::make_Float({tok.datatype(), tok.floatval()}) );
    case TOK_INTEGER:
        return AST::Pattern( AST::Pattern::TagValue(), lex.end_span(ps), AST::Pattern::Value::make_Integer({tok.datatype(), tok.intval()}) );
    case TOK_RWORD_TRUE:
        return AST::Pattern( AST::Pattern::TagValue(), lex.end_span(ps), AST::Pattern::Value::make_Integer({CORETYPE_BOOL, 1}) );
    case TOK_RWORD_FALSE:
        return AST::Pattern( AST::Pattern::TagValue(), lex.end_span(ps), AST::Pattern::Value::make_Integer({CORETYPE_BOOL, 0}) );
    case TOK_STRING:
        return AST::Pattern( AST::Pattern::TagValue(), lex.end_span(ps), AST::Pattern::Value::make_String( mv$(tok.str()) ) );
    case TOK_BYTESTRING:
        return AST::Pattern( AST::Pattern::TagValue(), lex.end_span(ps), AST::Pattern::Value::make_ByteString({ mv$(tok.str()) }) );
    case TOK_INTERPOLATED_EXPR: {
        auto e = tok.take_frag_node();
        if( auto* n = dynamic_cast<AST::ExprNode_String*>(e.get()) ) {
            return AST::Pattern( AST::Pattern::TagValue(), lex.end_span(ps), AST::Pattern::Value::make_String( mv$(n->m_value) ) );
        }
        else if( auto* n = dynamic_cast<AST::ExprNode_ByteString*>(e.get()) ) {
            return AST::Pattern( AST::Pattern::TagValue(), lex.end_span(ps), AST::Pattern::Value::make_ByteString({ mv$(n->m_value) }) );
        }
        else if( auto* n = dynamic_cast<AST::ExprNode_Bool*>(e.get()) ) {
            return AST::Pattern( AST::Pattern::TagValue(), lex.end_span(ps), AST::Pattern::Value::make_Integer({CORETYPE_BOOL, n->m_value}) );
        }
        else if( auto* n = dynamic_cast<AST::ExprNode_Integer*>(e.get()) ) {
            return AST::Pattern( AST::Pattern::TagValue(), lex.end_span(ps), AST::Pattern::Value::make_Integer({n->m_datatype, n->m_value}) );
        }
        else if( auto* n = dynamic_cast<AST::ExprNode_Float*>(e.get()) ) {
            return AST::Pattern( AST::Pattern::TagValue(), lex.end_span(ps), AST::Pattern::Value::make_Float({n->m_datatype, n->m_value}) );
        }
        else {
            TODO(lex.point_span(), "Convert :expr into a pattern value - " << *e);
        }
        } break;

    case TOK_PAREN_OPEN:
        return AST::Pattern( AST::Pattern::TagTuple(), lex.end_span(ps), Parse_PatternTuple(lex, is_refutable) );
    case TOK_SQUARE_OPEN:
        return Parse_PatternReal_Slice(lex, is_refutable);
    default:
        throw ParseError::Unexpected(lex, tok);
    }
}
AST::Pattern Parse_PatternReal_Path(TokenStream& lex, ProtoSpan ps, AST::Path path, bool is_refutable)
{
    Token   tok;

    switch( GET_TOK(tok, lex) )
    {
    case TOK_PAREN_OPEN:
        return AST::Pattern( AST::Pattern::TagNamedTuple(), lex.end_span(ps), mv$(path), Parse_PatternTuple(lex, is_refutable) );
    case TOK_BRACE_OPEN:
        return Parse_PatternStruct(lex, ps, mv$(path), is_refutable);
    default:
        PUTBACK(tok, lex);
        return AST::Pattern( AST::Pattern::TagValue(), lex.end_span(ps), AST::Pattern::Value::make_Named(mv$(path)) );
    }
}

AST::Pattern Parse_PatternReal_Slice(TokenStream& lex, bool is_refutable)
{
    auto ps = lex.start_span();
    Token   tok;

    ::std::vector< ::AST::Pattern>  leading;
    ::std::vector< ::AST::Pattern>  trailing;
    ::AST::PatternBinding   inner_binding;
    bool is_split = false;

    while(GET_TOK(tok, lex) != TOK_SQUARE_CLOSE)
    {
        bool has_binding = true;
        ::AST::PatternBinding  binding;
        if( tok.type() == TOK_RWORD_REF && lex.lookahead(0) == TOK_IDENT && lex.lookahead(1) == TOK_DOUBLE_DOT ) {
            GET_TOK(tok, lex);
            binding = ::AST::PatternBinding( lex.get_ident(mv$(tok)), ::AST::PatternBinding::Type::REF, false );
        }
        else if( tok.type() == TOK_IDENT && lex.lookahead(0) == TOK_DOUBLE_DOT) {
            binding = ::AST::PatternBinding( lex.get_ident(mv$(tok)), ::AST::PatternBinding::Type::MOVE, false );
        }
        else if( tok.type() == TOK_UNDERSCORE && lex.lookahead(0) == TOK_DOUBLE_DOT) {
            // No binding, but switching to trailing
        }
        else if( tok.type() == TOK_DOUBLE_DOT ) {
            // No binding, but switching to trailing
            PUTBACK(tok, lex);
        }
        else {
            has_binding = false;
        }

        if( has_binding ) {
            if(is_split)
                ERROR(lex.end_span(ps), E0000, "Multiple instances of .. in a slice pattern");

            inner_binding = mv$(binding);
            is_split = true;
            GET_TOK(tok, lex);  // TOK_DOUBLE_DOT
        }
        else {
            PUTBACK(tok, lex);
            if(!is_split) {
                leading.push_back( Parse_Pattern(lex, is_refutable) );
            }
            else {
                trailing.push_back( Parse_Pattern(lex, is_refutable) );
            }
        }

        if( GET_TOK(tok, lex) != TOK_COMMA )
            break;
    }
    CHECK_TOK(tok, TOK_SQUARE_CLOSE);

    if( is_split )
    {
        return ::AST::Pattern( lex.end_span(ps), ::AST::Pattern::Data::make_SplitSlice({ mv$(leading), mv$(inner_binding), mv$(trailing) }) );
    }
    else
    {
        assert( !inner_binding.is_valid() );
        assert( trailing.empty() );
        return ::AST::Pattern( lex.end_span(ps), ::AST::Pattern::Data::make_Slice({ mv$(leading) }) );
    }
}

::AST::Pattern::TuplePat Parse_PatternTuple(TokenStream& lex, bool is_refutable)
{
    TRACE_FUNCTION;
    auto sp = lex.start_span();
    Token tok;

    ::std::vector<AST::Pattern> leading;
    while( LOOK_AHEAD(lex) != TOK_PAREN_CLOSE && LOOK_AHEAD(lex) != TOK_DOUBLE_DOT )
    {
        leading.push_back( Parse_Pattern(lex, is_refutable) );

        if( GET_TOK(tok, lex) != TOK_COMMA ) {
            CHECK_TOK(tok, TOK_PAREN_CLOSE);
            return AST::Pattern::TuplePat { mv$(leading), false, {} };
        }
    }

    if( LOOK_AHEAD(lex) != TOK_DOUBLE_DOT )
    {
        GET_TOK(tok, lex);

        CHECK_TOK(tok, TOK_PAREN_CLOSE);
        return AST::Pattern::TuplePat { mv$(leading), false, {} };
    }
    GET_CHECK_TOK(tok, lex, TOK_DOUBLE_DOT);

    ::std::vector<AST::Pattern> trailing;
    if( GET_TOK(tok, lex) == TOK_COMMA )
    {
        while( LOOK_AHEAD(lex) != TOK_PAREN_CLOSE )
        {
            trailing.push_back( Parse_Pattern(lex, is_refutable) );

            if( GET_TOK(tok, lex) != TOK_COMMA ) {
                PUTBACK(tok, lex);
                break;
            }
        }
        GET_TOK(tok, lex);
    }

    CHECK_TOK(tok, TOK_PAREN_CLOSE);
    return ::AST::Pattern::TuplePat { mv$(leading), true, mv$(trailing) };
}

AST::Pattern Parse_PatternStruct(TokenStream& lex, ProtoSpan ps, AST::Path path, bool is_refutable)
{
    TRACE_FUNCTION;
    Token tok;

    // #![feature(relaxed_adts)]
    if( LOOK_AHEAD(lex) == TOK_INTEGER )
    {
        bool split_allowed = false;
        ::std::map<unsigned int, AST::Pattern> pats;
        while( GET_TOK(tok, lex) == TOK_INTEGER )
        {
            unsigned int ofs = static_cast<unsigned int>(tok.intval());
            GET_CHECK_TOK(tok, lex, TOK_COLON);
            auto val = Parse_Pattern(lex, is_refutable);
            if( ! pats.insert( ::std::make_pair(ofs, mv$(val)) ).second ) {
                ERROR(lex.point_span(), E0000, "Duplicate index");
            }

            if( GET_TOK(tok,lex) == TOK_BRACE_CLOSE )
                break;
            CHECK_TOK(tok, TOK_COMMA);
        }
        if( tok.type() == TOK_DOUBLE_DOT ) {
            split_allowed = true;
            GET_TOK(tok, lex);
        }
        CHECK_TOK(tok, TOK_BRACE_CLOSE);

        bool has_split = false;
        ::std::vector<AST::Pattern> leading;
        ::std::vector<AST::Pattern> trailing;
        unsigned int i = 0;
        for(auto& p : pats)
        {
            if( p.first != i ) {
                if( has_split || !split_allowed ) {
                    ERROR(lex.point_span(), E0000, "Missing index " << i);
                }
                has_split = true;
                i = p.first;
            }
            if( ! has_split ) {
                leading.push_back( mv$(p.second) );
            }
            else {
                trailing.push_back( mv$(p.second) );
            }
            i ++;
        }

        return AST::Pattern(AST::Pattern::TagNamedTuple(), lex.end_span(ps), mv$(path),  AST::Pattern::TuplePat { mv$(leading), has_split, mv$(trailing) });
    }

    bool is_exhaustive = true;
    ::std::vector< ::std::pair< RcString, AST::Pattern> >  subpats;
    do {
        GET_TOK(tok, lex);
        DEBUG("tok = " << tok);
        if( tok.type() == TOK_BRACE_CLOSE )
            break;
        if( tok.type() == TOK_DOUBLE_DOT ) {
            is_exhaustive = false;
            GET_TOK(tok, lex);
            break;
        }

        auto inner_ps = lex.start_span();
        bool is_short_bind = false;
        bool is_box = false;
        auto bind_type = AST::PatternBinding::Type::MOVE;
        bool is_mut = false;
        if( tok.type() == TOK_RWORD_BOX ) {
            is_box = true;
            is_short_bind = true;
            GET_TOK(tok, lex);
        }
        if( tok.type() == TOK_RWORD_REF ) {
            is_short_bind = true;
            GET_TOK(tok, lex);
            if( tok.type() == TOK_RWORD_MUT ) {
                bind_type = AST::PatternBinding::Type::MUTREF;
                GET_TOK(tok, lex);
            }
            else {
                bind_type = AST::PatternBinding::Type::REF;
            }
        }
        else if( tok.type() == TOK_RWORD_MUT ) {
            is_mut = true;
            is_short_bind = true;
            GET_TOK(tok, lex);
        }

        CHECK_TOK(tok, TOK_IDENT);
        auto field_ident = lex.get_ident(mv$(tok));
        RcString field_name;
        GET_TOK(tok, lex);

        AST::Pattern    pat;
        if( is_short_bind || tok.type() != TOK_COLON ) {
            PUTBACK(tok, lex);
            pat = AST::Pattern(lex.end_span(inner_ps), {});
            field_name = field_ident.name;
            pat.set_bind(mv$(field_ident), bind_type, is_mut);
            if( is_box )
            {
                pat = AST::Pattern(AST::Pattern::TagBox(), lex.end_span(inner_ps), mv$(pat));
            }
        }
        else {
            CHECK_TOK(tok, TOK_COLON);
            field_name = mv$(field_ident.name);
            pat = Parse_Pattern(lex, is_refutable);
        }

        subpats.push_back( ::std::make_pair(mv$(field_name), mv$(pat)) );
    } while( GET_TOK(tok, lex) == TOK_COMMA );
    CHECK_TOK(tok, TOK_BRACE_CLOSE);

    return AST::Pattern(AST::Pattern::TagStruct(), lex.end_span(ps), ::std::move(path), ::std::move(subpats), is_exhaustive);
}