tree-sitter-freemarker

Parser for FreeMarker Template Language


Keywords
parsing, incremental, tree-sitter, freemarker
License
MIT

Documentation

tree-sitter-freemarker

CI Cargo Publish Latest Version License

A tree-sitter grammar for the FreeMarker Template Language (FTL).

Features

  • Core FTL directives: assign, if/elseif/else, list/as/items/sep, ftl, function, macro, switch/on/case/default, import, local, return, break, continue
  • Full expression grammar: arithmetic, comparison, equality, logical/bitwise, in, ??, unary !/-/+, member/subscript access, function calls, and the ?builtin family
  • Interpolations (${...}), strings (single/double with escapes), numbers, arrays, and objects
  • Both the default <#...> and the square-bracket [#...] tag syntaxes, mutually exclusive per file (the mode defaults to angle brackets and is declared by the first <#ftl> / [#ftl] directive; wrong-syntax tags are treated as static text
  • <#-- ... --> and [#-- ... --] comments

Installation

Rust (crate)

[dependencies]
tree-sitter = "0.26"
tree-sitter-freemarker = "0.1"

Usage

Rust

use tree_sitter::Parser;
use tree_sitter_freemarker::LANGUAGE;

let mut parser = Parser::new();
parser
    .set_language(&LANGUAGE.into())
    .expect("Error loading FreeMarker parser");

let source = "<#if user.age >= 18>Adult<#else>Minor</#if>";
let tree = parser.parse(source, None).unwrap();
assert!(!tree.root_node().has_error());

tree-sitter CLI

tree-sitter parse example.ftl     # pretty-print a parse tree
tree-sitter test                  # run the corpus tests

Development

Prerequisites are pinned in docs/toolchain.md (tree-sitter-cli, Node, Rust).

npm install                     # installs tree-sitter-cli
tree-sitter generate --abi=15   # regenerate src/parser.c from grammar.js
cargo test                      # Rust bindings + scanner tests
npm test                        # corpus tests (tree-sitter test)
npm run format:check            # clang-format check on the scanner

License

MIT — see LICENSE.