Skip to content

Commit f4ded17

Browse files
feat: add python
1 parent f5399bd commit f4ded17

File tree

9 files changed

+148
-4
lines changed

9 files changed

+148
-4
lines changed

packages/csharp/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
In a pnpm project, run:
66

77
```bash
8-
pnpm install @ast-grep/csharp
8+
pnpm install @ast-grep/lang-csharp
99
pnpm install @ast-grep/napi
1010
# install the tree-sitter-cli if no prebuild is available
1111
pnpm install @tree-sitter/cli --save-dev
@@ -14,7 +14,7 @@ pnpm install @tree-sitter/cli --save-dev
1414
## Usage
1515

1616
```js
17-
import csharp from '@ast-grep/csharp'
17+
import csharp from '@ast-grep/lang-csharp'
1818
import { registerDynamicLanguage, parse } from '@ast-grep/napi'
1919

2020
registerDynamicLanguage({ csharp })

packages/csharp/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "@ast-grep/csharp",
2+
"name": "@ast-grep/lang-csharp",
33
"version": "0.0.1",
44
"description": "",
55
"main": "index.js",
@@ -45,7 +45,7 @@
4545
},
4646
"pnpm": {
4747
"onlyBuiltDependencies": [
48-
"@ast-grep/csharp",
48+
"@ast-grep/lang-csharp",
4949
"tree-sitter-cli"
5050
]
5151
}

packages/python/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# ast-grep napi language for python
2+
3+
## Installation
4+
5+
In a pnpm project, run:
6+
7+
```bash
8+
pnpm install @ast-grep/lang-python
9+
pnpm install @ast-grep/napi
10+
# install the tree-sitter-cli if no prebuild is available
11+
pnpm install @tree-sitter/cli --save-dev
12+
```
13+
14+
## Usage
15+
16+
```js
17+
import python from '@ast-grep/lang-python'
18+
import { registerDynamicLanguage, parse } from '@ast-grep/napi'
19+
20+
registerDynamicLanguage({ python })
21+
22+
const sg = parse('python', `your code`)
23+
sg.root().kind()
24+
```

packages/python/index.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
type LanguageRegistration = {
2+
libraryPath: string
3+
extensions: string[]
4+
languageSymbol?: string
5+
metaVarChar?: string
6+
expandoChar?: string
7+
}
8+
9+
declare const registratoin: LanguageRegistration
10+
export default registratoin

packages/python/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const path = require('node:path')
2+
const libPath = path.join(__dirname, 'parser.so')
3+
4+
module.exports = {
5+
libraryPath: libPath,
6+
extensions: ["py"],
7+
languageSymbol: 'tree_sitter_python',
8+
expandoChar: 'µ',
9+
}

packages/python/nursery.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const { setup } = require('@ast-grep/nursery')
2+
const languageRegistration = require('./index')
3+
const assert = require('node:assert')
4+
5+
setup({
6+
dirname: __dirname,
7+
name: 'python',
8+
treeSitterPackage: 'tree-sitter-python',
9+
languageRegistration,
10+
testRunner: (parse) => {
11+
const sg = parse('a = 123')
12+
const root = sg.root()
13+
const node = root.find('$FIELD = 123')
14+
assert.equal(node.kind(), 'assignment')
15+
}
16+
})

packages/python/package.json

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"name": "@ast-grep/lang-python",
3+
"version": "0.0.1",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"build": "tree-sitter build -o parser.so",
8+
"source": "node nursery.js source",
9+
"prepublishOnly": "node nursery.js source",
10+
"postinstall": "node postinstall.js",
11+
"test": "node nursery.js test"
12+
},
13+
"files": [
14+
"index.js",
15+
"index.d.ts",
16+
"type.d.ts",
17+
"postinstall.js",
18+
"src",
19+
"prebuilds"
20+
],
21+
"keywords": [
22+
"ast-grep"
23+
],
24+
"author": "",
25+
"license": "ISC",
26+
"dependencies": {
27+
"@ast-grep/setup-lang": "0.0.3"
28+
},
29+
"peerDependencies": {
30+
"tree-sitter-cli": "0.24.6"
31+
},
32+
"peerDependenciesMeta": {
33+
"tree-sitter-cli": {
34+
"optional": true
35+
}
36+
},
37+
"devDependencies": {
38+
"@ast-grep/nursery": "0.0.2",
39+
"tree-sitter-cli": "0.24.6",
40+
"tree-sitter-python": "0.23.6"
41+
},
42+
"publishConfig": {
43+
"access": "public",
44+
"registry": "https://registry.npmjs.org/"
45+
},
46+
"pnpm": {
47+
"onlyBuiltDependencies": [
48+
"@ast-grep/lang-python",
49+
"tree-sitter-cli"
50+
]
51+
}
52+
}

packages/python/postinstall.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const { postinstall } = require('@ast-grep/setup-lang')
2+
postinstall({
3+
dirname: __dirname,
4+
})

pnpm-lock.yaml

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)