@igorivaniuk/tlb-parser
Version:
Parse TLB syntax into TypeScript objects
210 lines (209 loc) • 6.09 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NumberExpr = exports.NameExpr = exports.RefExpr = exports.NegateExpr = exports.MathExpr = exports.MathOperator = exports.CombinatorExpr = exports.BuiltinZeroArgs = exports.BuiltinZeroArgsOperators = exports.BuiltinOneArgExpr = exports.BuiltinOneArgOperators = exports.BuiltinExpr = exports.CellRefExpr = exports.CompareExpr = exports.CompareOperator = exports.CondExpr = exports.Expression = exports.Combinator = exports.FieldExprDef = exports.FieldNamedDef = exports.FieldAnonymousDef = exports.FieldCurlyExprDef = exports.FieldBuiltinDef = exports.FieldBuiltinType = exports.Field = exports.Constructor = exports.Declaration = exports.Program = exports.ASTBase = exports.ASTRootBase = void 0;
class ASTRootBase {
}
exports.ASTRootBase = ASTRootBase;
// Attributes of this node, required for `walk` function, don't use directly.
ASTRootBase._attributes = [];
class ASTBase extends ASTRootBase {
}
exports.ASTBase = ASTBase;
class Program extends ASTRootBase {
constructor(declarations) {
super();
this.declarations = declarations;
this.parent = null;
}
}
exports.Program = Program;
Program._attributes = ['declarations'];
class Declaration extends ASTBase {
constructor(constructorDef, fields, combinator) {
super();
this.constructorDef = constructorDef;
this.fields = fields;
this.combinator = combinator;
}
}
exports.Declaration = Declaration;
Declaration._attributes = [
'constructorDef', 'fields', 'combinator',
];
class Constructor extends ASTBase {
constructor(name, tag) {
super();
this.name = name;
this.tag = tag;
}
getTagType() {
if (this.tag === null) {
return null;
}
return this.tag.startsWith('$') ? 'binary' : 'hex';
}
}
exports.Constructor = Constructor;
class Field extends ASTBase {
}
exports.Field = Field;
exports.FieldBuiltinType = ['+', 'Type'];
class FieldBuiltinDef extends Field {
constructor(name, type) {
super();
this.name = name;
this.type = type;
}
}
exports.FieldBuiltinDef = FieldBuiltinDef;
class FieldCurlyExprDef extends Field {
constructor(expr) {
super();
this.expr = expr;
}
}
exports.FieldCurlyExprDef = FieldCurlyExprDef;
FieldCurlyExprDef._attributes = ['expr'];
// TODO: I am not sure that `name` is allowed. Maybe it can only be `_`
// See https://github.com/ton-blockchain/ton/issues/540
class FieldAnonymousDef extends Field {
constructor(name, isRef, fields) {
super();
this.name = name;
this.isRef = isRef;
this.fields = fields;
}
}
exports.FieldAnonymousDef = FieldAnonymousDef;
FieldAnonymousDef._attributes = ['fields'];
class FieldNamedDef extends Field {
constructor(name, expr) {
super();
this.name = name;
this.expr = expr;
}
}
exports.FieldNamedDef = FieldNamedDef;
FieldNamedDef._attributes = ['expr'];
class FieldExprDef extends Field {
constructor(expr) {
super();
this.expr = expr;
}
}
exports.FieldExprDef = FieldExprDef;
FieldExprDef._attributes = ['expr'];
// Combinators
// -----------
class Combinator extends ASTBase {
constructor(name, args) {
super();
this.name = name;
this.args = args;
}
}
exports.Combinator = Combinator;
Combinator._attributes = ['args'];
// Expressions
// -----------
class Expression extends ASTBase {
}
exports.Expression = Expression;
// TODO: add validation that `dotExpr` cannot be set without `condExpr`
class CondExpr extends Expression {
constructor(left, dotExpr, condExpr) {
super();
this.left = left;
this.dotExpr = dotExpr;
this.condExpr = condExpr;
}
}
exports.CondExpr = CondExpr;
CondExpr._attributes = ['left', 'condExpr'];
exports.CompareOperator = ['<=', '>=', '!=', '=', '<', '>'];
class CompareExpr extends Expression {
constructor(left, op, right) {
super();
this.left = left;
this.op = op;
this.right = right;
}
}
exports.CompareExpr = CompareExpr;
CompareExpr._attributes = ['left', 'right'];
class CellRefExpr extends Expression {
constructor(expr) {
super();
this.expr = expr;
}
}
exports.CellRefExpr = CellRefExpr;
CellRefExpr._attributes = ['expr'];
class BuiltinExpr extends Expression {
}
exports.BuiltinExpr = BuiltinExpr;
exports.BuiltinOneArgOperators = ['#<=', '#<', '##'];
class BuiltinOneArgExpr extends BuiltinExpr {
constructor(name, arg) {
super();
this.name = name;
this.arg = arg;
}
}
exports.BuiltinOneArgExpr = BuiltinOneArgExpr;
BuiltinOneArgExpr._attributes = ['arg'];
exports.BuiltinZeroArgsOperators = ['#'];
class BuiltinZeroArgs extends BuiltinExpr {
constructor(name) {
super();
this.name = name;
}
}
exports.BuiltinZeroArgs = BuiltinZeroArgs;
class CombinatorExpr extends Expression {
constructor(name, args) {
super();
this.name = name;
this.args = args;
}
}
exports.CombinatorExpr = CombinatorExpr;
CombinatorExpr._attributes = ['args'];
exports.MathOperator = ['*', '+'];
class MathExpr extends Expression {
// TODO: narrower type for `left` and `right`?
// TODO: use `SimpleExpr` and `number`?
constructor(left, op, right) {
super();
this.left = left;
this.op = op;
this.right = right;
}
}
exports.MathExpr = MathExpr;
MathExpr._attributes = ['left', 'right'];
class NegateExpr extends Expression {
constructor(expr) {
super();
this.expr = expr;
}
}
exports.NegateExpr = NegateExpr;
NegateExpr._attributes = ['expr'];
class RefExpr extends Expression {
}
exports.RefExpr = RefExpr;
class NameExpr extends RefExpr {
constructor(name) {
super();
this.name = name;
}
}
exports.NameExpr = NameExpr;
class NumberExpr extends RefExpr {
constructor(num) {
super();
this.num = num;
}
}
exports.NumberExpr = NumberExpr;