Skip to content
This repository was archived by the owner on Nov 20, 2023. It is now read-only.

Commit fa8732d

Browse files
committedAug 9, 2022
LICENSE and README.md
1 parent 6514ca3 commit fa8732d

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed
 

‎LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2022-present Kevin Newton
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

‎README.md

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# node-syntax-tree
2+
3+
`node-syntax-tree` is a node package for executing Syntax Tree in a WASM environment. It functions by compiling the Ruby interpreter and Syntax Tree source files into a WASM package that is loaded and executed at runtime.
4+
5+
## Getting started
6+
7+
If you're using the `npm`:
8+
9+
```bash
10+
npm install --save node-syntax-tree
11+
```
12+
13+
Or if you're using `yarn`, then add the plugin by:
14+
15+
```bash
16+
yarn add node-syntax-tree
17+
```
18+
19+
## Usage
20+
21+
`node-syntax-tree` provides both CJS and ESM modules distributions. To require through CJS, run:
22+
23+
```js
24+
const { default: createSyntaxTree } = require("node-syntax-tree");
25+
```
26+
27+
To require through ESM, run:
28+
29+
```js
30+
import createSyntaxTree from "node-syntax-tree";
31+
```
32+
33+
Once you have the import, you can initial the virtual machine and run the associated functions, as in:
34+
35+
```js
36+
createSyntaxTree().then((syntaxTree) => {
37+
console.log(syntaxTree.handlers.ruby.parse("foo"));
38+
// => (program (statements ((vcall (ident "foo")))))
39+
40+
console.log(syntaxTree.handlers.ruby.format("1+1"));
41+
// => 1 + 1
42+
43+
console.log(syntaxTree.handlers.haml.parse("= foo"));
44+
// => (root children=[(script text=" foo")])
45+
46+
console.log(syntaxTree.handlers.haml.format("= foo"));
47+
// => = foo
48+
});
49+
```
50+
51+
TypeScript types are provided along with this package, and effectively boil down to:
52+
53+
```js
54+
type SyntaxTreeHandler = {
55+
format(source: string): string;
56+
parse(source: string): string;
57+
read(filepath: string): string;
58+
};
59+
60+
type SyntaxTree = {
61+
handlers: Record<"haml" | "ruby", SyntaxTreeHandler>
62+
};
63+
64+
function createSyntaxTree(): Promise<SyntaxTree>;
65+
```
66+
67+
## Contributing
68+
69+
Bug reports and pull requests are welcome on GitHub at https://github.com/ruby-syntax-tree/node-syntax-tree.
70+
71+
## License
72+
73+
The package is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
74+
75+
76+
import { RubyVM } from "ruby-head-wasm-wasi/dist/index";
77+
declare type SyntaxTreeHandler = {
78+
format(source: string): string;
79+
parse(source: string): string;
80+
read(filepath: string): string;
81+
};
82+
export declare type SyntaxTree = {
83+
rubyVM: RubyVM;
84+
handlers: Record<"haml" | "ruby", SyntaxTreeHandler>;
85+
};
86+
export default function createSyntaxTree(): Promise<SyntaxTree>;
87+
export {};
88+
89+
90+
test("handlers.haml.parse", () => {
91+
assert.equal(
92+
syntaxTree.handlers.haml.parse("= foo"),
93+
`(root children=[(script text=" foo")])\n`
94+
);
95+
});

0 commit comments

Comments
 (0)