WIP xQuery tree-sitter
  • C 99.4%
  • JavaScript 0.4%
  • Tree-sitter Query 0.1%
Find a file
Andy Bunce 00b463030f
Some checks failed
main / build (push) Failing after 8s
[add xquery.so and examples
2026-06-26 14:28:14 +01:00
.github/workflows update wasm and web-ui 2023-06-04 07:09:14 +12:00
.yarn [MOD] move tree-sitter config from package.jsonto tree-sitter.json 2026-06-25 18:17:14 +01:00
_config [MOD] move tree-sitter config from package.jsonto tree-sitter.json 2026-06-25 18:17:14 +01:00
assets show and tell (#15) 2021-09-02 12:55:51 +12:00
bindings rework with yarn 2022-06-24 12:03:58 +12:00
docs add window clause to playground 2023-06-06 11:05:54 +12:00
examples [add xquery.so and examples 2026-06-26 14:28:14 +01:00
queries feat: add tags.scm for symbol-kind navigation 2026-06-16 22:36:50 +02:00
src fix: grammar review fixes 2026-06-07 13:24:18 +03:00
test/corpus for_binding consistent with let_binding 2022-07-21 11:10:46 +12:00
.env make var ref more explicit 2022-06-20 17:30:46 +12:00
.gitignore [MOD] move tree-sitter config from package.jsonto tree-sitter.json 2026-06-25 18:17:14 +01:00
.nojekyll nojekyll 2022-06-25 10:13:42 +12:00
.npmignore rework with yarn 2022-06-24 12:03:58 +12:00
.prettierrc rework with yarn 2022-06-24 12:03:58 +12:00
binding.gyp dev/work on xpath (#6) 2021-06-18 18:28:40 +12:00
Cargo.toml rework with yarn 2022-06-24 12:03:58 +12:00
CONTRIBUTING.md show and tell (#15) 2021-09-02 12:55:51 +12:00
grammar.js fix: grammar review fixes 2026-06-07 13:24:18 +03:00
LICENSE rework with yarn 2022-06-24 12:03:58 +12:00
Makefile update wasm and web-ui 2023-06-04 07:09:14 +12:00
package.json [MOD] move tree-sitter config from package.jsonto tree-sitter.json 2026-06-25 18:17:14 +01:00
README.md dev (#17) 2023-06-03 08:25:44 +12:00
tree-sitter.json [add] tags 2026-06-25 18:28:47 +01:00
xquery.so [add xquery.so and examples 2026-06-26 14:28:14 +01:00
yarn.lock [MOD] move tree-sitter config from package.jsonto tree-sitter.json 2026-06-25 18:17:14 +01:00

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.