@whisklabs/grpc
Version:
gRPC generator and http library for typescript
101 lines • 3.68 kB
JavaScript
import { cleanComment, next, setComment } from './comment';
import { Thrower } from './thrower';
import { tokenizer } from './tokenizer';
import { ParseEnums } from './tokens/enums';
import { ParseImport } from './tokens/import';
import { ParseExtend, ParseMessage } from './tokens/message';
import { ParseOptions } from './tokens/options';
import { ParsePackage } from './tokens/package';
import { ParseService } from './tokens/service';
import { ParseSyntax } from './tokens/syntax';
import { insertOption } from './utils';
function parse(tokens) {
var schema = {
syntax: 3,
imports: [],
enums: [],
messages: [],
options: {},
extends: [],
services: [],
};
cleanComment();
setComment(schema);
var first = true;
while (tokens.length > 0) {
switch (next(tokens)) {
case 'syntax':
if (!first) {
throw new Thrower('syntax', [['must be on first line', 0]]);
}
schema.syntax = ParseSyntax(tokens);
break;
case 'package':
schema.package = ParsePackage(tokens);
break;
case 'message':
schema.messages.push(ParseMessage(tokens));
break;
case 'import':
schema.imports.push(ParseImport(tokens));
break;
case 'enum':
schema.enums.push(ParseEnums(tokens));
break;
case 'option': {
var _a = ParseOptions(tokens), field = _a.field, value = _a.value;
insertOption(schema.options, field, value);
break;
}
case 'extend':
schema.extends.push(ParseExtend(tokens));
break;
case 'service':
schema.services.push(ParseService(tokens));
break;
default:
throw new Thrower('common', [["Unexpected token: ".concat(tokens[0]), 0]]);
}
first = false;
}
Extend(schema);
return schema;
}
function Extend(schema) {
for (var _i = 0, _a = schema.extends; _i < _a.length; _i++) {
var ext = _a[_i];
for (var _b = 0, _c = schema.messages; _b < _c.length; _b++) {
var msg = _c[_b];
if (msg.name === ext.name) {
if (msg.extensions.length === 0) {
throw new Thrower('extends', [["".concat(msg.name, " does not have extensions"), 0]]);
}
for (var _d = 0, _e = ext.message.fields; _d < _e.length; _d++) {
var field = _e[_d];
for (var _f = 0, _g = msg.extensions; _f < _g.length; _f++) {
var extension = _g[_f];
if (field.tag < extension.from || field.tag > extension.to) {
throw new Thrower('extends', [["".concat(msg.name, " does not declare ").concat(field.tag, " as an extension number"), 0]]);
}
msg.fields.push(field);
}
}
}
}
}
}
export function parser(buf) {
var _a = tokenizer(buf.toString()), tokens = _a.tokens, lines = _a.lines, columns = _a.columns;
try {
return parse(tokens);
}
catch (e) {
if (e instanceof Thrower) {
e.addRange(tokens);
e.addLine(lines[lines.length - tokens.length]);
e.addColumn(columns[lines.length - tokens.length + e.message[0][1]]);
}
throw e;
}
}
//# sourceMappingURL=parser.js.map