Skip to content

Commit 597f386

Browse files
authored
✨ PHP (#61)
* ✨ php * ✨ Add a `src` parameter to `SetupConfig` * 🚧 Use the new `src` config * ✅ Add a test for PHP * 📝 Add changeset
1 parent 785e220 commit 597f386

File tree

9 files changed

+157
-2
lines changed

9 files changed

+157
-2
lines changed

.changeset/wet-nights-cover.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@ast-grep/nursery": patch
3+
"@ast-grep/lang-php": patch
4+
---
5+
6+
Add @ast-grep/lang-php

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

packages/php/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 registration: LanguageRegistration
10+
export default registration

packages/php/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: ['php'],
7+
languageSymbol: 'tree_sitter_php',
8+
expandoChar: '$',
9+
}

packages/php/nursery.js

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

packages/php/package.json

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"name": "@ast-grep/lang-php",
3+
"version": "0.0.1",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"build": "tree-sitter build -o parser.so ./node_modules/tree-sitter-php/php",
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": ["ast-grep"],
22+
"author": "",
23+
"license": "ISC",
24+
"dependencies": {
25+
"@ast-grep/setup-lang": "0.0.3"
26+
},
27+
"peerDependencies": {
28+
"tree-sitter-cli": "0.24.6"
29+
},
30+
"peerDependenciesMeta": {
31+
"tree-sitter-cli": {
32+
"optional": true
33+
}
34+
},
35+
"devDependencies": {
36+
"@ast-grep/nursery": "workspace:*",
37+
"tree-sitter-cli": "0.24.6",
38+
"tree-sitter-php": "0.23.12"
39+
},
40+
"publishConfig": {
41+
"access": "public",
42+
"registry": "https://registry.npmjs.org/"
43+
},
44+
"pnpm": {
45+
"onlyBuiltDependencies": ["@ast-grep/lang-php", "tree-sitter-cli"]
46+
}
47+
}

packages/php/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: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/nursery/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ interface SetupConfig {
2222
treeSitterPackage: string
2323
/** Test cases running in CI */
2424
testRunner: (parse: (c: string) => SgRoot) => void
25+
/** Path of the `src` directory inside the `tree-sitter-*` package. Useful for
26+
* `tree-sitter-php`, `tree-sitter-typescript` and `tree-sitter-yaml`.
27+
* @default "src" */
28+
src?: string
2529
}
2630

2731
function test(setupConfig: SetupConfig) {
@@ -44,13 +48,15 @@ export function setup(setupConfig: SetupConfig) {
4448
function copySrcIfNeeded(config: SetupConfig) {
4549
const { dirname, treeSitterPackage } = config
4650
const existing = path.join(dirname, 'src')
47-
const src = path.join(dirname, 'node_modules', treeSitterPackage, 'src')
51+
const src = config.src || 'src'
52+
const source = path.join(dirname, 'node_modules', treeSitterPackage, src)
4853
if (fs.existsSync(existing)) {
4954
log('src exists, skipping copy')
5055
return
5156
}
57+
5258
log('copying tree-sitter src')
53-
fs.cpSync(src, 'src', { recursive: true })
59+
fs.cpSync(source, 'src', { recursive: true })
5460
}
5561

5662
interface NodeBasicInfo {

0 commit comments

Comments
 (0)