-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQEnumDefinition.cpp
More file actions
149 lines (118 loc) · 3.47 KB
/
Copy pathQEnumDefinition.cpp
File metadata and controls
149 lines (118 loc) · 3.47 KB
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
#include <assert.h>
#include <iostream>
#include "FileLexer.h"
#include "Type.h"
#include "Expression.h"
#include "CompilerHelpers.h"
#include "logging.h"
SET_LOG_CAT( LOG_CAT_ALL );
SET_LOG_LEVEL( LOG_LVL_NOISE );
using namespace QLang;
using namespace std;
// Parse: enum Name { Variant1, Variant2(Type1, Type2), ... }
// Parse: enum Name<T> { Variant1, Variant2(T), ... }
EnumDefinition *EnumDefinition::Parse( Lexer &l, Scope *s, bool isPublic )
{
TRACE_BEGIN( LOG_LVL_INFO );
SourceLocation loc = l.getTokenLocation();
// Consume 'enum' keyword
int sym = l.getSymbol();
if ( sym != Lexer::KEYWORD_ENUM )
{
COMPILE_ERROR( l, "Internal Error: expected 'enum' keyword" );
}
// Parse enum name
sym = l.getSymbol();
if ( sym != Lexer::SYMBOL )
{
COMPILE_ERROR( l, "Expected enum name" );
}
string enumName = l.getSymbolText();
EnumDefinition *enumDef = new EnumDefinition( enumName );
enumDef->setLocation( loc );
enumDef->mIsPublic = isPublic;
// Check for generic parameters: <T> or <T: Constraint>
if ( l.peekSymbol() == '<' )
{
l.getSymbol(); // consume '<'
do {
sym = l.getSymbol();
if ( sym != Lexer::SYMBOL )
COMPILE_ERROR( l, "Expected type parameter name" );
GenericParam param;
param.mName = l.getSymbolText();
// Check for constraint: <T: Comparable>
if ( l.peekSymbol() == ':' )
{
l.getSymbol(); // consume ':'
sym = l.getSymbol();
if ( sym != Lexer::SYMBOL )
COMPILE_ERROR( l, "Expected constraint name after ':'" );
param.mConstraint = l.getSymbolText();
}
enumDef->mGenericParams.push_back( param );
// Register type parameter in scope
s->addType( new Type( param.mName ) );
sym = l.getSymbol();
} while ( sym == ',' );
if ( sym != '>' )
COMPILE_ERROR( l, "Expected '>' after generic parameters" );
}
// Register the enum's TYPE name before parsing the variants so a variant
// payload can reference the enum itself (recursive enums:
// `enum Expr { num(int), add(Expr, Expr) }`). The definition symbol is
// still registered after the body parses.
s->addType( new Type( enumName ) );
// Expect '{'
sym = l.getSymbol();
if ( sym != '{' )
{
COMPILE_ERROR( l, "Expected '{' after enum name" );
}
// Parse variants until '}'
while ( l.peekSymbol() != '}' )
{
Variant variant;
variant.mLocation = l.getTokenLocation();
// Parse variant name
sym = l.getSymbol();
if ( sym != Lexer::SYMBOL )
{
COMPILE_ERROR( l, "Expected variant name in enum" );
}
variant.mName = l.getSymbolText();
// Check for associated types: Variant(Type1, Type2)
if ( l.peekSymbol() == '(' )
{
l.getSymbol(); // consume '('
if ( l.peekSymbol() != ')' )
{
do {
Type *t = Type::Parse( l, s, false );
if ( t == nullptr )
COMPILE_ERROR( l, "Expected type in enum variant" );
variant.mAssociatedTypes.push_back( t );
sym = l.getSymbol();
} while ( sym == ',' );
if ( sym != ')' )
COMPILE_ERROR( l, "Expected ')' after variant types" );
}
else
{
l.getSymbol(); // consume ')'
}
}
enumDef->mVariants.push_back( variant );
// Optional comma between variants
if ( l.peekSymbol() == ',' )
l.getSymbol();
}
// Consume '}'
sym = l.getSymbol();
assert( sym == '}' );
// Register the enum definition symbol (the type name was registered before
// the variant loop, for recursive self-references)
s->addSymbol( enumDef );
LOG( "Parsed enum: %s with %d variants", enumName.c_str(), (int)enumDef->mVariants.size() );
return enumDef;
}