Skip to content

Commit

Permalink
add abstraction where it adds usability / helps to understand code
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm committed Mar 31, 2024
1 parent 480a3a4 commit df570b9
Showing 1 changed file with 24 additions and 59 deletions.
83 changes: 24 additions & 59 deletions tree_sitter_v/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
/* eslint-disable camelcase */
/* eslint-disable-next-line spaced-comment */
/// <reference types="tree-sitter-cli/dsl" />
// @ts-check

const PREC = {
attributes: 10,
Expand Down Expand Up @@ -878,70 +877,21 @@ module.exports = grammar({
choice($.interpreted_string_literal, $.c_string_literal, $.raw_string_literal),

interpreted_string_literal: ($) =>
prec(
1,
choice(
seq(
"'",
repeat(
choice(
token.immediate(prec.right(1, /[^'\\$]+/)),
$.escape_sequence,
$.string_interpolation,
),
),
"'",
),
seq(
'"',
repeat(
choice(
token.immediate(prec.right(1, /[^"\\$]+/)),
$.escape_sequence,
$.string_interpolation,
),
),
'"',
),
),
choice(
seq("'", repeat(stringBody(/[^'\\$]+/, $)), "'"),
seq('"', repeat(stringBody(/[^"\\$]+/, $)), '"'),
),

c_string_literal: ($) =>
prec(
1,
choice(
seq(
"c'",
repeat(
choice(
token.immediate(prec.right(1, /[^'\\$]+/)),
$.escape_sequence,
$.string_interpolation,
),
),
"'",
),
seq(
'c"',
repeat(
choice(
token.immediate(prec.right(1, /[^"\\$]+/)),
$.escape_sequence,
$.string_interpolation,
),
),
'"',
),
),
choice(
seq("c'", repeat(stringBody(/[^'\\$]+/, $)), "'"),
seq('c"', repeat(stringBody(/[^"\\$]+/, $)), '"'),
),

raw_string_literal: (_) =>
prec(
1,
choice(
seq("r'", repeat(token.immediate(prec.right(1, /[^']+/))), "'"),
seq('r"', repeat(token.immediate(prec.right(1, /[^"]+/))), '"'),
),
choice(
seq("r'", repeat(token.immediate(prec.right(1, /[^']+/))), "'"),
seq('r"', repeat(token.immediate(prec.right(1, /[^"]+/))), '"'),
),

string_interpolation: ($) =>
Expand Down Expand Up @@ -1288,6 +1238,21 @@ function sep(rule) {
return seq(rule, repeat(seq(',', rule)));
}

/**
*
* @param {RegExp} re
* @param {$} $
*
* @return {SeqRule}
*
*/
function stringBody(re, $) {
return choice(
token.immediate(prec.right(1, re)),
choice($.escape_sequence, $.string_interpolation),
);
}

/**
* @param {...string} args - One or more regular expression patterns.
*
Expand Down

0 comments on commit df570b9

Please sign in to comment.