Files
tree-sitter-tumblr/grammar.js
T

80 lines
1.7 KiB
JavaScript

/**
* @file Tumblr grammar for tree-sitter
* @author Oscar Wallberg <oscar.wallberg@outlook.com>
* @license Apache-2.0
*/
/// <reference types="tree-sitter-cli/dsl" />
// @ts-check
export default grammar({
name: "tumblr",
extras: _ => [],
rules: {
template: $ => repeat($._node),
_node: $ => choice(
$.content,
$.block_open,
$.block_close,
$.lang_tag,
$.variable,
),
content: _ => token(prec(-1, /([^{]|\{[^A-Za-z/])+/)),
block_open: $ => seq(
"{",
$.block_keyword,
":",
$.block_name,
optional($.attributes),
"}",
),
block_close: $ => seq(
"{",
"/",
$.block_keyword,
":",
$.block_name,
"}",
),
lang_tag: $ => seq(
"{",
$.lang_keyword,
":",
$.lang_text,
"}",
),
variable: $ => seq(
"{",
choice(
seq($.variable_name, optional(seq("-", $.variable_modifier))),
seq($.variable_prefix, ":", $.prefix_argument),
),
"}",
),
attributes: $ => repeat1($.attribute),
attribute: $ => seq($._space, $.attribute_name, "=", $.attribute_value),
attribute_name: _ => /[A-Za-z_][A-Za-z0-9_-]*/,
attribute_value: _ => /"[^"]*"/,
block_name: _ => /[A-Za-z][A-Za-z0-9_]*/,
variable_name: _ => /[A-Z][A-Za-z0-9_]*/,
variable_modifier: _ => /[A-Za-z0-9]+/,
variable_prefix: _ => choice("text", "color", "font", "image"),
prefix_argument: _ => /[A-Za-z][A-Za-z0-9 _-]*/,
lang_text: _ => /[^}]+/,
block_keyword: _ => token(prec(1, /[Bb][Ll][Oo][Cc][Kk]/)),
lang_keyword: _ => token(prec(1, /[Ll][Aa][Nn][Gg]/)),
_space: _ => /[ \t]+/,
},
});