@accordproject/concerto-cto 1.2.2-20211124162019
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +11 -0
- package/README.md +13 -0
- package/index.js +28 -0
- package/lib/parser.js +9477 -0
- package/lib/parser.pegjs +1380 -0
- package/lib/printer.js +250 -0
- package/package.json +111 -0
package/lib/printer.js
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
3
|
+
* you may not use this file except in compliance with the License.
|
|
4
|
+
* You may obtain a copy of the License at
|
|
5
|
+
*
|
|
6
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
*
|
|
8
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
9
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
10
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
+
* See the License for the specific language governing permissions and
|
|
12
|
+
* limitations under the License.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
'use strict';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Create decorator argument string from a metamodel
|
|
19
|
+
* @param {object} mm - the metamodel
|
|
20
|
+
* @return {string} the string for the decorator argument
|
|
21
|
+
*/
|
|
22
|
+
function decoratorArgFromMetaModel(mm) {
|
|
23
|
+
let result = '';
|
|
24
|
+
switch (mm.$class) {
|
|
25
|
+
case 'concerto.metamodel.DecoratorTypeReference':
|
|
26
|
+
result += `${mm.type.name}${mm.isArray ? '[]' : ''}`;
|
|
27
|
+
break;
|
|
28
|
+
case 'concerto.metamodel.DecoratorString':
|
|
29
|
+
result += `"${mm.value}"`;
|
|
30
|
+
break;
|
|
31
|
+
default:
|
|
32
|
+
result += `${mm.value}`;
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
return result;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Create decorator string from a metamodel
|
|
40
|
+
* @param {object} mm - the metamodel
|
|
41
|
+
* @return {string} the string for the decorator
|
|
42
|
+
*/
|
|
43
|
+
function decoratorFromMetaModel(mm) {
|
|
44
|
+
let result = '';
|
|
45
|
+
result += `@${mm.name}`;
|
|
46
|
+
if (mm.arguments) {
|
|
47
|
+
result += '(';
|
|
48
|
+
result += mm.arguments.map(decoratorArgFromMetaModel).join(',');
|
|
49
|
+
result += ')';
|
|
50
|
+
}
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Create decorators string from a metamodel
|
|
56
|
+
* @param {object} mm - the metamodel
|
|
57
|
+
* @param {string} prefix - indentation
|
|
58
|
+
* @return {string} the string for the decorators
|
|
59
|
+
*/
|
|
60
|
+
function decoratorsFromMetaModel(mm, prefix) {
|
|
61
|
+
let result = '';
|
|
62
|
+
result += mm.map(decoratorFromMetaModel).join(`\n${prefix}`);
|
|
63
|
+
result += `\n${prefix}`;
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Create a property string from a metamodel
|
|
69
|
+
* @param {object} mm - the metamodel
|
|
70
|
+
* @return {string} the string for that property
|
|
71
|
+
*/
|
|
72
|
+
function propertyFromMetaModel(mm) {
|
|
73
|
+
let result = '';
|
|
74
|
+
let defaultString = '';
|
|
75
|
+
let validatorString = '';
|
|
76
|
+
|
|
77
|
+
if (mm.decorators) {
|
|
78
|
+
result += decoratorsFromMetaModel(mm.decorators, ' ');
|
|
79
|
+
}
|
|
80
|
+
if (mm.$class === 'concerto.metamodel.RelationshipProperty') {
|
|
81
|
+
result += '-->';
|
|
82
|
+
} else {
|
|
83
|
+
result += 'o';
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
switch (mm.$class) {
|
|
87
|
+
case 'concerto.metamodel.EnumProperty':
|
|
88
|
+
break;
|
|
89
|
+
case 'concerto.metamodel.BooleanProperty':
|
|
90
|
+
result += ' Boolean';
|
|
91
|
+
if (mm.defaultValue === true || mm.defaultValue === false) {
|
|
92
|
+
if (mm.defaultValue) {
|
|
93
|
+
defaultString += ' default=true';
|
|
94
|
+
} else {
|
|
95
|
+
defaultString += ' default=false';
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
break;
|
|
99
|
+
case 'concerto.metamodel.DateTimeProperty':
|
|
100
|
+
result += ' DateTime';
|
|
101
|
+
break;
|
|
102
|
+
case 'concerto.metamodel.DoubleProperty':
|
|
103
|
+
result += ' Double';
|
|
104
|
+
if (mm.defaultValue) {
|
|
105
|
+
const doubleString = mm.defaultValue.toFixed(Math.max(1, (mm.defaultValue.toString().split('.')[1] || []).length));
|
|
106
|
+
|
|
107
|
+
defaultString += ` default=${doubleString}`;
|
|
108
|
+
}
|
|
109
|
+
if (mm.validator) {
|
|
110
|
+
const lowerString = mm.validator.lower ? mm.validator.lower : '';
|
|
111
|
+
const upperString = mm.validator.upper ? mm.validator.upper : '';
|
|
112
|
+
validatorString += ` range=[${lowerString},${upperString}]`;
|
|
113
|
+
}
|
|
114
|
+
break;
|
|
115
|
+
case 'concerto.metamodel.IntegerProperty':
|
|
116
|
+
result += ' Integer';
|
|
117
|
+
if (mm.defaultValue) {
|
|
118
|
+
defaultString += ` default=${mm.defaultValue.toString()}`;
|
|
119
|
+
}
|
|
120
|
+
if (mm.validator) {
|
|
121
|
+
const lowerString = mm.validator.lower ? mm.validator.lower : '';
|
|
122
|
+
const upperString = mm.validator.upper ? mm.validator.upper : '';
|
|
123
|
+
validatorString += ` range=[${lowerString},${upperString}]`;
|
|
124
|
+
}
|
|
125
|
+
break;
|
|
126
|
+
case 'concerto.metamodel.LongProperty':
|
|
127
|
+
result += ' Long';
|
|
128
|
+
if (mm.defaultValue) {
|
|
129
|
+
defaultString += ` default=${mm.defaultValue.toString()}`;
|
|
130
|
+
}
|
|
131
|
+
if (mm.validator) {
|
|
132
|
+
const lowerString = mm.validator.lower ? mm.validator.lower : '';
|
|
133
|
+
const upperString = mm.validator.upper ? mm.validator.upper : '';
|
|
134
|
+
validatorString += ` range=[${lowerString},${upperString}]`;
|
|
135
|
+
}
|
|
136
|
+
break;
|
|
137
|
+
case 'concerto.metamodel.StringProperty':
|
|
138
|
+
result += ' String';
|
|
139
|
+
if (mm.defaultValue) {
|
|
140
|
+
defaultString += ` default="${mm.defaultValue}"`;
|
|
141
|
+
}
|
|
142
|
+
if (mm.validator) {
|
|
143
|
+
validatorString += ` regex=/${mm.validator.pattern}/${mm.validator.flags}`;
|
|
144
|
+
}
|
|
145
|
+
break;
|
|
146
|
+
case 'concerto.metamodel.ObjectProperty':
|
|
147
|
+
result += ` ${mm.type.name}`;
|
|
148
|
+
if (mm.defaultValue) {
|
|
149
|
+
defaultString += ` default="${mm.defaultValue}"`;
|
|
150
|
+
}
|
|
151
|
+
break;
|
|
152
|
+
case 'concerto.metamodel.RelationshipProperty':
|
|
153
|
+
result += ` ${mm.type.name}`;
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
if (mm.isArray) {
|
|
157
|
+
result += '[]';
|
|
158
|
+
}
|
|
159
|
+
result += ` ${mm.name}`;
|
|
160
|
+
if (mm.isOptional) {
|
|
161
|
+
result += ' optional';
|
|
162
|
+
}
|
|
163
|
+
result += defaultString;
|
|
164
|
+
result += validatorString;
|
|
165
|
+
return result;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Create a declaration string from a metamodel
|
|
170
|
+
* @param {object} mm - the metamodel
|
|
171
|
+
* @return {string} the string for that declaration
|
|
172
|
+
*/
|
|
173
|
+
function declFromMetaModel(mm) {
|
|
174
|
+
let result = '';
|
|
175
|
+
if (mm.decorators) {
|
|
176
|
+
result += decoratorsFromMetaModel(mm.decorators, '');
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (mm.isAbstract) {
|
|
180
|
+
result += 'abstract ';
|
|
181
|
+
}
|
|
182
|
+
switch (mm.$class) {
|
|
183
|
+
case 'concerto.metamodel.AssetDeclaration':
|
|
184
|
+
result += `asset ${mm.name} `;
|
|
185
|
+
break;
|
|
186
|
+
case 'concerto.metamodel.ConceptDeclaration':
|
|
187
|
+
result += `concept ${mm.name} `;
|
|
188
|
+
break;
|
|
189
|
+
case 'concerto.metamodel.EventDeclaration':
|
|
190
|
+
result += `event ${mm.name} `;
|
|
191
|
+
break;
|
|
192
|
+
case 'concerto.metamodel.ParticipantDeclaration':
|
|
193
|
+
result += `participant ${mm.name} `;
|
|
194
|
+
break;
|
|
195
|
+
case 'concerto.metamodel.TransactionDeclaration':
|
|
196
|
+
result += `transaction ${mm.name} `;
|
|
197
|
+
break;
|
|
198
|
+
case 'concerto.metamodel.EnumDeclaration':
|
|
199
|
+
result += `enum ${mm.name} `;
|
|
200
|
+
break;
|
|
201
|
+
}
|
|
202
|
+
if (mm.superType) {
|
|
203
|
+
result += `extends ${mm.superType.name} `;
|
|
204
|
+
}
|
|
205
|
+
// XXX Needs to be fixed to support `identified`
|
|
206
|
+
if (mm.identified) {
|
|
207
|
+
if (mm.identified.$class === 'concerto.metamodel.IdentifiedBy') {
|
|
208
|
+
result += `identified by ${mm.identified.name} `;
|
|
209
|
+
} else {
|
|
210
|
+
result += 'identified ';
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
result += '{';
|
|
214
|
+
mm.properties.forEach((property) => {
|
|
215
|
+
result += `\n ${propertyFromMetaModel(property)}`;
|
|
216
|
+
});
|
|
217
|
+
result += '\n}';
|
|
218
|
+
return result;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Create a model string from a metamodel
|
|
223
|
+
* @param {object} metaModel - the metamodel
|
|
224
|
+
* @return {string} the string for that model
|
|
225
|
+
*/
|
|
226
|
+
function toCTO(metaModel) {
|
|
227
|
+
let result = '';
|
|
228
|
+
result += `namespace ${metaModel.namespace}`;
|
|
229
|
+
if (metaModel.imports && metaModel.imports.length > 0) {
|
|
230
|
+
result += '\n';
|
|
231
|
+
metaModel.imports.forEach((imp) => {
|
|
232
|
+
let name = '*';
|
|
233
|
+
if (imp.$class === 'concerto.metamodel.ImportType') {
|
|
234
|
+
name = imp.name;
|
|
235
|
+
}
|
|
236
|
+
result += `\nimport ${imp.namespace}.${name}`;
|
|
237
|
+
if (imp.uri) {
|
|
238
|
+
result += ` from ${imp.uri}`;
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
if (metaModel.declarations && metaModel.declarations.length > 0) {
|
|
243
|
+
metaModel.declarations.forEach((decl) => {
|
|
244
|
+
result += `\n\n${declFromMetaModel(decl)}`;
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
return result;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
module.exports = { toCTO };
|
package/package.json
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@accordproject/concerto-cto",
|
|
3
|
+
"version": "1.2.2-20211124162019",
|
|
4
|
+
"description": "Parser the Concerto Modeling Language",
|
|
5
|
+
"homepage": "https://github.com/accordproject/concerto",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=12",
|
|
8
|
+
"npm": ">=6"
|
|
9
|
+
},
|
|
10
|
+
"main": "index.js",
|
|
11
|
+
"scripts": {
|
|
12
|
+
"prepare": "peggy ./lib/parser.pegjs",
|
|
13
|
+
"pretest": "npm run lint",
|
|
14
|
+
"lint": "eslint .",
|
|
15
|
+
"postlint": "npm run licchk",
|
|
16
|
+
"licchk": "license-check-and-add",
|
|
17
|
+
"postlicchk": "npm run doc",
|
|
18
|
+
"doc": "jsdoc --pedantic --recurse -c jsdoc.json",
|
|
19
|
+
"test": "nyc mocha --recursive -t 10000",
|
|
20
|
+
"test:watch": "nyc mocha --watch --recursive -t 10000",
|
|
21
|
+
"mocha": "mocha --recursive -t 10000",
|
|
22
|
+
"nyc": "nyc mocha --recursive -t 10000"
|
|
23
|
+
},
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "https://github.com/accordproject/concerto.git",
|
|
27
|
+
"directory": "packages/concerto-core"
|
|
28
|
+
},
|
|
29
|
+
"keywords": [
|
|
30
|
+
"blockchain",
|
|
31
|
+
"hyperledger",
|
|
32
|
+
"solutions"
|
|
33
|
+
],
|
|
34
|
+
"author": "accordproject.org",
|
|
35
|
+
"license": "Apache-2.0",
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"chai": "4.3.4",
|
|
38
|
+
"chai-as-promised": "7.1.1",
|
|
39
|
+
"chai-things": "0.2.0",
|
|
40
|
+
"eslint": "8.2.0",
|
|
41
|
+
"jsdoc": "^3.6.7",
|
|
42
|
+
"license-check-and-add": "2.3.6",
|
|
43
|
+
"mocha": "8.3.2",
|
|
44
|
+
"nyc": "15.1.0",
|
|
45
|
+
"peggy": "1.2.0"
|
|
46
|
+
},
|
|
47
|
+
"browserslist": "> 0.25%, not dead",
|
|
48
|
+
"license-check-and-add-config": {
|
|
49
|
+
"folder": "./lib",
|
|
50
|
+
"license": "HEADER",
|
|
51
|
+
"exact_paths_method": "EXCLUDE",
|
|
52
|
+
"exact_paths": [
|
|
53
|
+
"api.txt",
|
|
54
|
+
"composer-logs",
|
|
55
|
+
"coverage",
|
|
56
|
+
"./parser.js",
|
|
57
|
+
"LICENSE",
|
|
58
|
+
"node_modules",
|
|
59
|
+
".nyc-output",
|
|
60
|
+
"out",
|
|
61
|
+
".tern-project"
|
|
62
|
+
],
|
|
63
|
+
"file_type_method": "EXCLUDE",
|
|
64
|
+
"file_types": [
|
|
65
|
+
".yml",
|
|
66
|
+
".yaml",
|
|
67
|
+
".zip",
|
|
68
|
+
".tgz"
|
|
69
|
+
],
|
|
70
|
+
"insert_license": false,
|
|
71
|
+
"license_formats": {
|
|
72
|
+
"js|njk|pegjs|cto|acl|qry": {
|
|
73
|
+
"prepend": "/*",
|
|
74
|
+
"append": " */",
|
|
75
|
+
"eachLine": {
|
|
76
|
+
"prepend": " * "
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
"npmrc|editorconfig|txt": {
|
|
80
|
+
"eachLine": {
|
|
81
|
+
"prepend": "# "
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
"md": {
|
|
85
|
+
"file": "HEADER.md"
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
"nyc": {
|
|
90
|
+
"produce-source-map": "true",
|
|
91
|
+
"sourceMap": "inline",
|
|
92
|
+
"reporter": [
|
|
93
|
+
"lcov",
|
|
94
|
+
"text-summary",
|
|
95
|
+
"html",
|
|
96
|
+
"json"
|
|
97
|
+
],
|
|
98
|
+
"include": [
|
|
99
|
+
"lib/**/*.js"
|
|
100
|
+
],
|
|
101
|
+
"exclude": [
|
|
102
|
+
"lib/parser.js"
|
|
103
|
+
],
|
|
104
|
+
"all": true,
|
|
105
|
+
"check-coverage": true,
|
|
106
|
+
"statements": 54,
|
|
107
|
+
"branches": 41,
|
|
108
|
+
"functions": 70,
|
|
109
|
+
"lines": 55
|
|
110
|
+
}
|
|
111
|
+
}
|