Home Reference Source Repository
public class | source

Parser

Parser class allows for parsing Fincontract descriptions, using math.js as its back-end.

Example:

import Parser from './fincontract-parser';
try {
  const p = new Parser();
  const expression = 'And(Give(Scale(11,One(USD))),Scale(10,One(EUR)))';
  const desc = await p.parse(expression);
} catch (err) {
  console.log(err);
}

Static Member Summary

Static Public Members
public static get

Time tolerance for At primitive in seconds.

public static get

Expiration time for Fincontracts.

Method Summary

Public Methods
public

parse(expression: String): FincNode

Parses the Fincontract description expression and creates a FincNode description tree.

public

Parses address from an AST node.

public

Parses scale from an AST node.

public

Traverses the Abstract Syntax Tree, that was output from math.js#parse function and recursively constructs FincNode description tree.

Static Public Members

public static get Delta: Number source

Time tolerance for At primitive in seconds. If At primitive is scheduled at time t then the Fincontract will only be valid from t - Delta/2 to t + Delta/2. By default it's 30 seconds.

public static get Expiration: Number source

Expiration time for Fincontracts. All Fincontracts expire by default after 1 year. Where 1 year is not exactly 1 year, but 3600 * 24 * 365 seconds

Public Methods

public parse(expression: String): FincNode source

Parses the Fincontract description expression and creates a FincNode description tree. The expression has to be well-defined meaning it cannot contain white characters, it has to have balanced parentheses and contain only valid primitives (keywords) as described in the paper. Keywords At, Before and After are also supported. https://orbilu.uni.lu/bitstream/10993/30975/1/Findel_2017-03-08-CR.pdf

Params:

NameTypeAttributeDescription
expression String

well-defined Fincontract description

Return:

FincNode

FincNode description tree

public parseAddress(node: Object): String source

Parses address from an AST node. Address in form 0x.... has to be processed further since math.js interprets 0x... as multiplication.

Params:

NameTypeAttributeDescription
node Object

current AST node containing address

Return:

String

address - parsed address as String

public parseScale(node: Object): String source

Parses scale from an AST node. Negative scales have to be processed further since math.js interprets e.g. -4 as operation.

Params:

NameTypeAttributeDescription
node Object

current AST node containing an integer scale factor

Return:

String

address - parsed integer scale factor as String

public visit(node: Object): FincNode source

Traverses the Abstract Syntax Tree, that was output from math.js#parse function and recursively constructs FincNode description tree.

Params:

NameTypeAttributeDescription
node Object

node of AST from math.js#parse

Return:

FincNode

currently constructed FincNode

Throw:

Error

If unknown node is processed