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
|
/*
* MRustC - Rust Compiler
* - By John Hodge (Mutabah/thePowersGang)
*
* parse/paths.cpp
* - Parsing for module paths
*/
#include "parseerror.hpp"
#include "common.hpp"
#include "../ast/ast.hpp"
AST::Path Parse_Path(TokenStream& lex, eParsePathGenericMode generic_mode);
AST::Path Parse_Path(TokenStream& lex, bool is_abs, eParsePathGenericMode generic_mode);
AST::Path Parse_PathFrom(TokenStream& lex, AST::Path path, eParsePathGenericMode generic_mode);
::std::vector<TypeRef> Parse_Path_GenericList(TokenStream& lex);
AST::Path Parse_Path(TokenStream& lex, eParsePathGenericMode generic_mode)
{
Token tok;
if( GET_TOK(tok, lex) == TOK_DOUBLE_COLON )
return Parse_Path(lex, true, generic_mode);
else
{
lex.putback(tok);
return Parse_Path(lex, false, generic_mode);
}
}
AST::Path Parse_Path(TokenStream& lex, bool is_abs, eParsePathGenericMode generic_mode)
{
if( is_abs )
{
Token tok;
if( GET_TOK(tok, lex) == TOK_STRING ) {
::std::string cratename = tok.str();
GET_CHECK_TOK(tok, lex, TOK_DOUBLE_COLON);
return Parse_PathFrom(lex, AST::Path(cratename, {}), generic_mode);
}
else {
lex.putback(tok);
return Parse_PathFrom(lex, AST::Path(AST::Path::TagAbsolute()), generic_mode);
}
}
else
return Parse_PathFrom(lex, AST::Path(), generic_mode);
}
AST::Path Parse_PathFrom(TokenStream& lex, AST::Path path, eParsePathGenericMode generic_mode)
{
TRACE_FUNCTION_F("path = " << path);
Token tok;
tok = lex.getToken();
while(true)
{
::std::vector<TypeRef> params;
CHECK_TOK(tok, TOK_IDENT);
::std::string component = tok.str();
GET_TOK(tok, lex);
if( generic_mode == PATH_GENERIC_TYPE )
{
if( tok.type() == TOK_LT || tok.type() == TOK_DOUBLE_LT )
{
// HACK! Handle breaking << into < <
if( tok.type() == TOK_DOUBLE_LT )
lex.putback( Token(TOK_LT) );
// Type-mode generics "::path::to::Type<A,B>"
params = Parse_Path_GenericList(lex);
tok = lex.getToken();
}
// HACK - 'Fn*(...) -> ...' notation
else if( tok.type() == TOK_PAREN_OPEN )
{
DEBUG("Fn() hack");
::std::vector<TypeRef> args;
if( GET_TOK(tok, lex) == TOK_PAREN_CLOSE )
{
// Empty list
}
else
{
lex.putback(tok);
do {
args.push_back( Parse_Type(lex) );
} while( GET_TOK(tok, lex) == TOK_COMMA );
}
CHECK_TOK(tok, TOK_PAREN_CLOSE);
TypeRef ret_type = TypeRef( TypeRef::TagUnit() );
if( GET_TOK(tok, lex) == TOK_THINARROW ) {
ret_type = Parse_Type(lex);
}
else {
lex.putback(tok);
}
DEBUG("- Fn("<<args<<")->"<<ret_type<<"");
// Encode into path, by converting Fn(A,B)->C into Fn<(A,B),Ret=C>
params = ::std::vector<TypeRef> { TypeRef(TypeRef::TagTuple(), ::std::move(args)) };
// TODO: Use 'ret_type' as an associated type bound
GET_TOK(tok, lex);
}
else
{
}
}
if( tok.type() != TOK_DOUBLE_COLON ) {
path.append( AST::PathNode(component, params) );
break;
}
tok = lex.getToken();
if( generic_mode == PATH_GENERIC_EXPR && (tok.type() == TOK_LT || tok.type() == TOK_DOUBLE_LT) )
{
// HACK! Handle breaking << into < <
if( tok.type() == TOK_DOUBLE_LT )
lex.putback( Token(TOK_LT) );
// Expr-mode generics "::path::to::function::<Type1,Type2>(arg1, arg2)"
params = Parse_Path_GenericList(lex);
tok = lex.getToken();
if( tok.type() != TOK_DOUBLE_COLON ) {
path.append( AST::PathNode(component, params) );
break;
}
GET_TOK(tok, lex);
}
path.append( AST::PathNode(component, params) );
}
lex.putback(tok);
DEBUG("path = " << path);
return path;
}
/// Parse a list of parameters within a path
::std::vector<TypeRef> Parse_Path_GenericList(TokenStream& lex)
{
TRACE_FUNCTION;
Token tok;
::std::vector<TypeRef> types;
::std::vector< ::std::string> lifetimes;
::std::map< ::std::string, TypeRef> assoc_bounds;
::std::vector<unsigned int> int_args;
do {
switch(GET_TOK(tok, lex))
{
case TOK_LIFETIME:
lifetimes.push_back( tok.str() );
break;
case TOK_IDENT:
if( LOOK_AHEAD(lex) == TOK_EQUAL )
{
::std::string name = tok.str();
GET_CHECK_TOK(tok, lex, TOK_EQUAL);
assoc_bounds.insert( ::std::make_pair( ::std::move(name), Parse_Type(lex) ) );
break;
}
default:
lex.putback(tok);
types.push_back( Parse_Type(lex) );
break;
}
} while( GET_TOK(tok, lex) == TOK_COMMA );
// HACK: Split >> into >
if(tok.type() == TOK_DOUBLE_GT) {
lex.putback(Token(TOK_GT));
}
else {
CHECK_TOK(tok, TOK_GT);
}
// TODO: Actually use the lifetimes/assoc_bounds
return types;
}
|