Legesher Docs

The Legesher Pride

Welcome to the Legesher Pride! Here, you'll find comprehensive guides and documentation to help you start working with Legesher as quickly as possible, as well as support if you get stuck. Let's jump right in!

Get Started    

Tree-Sitter-Legesher-Language Guide

Tree 🌴 Sitter Guide

This is the documentation I've been following when making changes to the tree-sitter repositories.

For Atom

At this time, Atom is our main focus for developing text editors for other spoken languages. The way Atom has set up their language recognition features (like syntax-highlighting, code folding, autocomplete and more) is through Tree-Sitter. As Atom.io is transitioning from the original TextMate grammars (this is the grammar used by other text editors like Sublime) that use regex for text matching to the new Tree-Sitter grammars, abstract syntax trees are the power behind the curtain.

Tree Sitter Grammars

Atom is currently in the process of transitioning all of their TextMate grammars to Tree-Sitter grammars, so Legesher at this time will be focusing on the newer tree-sitter language grammars and building on top of them. Eventually, we hope to expand to VSCode (which uses their own API of sorts for AST) and Sublime (which uses TextMate).

Whether or not you're new to abstract syntax trees or tree-sitter or Legesher, this gist has been really helpful for the team to understand tree-sitter grammars and how it relates to Legesher. If you ever get lost in the fine tune details of the commands we're running, why the structure is the way it is, etc. this would be a great resource to turn to.

For New Tree-Sitter Repositories

Here is the list of steps we'll go through in order to setup a new language's tree-sitter repositories.

Requirements

Please refer to the Requirements section in this gist for the most up to date requirements for tree-sitter grammar set-up.

Setup

<!-- **Install Tree-Sitter** - Make sure you have two versions of Python installed: some version of [Python 2](https://www.python.org/downloads/) and [Python 3](https://www.python.org/downloads/release/python-373/) - Run `python --version` to get the python 2 version number (this must point to version 2) - Run `python3 --version` to get the python 3 version number - I used [these instructions](https://evansdianga.com/install-pip-osx/) to get pip installed - Securely download get-pip.py by running this command `curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py` - Run the command `pip3 install tree-sitter` to get tree-sitter on your computer -->

Install node-gyp

  • Follow these instructions to install node-gyp on your computer.
  • Install node-gyp as a global module by running the npm install -g node-gyp command

Project Check _(this is adapted from @Aerijo's tree-sitter_guide)_

  • Clone the new tree-sitter-LANGUAGE grammar repository
    git clone https://github.com/tree-sitter/tree-sitter-LANGUAGE
    • NOTE: this is the normal convention of the tree-sitter grammars, but as they add more languages their might be exceptions to the naming convention.
  • Navigate to the root of this project in a terminal/command prompt
  • Run npm install
  • Add ./node_modules/.bin to your PATH
    • NOTE: you can check if that is added correctly to your PATH variable by running echo $PATH
    • If you don't add this, prefix all tree-sitter commands with ./node_modules/.bin/; e.g., tree-sitter generate becomes ./node_modules/.bin/tree-sitter generate
    • Windows users need to replace forwards slashes with back slashes. You can do this by typing export PATH=./node_modules/.bin:$PATH.
  • Run tree-sitter generate
  • Run node-gyp configure
  • Run node-gyp build
  • Run tree-sitter test

Repository Familiarity

**corpus**: this is where test files of how the programming language is implemented and used. Below is a list of possible files you can expect to find within this folder.

  • declarations.txt
  • expressions.txt
  • literals.txt
  • statements.txt
  • types.txt

**examples**: this is a folder of test files for checking the validity of the grammar with real use cases.

**script**: this is where language specific use cases are introduced and explained.

**src**: this is where all the magic happens. more information on this folder later.

**grammar.js**: key file that denotes the grammar, we'll be using this more going forward.

Legesher Add-In

Alright, so here's where it gets fun! 🎉 This is where we will find all of the keywords / reserved words in the respective programming language and template them out. (would love for this to be abstracted out eventually)

Getting Keyword List

  • Navigate to the **Legesher** project
  • Within the repository, navigate to /lib/config/locale/language-template.yml to see all of the keywords / reserved words for each language
  • Find the programming language that matches the tree-sitter grammar you're creating
  • For every word in that list under # , we will go through the tree-sitter grammar repo and template out. (this would be a great idea for automation)

Templating Tree-Sitter Grammar
The templating agent we are using looks for **{KEYWORD}**, so as you traverse through the following files every time you encounter a keyword (there are some exceptions) put {curly braces} around it! I'll keep note of exceptions that happen for more than one language, but the tests are a great indicator that a keyword was missed or incorrectly templated out.

  • grammar.js
    • All of the necessary keywords to change should happen within the module.exports = grammar({}) section of the file. A good thing to note, is that most of the keywords you will be templating out are in 'string' format.
    • Some keywords are within a seq() function like this lambda function below:
      lambda: $ => prec(PREC.lambda, seq(
      '{lambda}',
      optional($.lambda_parameters),
      ':',
      $._expression
      )),
      
    • Others are simply in a prec.left() function:
      pass_statement: $ => prec.left('{pass}'),
      
  • /corpus/*.txt

    • For these text files, it's a little bit more complicated than a simple cmd-f keyword. Here's an example of the await and return keywords being templating out in the abstract syntax tree explanation of how an await expression is handled.

      ``` txt

      Await expressions

    {await} i(j, 5)
    {return} {await} i(j, 5)


    (module
    (expression_statement

    (await (call
      (identifier)
      (argument_list (identifier) (integer)))))
    

    (return_statement

    (expression_list
      (await (call
        (identifier)
        (argument_list (identifier) (integer)))))))
    

    ```

  • /examples/*
    • These are the test files that the grammar will run through to make sure the abstract syntax tree of the grammar is sound. Some of these files are extremely long, while others are a few lines. This is where the syntax highlighting may get a little off depending on the keyword you are templating. This is okay - rather this shows that the work you're doing is making a difference! This will give us a good test for our syntax highlighting later on as well.
      ``` python
      {def} hi():
  {print} "hi"


{def} bye():
  {print} "bye"
```

Testing Grammar Changes
Next we'll need to test out all of the changes we've made to the grammar, to make sure the programming language is still in tact. When we've passed all of our tests, then we can continue going forward in adding this programming language to the Legesher repository.

We'll need to recompile the grammar with the changes we've made.

  • Run tree-sitter generate
  • Run node-gyp configure
  • Run node-gyp build

NOTE: Run npm run build to do all of the required steps in one command

We will eventually need to publish the tree-sitter parser to npm, but in the meantime for developing the package and the grammar, you can test it locally using apm link.

Syntax Highlighting
Tree-sitter grammars are now using the tree-sitter/highlight-schema JSON repository for their styling in the text editor. This can be found in the properties/highlights.css and src/highlights.json files.

Updating Version

This tool is available within npm's package listings and needs to be updated within there. Here are the instructions to follow to update this specific package:

  • Update the package.json file with the next version associated with the type of change (major, minor, or patch).
  • commit that change to the master branch
  • run npm publish to update package
  • run npm install to use new updated version

Tree-Sitter-Legesher-Language Guide


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.