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 | ;
|
16 |
|
17 | const CompilerCore=require('../extracted/compilercore');
|
18 | const Logger = require('@accordproject/concerto-util').Logger;
|
19 | const CTOParser = require('@accordproject/concerto-cto').Parser;
|
20 |
|
21 | /**
|
22 | * <p>
|
23 | * Compiler class. Compilation for Ergo logic.
|
24 | * </p>
|
25 | * @class
|
26 | * @public
|
27 | * @memberof module:ergo-compiler
|
28 | */
|
29 | class Compiler {
|
30 | /**
|
31 | * Parse CTO to JSON
|
32 | *
|
33 | * @param {string} ctoContent for CTO model
|
34 | * @returns {object} The parsed CTO model syntax tree in JSON
|
35 | */
|
36 | static parseCTOtoJSON(ctoContent) {
|
37 | const result = CTOParser.parse(ctoContent);
|
38 | return result;
|
39 | }
|
40 |
|
41 | /**
|
42 | * Contract call name
|
43 | *
|
44 | * @param {string} contractName name of the contract
|
45 | * @returns {string} name of the JavaScript class
|
46 | */
|
47 | static contractCallName(contractName) {
|
48 | return CompilerCore.call({ 'name' : contractName });
|
49 | }
|
50 |
|
51 | /**
|
52 | * Contract call name promise
|
53 | *
|
54 | * @param {string} contractName name of the contract
|
55 | * @returns {string} a promise to the name of the compiled JavaScript class
|
56 | */
|
57 | static contractCallNamePromise(contractName) {
|
58 | return Promise.resolve(this.contractCallName(contractName));
|
59 | }
|
60 |
|
61 | /**
|
62 | * Compile Ergo to JavaScript
|
63 | *
|
64 | * @param {Array<{name:string, content:string}>} ergoSources Ergo modules
|
65 | * @param {Array<{name:string, content:string}>} ctoSources CTO models
|
66 | * @param {Array<{name:string, content:string}>} templateSources formulas! XXX
|
67 | * @param {string} target language (es6|java)
|
68 | * @param {boolean} link whether to link the javascript runtime
|
69 | * @param {boolean} warnings whether to print warnings
|
70 | * @returns {string} The compiled JavaScript code
|
71 | */
|
72 | static compileToJavaScript(ergoSources,ctoSources,templateSources,target,link,warnings) {
|
73 | // Built-in config
|
74 | const config= {
|
75 | 'source' : 'ergo',
|
76 | 'target' : target,
|
77 | 'link' : link
|
78 | };
|
79 | config.sourceTemplate = [];
|
80 | if (templateSources) {
|
81 | for (let i = 0; i < templateSources.length; i++) {
|
82 | config.sourceTemplate.push(templateSources[i]);
|
83 | }
|
84 | }
|
85 | config.ergo = [];
|
86 | for (let i = 0; i < ergoSources.length; i++) {
|
87 | config.ergo.push(ergoSources[i]);
|
88 | }
|
89 | config.cto = [];
|
90 | for (let i = 0; i < ctoSources.length; i++) {
|
91 | let ctoFile = ctoSources[i].name;
|
92 | let ctoContent = ctoSources[i].content;
|
93 | config.cto.push({ 'name' : ctoFile, 'content' : JSON.stringify(this.parseCTOtoJSON(ctoContent)) });
|
94 | }
|
95 | // Call compiler
|
96 | const compiled = CompilerCore.compile(config);
|
97 | if (warnings) { compiled.warnings.map((w) => { Logger.warn(w); } ); }
|
98 | if (compiled.code) {
|
99 | return { 'error' : compiled.error };
|
100 | } else {
|
101 | return { 'success' : compiled.result, 'contractName' : compiled.contractName };
|
102 | }
|
103 | }
|
104 |
|
105 | /**
|
106 | * Compile Ergo
|
107 | *
|
108 | * @param {Array<{name:string, content:string}>} ergoSources Ergo modules
|
109 | * @param {Array<{name:string, content:string}>} ctoSources CTO models
|
110 | * @param {Array<{name:string, content:string}>} templateSources formulas! XXX
|
111 | * @param {string} target language (es6|java)
|
112 | * @param {boolean} link whether to link the javascript runtime
|
113 | * @param {boolean} warnings whether to print warnings
|
114 | * @returns {object} Promise to the compiled JavaScript code
|
115 | */
|
116 | static compile(ergoSources,ctoSources,templateSources,target,link,warnings) {
|
117 | const result = this.compileToJavaScript(ergoSources,ctoSources,templateSources,target,link,warnings);
|
118 | return Promise.resolve(result);
|
119 | }
|
120 |
|
121 | /**
|
122 | * Error message
|
123 | *
|
124 | * @param {object} error object returned by Ergo compiler
|
125 | * @returns {string} error message
|
126 | */
|
127 | static ergoErrorToString(error) {
|
128 | return error.message;
|
129 | }
|
130 |
|
131 | /**
|
132 | * Error message (verbose)
|
133 | *
|
134 | * @param {object} error object returned by Ergo compiler
|
135 | * @returns {string} verbose error message
|
136 | */
|
137 | static ergoVerboseErrorToString(error) {
|
138 | return error.fullMessage;
|
139 | }
|
140 |
|
141 |
|
142 | /**
|
143 | * Available compiler targets
|
144 | * @return {Array<string>} the available compilation targets for the logic
|
145 | */
|
146 | static availableTargets() {
|
147 | return CompilerCore.availabletargets();
|
148 | }
|
149 |
|
150 | /**
|
151 | * Is valid compiler target
|
152 | * @param {string} target - the target
|
153 | * @return {boolean} whether the target is valid
|
154 | */
|
155 | static isValidTarget(target) {
|
156 | const available = this.availableTargets();
|
157 | if (available.includes(target)) {
|
158 | return true;
|
159 | } else {
|
160 | throw new Error(`Unknown target: ${target} (available: ${available})`);
|
161 | }
|
162 | }
|
163 | }
|
164 |
|
165 | module.exports = Compiler;
|