Skip to content

Commit 5aecedb

Browse files
feat: add cpp
1 parent 3668575 commit 5aecedb

File tree

8 files changed

+160
-1
lines changed

8 files changed

+160
-1
lines changed

Diff for: packages/cpp/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# ast-grep napi language for cpp
2+
3+
## Installation
4+
5+
In a pnpm project, run:
6+
7+
```bash
8+
pnpm install @ast-grep/lang-cpp
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 cpp from '@ast-grep/lang-cpp'
18+
import { registerDynamicLanguage, parse } from '@ast-grep/napi'
19+
20+
registerDynamicLanguage({ cpp })
21+
22+
const sg = parse('cpp', `your code`)
23+
sg.root().kind()
24+
```

Diff for: packages/cpp/index.d.ts

+10
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

Diff for: packages/cpp/index.js

+9
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: ["cc","hpp","cpp","hh","cxx","cu","ino"],
7+
languageSymbol: 'tree_sitter_cpp',
8+
expandoChar: '_',
9+
}

Diff for: packages/cpp/nursery.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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: 'cpp',
8+
treeSitterPackage: 'tree-sitter-cpp',
9+
languageRegistration,
10+
testRunner: (parse) => {
11+
const sg = parse(`
12+
template <typename T>
13+
T add(T a, T b) {
14+
return a + b;
15+
}
16+
int main() {
17+
int a = 123;
18+
int result = add<int>(5, 10);
19+
return 0;
20+
}
21+
`)
22+
const root = sg.root()
23+
const node = root.find('$T $A = 123')
24+
assert.equal(node.kind(), 'declaration')
25+
}
26+
})

Diff for: packages/cpp/package.json

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"name": "@ast-grep/lang-cpp",
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-cpp": "0.23.4"
41+
},
42+
"publishConfig": {
43+
"access": "public",
44+
"registry": "https://registry.npmjs.org/"
45+
},
46+
"pnpm": {
47+
"onlyBuiltDependencies": [
48+
"@ast-grep/lang-cpp",
49+
"tree-sitter-cli"
50+
]
51+
}
52+
}

Diff for: packages/cpp/postinstall.js

+4
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+
})

Diff for: packages/csharp/nursery.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
const { setup } = require('@ast-grep/nursery')
22
const languageRegistration = require('./index')
3+
const assert = require('node:assert')
34

45
setup({
56
dirname: __dirname,
67
name: 'csharp',
78
treeSitterPackage: 'tree-sitter-c-sharp',
89
languageRegistration,
910
testRunner: (parse) => {
10-
// add test here
11+
const sg = parse('var a = 123;')
12+
const root = sg.root()
13+
const node = root.find('var $A = 123')
14+
assert.equal(node.kind(), 'variable_declaration')
1115
}
1216
})

Diff for: pnpm-lock.yaml

+30
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)