Skip to content

Commit 1718d98

Browse files
committedJan 15, 2025
fix: add readme
1 parent bdc0752 commit 1718d98

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed
 

‎README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,71 @@
11
# Monorepo for `@ast-grep/lang-*` packages
2+
3+
This monorepo contains two folders: `packages` and `scripts`.
4+
5+
## Packages
6+
7+
Packages contain a list of prebuild tree-sitter grammars to use with the `@ast-grep/napi` package.
8+
9+
Typical usage:
10+
11+
1. Install the language package in your project
12+
13+
```bash
14+
pnpm install @ast-grep/lang-{name}
15+
pnpm install @ast-grep/napi
16+
# install the tree-sitter-cli if no prebuild is available
17+
pnpm install @tree-sitter/cli --save-dev
18+
```
19+
20+
2. Use the language package in your project
21+
22+
```javascript
23+
import lang from '@ast-grep/lang-{name}'
24+
import { registerDynamicLanguage, parse } from '@ast-grep/napi'
25+
26+
registerDynamicLanguage({ [langName]: lang })
27+
28+
const sg = parse(langName, `your code`)
29+
sg.root().kind()
30+
```
31+
32+
## Scripts
33+
34+
Scripts contain a list of scripts to help with the development of the packages.
35+
36+
### `scripts/create-lang`
37+
38+
You can use `pnpm create @ast-grep/lang` to create a new language package.
39+
40+
```bash
41+
# create a new language package in current directory
42+
pnpm create @ast-grep/lang
43+
# create package in a specific directory
44+
pnpm create @ast-grep/lang some-dir
45+
```
46+
47+
You will be prompted to enter the language name, the language file extension, package name and tree-sitter grammar package.
48+
49+
Note the tree-sitter grammar package is different from the package name. package name is the repo you create, tree-sitter package should be a package already published on npm.
50+
51+
After the package is created, you can use these commands:
52+
53+
```bash
54+
pnpm source # copy tree-sitter grammar files to the src folder
55+
pnpm build # build the tree-sitter grammar files to dynamic lib
56+
pnpm test # test the tree-sitter grammar files
57+
```
58+
59+
You can edit `nursery.js` to add test case for your language.
60+
61+
62+
### `scripts/nursery`
63+
64+
A build-time dependency for the `@ast-grep/lang-*` packages.
65+
It is used to copy and test the tree-sitter grammars.
66+
67+
### `scripts/setup`
68+
69+
A postinstall script to setup parser dynamic library for the `@ast-grep/lang-*` packages.
70+
If there is prebuild available, it will copy the dynamic library according to the platform and architecture.
71+
If there is no prebuild available, it will build the dynamic library from the tree-sitter grammar files.

‎scripts/create-lang/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,21 @@ pnpm create @ast-grep/lang
88
# create package in a specific directory
99
pnpm create @ast-grep/lang some-dir
1010
```
11+
12+
You will be prompted to enter the language name, the language file extension, package name and tree-sitter grammar package.
13+
14+
Note the tree-sitter grammar package is different from the package name. package name is the repo you create, tree-sitter package should be a package already published on npm.
15+
16+
17+
## Commands
18+
After the package is created, you can use these commands:
19+
20+
```bash
21+
pnpm source # copy tree-sitter grammar files to the src folder
22+
pnpm build # build the tree-sitter grammar files to dynamic lib
23+
pnpm test # test the tree-sitter grammar files
24+
```
25+
26+
### Test file
27+
28+
You can edit `nursery.js` to add test case for your language.

0 commit comments

Comments
 (0)
Please sign in to comment.