diff --git a/.changeset/wet-nights-cover.md b/.changeset/wet-nights-cover.md new file mode 100644 index 0000000..e067ca9 --- /dev/null +++ b/.changeset/wet-nights-cover.md @@ -0,0 +1,6 @@ +--- +"@ast-grep/nursery": patch +"@ast-grep/lang-php": patch +--- + +Add @ast-grep/lang-php diff --git a/packages/php/README.md b/packages/php/README.md new file mode 100644 index 0000000..bd56beb --- /dev/null +++ b/packages/php/README.md @@ -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() +``` diff --git a/packages/php/index.d.ts b/packages/php/index.d.ts new file mode 100644 index 0000000..11140a6 --- /dev/null +++ b/packages/php/index.d.ts @@ -0,0 +1,10 @@ +type LanguageRegistration = { + libraryPath: string + extensions: string[] + languageSymbol?: string + metaVarChar?: string + expandoChar?: string +} + +declare const registration: LanguageRegistration +export default registration diff --git a/packages/php/index.js b/packages/php/index.js new file mode 100644 index 0000000..e64d7c0 --- /dev/null +++ b/packages/php/index.js @@ -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: '$', +} diff --git a/packages/php/nursery.js b/packages/php/nursery.js new file mode 100644 index 0000000..ecee25a --- /dev/null +++ b/packages/php/nursery.js @@ -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') + }, +}) diff --git a/packages/php/package.json b/packages/php/package.json new file mode 100644 index 0000000..a9a5333 --- /dev/null +++ b/packages/php/package.json @@ -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"] + } +} diff --git a/packages/php/postinstall.js b/packages/php/postinstall.js new file mode 100644 index 0000000..b9141ff --- /dev/null +++ b/packages/php/postinstall.js @@ -0,0 +1,4 @@ +const { postinstall } = require('@ast-grep/setup-lang') +postinstall({ + dirname: __dirname, +}) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2db243b..ecbe981 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -274,6 +274,22 @@ importers: specifier: 2.1.3 version: 2.1.3 + packages/php: + dependencies: + '@ast-grep/setup-lang': + specifier: 0.0.3 + version: 0.0.3 + devDependencies: + '@ast-grep/nursery': + specifier: workspace:* + version: link:../../scripts/nursery + tree-sitter-cli: + specifier: 0.24.6 + version: 0.24.6 + tree-sitter-php: + specifier: 0.23.12 + version: 0.23.12(tree-sitter@0.21.1) + packages/python: dependencies: '@ast-grep/setup-lang': @@ -1159,6 +1175,14 @@ packages: tree-sitter-lua@2.1.3: resolution: {integrity: sha512-BmRSRI0Y4J47cE2cODyXsPiueDSAnIrFLJqOP/gKIJhGa4HoGpvEccmNuhAEVGtCrgaHGhaIkWeqiMGCgQ0cfw==} + tree-sitter-php@0.23.12: + resolution: {integrity: sha512-VwkBVOahhC2NYXK/Fuqq30NxuL/6c2hmbxEF4jrB7AyR5rLc7nT27mzF3qoi+pqx9Gy2AbXnGezF7h4MeM6YRA==} + peerDependencies: + tree-sitter: ^0.21.1 + peerDependenciesMeta: + tree-sitter: + optional: true + tree-sitter-python@0.23.6: resolution: {integrity: sha512-yIM9z0oxKIxT7bAtPOhgoVl6gTXlmlIhue7liFT4oBPF/lha7Ha4dQBS82Av6hMMRZoVnFJI8M6mL+SwWoLD3A==} peerDependencies: @@ -1908,6 +1932,13 @@ snapshots: dependencies: nan: 2.22.2 + tree-sitter-php@0.23.12(tree-sitter@0.21.1): + dependencies: + node-addon-api: 8.3.1 + node-gyp-build: 4.8.4 + optionalDependencies: + tree-sitter: 0.21.1 + tree-sitter-python@0.23.6: dependencies: node-addon-api: 8.3.0 diff --git a/scripts/nursery/index.ts b/scripts/nursery/index.ts index de7ab89..1255b86 100644 --- a/scripts/nursery/index.ts +++ b/scripts/nursery/index.ts @@ -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) { @@ -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 {