Skip to content

Commit

Permalink
feat: make rules' names clearer and add some new fields
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotfontaine committed Sep 26, 2024
1 parent 2411bfa commit 83aaa6e
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 74 deletions.
106 changes: 51 additions & 55 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

const PREC = {
unquoted_string: 1,
number: 2,
integer: 2,
datetime: 2,
boolean: 4,
comment: 5,
Expand All @@ -23,19 +23,12 @@ module.exports = grammar({
$.comment,
],

// TODO: rename grammar rules:
// - root_section -> top_section
// - subsection_1 -> sub_section_1
// - subsection_2 -> sub_section_2
// - multiline_string -> multilines_string
// - number -> integer

rules: {
// ----------------------------- Start Symbol -----------------------------
configuration_file: ($) =>
workflow_configuration: ($) =>
seq(
optional($.jinja2_shebang),
repeat(choice($.root_section, $.include_statement)),
repeat(choice($.top_section, $.include_statement)),
),

// --------------------------- Terminal symbols ---------------------------
Expand All @@ -50,7 +43,9 @@ module.exports = grammar({

jinja2_comment: (_) => token(prec(PREC.jinja2, seq("{#", /[^#]*/, "#}"))),

cyclepoints: (_) => token(repeat1(choice(/[A-Z0-9/^$+\-,()!]+/, "min"))),
include_directive: (_) => "%include",

recurrence: (_) => token(repeat1(choice(/[A-Z0-9/^$+\-,()!]+/, "min"))),

graph_logical: (_) => token(choice("&", "|")),

Expand All @@ -65,10 +60,12 @@ module.exports = grammar({

boolean: (_) => token(choice("True", "False")),

number: (_) => prec.left(PREC.number, token(/\d+/)),
integer: (_) => prec.left(PREC.integer, token(/\d+/)),

_line_return: (_) => /\r?\n/,

assignment_operator: (_) => "=",

datetime: (_) =>
prec.right(
PREC.datetime,
Expand Down Expand Up @@ -143,48 +140,51 @@ module.exports = grammar({
choice($.jinja2_expression, $.jinja2_statement, $.jinja2_comment),

include_statement: ($) =>
seq(token("%include"), field("path", $.quoted_string)),
seq(
field("directive", $.include_directive),
field("path", $.quoted_string),
),

root_section: ($) =>
top_section: ($) =>
seq(
"[",
field("brackets_open", "["),
optional(
choice(
field("name", $.nametag),
field("name", alias("task parameters", $.nametag)),
),
),
"]",
field("brackets_close", "]"),
$._line_return,
optional(repeat($.setting)),
optional(repeat(choice($.subsection_1, $.graph_section))),
optional(repeat(choice($.sub_section_1, $.graph_section))),
),

subsection_1: ($) =>
sub_section_1: ($) =>
seq(
"[[",
field("brackets_open", "[["),
optional(field("name", $.nametag)),
optional($.task_parameter),
"]]",
field("brackets_close", "]]"),
$._line_return,
optional(repeat($.setting)),
optional(repeat($.subsection_2)),
optional(repeat($.sub_section_2)),
),

graph_section: ($) =>
seq(
"[[",
field("brackets_open", "[["),
field("name", alias("graph", $.nametag)),
"]]",
field("brackets_close", "]]"),
$._line_return,
optional(repeat($.recurrence)),
optional(repeat($.graph_setting)),
),

subsection_2: ($) =>
sub_section_2: ($) =>
seq(
"[[[",
field("brackets_open", "[[["),
optional(field("name", $.nametag)),
"]]]",
field("brackets_close", "]]]"),
$._line_return,
optional(repeat($.setting)),
),
Expand All @@ -194,15 +194,15 @@ module.exports = grammar({
field("key", $.key),
optional(
seq(
"=",
field("operator", $.assignment_operator),
field(
"value",
optional(
choice(
$.quoted_string,
$.multiline_string,
$.datetime,
$.number,
$.integer,
$.boolean,
$.unquoted_string,
),
Expand All @@ -213,37 +213,33 @@ module.exports = grammar({
$._line_return,
),

recurrence: ($) =>
graph_setting: ($) =>
seq(
field("cyclepoints", $.cyclepoints),
"=",
field("key", $.recurrence),
field("operator", $.assignment_operator),
field(
"graph_string",
choice(
//"\n", // Empty value
$.multiline_graphstring,
$.unquoted_graphstring,
),
"value",
choice($.multiline_graph_string, $.unquoted_graph_string),
),
$._line_return,
),

unquoted_graphstring: ($) =>
unquoted_graph_string: ($) =>
prec.left(
seq(repeat1(choice($.graph_logical, $.graph_task, $.graph_arrow))),
),

multiline_graphstring: ($) =>
multiline_graph_string: ($) =>
choice(
seq(
alias('"""', $.multiline_string_open),
repeat($.unquoted_graphstring),
alias('"""', $.multiline_string_close),
field("quotes_open", '"""'),
repeat($.unquoted_graph_string),
field("quotes_close", '"""'),
),
seq(
alias("'''", $.multiline_string_open),
repeat($.unquoted_graphstring),
alias("'''", $.multiline_string_close),
field("quotes_open", "'''"),
repeat($.unquoted_graph_string),
field("quotes_close", "'''"),
),
),

Expand All @@ -262,13 +258,13 @@ module.exports = grammar({
seq(token.immediate(":"), $.nametag, optional(token.immediate("?"))),

intercycle_annotation: ($) =>
seq(token.immediate("["), $.cyclepoints, token.immediate("]")),
seq(token.immediate("["), $.recurrence, token.immediate("]")),

multiline_string: ($) =>
choice(
// Triple double quotes
seq(
alias(token('"""'), $.multiline_string_open),
field("quotes_open", '"""'),
alias(
repeat(
choice(
Expand All @@ -282,18 +278,18 @@ module.exports = grammar({
),
$.string_content,
),
alias('"""', $.multiline_string_close),
field("quotes_close", '"""'),
),
// Triple single quotes
seq(
alias("'''", $.multiline_string_open),
field("quotes_open", "'''"),
alias(
token(
repeat(choice(/[^'\\]/, /\\./, /'[^']/, /''[^']/, /\\\s*\n/)),
),
$.string_content,
),
alias("'''", $.multiline_string_close),
field("quotes_close", "'''"),
),
),

Expand All @@ -302,7 +298,7 @@ module.exports = grammar({
choice(
// Match double quotes
seq(
'"',
field("quotes_open", '"'),
alias(
repeat1(
choice(
Expand All @@ -313,16 +309,16 @@ module.exports = grammar({
),
$.string_content,
),
'"',
field("quotes_close", '"'),
),
// Match single quotes
seq(
"'",
field("quotes_open", "'"),
alias(
repeat(choice(/[^'\\\n]/, /\\./, /\\\n\s*/)),
$.string_content,
),
"'",
field("quotes_close", "'"),
),
),
),
Expand Down
39 changes: 20 additions & 19 deletions queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,48 @@
(jinja2_expression)
(jinja2_statement)
(jinja2_comment)
(jinja2_shebang)
] @text.literal

(include_statement) @embedded

(comment) @comment

(root_section
"[" @operator
name: (nametag) @title
"]" @operator)
(top_section
brackets_open: _ @operator
name: (_) @title
brackets_close: _ @operator)

(subsection_1
(sub_section_1
"[[" @operator
name: (nametag) @title
name: (_) @title
"]]" @operator)

(subsection_2
(sub_section_2
"[[[" @operator
name: (nametag) @title
name: (_) @title
"]]]" @operator)

(graph_section
"[[" @property
name: (nametag) @hint
"]]" @property)

(recurrence
cyclepoints: (cyclepoints) @number
"=" @operator)
(graph_setting
key: (_) @number
operator: (_) @operator)

(multiline_graphstring
(multiline_string_open) @operator
(multiline_string_close) @operator)
(multiline_graph_string
quotes_open: _ @operator
quotes_close: _ @operator)

[
(graph_logical)
(graph_arrow)
] @operator

(intercycle_annotation
(cyclepoints) @number)
(recurrence) @number)

(graph_task
(nametag) @emphasis)
Expand All @@ -56,7 +57,7 @@

(intercycle_annotation
"[" @punctuation
(cyclepoints) @number
(recurrence) @number
"]" @punctuation)

(task_output
Expand All @@ -65,14 +66,14 @@
"?"? @punctuation)

(setting
key: (key) @variable
"=" @operator
key: (_) @variable
operator: (_) @operator
value: [
(unquoted_string) @string
(quoted_string) @string
(multiline_string) @string
(boolean) @boolean
(number) @number
(integer) @number
]?)

(datetime) @number
Expand Down

0 comments on commit 83aaa6e

Please sign in to comment.