1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | 'use strict';
|
15 |
|
16 | const APModelManager = require('@accordproject/ergo-compiler').APModelManager;
|
17 |
|
18 | const Field = require('composer-concerto').Field;
|
19 |
|
20 | const ModelFile = require('composer-concerto').ModelFile;
|
21 |
|
22 | const RelationshipDeclaration = require('composer-concerto').RelationshipDeclaration;
|
23 |
|
24 | const EnumDeclaration = require('composer-concerto').EnumDeclaration;
|
25 |
|
26 | const EnumValueDeclaration = require('composer-concerto').EnumValueDeclaration;
|
27 |
|
28 | const ClassDeclaration = require('composer-concerto').ClassDeclaration;
|
29 |
|
30 | const util = require('util');
|
31 |
|
32 | const debug = require('debug')('cicero:grammarvisitor');
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | class GrammarVisitor {
|
43 | |
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | visit(thing, parameters) {
|
51 | if (thing instanceof APModelManager) {
|
52 | return this.visitModelManager(thing, parameters);
|
53 | } else if (thing instanceof ModelFile) {
|
54 | return this.visitModelFile(thing, parameters);
|
55 | } else if (thing instanceof EnumDeclaration) {
|
56 | return this.visitEnumDeclaration(thing, parameters);
|
57 | } else if (thing instanceof ClassDeclaration) {
|
58 | return this.visitClassDeclaration(thing, parameters);
|
59 | } else if (thing instanceof Field) {
|
60 | return this.visitField(thing, parameters);
|
61 | } else if (thing instanceof RelationshipDeclaration) {
|
62 | return this.visitRelationshipDeclaration(thing, parameters);
|
63 | } else if (thing instanceof EnumValueDeclaration) {
|
64 | return this.visitEnumValueDeclaration(thing, parameters);
|
65 | } else {
|
66 | throw new Error('Unrecognised type: ' + typeof thing + ', value: ' + util.inspect(thing, {
|
67 | showHidden: true,
|
68 | depth: 1
|
69 | }));
|
70 | }
|
71 | }
|
72 | |
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | visitModelManager(modelManager, parameters) {
|
82 | debug('entering visitModelManager');
|
83 |
|
84 | parameters.modelManager = modelManager;
|
85 |
|
86 | modelManager.getModelFiles().forEach(modelFile => {
|
87 | modelFile.accept(this, parameters);
|
88 | });
|
89 | return null;
|
90 | }
|
91 | |
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | visitModelFile(modelFile, parameters) {
|
101 | debug('entering visitModelFile', modelFile.getNamespace());
|
102 |
|
103 | parameters.modelFile = modelFile;
|
104 |
|
105 | modelFile.getAllDeclarations().filter(declaration => {
|
106 | return !(declaration.isAbstract() || declaration.isSystemType());
|
107 | }).forEach(declaration => {
|
108 | declaration.accept(this, parameters);
|
109 | });
|
110 | return null;
|
111 | }
|
112 | |
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | visitEnumDeclaration(enumDeclaration, parameters) {
|
122 | let result = '';
|
123 | enumDeclaration.getOwnProperties().forEach(property => {
|
124 | if (result.length > 0) {
|
125 | result += ' | ';
|
126 | }
|
127 |
|
128 | result += property.accept(this, parameters);
|
129 | });
|
130 | parameters.rules.push({
|
131 | prefix: enumDeclaration.getName(),
|
132 | symbols: [result],
|
133 | properties: false
|
134 | });
|
135 | return result;
|
136 | }
|
137 | |
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 | visitClassDeclaration(classDeclaration, parameters) {
|
147 | debug('entering visitClassDeclaration', classDeclaration.getName());
|
148 |
|
149 |
|
150 | if (!classDeclaration.getDecorator('AccordTemplateModel') &&
|
151 |
|
152 | classDeclaration.getProperties().length > 0) {
|
153 | let result = [];
|
154 |
|
155 | classDeclaration.getProperties().forEach(property => {
|
156 | if (result.length > 0) {
|
157 | result.push(' __ ');
|
158 | }
|
159 |
|
160 | result.push(property.accept(this, parameters));
|
161 | });
|
162 |
|
163 | const properties = [];
|
164 | classDeclaration.getProperties().forEach((property, index) => {
|
165 | const sep = index < classDeclaration.getProperties().length - 1 ? ',' : '';
|
166 | properties.push("".concat(property.getName(), " : data[").concat(index * 2, "]").concat(sep));
|
167 | });
|
168 | parameters.rules.push({
|
169 | prefix: classDeclaration.getName(),
|
170 | class: classDeclaration.getFullyQualifiedName(),
|
171 | symbols: result,
|
172 | properties
|
173 | });
|
174 | return null;
|
175 | }
|
176 | }
|
177 | |
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 | visitField(field, parameters) {
|
187 | debug('entering visitField', field.getName());
|
188 | let qualifier = '';
|
189 |
|
190 | if (field.isArray()) {
|
191 | if (field.isOptional()) {
|
192 | qualifier = ':*';
|
193 | } else {
|
194 | qualifier = ':+';
|
195 | }
|
196 | } else {
|
197 | if (field.isOptional()) {
|
198 | qualifier = ':?';
|
199 | }
|
200 | }
|
201 |
|
202 | return this.toGrammarType(field.getType()) + qualifier;
|
203 | }
|
204 | |
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 | visitEnumValueDeclaration(enumValueDeclaration, parameters) {
|
214 | debug('entering visitEnumValueDeclaration', enumValueDeclaration.getName());
|
215 | return "\"".concat(enumValueDeclaration.getName(), "\" {% id %}");
|
216 | }
|
217 | |
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 | visitRelationshipDeclaration(relationship, parameters) {
|
227 | debug('entering visitRelationshipDeclaration', relationship.getName());
|
228 | return 'String';
|
229 | }
|
230 | |
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 | toGrammarType(type) {
|
239 | return type;
|
240 | }
|
241 |
|
242 | }
|
243 |
|
244 | module.exports = GrammarVisitor; |
\ | No newline at end of file |