tree-sitter-printf 0.5.1

printf format grammar for tree-sitter
Documentation
/**
 * @file Tree-sitter grammar definition
 * @author Peter Stuifzand
 * @license ISC
 */

/// <reference types="tree-sitter-cli/dsl" />
// @ts-check

module.exports = grammar({
  name: 'printf',

  rules: {
    format_string: $ => repeat(
      choice($._text, $.format, '%%')
    ),

    format: $ => seq(
      '%',
      optional($.flags),
      optional($.width),
      optional($.precision),
      optional($.size),
      $.type
    ),

    // TODO: restrict?
    type: _ => /[a-zA-Z]/,

    flags: _ => /[-#0 +'I]/,

    width: _ => /0?[0-9]+|\*/,

    precision: _ => /\.([0-9]*|\*)/,

    size: _ => token(prec(1, choice(
      'hh',
      'h',
      'l',
      'll',
      'L',
      'j',
      'z',
      't',
      'I',
      'I32',
      'I64',
      'w',
    ))),

    _text: _ => prec(-1, /[^%]+/)
  }
});