From b6019c4d8ff1639e390e0c0b7d61b275cd67388c Mon Sep 17 00:00:00 2001 From: thekiba Date: Sun, 11 Feb 2024 20:46:35 +0400 Subject: [PATCH] fix: resolve cli issues, update docs, and correct package.json --- README.md | 52 +++++++++++++++++++++++++++++++++--- package-lock.json | 7 +++-- package.json | 6 ++++- src/cli.ts | 68 ++++++++++++++++++++++++++++++++--------------- src/index.ts | 31 ++++++++++++++++++++- 5 files changed, 134 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 07ba8ae..3092407 100644 --- a/README.md +++ b/README.md @@ -8,16 +8,60 @@ npm install @toncommunity/tlb-parser ## Usage +Create a file with TLB scheme according to the [documentation](https://docs.ton.org/develop/data-formats/tl-b-language). This is an example of such a file (call it `example.tlb`): +``` +t$_ x:# y:(uint 5) = A; +``` + +Then do: +```bash +npx tlb-parser example.tlb +``` + +Or you can use the tool from inside JS or TS code. + ```typescript -import { parse } from "@toncommunity/tlb-parser" +import { ast, NodeVisitor, ASTRootBase } from "@toncommunity/tlb-parser"; + +class TestVisitor extends NodeVisitor { + public visited: { [key: string]: number }; + + constructor() { + super(); + this.visited = {}; + } + + override genericVisit(node: nodes.ASTRootBase): void { + if (this.visited[node.constructor.name] === undefined) { + this.visited[node.constructor.name] = 0; + } + + this.visited[node.constructor.name] += 1; + return super.genericVisit(node); + } +} const scheme = ` t$_ x:# y:(uint 5) = A; -` +`; + +const tree = ast(scheme); +const visitor = new TestVisitor(); +visitor.visit(tree); -const ast = parse(scheme) +console.log( + util.inspect( + visitor.visited, + {showHidden: false, depth: null, colors: true}, + ), +); -console.log(ast) +console.log( + util.inspect( + tree, + {showHidden: false, depth: null, colors: true}, + ), +); ``` ## Related diff --git a/package-lock.json b/package-lock.json index 927b93b..56bedde 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,16 +1,19 @@ { - "name": "tlb-parser", + "name": "@toncommunity/tlb-parser", "version": "0.1.0", "lockfileVersion": 2, "requires": true, "packages": { "": { - "name": "tlb-parser", + "name": "@toncommunity/tlb-parser", "version": "0.1.0", "license": "MIT", "dependencies": { "ohm-js": "^16.6.0" }, + "bin": { + "tlb-parser": "dist/cli.js" + }, "devDependencies": { "@types/dedent": "^0.7.2", "@types/jest": "^29.5.12", diff --git a/package.json b/package.json index cbf893a..4baa4b1 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,11 @@ "name": "@toncommunity/tlb-parser", "version": "0.1.0", "description": "Parse TLB syntax into TypeScript objects", - "main": "index.js", + "main": "./dist/index.js", + "types": "./dist/index.d.ts", + "bin": { + "tlb-parser": "./dist/cli.js" + }, "scripts": { "build": "tsc --project tsconfig.build.json", "test": "jest" diff --git a/src/cli.ts b/src/cli.ts index 3a7d812..95d8d8d 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -1,44 +1,68 @@ -// Only used for tests now, CLI will be awailable later -// TODO: build normal CLI +#!/usr/bin/env node -import fs from 'fs' -import util from 'util' +import fs from 'fs'; +import util from 'util'; +import {ast, NodeVisitor, ASTRootBase} from './index'; -import { ast } from './index' +if (process.argv[2] === '--help' || process.argv[2] === '-h' || process.argv[2] === 'help') { + help(); + process.exit(0); +} + +const inputPath = process.argv[2]; +if (!inputPath) { + console.error('No input file'); + help(); + process.exit(1); +} + +if (!fs.existsSync(inputPath)) { + console.error('Input file does not exist'); + process.exit(1); +} const input = fs.readFileSync( - process.argv[2] as string, + inputPath, 'utf-8', -) +); -import { NodeVisitor } from './ast/visit' -import * as nodes from './ast/nodes' class TestVisitor extends NodeVisitor { - public visited: {[key: string]: number} + public visited: { [key: string]: number }; constructor() { - super() - this.visited = {} + super(); + this.visited = {}; } - override genericVisit(node: nodes.ASTRootBase): void { + override genericVisit(node: ASTRootBase): void { if (this.visited[node.constructor.name] === undefined) { - this.visited[node.constructor.name] = 0 + this.visited[node.constructor.name] = 0; } - this.visited[node.constructor.name] += 1 - return super.genericVisit(node) + this.visited[node.constructor.name] += 1; + return super.genericVisit(node); } } -const tree = ast(input) -const visitor = new TestVisitor() -visitor.visit(tree) -console.log(JSON.stringify(visitor.visited, null, 2)) +const tree = ast(input); +const visitor = new TestVisitor(); +visitor.visit(tree); + +console.log( + util.inspect( + visitor.visited, + {showHidden: false, depth: null, colors: true}, + ), +); console.log( util.inspect( tree, - { showHidden: false, depth: null, colors: true }, + {showHidden: false, depth: null, colors: true}, ), -) +); + +function help() { + console.log('Usage: tlb-parser '); + console.log(' input: input file'); +} diff --git a/src/index.ts b/src/index.ts index f8c844c..753b36c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,7 +3,6 @@ import type { Grammar, MatchResult } from 'ohm-js' import type { Program } from './ast/nodes' import { buildGrammar, buildAST } from './intermediate' - export function parse ( input: string, grammar: Grammar | undefined = undefined, @@ -18,3 +17,33 @@ export function parse ( export function ast (input: string): Program { return buildAST(input, buildGrammar()) } + +export {NodeVisitor} from './ast/visit'; + +export { + ASTRootBase, + ASTBase, + Program, + Declaration, + Constructor, + Field, + FieldBuiltinDef, + FieldCurlyExprDef, + FieldAnonymousDef, + FieldNamedDef, + FieldExprDef, + Combinator, + Expression, + CondExpr, + CompareExpr, + CellRefExpr, + BuiltinExpr, + BuiltinOneArgExpr, + BuiltinZeroArgs, + CombinatorExpr, + MathExpr, + NegateExpr, + RefExpr, + NameExpr, + NumberExpr, +} from './ast/nodes'