Skip to content

Commit

Permalink
fix: resolve cli issues, update docs, and correct package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
thekiba committed Feb 11, 2024
1 parent ae66754 commit b6019c4
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 30 deletions.
52 changes: 48 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
68 changes: 46 additions & 22 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -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 <input>');
console.log(' input: input file');
}
31 changes: 30 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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'

0 comments on commit b6019c4

Please sign in to comment.