Skip to content

✨ PHP #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/wet-nights-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@ast-grep/nursery": patch
"@ast-grep/lang-php": patch
---

Add @ast-grep/lang-php
24 changes: 24 additions & 0 deletions packages/php/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# ast-grep napi language for php

## Installation

In a pnpm project, run:

```bash
pnpm install @ast-grep/lang-php
pnpm install @ast-grep/napi
# install the tree-sitter-cli if no prebuild is available
pnpm install @tree-sitter/cli --save-dev
```

## Usage

```js
import php from '@ast-grep/lang-php'
import { registerDynamicLanguage, parse } from '@ast-grep/napi'

registerDynamicLanguage({ php })

const sg = parse('php', `your code`)
sg.root().kind()
```
10 changes: 10 additions & 0 deletions packages/php/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
type LanguageRegistration = {
libraryPath: string
extensions: string[]
languageSymbol?: string
metaVarChar?: string
expandoChar?: string
}

declare const registration: LanguageRegistration
export default registration
9 changes: 9 additions & 0 deletions packages/php/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const path = require('node:path')
const libPath = path.join(__dirname, 'parser.so')

module.exports = {
libraryPath: libPath,
extensions: ['php'],
languageSymbol: 'tree_sitter_php',
expandoChar: '$',
}
18 changes: 18 additions & 0 deletions packages/php/nursery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { setup } = require('@ast-grep/nursery')
const assert = require('node:assert')
const languageRegistration = require('./index')
const path = require('node:path')

setup({
dirname: __dirname,
name: 'php',
treeSitterPackage: 'tree-sitter-php',
src: path.join('php', 'src'),
languageRegistration,
testRunner: parse => {
const sg = parse('123')
const root = sg.root()
const node = root.find('123')
assert.equal(node.kind(), 'text')
},
})
47 changes: 47 additions & 0 deletions packages/php/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "@ast-grep/lang-php",
"version": "0.0.1",
"description": "",
"main": "index.js",
"scripts": {
"build": "tree-sitter build -o parser.so ./node_modules/tree-sitter-php/php",
"source": "node nursery.js source",
"prepublishOnly": "node nursery.js source",
"postinstall": "node postinstall.js",
"test": "node nursery.js test"
},
"files": [
"index.js",
"index.d.ts",
"type.d.ts",
"postinstall.js",
"src",
"prebuilds"
],
"keywords": ["ast-grep"],
"author": "",
"license": "ISC",
"dependencies": {
"@ast-grep/setup-lang": "0.0.3"
},
"peerDependencies": {
"tree-sitter-cli": "0.24.6"
},
"peerDependenciesMeta": {
"tree-sitter-cli": {
"optional": true
}
},
"devDependencies": {
"@ast-grep/nursery": "workspace:*",
"tree-sitter-cli": "0.24.6",
"tree-sitter-php": "0.23.12"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org/"
},
"pnpm": {
"onlyBuiltDependencies": ["@ast-grep/lang-php", "tree-sitter-cli"]
}
}
4 changes: 4 additions & 0 deletions packages/php/postinstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const { postinstall } = require('@ast-grep/setup-lang')
postinstall({
dirname: __dirname,
})
31 changes: 31 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions scripts/nursery/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ interface SetupConfig {
treeSitterPackage: string
/** Test cases running in CI */
testRunner: (parse: (c: string) => SgRoot) => void
/** Path of the `src` directory inside the `tree-sitter-*` package. Useful for
* `tree-sitter-php`, `tree-sitter-typescript` and `tree-sitter-yaml`.
* @default "src" */
src?: string
}

function test(setupConfig: SetupConfig) {
Expand All @@ -44,13 +48,15 @@ export function setup(setupConfig: SetupConfig) {
function copySrcIfNeeded(config: SetupConfig) {
const { dirname, treeSitterPackage } = config
const existing = path.join(dirname, 'src')
const src = path.join(dirname, 'node_modules', treeSitterPackage, 'src')
const src = config.src || 'src'
const source = path.join(dirname, 'node_modules', treeSitterPackage, src)
if (fs.existsSync(existing)) {
log('src exists, skipping copy')
return
}

log('copying tree-sitter src')
fs.cpSync(src, 'src', { recursive: true })
fs.cpSync(source, 'src', { recursive: true })
}

interface NodeBasicInfo {
Expand Down