This repository contains a Tree-sitter grammar for the Chicory programming language. Tree-sitter is an incremental parsing system that can build a syntax tree for source files and efficiently update the syntax tree as the source file is edited.
- Add this to your Helix
languages.toml
:
[[language]]
name="chicory"
scope="source.chic"
file-types=["chic"]
[[grammar]]
name = "chicory"
source = {git = "https://github.com/chicory-lang/tree-sitter-chicory" , rev="CHANGE ME: target commit hash, probably latest for now"}
- Run
hx -g fetch
to update the grammar - Run
hx -g build
to build the grammar - Copy the
queries
into~/.config/helix/runtime/queries/chicory
-
Add
chicory
to the list ofensure_installed
languages in yourrequire('nvim-treesitter.configs').setup
block. -
Associate
.chic
files withchicory
:
vim.filetype.add({
extension = {
chic = "chicory", -- Map `.chic` files to the `chicory` filetype
},
})
- Tell neovim where to find the Chicory parser:
local parser_config = require("nvim-treesitter.parsers").get_parser_configs()
parser_config.chicory = {
install_info = {
url = "https://github.com/chicory-lang/tree-sitter-chicory.git", -- Replace with your grammar repository
files = { "src/parser.c" }, -- Add other files like `src/scanner.c` if needed
branch = "main", -- Replace with the appropriate branch
generate_requires_npm = false, -- Set to true if your grammar requires npm
requires_generate_from_grammar = false, -- Set to true if you need to generate the parser
},
filetype = "chicory", -- Associate the parser with your language's filetype
}
- Put/link the queries in the right place (I used a symlink):
$ ln -s .config/nvim/queries/chicory $TREE_SITTER_CHICORY/queries
Note: You can also point the parser to a local copy of the tree-sitter-chicory
repo instead of the github url.
For more information, see https://github.com/nvim-treesitter/nvim-treesitter/#adding-parsers
Clone the repo and install the tree-sitter
CLI tool. You can do this via npm:
npm install -g tree-sitter-cli
Then, you can run the following commands to generate the parser and test it:
# Generate the parser
tree-sitter generate
# Test the parser with a sample file
tree-sitter parse path/to/sample.chic
# Run the tests
tree-sitter test
If you want to add syntax, modify grammar.js
and add tests to test/corpus/
. The tests are written in a format that Tree-sitter can understand, and you can run them using the tree-sitter test
command.
This section tracks the implementation of major language features from the ANTLR grammar.
- Primitive types (number, string, boolean, void)
- Tuple types (e.g.,
[number, string]
) - Record types (e.g.,
{ x: number, y: number }
) - Algebraic Data Types (ADTs)
- Simple variants (e.g.,
type Shape = Circle | Square
) - Variants with primitive types (e.g.,
type Option = None | Some(number)
) - Variants with record fields (e.g.,
type Result = Ok(string) | Error({ message: string })
)
- Simple variants (e.g.,
- Type references (uppercase identifiers)
- Negative numbers (e.g.,
-42
,-3.14
)
- Match expressions (e.g.,
match (value) { Some(x) => x + 1, None => 0 }
)- Pattern matching on variants
- Pattern matching with literals
- Pattern matching with identifiers
- Binary operations
- Function calls
- Function expressions
- Regular function expressions (e.g.,
(x) => x * 2
) - Parenless function expressions (e.g.,
x => x * 2
)
- Regular function expressions (e.g.,
- Literals (number, string, boolean)
- Array expressions (e.g.,
[1, 2, 3]
,[]
) - Identifiers
- Parenthesized expressions
- JSX expressions
- Self-closing elements (e.g.,
<Div />
) - Elements with children (e.g.,
<Div>Hello world</Div>
) - Elements with attributes (e.g.,
<Div className="container" count={5} />
) - Nested elements
- Expression children (e.g.,
<Div>{count}</Div>
)
- Self-closing elements (e.g.,
- Let assignments (e.g.,
let x = 42
)- With destructuring patterns (e.g.,
let [a, b] = [1, 2]
orlet {x, y} = point
)
- With destructuring patterns (e.g.,
- Const declarations (e.g.,
const PI = 3.14
)- With destructuring patterns (e.g.,
const {a, b} = obj
orconst [first, second] = array
)
- With destructuring patterns (e.g.,
- Assignment statements
- Import statements
- Export statements
- Global statements
- Comments (single-line and multi-line)
- Updated match expression syntax
The following features from the chicory.g4 grammar are still pending implementation:
- Array type expressions (e.g.,
string[]
,number[][]
) - Function array types (e.g.,
(number => boolean)[]
)