Skip to content

simplificare-org/tree-sitter-cypher

Repository files navigation

Tree Sitter Cypher

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.


Table of Contents


Overview

This project implements a Tree-sitter parser for Cypher, the query language for Neo4j, following openCypher standards and the Cypher Style Guide.

Installation and Setup

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.

Step 1: Install Node.js and npm

Download and install Node.js, which includes npm.

Step 2: Install Tree-sitter CLI

Install the Tree-sitter CLI globally:

npm install -g tree-sitter-cli

Step 3: Create the Project Directory

Create a new directory for the Tree-sitter parser:

mkdir tree-sitter-cypher
cd tree-sitter-cypher

Step 4: Initialize the Tree-sitter Project

Run the following command to initialize your Tree-sitter project. This creates a grammar.js file and package.json:

tree-sitter init

Step 5: Updating grammar.js with Cypher Grammar

The 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.js aligns 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)
      ...
  }  

});

Step 6: Running Tree-sitter Commands

Once grammar.js is updated, you can use the following commands to generate, build, and test your parser.

Generate the Parser

tree-sitter generate

Build the Parser

tree-sitter build

Step 7: Push to GitHub

Creating a Zed Extension

To 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.

References and Acknowledgments

Many thanks to the following resources for guidance and support in creating this extension:

  1. Installing Extensions in Zed - Guide to setting up and installing extensions in Zed.
  2. Zed Decoded: Extensions Blog Post - Insights on how Zed handles extensions and the possibilities they open.
  3. openCypher GitHub Repository - Provides tools and libraries for working with the Cypher query language.
  4. openCypher Resources - Reference materials on Cypher syntax, language style, and best practices.
  5. Cypher Test Cases - Useful Cypher syntax examples for testing and validation.
  6. Extension Structure Inspiration - Thank you to the repository for guidance on structuring the extension files and configurations.

About

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.

Topics

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors