Skip to content
This repository has been archived by the owner on Sep 7, 2023. It is now read-only.

Commit

Permalink
Merge dbe0d9e into eceb58f
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromesimeon committed Mar 13, 2019
2 parents eceb58f + dbe0d9e commit b92ee47
Show file tree
Hide file tree
Showing 48 changed files with 1,362 additions and 167 deletions.
162 changes: 81 additions & 81 deletions packages/ergo-cli/test/commands.js

Large diffs are not rendered by default.

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ Feature: Acceptance of delivery Contract
This describes the expected behavior for the Accord Project's Acceptance of delivery contract

Background:
Given the Ergo contract "org.accordproject.acceptanceofdelivery.AcceptanceOfDelivery" in file "data/acceptance-of-delivery/logic.ergo"
And the model in file "data/acceptance-of-delivery/model.cto"
Given the Ergo contract "org.accordproject.acceptanceofdelivery.AcceptanceOfDelivery" in file "examples/acceptance-of-delivery/logic.ergo"
And the model in file "examples/acceptance-of-delivery/model.cto"
And the contract data
"""
{
Expand Down
6 changes: 4 additions & 2 deletions packages/ergo-compiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* @module ergo-compiler
*/

module.exports.Compiler = require('./lib/compiler.js');
module.exports.Logger = require('./lib/logger.js');

module.exports.APModelManager = require('./lib/apmodelmanager.js');
module.exports.ScriptManager = require('./lib/scriptmanager.js');
module.exports.Compiler = require('./lib/compiler.js');
module.exports.TemplateLogic = require('./lib/templatelogic.js');
73 changes: 73 additions & 0 deletions packages/ergo-compiler/lib/apmodelmanager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const fsPath = require('path');

const ModelManager = require('composer-concerto').ModelManager;

const systemModel = `namespace org.accordproject.base
abstract asset Asset { }
abstract participant Participant { }
abstract transaction Transaction identified by transactionId {
o String transactionId
}
abstract event Event identified by eventId {
o String eventId
}`;

/**
* Accord Project Model Manager. Bootstraps the ModelManager with system files.
* @class
* @public
* @abstract
* @memberof module:ergo-compiler
*/
class APModelManager extends ModelManager {

/**
*/
constructor() {
super();
this.addModelFile(systemModel, 'org.accordproject.base.cto', false, true);
}

/**
* Gets all the CTO models
* @return {Array<{name:string, content:string}>} the name and content of each CTO file
*/
getModels() {
const modelFiles = this.getModelFiles();
let models = [];
modelFiles.forEach(function (file) {
let fileName;
// ignore the system namespace when creating an archive
if (file.isSystemModelFile()) {
return;
}
if (file.fileName === 'UNKNOWN' || file.fileName === null || !file.fileName) {
fileName = file.namespace + '.cto';
} else {
let fileIdentifier = file.fileName;
fileName = fsPath.basename(fileIdentifier);
}
models.push({ 'name' : fileName, 'content' : file.definitions });
});
return models;
}

}

module.exports = APModelManager;
156 changes: 156 additions & 0 deletions packages/ergo-compiler/lib/functiondeclaration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

/**
* FunctionDeclaration defines a function that has been defined
* in a model file. If the name of the function starts with 'on'
* then the name of the function denotes the name of a transaction
* declaration that the function processes.
*
* @class
* @memberof module:cicero-core
*/
class FunctionDeclaration {

/**
* Create a FunctionDeclaration
*
* @param {ModelManager} modelManager - the ModelManager used to validate this function
* @param {string} language - the language that the function is written in. E.g. JS.
* @param {string} name - the name of the function
* @param {string} visibility - the visibility of the function
* @param {string} returnType - the return type of the function
* @param {string} throws - the type that is thrown by the function
* @param {string[]} parameterNames - the names of parameters of the function
* @param {string[]} parameterTypes - the type names of parameters of the function
* @param {string[]} decorators - the function decorators
* @param {string} functionText - the function as text
* @throws {IllegalModelException}
*/
constructor(modelManager, language, name, visibility, returnType, throws, parameterNames, parameterTypes, decorators, functionText) {

if(modelManager === null) {
throw new Error('ModelManager is required.');
}

this.modelManager = modelManager;
this.name = name;
this.language = language;
this.visibility = visibility;
this.returnType = returnType;
this.throws = throws;
this.decorators = decorators;
this.parameterNames = parameterNames;
this.parameterTypes = parameterTypes;
this.functionText = functionText;
}

/**
* Visitor design pattern
* @param {Object} visitor - the visitor
* @param {Object} parameters - the parameter
* @return {Object} the result of visiting or null
* @private
*/
accept(visitor,parameters) {
return visitor.visit(this, parameters);
}

/**
* Returns the text of this function.
*
* @return {string} the text that defines the function
*/
getFunctionText() {
return this.functionText;
}

/**
* Returns the type thrown by this function
*
* @return {string} the type thrown by the function
*/
getThrows() {
return this.throws;
}

/**
* Returns the programming language that the function is written in
*
* @return {string} the language of the function
*/
getLanguage() {
return this.language;
}

/**
* Returns the decorators that the function was tagged with
*
* @return {string[]} the @ prefixed decorators for the function
*/
getDecorators() {
return this.decorators;
}

/**
* Returns the visibility of this function
*
* @return {string} the visibility of the function (+ is public),
* (- is private)
*/
getVisibility() {
return this.visibility;
}

/**
* Returns the return type for this function
*
* @return {string} the return type for the function
*/
getReturnType() {
return this.returnType;
}

/**
* Returns the name of the function
*
* @return {string} the name of the function.
*/
getName() {
return this.name;
}

/**
* Returns the names of the parameters processed by the function.
*
* @return {string[]} the names of the parameters.
*/
getParameterNames() {
return this.parameterNames;
}

/**
* Returns the types of the parameters processed by the function.
*
* @return {string[]} the types of the parameters.
*/
getParameterTypes() {
return this.parameterTypes;
}

}

module.exports = FunctionDeclaration;
143 changes: 143 additions & 0 deletions packages/ergo-compiler/lib/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const FunctionDeclaration = require('./functiondeclaration');
const JavaScriptParser = require('composer-concerto/lib/codegen/javascriptparser');
const debug = require('debug')('cicero:Script');

/**
* <p>
* An executable script.
* </p>
* @private
* @class
* @memberof module:cicero-core
*/
class Script {

/**
* Create the Script.
* <p>
* @param {ModelManager} modelManager - The ModelManager associated with this Script
* @param {string} identifier - The identifier of the script
* @param {string} language - The language type of the script
* @param {string} contents - The contents of the script
*/
constructor(modelManager, identifier, language, contents) {
this.modelManager = modelManager;
this.identifier = identifier;
this.language = language;
this.contents = contents;
this.functions = [];
this.tokens = [];

if(!contents) {
throw new Error('Empty script contents');
}
if (this.language !== '.ergo') {
let data = {errorStatement:''};
let parser;

try {
parser = new JavaScriptParser(this.contents, false, 8);
} catch (cause) {
// consider adding a toHex method in the exception to put out the pure hex values of the file.
const error = new SyntaxError('Failed to parse ' + this.identifier + ': ' + cause.message+'\n'+data.errorStatement);
error.cause = cause;
debug('constructor', error.message, contents);
throw error;
}

const functions = parser.getFunctions();

for(let n=0; n < functions.length; n++) {
const func = functions[n];
const functionDeclaration =
new FunctionDeclaration(this.modelManager, this.language,
func.name, func.visibility,
func.returnType, func.throws, func.parameterNames,
func.parameterTypes, func.decorators, func.functionText );
this.functions.push( functionDeclaration );
}

this.tokens = parser.getTokens();
}
}

/**
* Visitor design pattern
* @param {Object} visitor - the visitor
* @param {Object} parameters - the parameter
* @return {Object} the result of visiting or null
* @private
*/
accept(visitor,parameters) {
return visitor.visit(this, parameters);
}

/**
* Returns the identifier of the script
* @return {string} the identifier of the script
*/
getIdentifier() {
return this.identifier;
}

/**
* Returns the identifier of the script
* @return {string} the identifier of the script
*/
getName() {
return this.identifier;
}

/**
* Returns the language of the script
* @return {string} the language of the script
*/
getLanguage() {
return this.language;
}

/**
* Returns the contents of the script
* @return {string} the content of the script
*/
getContents() {
return this.contents;
}

/**
* Returns the FunctionDeclaration for all functions that have been defined in this
* Script.
*
* @return {FunctionDeclaration[]} The array of FunctionDeclarations
*/
getFunctionDeclarations() {
return this.functions;
}

/**
* Returns the tokens of the script
* @return {Object[]} the tokens of the script
*/
getTokens() {
return this.tokens;
}

}

module.exports = Script;

0 comments on commit b92ee47

Please sign in to comment.