Skip to content

Commit

Permalink
feat: support YAML 1.1 schema
Browse files Browse the repository at this point in the history
  • Loading branch information
ObserverOfTime committed Jan 25, 2025
1 parent 1805917 commit cdb54d4
Show file tree
Hide file tree
Showing 28 changed files with 13,981 additions and 10,776 deletions.
24 changes: 19 additions & 5 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -534,11 +534,11 @@ module.exports = grammar({

// plain scalar schema

_r_sgl_pln_blk: $ => choice($._r_sgl_pln_nul_blk, $._r_sgl_pln_bol_blk, $._r_sgl_pln_int_blk, $._r_sgl_pln_flt_blk, $._r_sgl_pln_str_blk),
_br_sgl_pln_blk: $ => choice($._br_sgl_pln_nul_blk, $._br_sgl_pln_bol_blk, $._br_sgl_pln_int_blk, $._br_sgl_pln_flt_blk, $._br_sgl_pln_str_blk),
_b_sgl_pln_blk: $ => choice($._b_sgl_pln_nul_blk, $._b_sgl_pln_bol_blk, $._b_sgl_pln_int_blk, $._b_sgl_pln_flt_blk, $._b_sgl_pln_str_blk),
_r_sgl_pln_flw: $ => choice($._r_sgl_pln_nul_flw, $._r_sgl_pln_bol_flw, $._r_sgl_pln_int_flw, $._r_sgl_pln_flt_flw, $._r_sgl_pln_str_flw),
_br_sgl_pln_flw: $ => choice($._br_sgl_pln_nul_flw, $._br_sgl_pln_bol_flw, $._br_sgl_pln_int_flw, $._br_sgl_pln_flt_flw, $._br_sgl_pln_str_flw),
_r_sgl_pln_blk: $ => scalar_types($, 'r', 'blk'),
_br_sgl_pln_blk: $ => scalar_types($, 'br', 'blk'),
_b_sgl_pln_blk: $ => scalar_types($, 'b', 'blk'),
_r_sgl_pln_flw: $ => scalar_types($, 'r', 'flw'),
_br_sgl_pln_flw: $ => scalar_types($, 'br', 'flw'),

_r_mtl_pln_blk: $ => $._r_mtl_pln_str_blk,
_br_mtl_pln_blk: $ => $._br_mtl_pln_str_blk,
Expand Down Expand Up @@ -613,6 +613,7 @@ module.exports.grammar = global_alias(global_alias(module.exports.grammar, {
..._('boolean_scalar', '_r_sgl_pln_bol_blk', '_br_sgl_pln_bol_blk', '_b_sgl_pln_bol_blk', '_r_sgl_pln_bol_flw', '_br_sgl_pln_bol_flw'),
..._('integer_scalar', '_r_sgl_pln_int_blk', '_br_sgl_pln_int_blk', '_b_sgl_pln_int_blk', '_r_sgl_pln_int_flw', '_br_sgl_pln_int_flw'),
..._('float_scalar', '_r_sgl_pln_flt_blk', '_br_sgl_pln_flt_blk', '_b_sgl_pln_flt_blk', '_r_sgl_pln_flt_flw', '_br_sgl_pln_flt_flw'),
..._('timestamp_scalar', '_r_sgl_pln_tms_blk', '_br_sgl_pln_tms_blk', '_b_sgl_pln_tms_blk', '_r_sgl_pln_tms_flw', '_br_sgl_pln_tms_flw'),
..._('string_scalar', '_r_sgl_pln_str_blk', '_br_sgl_pln_str_blk', '_b_sgl_pln_str_blk', '_r_sgl_pln_str_flw', '_br_sgl_pln_str_flw',
'_r_mtl_pln_str_blk', '_br_mtl_pln_str_blk', '_r_mtl_pln_str_flw', '_br_mtl_pln_str_flw'),
}), {
Expand Down Expand Up @@ -714,3 +715,16 @@ function recursive_alias(rule, alias_map, checklist) {
throw new Error(`Unexpected rule type ${JSON.stringify(rule.type)}`);
}
}

/**
* @param {GrammarSymbols<string>} symbols
* @param {'r' | 'br' | 'b'} prefix
* @param {'blk' | 'flw'} suffix
*/
function scalar_types(symbols, prefix, suffix) {
const types = Array.from(
['nul', 'bol', 'int', 'flt', 'tms', 'str'],
t => symbols[`_${prefix}_sgl_pln_${t}_${suffix}`],
);
return choice(...types);
}
2 changes: 1 addition & 1 deletion schema/core/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions schema/core/src/grammar.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions schema/core/src/node-types.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions schema/core/src/tree_sitter/alloc.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions schema/core/src/tree_sitter/parser.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion schema/json/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions schema/json/src/grammar.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions schema/json/src/node-types.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions schema/json/src/tree_sitter/alloc.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions schema/json/src/tree_sitter/parser.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions schema/legacy/grammar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/** @see {@link YAML 1.1|https://yaml.org/type/} */

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

module.exports = grammar({
name: "core_schema",

extras: _ => [],

rules: {
scalar: $ => choice($.null, $.bool, $.int, $.float, $.timestamp),

null: _ => token(choice("~", "null", "Null", "NULL")),

bool: _ => token(choice(
"y", "Y", "yes", "Yes", "YES", "n", "N", "no", "No", "NO",
"true", "True", "TRUE", "false", "False", "FALSE",
"on", "On", "ON", "off", "Off", "OFF",
)),

int: _ => token(choice(
/[-+]?0b[0-1_]+/, // base 2
/[-+]?0[0-7_]+/, // base 8
/[-+]?(0|[1-9][0-9_]*)/, // base 10
/[-+]?0x[0-9a-fA-F_]+/, // base 16
/[-+]?[1-9][0-9_]*(:[0-5]?[0-9])+/, // base 60
)),

float: _ => token(choice(
/[-+]?([0-9][0-9_]*)?\.[0-9.]*([eE][-+][0-9]+)?/, // base 10
/[-+]?[0-9][0-9_]*(:[0-5]?[0-9])+\.[0-9_]*/, // base 60
seq(
optional(choice("-", "+")),
choice(".inf", ".Inf", ".INF")
), // infinity
choice(".nan", ".NaN", ".NAN"), // not a number
)),

timestamp: _ => token(choice(
/\d\d\d\d-\d\d-\d\d/, // ymd
seq(
/\d\d\d\d-\d\d?-\d\d?/, // year-month-day
choice("T", "t", /[ \t]+/),
/\d\d?:\d\d:\d\d/, // hour:minute:second
optional(/\.\d+/), // fraction
optional(choice(
/[ \t]*Z/,
/[-+]\d\d?(:\d\d)?/
)) // time zone
)
))
},
});
7 changes: 7 additions & 0 deletions schema/legacy/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cdb54d4

Please sign in to comment.