| 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 |
1
1
1
1
1
10
10
10
10
10
10
10
10
1
10
10
1
34
34
34
1
48
48
1
24
24
1
24
24
24
24
1
8
8
8
8
8
24
24
24
8
8
8
1
1
42
42
40
1
1
10
10
10
10
50
50
8
8
42
40
1
| // Copyright (c) 2015 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
'use strict';
var util = require('util');
var specs = require('../specs');
var debug = require('debug')('compiler');
var owns = Object.prototype.hasOwnProperty;
// The types structure starts with the default types, and devolves into a
// dictionary of type declarations.
function Types() {
this.binary = specs.ABinary;
this.string = specs.AString;
this.bool = specs.ABoolean;
this.byte = specs.AByte;
this.i16 = specs.AInt16;
this.i32 = specs.AInt32;
this.i64 = specs.AInt64;
this.double = specs.ADouble;
};
function Spec() {
this.types = new Types();
this.servicesAndFunctions = {};
}
Spec.prototype.getType = function getType(name) {
var type = this.types[name];
Iif (!type) {
throw new Error(util.format('type %s not found', name));
}
return type;
};
Spec.prototype.setType = function setType(name, type) {
Iif (this.types[name]) {
throw new Error(util.format('type %s already exists', name));
}
this.types[name] = type;
};
Spec.prototype.lookupType = function lookupType(t) {
// TODO: handle const
// TODO: handle typedef
// TODO: handle Set
Eif (t.type === 'Identifier') {
return this.getType(t.name);
} else if (t.type === 'List') {
return specs.AList(this.lookupType(t.fieldType));
} else if (t.type === 'Map') {
return specs.AMap(this.lookupType(t.left), this.lookupType(t.right));
} else {
throw new Error(util.format('unknown type %s', t.type));
}
};
Spec.prototype.parseField = function parseField(f) {
var fieldName = f.id.name;
var fieldType = this.lookupType(f.fieldType);
var fieldId = f.fid;
return specs.AField({id: fieldId, name: fieldName, type: fieldType});
};
Spec.prototype.processStruct = function processStruct(s) {
var self = this;
debug('enter struct', s);
var fieldNames = Object.keys(s.fields);
var fields = [];
for (var index = 0; index < fieldNames.length; index++) {
var fieldName = fieldNames[index];
var field = s.fields[fieldName];
fields[index] = self.parseField(field);
}
var astruct = new specs.AStruct({
fields: fields
});
var name = s.id.name;
this.setType(name, astruct);
};
Spec.prototype.processFunction = function processFunction(func, opts) {
debug('enter function', func);
var self = this;
var funcName = func.id.name;
if (opts.functions.indexOf(funcName) >= 0) {
throw new Error(util.format('service %s function %s already exists', opts.serviceName, funcName));
}
opts.functions.push(funcName);
var typePrefix = util.format('%s::%s', opts.serviceName, funcName);
var fields = [];
for (var index = 0; index < func.fields.length; index++) {
var field = func.fields[index];
fields[index] = self.parseField(field);
}
this.setType(
util.format('%s_args', typePrefix),
specs.AStruct({
fields: fields
}));
var resultFields = [];
if (func.ft !== 'void') {
resultFields.push(
specs.AField({id: 0, name: 'success', type: this.lookupType(func.ft)})
);
}
if (func.throws) {
var fields = [];
for (var index = 0; index < func.throws.fields; index++) {
var field = func.throws.fields[index];
resultFields[index] = self.parseField(field);
}
}
// TODO: add exceptions in _result struct
this.setType(util.format('%s_result', typePrefix),
specs.AStruct({fields: resultFields}));
};
Spec.prototype.processEnum = function processEnum(e) {
var name = e.id.name;
var entity = new specs.AEnum(e.enumDefinitions);
this.setType(name, entity);
};
Spec.prototype.processService = function processService(service) {
var self = this;
debug('enter service', service);
var serviceName = service.id.name;
if (owns.call(this.servicesAndFunctions, serviceName)) {
throw new Error(util.format('service %s already exists', serviceName));
}
this.servicesAndFunctions[serviceName] = [];
var options = {
serviceName: serviceName,
functions: self.servicesAndFunctions[serviceName]
};
for (var index = 0; index < service.functions.length; index++) {
var func = service.functions[index];
self.processFunction(func, options);
}
};
Spec.prototype.processProgram = function(root) {
Iif (root.type !== 'Program') {
throw new Error('expects type Program; received ' + root.type);
}
debug('program starts');
var self = this;
for (var index = 0; index < root.definitions.length; index++) {
var def = root.definitions[index];
switch (def.type) {
case 'Exception':
self.processStruct(def);
break;
case 'Struct':
self.processStruct(def);
break;
case 'Service':
self.processService(def);
break;
case 'Enum':
self.processEnum(def);
break;
default:
throw new Error('definition type is not supported' + def.type);
}
}
};
module.exports = Spec;
|