UNPKG

6 kBJavaScriptView Raw
1/**
2 * Copyright (c) 2016, John Hewson
3 * All rights reserved.
4 */
5"use strict";
6var __extends = (this && this.__extends) || function (d, b) {
7 for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
8 function __() { this.constructor = d; }
9 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
10};
11// types
12var ElmType = (function () {
13 function ElmType() {
14 }
15 return ElmType;
16}());
17exports.ElmType = ElmType;
18var ElmTypeName = (function (_super) {
19 __extends(ElmTypeName, _super);
20 function ElmTypeName(name) {
21 _super.call(this);
22 this.name = name;
23 }
24 return ElmTypeName;
25}(ElmType));
26exports.ElmTypeName = ElmTypeName;
27var ElmTypeApp = (function (_super) {
28 __extends(ElmTypeApp, _super);
29 function ElmTypeApp(name, args) {
30 _super.call(this);
31 this.name = name;
32 this.args = args;
33 }
34 return ElmTypeApp;
35}(ElmType));
36exports.ElmTypeApp = ElmTypeApp;
37var ElmTypeRecord = (function (_super) {
38 __extends(ElmTypeRecord, _super);
39 function ElmTypeRecord(fields, typeParam) {
40 _super.call(this);
41 this.fields = fields;
42 this.typeParam = typeParam;
43 }
44 return ElmTypeRecord;
45}(ElmType));
46exports.ElmTypeRecord = ElmTypeRecord;
47// decls
48var ElmDecl = (function () {
49 function ElmDecl() {
50 }
51 return ElmDecl;
52}());
53exports.ElmDecl = ElmDecl;
54var ElmTypeDecl = (function (_super) {
55 __extends(ElmTypeDecl, _super);
56 function ElmTypeDecl(name, constructors) {
57 _super.call(this);
58 this.name = name;
59 this.constructors = constructors;
60 }
61 return ElmTypeDecl;
62}(ElmDecl));
63exports.ElmTypeDecl = ElmTypeDecl;
64var ElmTypeAliasDecl = (function (_super) {
65 __extends(ElmTypeAliasDecl, _super);
66 function ElmTypeAliasDecl(name, type, typeParams) {
67 if (typeParams === void 0) { typeParams = []; }
68 _super.call(this);
69 this.name = name;
70 this.type = type;
71 this.typeParams = typeParams;
72 }
73 return ElmTypeAliasDecl;
74}(ElmDecl));
75exports.ElmTypeAliasDecl = ElmTypeAliasDecl;
76var ElmFunctionDecl = (function (_super) {
77 __extends(ElmFunctionDecl, _super);
78 function ElmFunctionDecl(name, parameters, returnType, body) {
79 _super.call(this);
80 this.name = name;
81 this.parameters = parameters;
82 this.returnType = returnType;
83 this.body = body;
84 }
85 return ElmFunctionDecl;
86}(ElmDecl));
87exports.ElmFunctionDecl = ElmFunctionDecl;
88var ElmFieldDecl = (function () {
89 function ElmFieldDecl(name, type) {
90 this.name = name;
91 this.type = type;
92 }
93 return ElmFieldDecl;
94}());
95exports.ElmFieldDecl = ElmFieldDecl;
96var ElmParameterDecl = (function () {
97 function ElmParameterDecl(name, type) {
98 this.name = name;
99 this.type = type;
100 }
101 return ElmParameterDecl;
102}());
103exports.ElmParameterDecl = ElmParameterDecl;
104function moduleToString(name, expose, imports, decls) {
105 var warn = '{-\n This file was automatically generated by elm-graphql.\n-}\n';
106 return warn + 'module ' + name + ' exposing (' + expose.join(', ') + ')\n' +
107 imports.map(function (str) { return '\nimport ' + str; }).join('') + '\n\n' +
108 decls.map(declToString).join('\n\n');
109}
110exports.moduleToString = moduleToString;
111function declToString(decl) {
112 if (decl instanceof ElmTypeDecl) {
113 return typeDeclToString(decl);
114 }
115 else if (decl instanceof ElmFunctionDecl) {
116 return funtionToString(decl);
117 }
118 else if (decl instanceof ElmTypeAliasDecl) {
119 return typeAliasDeclToString(decl);
120 }
121 else {
122 throw new Error('unexpected decl: ' + decl.constructor.name + ' ' + JSON.stringify(decl));
123 }
124}
125exports.declToString = declToString;
126function typeDeclToString(type) {
127 return 'type ' + type.name + '\n' +
128 ' = ' + type.constructors.join('\n | ') + '\n';
129}
130exports.typeDeclToString = typeDeclToString;
131function typeAliasDeclToString(type) {
132 return 'type alias ' + type.name + ' ' + type.typeParams.join(' ') + '\n' +
133 ' = ' + typeToString(type.type, 0) + '\n';
134}
135exports.typeAliasDeclToString = typeAliasDeclToString;
136function funtionToString(func) {
137 var paramTypes = func.parameters.map(function (p) { return typeToString(p.type, 1); }).join(' -> ');
138 var paramNames = func.parameters.map(function (p) { return p.name; }).join(' ');
139 var arrow = paramTypes.length > 0 ? ' -> ' : '';
140 var space = paramTypes.length > 0 ? ' ' : '';
141 return func.name + ' : ' + paramTypes + arrow + typeToString(func.returnType, 0) + '\n' +
142 func.name + space + paramNames + ' =\n ' + exprToString(func.body, 0) + '\n';
143}
144exports.funtionToString = funtionToString;
145function fieldToString(field, level) {
146 return field.name + ' : ' + typeToString(field.type, level, true) + '\n';
147}
148function typeToString(ty, level, isField) {
149 if (ty instanceof ElmTypeName) {
150 return ty.name;
151 }
152 else if (ty instanceof ElmTypeApp) {
153 var str = ty.name + ' ' + ty.args.map(function (arg) { return typeToString(arg, level); }).join(' ');
154 if (isField) {
155 return str;
156 }
157 else {
158 return '(' + str + ')';
159 }
160 }
161 else if (ty instanceof ElmTypeRecord) {
162 var indent = makeIndent(level);
163 var pipe = ty.typeParam ? ty.typeParam + ' | ' : '';
164 return (indent + "{ ") + pipe +
165 ty.fields.map(function (f) { return fieldToString(f, level + 1); }).join(indent + " , ") +
166 (indent + "}");
167 }
168 else {
169 throw new Error('unexpected type: ' + ty.constructor.name + ' ' + JSON.stringify(ty));
170 }
171}
172exports.typeToString = typeToString;
173function makeIndent(level) {
174 var str = '';
175 for (var i = 0; i < level; i++) {
176 str += ' ';
177 }
178 return str;
179}
180function exprToString(expr, level) {
181 return expr.expr; // todo: expression trees
182}
183exports.exprToString = exprToString;
184//# sourceMappingURL=elm-ast.js.map
\No newline at end of file