A Tree-sitter parser for the Cypher query language, based on the Cypher Style Guide and the openCypher grammar. This parser is designed for syntax highlighting and editor support, such as integration with a Zed extension.
- Overview
- Installation and Setup
- Updating
grammar.jswith Cypher Grammar - Running Tree-sitter Commands
- Pushing to GitHub
- Creating a Zed Extension
This project implements a Tree-sitter parser for Cypher, the query language for Neo4j, following openCypher standards and the Cypher Style Guide.
To start, make sure you have Node.js and npm installed, as Tree-sitter uses npm for setup. You’ll also need the Tree-sitter CLI tool, which can be installed via npm.
Download and install Node.js, which includes npm.
Install the Tree-sitter CLI globally:
npm install -g tree-sitter-cliCreate a new directory for the Tree-sitter parser:
mkdir tree-sitter-cypher
cd tree-sitter-cypherRun the following command to initialize your Tree-sitter project. This creates a grammar.js file and package.json:
tree-sitter initThe grammar for Cypher can be derived from openCypher’s official resources:
- Refer to the openCypher grammar for details on Cypher syntax.
- Follow the Cypher Style Guide to ensure that your
grammar.jsaligns with Cypher’s conventions for readability and consistency.
Update grammar.js to define Cypher-specific constructs such as MATCH, WHERE, RETURN, and nodes/relationships. For example, here’s a sample snippet:
module.exports = grammar({
name: 'cypher',
rules: {
cypher: ($) => seq($.statement, optional(';')),
statement: ($) => $.query,
query: $ => choice($.regular_query, $.standalone_call)
...
}
});Once grammar.js is updated, you can use the following commands to generate, build, and test your parser.
tree-sitter generatetree-sitter buildTo use this Tree-sitter parser in a Zed extension for syntax highlighting and other features, refer to the cypher project on GitHub.
For more information on creating Zed extensions, refer to the official Zed documentation.
Many thanks to the following resources for guidance and support in creating this extension:
- Installing Extensions in Zed - Guide to setting up and installing extensions in Zed.
- Zed Decoded: Extensions Blog Post - Insights on how Zed handles extensions and the possibilities they open.
- openCypher GitHub Repository - Provides tools and libraries for working with the Cypher query language.
- openCypher Resources - Reference materials on Cypher syntax, language style, and best practices.
- Cypher Test Cases - Useful Cypher syntax examples for testing and validation.
- Extension Structure Inspiration - Thank you to the repository for guidance on structuring the extension files and configurations.