Skip to content

grantmacken/tree-sitter-xquery

Repository files navigation

A tree-sitter for XQuery

A tree-sitter grammar is built for an as-you-type experience in a text editor.

The aim is to provide fast identification of syntax tree parts that enable text highlighting, indenting, folding, scope info and more for a text editor.

The incremental tree-sitter parse should be a step above text highlighting with regular expressions, however tree-sitter should not be confused with a validating parser. Unlike a validating parser, tree-sitter will not stop on-error, but continue to parse and provide a syntax highlighting.

A tree-sitter web playground

Visit the interactive treesitter web playground to see the XQuery tree-sitter in action.

Building

Both Make and Yarn are required to use this repo, so you will need to install both. Clone and cd into this repo then run make install which will use Yarn to install the tree-sitter cli.

All the work is done in the grammar.js file

The repo contains a Makefile as I use make for treesitter aliases. The default make target is an alias for tree-sitter generate which will create tree-sitter files from the grammar

To see other make targets type make help

Identifiers in XQuery

An identifier in XQuery is a Extended QName, aka an EQName. The aim of the XQuery highlight captures for identifiers is show their syntactic role.

my:salary(),
(: highlight 'my' as 'namespace', 'salary' as 'function.call' :)
(salary, bonus),
(: highlight 'salary' and  'bonus' as type.name_test :)

I have attempted to make highlight captures indicates syntactic context

let $salary := 1000 return $salary
(: 
first '$salary' as  variable.let_binding 
'return' as  keyword.return.flwor 
next '$salary' as variable.reference 
:)

An attempt to eliminate semantic token ambiguity

Note: This list is incomplete

brackets

  • "[" "]" predicate in postfix expression
  • "[" "]" predicate in axis step
  • "[" "]" square array constructor

keywords

  • element in prolog declarations

  • element in computed element constructor

  • element in element kind test

  • function in prolog declarations 'default namespace declaration' and 'function declaration'

  • function in inline function expression

  • function in any function test

  • item in prolog declarations 'context item declarations'

  • item in any item test

Testing Goals:

  1. make parse-all The parser SHOULD NOT throw a parse error with any valid XQuery module text.

Tests are run via GitHub actions

The parsing examples that are derived from the W3C xQuery recommendation are found in the 'examples/spec' folder. Other parse examples are from the qt3tests suite and are in the examples/qt3tests folder

To peek at tree-sitter highlight captures in action, I run some query examples in GitHub actions, which you might want to look at.

using tree-sitter with neovim

Follow the instructions at GitHub: nvim-treesitter to use the xquery treesitter

local parser_config = require("nvim-treesitter.parsers").get_parser_configs()
parser_config.xquery = {
  install_info = {
    url = "~/projects/grantmacken/tree-sitter-xquery", -- where you have cloned this project
    files = {"src/parser.c"}
  }
}

Contributing, Discussions and Issues

Contributions and suggestions in form of issues are welcome.