Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add literal aliases #11

Merged
merged 1 commit into from
Sep 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions corpus/spec.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1105,8 +1105,9 @@ block: ># comment
--------------------------------------------------------------------------------

(ERROR
(flow_node
(plain_scalar)))
(block_mapping_pair
key: (flow_node
(plain_scalar))))

================================================================================
Comment without whitespace after doublequoted scalar [INVALID]
Expand Down Expand Up @@ -2129,8 +2130,9 @@ a:
--------------------------------------------------------------------------------

(ERROR
(flow_node
(plain_scalar)))
(block_mapping_pair
key: (flow_node
(plain_scalar))))

================================================================================
Invalid tag [INVALID]
Expand Down Expand Up @@ -2310,9 +2312,11 @@ Mapping starting at --- line [INVALID]

--------------------------------------------------------------------------------

(ERROR
(flow_node
(plain_scalar)))
(stream
(document
(flow_node
(plain_scalar)))
(MISSING _eof))

================================================================================
Mapping with anchor on document start line [INVALID]
Expand Down Expand Up @@ -3581,8 +3585,9 @@ key: - a
--------------------------------------------------------------------------------

(ERROR
(flow_node
(plain_scalar)))
(block_mapping_pair
key: (flow_node
(plain_scalar))))

================================================================================
Sequence With Same Indentation as Parent Mapping
Expand Down Expand Up @@ -8576,10 +8581,8 @@ c"
key: (flow_node
(plain_scalar)))
(ERROR
(flow_node
(plain_scalar))
(flow_node
(plain_scalar)))))))
(plain_scalar)
(plain_scalar))))))

================================================================================
Wrong indented sequence item [INVALID]
Expand Down
34 changes: 29 additions & 5 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ module.exports = grammar({
$._r_blk_key_bgn, $._br_blk_key_bgn, $._b_blk_key_bgn, // ?
$._r_blk_val_bgn, $._br_blk_val_bgn, $._b_blk_val_bgn, // :
$._r_blk_imp_bgn, // : (implicit)
$._r_blk_str_bgn, $._br_blk_str_bgn, // | >
$._r_blk_lit_bgn, $._br_blk_lit_bgn, // |
$._r_blk_fld_bgn, $._br_blk_fld_bgn, // >
$._br_blk_str_ctn, // block scalar content
$._r_flw_seq_bgn, $._br_flw_seq_bgn, $._b_flw_seq_bgn, // [
$._r_flw_seq_end, $._br_flw_seq_end, // ]
Expand Down Expand Up @@ -229,8 +230,8 @@ module.exports = grammar({
_r_blk_str_val: $ => choice($._r_blk_str, seq($._r_prp, choice($._r_blk_str, $._br_blk_str))),
_br_blk_str_val: $ => choice($._br_blk_str, seq($._br_prp, choice($._r_blk_str, $._br_blk_str))),

_r_blk_str: $ => seq($._r_blk_str_bgn, repeat($._br_blk_str_ctn), $._bl),
_br_blk_str: $ => seq($._br_blk_str_bgn, repeat($._br_blk_str_ctn), $._bl),
_r_blk_str: $ => seq(choice($._r_blk_lit_bgn, $._r_blk_fld_bgn), repeat($._br_blk_str_ctn), $._bl),
_br_blk_str: $ => seq(choice($._br_blk_lit_bgn, $._br_blk_fld_bgn), repeat($._br_blk_str_ctn), $._bl),

// flow value in block

Expand Down Expand Up @@ -435,7 +436,7 @@ module.exports = grammar({
},
});

module.exports = global_alias(module.exports, {
module.exports = global_alias(global_alias(module.exports, {
..._("yaml_directive", "_s_dir_yml"),
..._("yaml_version", "_r_dir_yml_ver"),
..._("tag_directive", "_s_dir_tag"),
Expand Down Expand Up @@ -479,6 +480,27 @@ module.exports = global_alias(module.exports, {
..._("escape_sequence", "_r_dqt_esc_nwl", "_br_dqt_esc_nwl",
"_r_dqt_esc_seq", "_br_dqt_esc_seq",
"_r_sqt_esc_sqt", "_br_sqt_esc_sqt"),
}), {
..._("---", "_s_drs_end"),
..._("...", "_s_doc_end"),
..._("-", "_r_blk_seq_bgn", "_br_blk_seq_bgn", "_b_blk_seq_bgn"),
..._("?", "_r_blk_key_bgn", "_br_blk_key_bgn", "_b_blk_key_bgn"),
..._(":", "_r_blk_val_bgn", "_br_blk_val_bgn", "_b_blk_val_bgn"),
..._(":", "_r_blk_imp_bgn"),
..._("|", "_r_blk_lit_bgn", "_br_blk_lit_bgn"),
..._(">", "_r_blk_fld_bgn", "_br_blk_fld_bgn"),
..._("[", "_r_flw_seq_bgn", "_br_flw_seq_bgn", "_b_flw_seq_bgn"),
..._("]", "_r_flw_seq_end", "_br_flw_seq_end"),
..._("{", "_r_flw_map_bgn", "_br_flw_map_bgn", "_b_flw_map_bgn"),
..._("}", "_r_flw_map_end", "_br_flw_map_end"),
..._(",", "_r_flw_sep_bgn", "_br_flw_sep_bgn"),
..._("?", "_r_flw_key_bgn", "_br_flw_key_bgn"),
..._(":", "_r_flw_jsv_bgn", "_br_flw_jsv_bgn"),
..._(":", "_r_flw_njv_bgn", "_br_flw_njv_bgn"),
..._("\"", "_r_dqt_str_bgn", "_br_dqt_str_bgn", "_b_dqt_str_bgn"),
..._("\"", "_r_dqt_str_end", "_br_dqt_str_end"),
..._("'", "_r_sqt_str_bgn", "_br_sqt_str_bgn", "_b_sqt_str_bgn"),
..._("'", "_r_sqt_str_end", "_br_sqt_str_end"),
});

function _(alias_value, ...rule_names) {
Expand Down Expand Up @@ -529,9 +551,11 @@ function recursive_alias(rule, alias_map, checklist) {
case "SYMBOL":
if (rule.name in alias_map) {
checklist[rule.name]++;
return { type: "ALIAS", content: rule, named: true, value: alias_map[rule.name].name };
const alias = alias_map[rule.name].name;
return { type: "ALIAS", content: rule, named: /[a-z]/i.test(alias), value: alias };
}
case "BLANK":
case "ALIAS":
return rule;
default:
throw new Error(`Unexpected rule type ${JSON.stringify(rule.type)}`);
Expand Down
Loading