-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQProtocolDefinition.cpp
More file actions
102 lines (78 loc) · 2.48 KB
/
QProtocolDefinition.cpp
File metadata and controls
102 lines (78 loc) · 2.48 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
#include <assert.h>
#include <iostream>
#include "FileLexer.h"
#include "Type.h"
#include "Expression.h"
#include "CompilerHelpers.h"
#include "logging.h"
using namespace QLang;
using namespace std;
ProtocolDefinition *ProtocolDefinition::Parse( Lexer &l, Scope *s, bool isPublic )
{
int sym = l.getSymbol();
if ( sym != Lexer::KEYWORD_PROTOCOL )
{
COMPILE_ERROR( l, "Internal Compiler Error" );
}
sym = l.getSymbol();
if ( sym != Lexer::SYMBOL )
{
COMPILE_ERROR( l, "Expected protocol name" );
}
ProtocolDefinition *protoDef = new ProtocolDefinition( l.getSymbolText() );
protoDef->mIsPublic = isPublic;
// Check for generic parameters: protocol Name<T>
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();
}
protoDef->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" );
}
sym = l.getSymbol();
if ( sym != '{' )
{
COMPILE_ERROR( l, "Expected '{' in protocol definition" );
}
// Use a local scope for method signatures so they don't pollute the global
// scope and don't conflict with impl block methods of the same name.
SmartPtr<Scope> protoScope = new Scope( Scope::kScope_Class, protoDef->getName() );
protoScope->setParent( s );
while ( l.peekSymbol() != '}' )
{
// Each method signature must start with 'fn'
if ( l.peekSymbol() != Lexer::KEYWORD_FN )
{
COMPILE_ERROR( l, "Expected 'fn' for method signature in protocol" );
}
// Use FunctionDefinition::Parse to handle the fn signature.
// The signature ends with ';' (no body), which FunctionDefinition::Parse
// handles as a bodyless declaration.
SmartPtr<FunctionDefinition> method = FunctionDefinition::Parse( l, protoScope );
protoDef->mRequiredMethods.push_back( method );
}
sym = l.getSymbol();
assert( sym == '}' );
s->addSymbol( protoDef );
cout << "Completed protocol " << protoDef->getName() << endl;
return protoDef;
}