diff --git a/src/implementation/body-parser.ts b/src/implementation/body-parser.ts deleted file mode 100644 index 47470d1..0000000 --- a/src/implementation/body-parser.ts +++ /dev/null @@ -1,59 +0,0 @@ -import { - AWAIT_TOKEN, - CLOSE_PAR, - COMMA, - CONSTANT_TOKEN, - DEFAULT_CONTRACT_CALL, - DEFAULT_TX_NAME, - EQ_SIGN, - NEWLINE, - OPEN_PAR, - RETURN_TOKEN, - SEMI_COLON, - SPACE, - FORMAT_LINE, -} from "./token"; -import { Tree } from "./type-mapping"; - -export class TypescriptBodyParser { - public static parse(fn: Tree) { - const txLiteral = NEWLINE.concat( - FORMAT_LINE, - FORMAT_LINE, - CONSTANT_TOKEN, - SPACE, - DEFAULT_TX_NAME, - SPACE, - EQ_SIGN, - SPACE, - AWAIT_TOKEN, - SPACE, - DEFAULT_CONTRACT_CALL, - fn.name, - this.parseInput(fn), - SEMI_COLON, - NEWLINE, - FORMAT_LINE, - FORMAT_LINE, - RETURN_TOKEN, - SPACE, - DEFAULT_TX_NAME, - SEMI_COLON - ); - - return txLiteral; - } - - private static parseInput(fn: Tree) { - let input = OPEN_PAR; - const lastIndex = fn.attributes.inputs.obj.length; - for (const i in fn.attributes.inputs.obj) { - input = input.concat(fn.attributes.inputs.obj[i].name); - if (parseInt(i) !== lastIndex - 1) { - input = input.concat(COMMA); - } - } - input = input.concat(CLOSE_PAR); - return input; - } -} diff --git a/src/implementation/class-parser.ts b/src/implementation/class-parser.ts deleted file mode 100644 index 0a4302b..0000000 --- a/src/implementation/class-parser.ts +++ /dev/null @@ -1,130 +0,0 @@ -import { - ANY_TOKEN, - CLOSE_BRACE, - COLON, - COMMA, - CONTRACT_TOKEN, - DEFAULT_ABI_PARAM, - DEFAULT_ADDRESS_NAME, - DEFAULT_INSTANCE_NAME, - NEWLINE, - OPEN_BRACE, - PRIVATE_IDENT, - SPACE, - STRING_TOKEN, - FORMAT_LINE, - SIGNER_OR_PROVIDER_TOKEN, - SIGNER_OR_PROVIDER_TYPE_TOKEN, - OPT_TOKEN, -} from "./token"; - -export class TypescriptClassParser { - private defaultAbiParam: string; - private defaultInstanceName: string; - private defaultAddressName: string; - private defaultSignerOrProviderName: string; - - constructor() { - this.defaultInstanceName = DEFAULT_INSTANCE_NAME; - this.defaultAddressName = DEFAULT_ADDRESS_NAME; - this.defaultAbiParam = DEFAULT_ABI_PARAM; - this.defaultSignerOrProviderName = SIGNER_OR_PROVIDER_TOKEN; - } - public parse(name: string, body: string) { - const importDirective = 'import * as ethers from "ethers"'; - const classSignature = `export default class ${name} `; - const contract = importDirective.concat( - NEWLINE, - NEWLINE, - classSignature, - OPEN_BRACE, - NEWLINE, - this.getMemberClass(), - NEWLINE, - this.getConstructor(), - NEWLINE, - body, - CLOSE_BRACE - ); - - return contract; - } - - private getConstructor() { - const constructorLiteral = `constructor(${this.inputLiteral()})`; - return FORMAT_LINE.concat(constructorLiteral, this.getConstructorBody()); - } - private inputLiteral() { - return this.defaultAddressName.concat( - COLON, - SPACE, - STRING_TOKEN, - COMMA, - SPACE, - this.defaultAbiParam, - COLON, - SPACE, - ANY_TOKEN, - COMMA, - SPACE, - SIGNER_OR_PROVIDER_TOKEN, - OPT_TOKEN, - COLON, - SPACE, - SIGNER_OR_PROVIDER_TYPE_TOKEN - ); - } - private getConstructorBody() { - const instanceLiteral = `this.${this.defaultInstanceName} = new ethers.Contract(${this.defaultAddressName}, ${this.defaultAbiParam}, ${this.defaultSignerOrProviderName});`; - const addressLiteral = `this.${this.defaultAddressName} = ${this.defaultAddressName};`; - - return SPACE.concat( - OPEN_BRACE, - NEWLINE, - FORMAT_LINE, - FORMAT_LINE, - instanceLiteral, - NEWLINE, - FORMAT_LINE, - FORMAT_LINE, - addressLiteral, - NEWLINE, - FORMAT_LINE, - CLOSE_BRACE, - NEWLINE - ); - } - - private getMemberClass() { - const contractInstanceName = this.getContractInstanceName(); - const addressName = this.getAddressName(); - - return FORMAT_LINE.concat( - contractInstanceName, - NEWLINE, - FORMAT_LINE, - addressName, - NEWLINE - ); - } - - private getContractInstanceName() { - return PRIVATE_IDENT.concat( - SPACE, - this.defaultInstanceName, - COLON, - SPACE, - CONTRACT_TOKEN - ); - } - - private getAddressName() { - return PRIVATE_IDENT.concat( - SPACE, - this.defaultAddressName, - COLON, - SPACE, - STRING_TOKEN - ); - } -} diff --git a/src/implementation/io-parser.ts b/src/implementation/io-parser.ts deleted file mode 100644 index 6a340cb..0000000 --- a/src/implementation/io-parser.ts +++ /dev/null @@ -1,210 +0,0 @@ -import { - iochild, - int, - uint, - intMapping, - address, - addressMapping, - string, - stringMapping, - bool, - boolMapping, - fallbackMapping, - ABI, - Tree, - _function, - arrayExt, -} from "./type-mapping"; -import { - COLON, - SPACE, - OPEN_PAR, - COMMA, - CLOSE_PAR, - OPEN_BRACE, - CLOSE_BRACE, - EXPORT, - ASYNC, - NEWLINE, - EMPTY_STRING, - PROMISE, - OPEN_ANGLE_BRACKET, - CLOSE_ANGLE_BRACKET, - OPEN_BRACKET, - CLOSE_BRACKET, - FORMAT_LINE, - PUBLIC_IDENT, -} from "./token"; -import { TreeBuilder } from "./grouper"; -import { TypescriptBodyParser } from "./body-parser"; - -const UNNAMED_VAR = "argv"; -const SINGLE_ELEMENT = 1; -export class TypescriptParser { - private unnamedCounter: number = 0; - - private incrementCounter() { - this.unnamedCounter++; - } - private resetCounter() { - this.unnamedCounter = 0; - } - private getVarCounter(): string { - this.incrementCounter(); - return UNNAMED_VAR.concat(this.unnamedCounter.toString()); - } - private buildInputLiteral(input: iochild): string { - const inputType = this.determineType(input.type); - const inputName = this.determineInputName(input).name; - const inputLiteral = inputName.concat(COLON, SPACE, inputType); - - return inputLiteral; - } - - // we intentionally modify this to generate arguments name for the function body - private determineInputName(input: iochild) { - input.name = input.name.length === 0 ? this.getVarCounter() : input.name; - return input; - } - - private parseInput(value: iochild[]) { - let literal: string[] = []; - for (const input of value) { - const inputLiteral = this.buildInputLiteral(input); - literal.push(inputLiteral); - } - this.resetCounter(); - - return literal; - } - - // TODO : make return type in Promise - private buildOutputLiteral(input: iochild) { - const outputType = this.determineType(input.type); - - return outputType; - } - private parseOutput(value: iochild[]) { - let literal: string[] = []; - for (const output of value) { - const outputLiteral = this.buildOutputLiteral(output); - literal.push(outputLiteral); - } - - return literal; - } - private getStartingParam() { - return SPACE.concat(OPEN_PAR); - } - - public parse(tree: Tree[]) { - for (const fn of tree) { - // it is IMPORTANT that we parse signature literal AFTER parsing input and output literals. - // because we need input and output literals to complete function signature literals. - - fn.attributes.inputs.literals = this.writeInput(fn.attributes.inputs.obj); - fn.attributes.outputs.literals = this.writeOutput( - fn.attributes.outputs.obj - ); - fn.bodyLiteral = TypescriptBodyParser.parse(fn); - fn.signatureLiteral = this.parseFnSignature(fn); - } - - return tree; - } - - private writeInput(value: iochild[]) { - if (value.length === 0) return OPEN_PAR.concat(CLOSE_PAR); - const lastIndex = value.length - 1; - let params = this.getStartingParam(); - let literal = this.parseInput(value); - for (const i in value) { - if (i == lastIndex.toString()) { - params = params.concat(literal[i]); - break; - } - params = params.concat(literal[i].concat(COMMA, SPACE)); - } - - return params.concat(CLOSE_PAR, SPACE); - } - - private writeOutput(value: iochild[]) { - if (value.length === 0) return EMPTY_STRING; - const lastIndex = value.length - 1; - let params = this.getAsyncOutput(value.length); - let literal = this.parseOutput(value); - for (const i in value) { - if (i == lastIndex.toString()) { - params = params.concat(literal[i]); - break; - } - params = params.concat(literal[i].concat(COMMA, SPACE)); - } - - if (value.length === SINGLE_ELEMENT) { - return params.concat(CLOSE_ANGLE_BRACKET, SPACE); - } else { - return params.concat(CLOSE_BRACKET, CLOSE_ANGLE_BRACKET, SPACE); - } - } - - private getAsyncOutput(length: number) { - if (length === SINGLE_ELEMENT) return PROMISE.concat(OPEN_ANGLE_BRACKET); - else return PROMISE.concat(OPEN_ANGLE_BRACKET, OPEN_BRACKET); - } - - private parseFnSignature(fnObj: Tree) { - const signature = FORMAT_LINE.concat( - PUBLIC_IDENT, - SPACE, - ASYNC, - SPACE, - fnObj.name, - fnObj.attributes.inputs.literals as string, - this.determineOutput(fnObj), - // function implementation will starts here - SPACE, - // TODO : populate function body with ethers js - // just put open and close brace for now - OPEN_BRACE, - fnObj.bodyLiteral as string, - NEWLINE, - FORMAT_LINE, - CLOSE_BRACE, - NEWLINE, - NEWLINE - ); - - return signature; - } - - private determineOutput(fnObj: Tree) { - const _eval = fnObj.attributes.outputs.obj.length; - const constant = fnObj.constant; - - if (_eval === 0 || _eval === undefined || _eval === null || !constant) - return COLON.concat( - SPACE, - PROMISE, - OPEN_ANGLE_BRACKET, - fallbackMapping, - CLOSE_ANGLE_BRACKET - ); - else return COLON.concat(SPACE, fnObj.attributes.outputs.literals as any); - } - - private determineType(value: string): string { - return value.includes(arrayExt) - ? this.inferTypes(value).concat(arrayExt) - : this.inferTypes(value); - } - - private inferTypes(value: string) { - if (value.includes(int) || value.includes(uint)) return intMapping; - else if (value === address) return addressMapping; - else if (value === string) return stringMapping; - else if (value === bool) return boolMapping; - else return fallbackMapping; - } -}