summaryrefslogtreecommitdiff
path: root/src/macro_rules/mod.cpp
blob: fcea5b25bb52a1e554b77df7d61a2fd72af7dfaa (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
/*
 * MRustC - Mutabah's Rust Compiler
 * - By John Hodge (Mutabah/thePowersGang)
 *
 * macro_rules/mod.cpp
 * - Top-level handling for macro_rules macros
 */
#include <common.hpp>
#include "macro_rules.hpp"
#include <parse/parseerror.hpp>
#include <parse/tokentree.hpp>
#include <parse/common.hpp>
#include <limits.h>

#include "pattern_checks.hpp"

bool is_token_path(eTokenType tt) {
    switch(tt)
    {
    case TOK_IDENT:
    case TOK_DOUBLE_COLON:
    case TOK_LT:
    case TOK_DOUBLE_LT:
    case TOK_RWORD_SELF:
    case TOK_RWORD_SUPER:
    case TOK_INTERPOLATED_PATH:
        return true;
    default:
        return false;
    }
}
bool is_token_pat(eTokenType tt) {
    if( is_token_path(tt) )
        return true;
    switch( tt )
    {
    case TOK_PAREN_OPEN:
    case TOK_SQUARE_OPEN:

    case TOK_UNDERSCORE:
    case TOK_AMP:
    case TOK_RWORD_BOX:
    case TOK_RWORD_REF:
    case TOK_RWORD_MUT:
    case TOK_STRING:
    case TOK_INTEGER:
    case TOK_CHAR:
    case TOK_INTERPOLATED_PATTERN:
        return true;
    default:
        return false;
    }
}
bool is_token_type(eTokenType tt) {
    if( is_token_path(tt) )
        return true;
    switch( tt )
    {
    case TOK_PAREN_OPEN:
    case TOK_SQUARE_OPEN:
    case TOK_STAR:
    case TOK_AMP:
    case TOK_RWORD_EXTERN:
    case TOK_RWORD_UNSAFE:
    case TOK_RWORD_FN:
    case TOK_INTERPOLATED_TYPE:
        return true;
    default:
        return false;
    }
}
bool is_token_expr(eTokenType tt) {
    if( is_token_path(tt) )
        return true;
    switch( tt )
    {
    // Leading unary operators
    case TOK_AMP:   // Borrow
    case TOK_STAR:  // Deref
    case TOK_DASH:  // Negate
    case TOK_EXCLAM:    // Invert
    case TOK_RWORD_BOX: // Box
    // Composite values
    case TOK_PAREN_OPEN:    // Parenthesised
    case TOK_SQUARE_OPEN:   // Array

    // Flow
    case TOK_RWORD_RETURN:
    case TOK_RWORD_BREAK:
    case TOK_RWORD_CONTINUE:

    // Blocks
    case TOK_BRACE_OPEN:
    case TOK_RWORD_MATCH:
    case TOK_RWORD_IF:
    case TOK_RWORD_FOR:
    case TOK_RWORD_WHILE:
    case TOK_RWORD_LOOP:
    case TOK_RWORD_UNSAFE:

    // Closures
    case TOK_RWORD_MOVE:
    case TOK_PIPE:
    case TOK_DOUBLE_PIPE:

    // Literal tokens
    case TOK_INTEGER:
    case TOK_FLOAT:
    case TOK_STRING:
    case TOK_BYTESTRING:
    case TOK_RWORD_TRUE:
    case TOK_RWORD_FALSE:

    case TOK_INTERPOLATED_EXPR:
        return true;
    default:
        return false;
    }
}
bool is_token_stmt(eTokenType tt) {
    if( is_token_expr(tt) )
        return true;
    switch( tt )
    {
    case TOK_BRACE_OPEN:
    case TOK_RWORD_LET:
    case TOK_INTERPOLATED_STMT:
        return true;
    default:
        return false;
    }
}

bool is_token_item(eTokenType tt) {
    switch( tt )
    {
    case TOK_HASH:

    case TOK_RWORD_PUB:
    case TOK_RWORD_UNSAFE:
    case TOK_RWORD_TYPE:
    case TOK_RWORD_CONST:
    case TOK_RWORD_STATIC:
    case TOK_RWORD_FN:
    case TOK_RWORD_STRUCT:
    case TOK_RWORD_ENUM:
    case TOK_RWORD_TRAIT:
    case TOK_RWORD_MOD:
    case TOK_RWORD_USE:
    case TOK_RWORD_EXTERN:
    case TOK_RWORD_IMPL:
    // TODO: more?
    case TOK_INTERPOLATED_ITEM:
        return true;
    default:
        return false;
    }
}
bool is_token_vis(eTokenType tt) {
    switch(tt)
    {
    case TOK_RWORD_PUB:
    case TOK_INTERPOLATED_VIS:
        return true;
    default:
        return true;    // TODO: Is this true? it can capture just nothing
    }
}

MacroRulesPtr::~MacroRulesPtr()
{
    if(m_ptr)
    {
        delete m_ptr;
        m_ptr = nullptr;
    }
}

::std::ostream& operator<<(::std::ostream& os, const MacroPatEnt& x)
{
    switch(x.type)
    {
    case MacroPatEnt::PAT_TOKEN: os << "=" << x.tok; break;
    case MacroPatEnt::PAT_LOOP:  os << "loop w/ "  << x.tok << " [" << x.subpats << "]";  break;
    default:
        os << "$" << x.name << ":";
        switch(x.type)
        {
        case MacroPatEnt::PAT_TOKEN: throw "";
        case MacroPatEnt::PAT_LOOP:  throw "";
        case MacroPatEnt::PAT_TT:    os << "tt";    break;
        case MacroPatEnt::PAT_PAT:   os << "pat";   break;
        case MacroPatEnt::PAT_IDENT: os << "ident"; break;
        case MacroPatEnt::PAT_PATH:  os << "path";  break;
        case MacroPatEnt::PAT_TYPE:  os << "type";  break;
        case MacroPatEnt::PAT_EXPR:  os << "expr";  break;
        case MacroPatEnt::PAT_STMT:  os << "stmt";  break;
        case MacroPatEnt::PAT_BLOCK: os << "block"; break;
        case MacroPatEnt::PAT_META:  os << "meta"; break;
        case MacroPatEnt::PAT_ITEM:  os << "item"; break;
        case MacroPatEnt::PAT_VIS:   os << "vis"; break;
        }
        break;
    }
    return os;
}
::std::ostream& operator<<(::std::ostream& os, const MacroPatEnt::Type& x)
{
    switch(x)
    {
    case MacroPatEnt::PAT_TOKEN: os << "PAT_TOKEN"; break;
    case MacroPatEnt::PAT_LOOP:  os << "PAT_LOOP";  break;
    case MacroPatEnt::PAT_TT:    os << "PAT_TT";    break;
    case MacroPatEnt::PAT_PAT:   os << "PAT_PAT";   break;
    case MacroPatEnt::PAT_IDENT: os << "PAT_IDENT"; break;
    case MacroPatEnt::PAT_PATH:  os << "PAT_PATH";  break;
    case MacroPatEnt::PAT_TYPE:  os << "PAT_TYPE";  break;
    case MacroPatEnt::PAT_EXPR:  os << "PAT_EXPR";  break;
    case MacroPatEnt::PAT_STMT:  os << "PAT_STMT";  break;
    case MacroPatEnt::PAT_BLOCK: os << "PAT_BLOCK"; break;
    case MacroPatEnt::PAT_META:  os << "PAT_META"; break;
    case MacroPatEnt::PAT_ITEM:  os << "PAT_ITEM"; break;
    case MacroPatEnt::PAT_VIS:   os << "PAT_VIS"; break;
    }
    return os;
}

::std::ostream& operator<<(::std::ostream& os, const MacroExpansionEnt& x)
{
    TU_MATCH( MacroExpansionEnt, (x), (e),
    (Token,
        os << "=" << e;
        ),
    (NamedValue,
        if( e >> 30 ) {
            os << "$crate";
        }
        else {
            os << "$" << e;
        }
        ),
    (Loop,
        os << "${" << *e.variables.begin() << "}(" << e.entries << ") " << e.joiner;
        )
    )
    return os;
}

MacroRules::~MacroRules()
{
}