Format grammar.js with prettier

This commit is contained in:
Camden Cheek 2021-05-11 20:00:07 -06:00
parent 6c1ef89aa8
commit f3a88aa686
No known key found for this signature in database
GPG key ID: 595BFFE3A04E48B9

View file

@ -1,364 +1,332 @@
module.exports = grammar({ module.exports = grammar({
name: 'dockerfile', name: "dockerfile",
extras: $ => [ extras: ($) => [$.comment, /\s+/, "\\\n"],
$.comment,
/\s+/,
'\\\n'
],
rules: { rules: {
source_file: $ => repeat(seq($._instruction, "\n")), source_file: ($) => repeat(seq($._instruction, "\n")),
_instruction: $ => choice( _instruction: ($) =>
$.from_instruction, choice(
$.run_instruction, $.from_instruction,
$.cmd_instruction, $.run_instruction,
$.label_instruction, $.cmd_instruction,
$.expose_instruction, $.label_instruction,
$.env_instruction, $.expose_instruction,
$.add_instruction, $.env_instruction,
$.copy_instruction, $.add_instruction,
$.entrypoint_instruction, $.copy_instruction,
$.volume_instruction, $.entrypoint_instruction,
$.user_instruction, $.volume_instruction,
$.workdir_instruction, $.user_instruction,
$.arg_instruction, $.workdir_instruction,
$.onbuild_instruction, $.arg_instruction,
$.stopsignal_instruction, $.onbuild_instruction,
$.healthcheck_instruction, $.stopsignal_instruction,
$.shell_instruction, $.healthcheck_instruction,
$.maintainer_instruction, $.shell_instruction,
$.cross_build_instruction, $.maintainer_instruction,
), $.cross_build_instruction
),
from_instruction: $ => seq( from_instruction: ($) =>
alias(/[fF][rR][oO][mM]/, "FROM"), seq(
$._non_newline_whitespace, alias(/[fF][rR][oO][mM]/, "FROM"),
optional($.param), $._non_newline_whitespace,
$.image_spec, optional($.param),
optional(seq( $.image_spec,
alias(/[aA][sS]/, "AS"), optional(seq(alias(/[aA][sS]/, "AS"), field("as", $.image_alias)))
field("as", $.image_alias), ),
)),
),
run_instruction: $ => seq( run_instruction: ($) =>
alias(/[rR][uU][nN]/, "RUN"), seq(
$._non_newline_whitespace, alias(/[rR][uU][nN]/, "RUN"),
choice( $._non_newline_whitespace,
$.string_array, choice($.string_array, $.shell_command)
$.shell_command, ),
),
),
cmd_instruction: $ => seq( cmd_instruction: ($) =>
alias(/[cC][mM][dD]/, "CMD"), seq(
$._non_newline_whitespace, alias(/[cC][mM][dD]/, "CMD"),
choice( $._non_newline_whitespace,
$.string_array, choice($.string_array, $.shell_command)
$.shell_command, ),
),
),
label_instruction: $ => seq( label_instruction: ($) =>
alias(/[lL][aA][bB][eE][lL]/, "LABEL"), seq(
$._non_newline_whitespace, alias(/[lL][aA][bB][eE][lL]/, "LABEL"),
repeat1($.label_pair), $._non_newline_whitespace,
), repeat1($.label_pair)
),
expose_instruction: $ => seq( expose_instruction: ($) =>
alias(/[eE][xX][pP][oO][sS][eE]/, "EXPOSE"), seq(
repeat1(choice($.expose_port, $.expansion)), alias(/[eE][xX][pP][oO][sS][eE]/, "EXPOSE"),
), repeat1(choice($.expose_port, $.expansion))
),
env_instruction: $ => seq( env_instruction: ($) =>
alias(/[eE][nN][vV]/, "ENV"), seq(
$._non_newline_whitespace, alias(/[eE][nN][vV]/, "ENV"),
choice( $._non_newline_whitespace,
repeat1($.env_pair), choice(repeat1($.env_pair), alias($._spaced_env_pair, $.env_pair))
alias($._spaced_env_pair, $.env_pair), ),
)
),
add_instruction: $ => seq( add_instruction: ($) =>
alias(/[aA][dD][dD]/, "ADD"), seq(
$._non_newline_whitespace, alias(/[aA][dD][dD]/, "ADD"),
optional($.param), $._non_newline_whitespace,
$.path, optional($.param),
$._non_newline_whitespace, $.path,
$.path, $._non_newline_whitespace,
), $.path
),
copy_instruction: $ => seq( copy_instruction: ($) =>
alias(/[cC][oO][pP][yY]/, "COPY"), seq(
$._non_newline_whitespace, alias(/[cC][oO][pP][yY]/, "COPY"),
optional($.param), $._non_newline_whitespace,
$.path, optional($.param),
$._non_newline_whitespace, $.path,
$.path, $._non_newline_whitespace,
), $.path
),
entrypoint_instruction: $ => seq( entrypoint_instruction: ($) =>
alias(/[eE][nN][tT][rR][yY][pP][oO][iI][nN][tT]/, "ENTRYPOINT"), seq(
$._non_newline_whitespace, alias(/[eE][nN][tT][rR][yY][pP][oO][iI][nN][tT]/, "ENTRYPOINT"),
choice( $._non_newline_whitespace,
$.string_array, choice($.string_array, $.shell_command)
$.shell_command, ),
),
),
volume_instruction: $ => seq( volume_instruction: ($) =>
alias(/[vV][oO][lL][uU][mM][eE]/, "VOLUME"), seq(
$._non_newline_whitespace, alias(/[vV][oO][lL][uU][mM][eE]/, "VOLUME"),
choice( $._non_newline_whitespace,
$.string_array, choice(
seq( $.string_array,
$.path, seq($.path, repeat(seq($._non_newline_whitespace, $.path)))
repeat(seq($._non_newline_whitespace, $.path)), )
), ),
),
),
user_instruction: $ => seq( user_instruction: ($) =>
alias(/[uU][sS][eE][rR]/, "USER"), seq(
$._non_newline_whitespace, alias(/[uU][sS][eE][rR]/, "USER"),
field("user", alias($._user_name_group, $.unquoted_string)), $._non_newline_whitespace,
optional(seq( field("user", alias($._user_name_group, $.unquoted_string)),
token.immediate(":"), optional(
field("group", alias($._user_name_group, $.unquoted_string)), seq(
)), token.immediate(":"),
), field("group", alias($._user_name_group, $.unquoted_string))
)
)
),
_user_name_group: $ => repeat1(choice( _user_name_group: ($) =>
token.immediate(/[a-z][-a-z0-9_]*/), repeat1(choice(token.immediate(/[a-z][-a-z0-9_]*/), $.expansion)),
$.expansion,
)),
workdir_instruction: $ => seq( workdir_instruction: ($) =>
alias(/[wW][oO][rR][kK][dD][iI][rR]/, "WORKDIR"), seq(
$._non_newline_whitespace, alias(/[wW][oO][rR][kK][dD][iI][rR]/, "WORKDIR"),
$.path, $._non_newline_whitespace,
), $.path
),
arg_instruction: $ => seq( arg_instruction: ($) =>
alias(/[aA][rR][gG]/, "ARG"), seq(
$._non_newline_whitespace, alias(/[aA][rR][gG]/, "ARG"),
field("name", alias(/[a-zA-Z0-9_]+/, $.unquoted_string)), $._non_newline_whitespace,
optional(seq( field("name", alias(/[a-zA-Z0-9_]+/, $.unquoted_string)),
token.immediate("="), optional(
field("default", choice( seq(
$.double_quoted_string, token.immediate("="),
$.unquoted_string, field("default", choice($.double_quoted_string, $.unquoted_string))
)), )
)), )
), ),
onbuild_instruction: $ => seq( onbuild_instruction: ($) =>
alias(/[oO][nN][bB][uU][iI][lL][dD]/, "ONBUILD"), seq(
$._non_newline_whitespace, alias(/[oO][nN][bB][uU][iI][lL][dD]/, "ONBUILD"),
$._instruction, $._non_newline_whitespace,
), $._instruction
),
stopsignal_instruction: $ => seq( stopsignal_instruction: ($) =>
alias(/[sS][tT][oO][pP][sS][iI][gG][nN][aA][lL]/, "STOPSIGNAL"), seq(
$._non_newline_whitespace, alias(/[sS][tT][oO][pP][sS][iI][gG][nN][aA][lL]/, "STOPSIGNAL"),
$._stopsignal_value, $._non_newline_whitespace,
), $._stopsignal_value
),
_stopsignal_value: $ => repeat1(choice( _stopsignal_value: ($) => repeat1(choice(/[A-Z0-9]+/, $.expansion)),
/[A-Z0-9]+/,
$.expansion,
)),
healthcheck_instruction: $ => seq( healthcheck_instruction: ($) =>
alias(/[hH][eE][aA][lL][tT][hH][cC][hH][eE][cC][kK]/, "HEALTHCHECK"), seq(
$._non_newline_whitespace, alias(/[hH][eE][aA][lL][tT][hH][cC][hH][eE][cC][kK]/, "HEALTHCHECK"),
choice( $._non_newline_whitespace,
"NONE", choice("NONE", seq(repeat($.param), $.cmd_instruction))
seq( ),
repeat($.param),
$.cmd_instruction,
),
),
),
shell_instruction: $ => seq( shell_instruction: ($) =>
alias(/[sS][hH][eE][lL][lL]/, "SHELL"), seq(
$._non_newline_whitespace, alias(/[sS][hH][eE][lL][lL]/, "SHELL"),
$.string_array, $._non_newline_whitespace,
), $.string_array
),
maintainer_instruction: $ => seq( maintainer_instruction: ($) =>
alias(/[mM][aA][iI][nN][tT][aA][iI][nN][eE][rR]/, "MAINTAINER"), seq(
/.*/, alias(/[mM][aA][iI][nN][tT][aA][iI][nN][eE][rR]/, "MAINTAINER"),
), /.*/
),
cross_build_instruction: $ => seq( cross_build_instruction: ($) =>
alias(/[cC][rR][oO][sS][sS]_[bB][uU][iI][lL][dD][a-zA-Z_]*/, "CROSS_BUILD"), seq(
/.*/, alias(
), /[cC][rR][oO][sS][sS]_[bB][uU][iI][lL][dD][a-zA-Z_]*/,
"CROSS_BUILD"
path: $ => seq( ),
choice( /.*/
/[^-\s\$]/, // cannot start with a '-' to avoid conflicts with params ),
$.expansion,
),
repeat(choice(
/[^\s\$]+/,
$.expansion,
)),
),
expansion: $ => seq( path: ($) =>
'$', seq(
choice( choice(
$.variable, /[^-\s\$]/, // cannot start with a '-' to avoid conflicts with params
seq('{', alias(/[^\}]+/, $.variable), '}'), $.expansion
) ),
), repeat(choice(/[^\s\$]+/, $.expansion))
),
variable: $ => /[a-zA-Z][a-zA-Z0-9_]*/, expansion: ($) =>
seq("$", choice($.variable, seq("{", alias(/[^\}]+/, $.variable), "}"))),
env_pair: $ => seq( variable: ($) => /[a-zA-Z][a-zA-Z0-9_]*/,
field("name", $._env_key),
token.immediate("="),
field("value", choice(
$.double_quoted_string,
$.unquoted_string,
)),
),
_spaced_env_pair: $ => seq( env_pair: ($) =>
field("name", $._env_key), seq(
token.immediate(/\s+/), field("name", $._env_key),
field("value", choice( token.immediate("="),
$.double_quoted_string, field("value", choice($.double_quoted_string, $.unquoted_string))
$.unquoted_string, ),
)),
),
_env_key: $ => alias(/[a-zA-Z][a-zA-Z0-9_]*[a-zA-Z0-9]/, $.unquoted_string), _spaced_env_pair: ($) =>
seq(
field("name", $._env_key),
token.immediate(/\s+/),
field("value", choice($.double_quoted_string, $.unquoted_string))
),
expose_port: $ => seq( _env_key: ($) =>
/\d+/, alias(/[a-zA-Z][a-zA-Z0-9_]*[a-zA-Z0-9]/, $.unquoted_string),
optional(choice(
"/tcp",
"/udp",
)),
),
label_pair: $ => seq( expose_port: ($) => seq(/\d+/, optional(choice("/tcp", "/udp"))),
field("key", alias(/[-a-zA-Z0-9\._]+/, $.unquoted_string)),
token.immediate("="),
field("value", choice(
$.double_quoted_string,
$.unquoted_string,
)),
),
image_spec: $ => seq( label_pair: ($) =>
field("name", $.image_name), seq(
seq( field("key", alias(/[-a-zA-Z0-9\._]+/, $.unquoted_string)),
field("tag", optional($.image_tag)), token.immediate("="),
field("digest", optional($.image_digest)), field("value", choice($.double_quoted_string, $.unquoted_string))
), ),
),
image_name: $ => repeat1(choice( image_spec: ($) =>
token.immediate(/[^@:\s\$]+/), seq(
$.expansion, field("name", $.image_name),
)), seq(
field("tag", optional($.image_tag)),
field("digest", optional($.image_digest))
)
),
image_tag: $ => seq( image_name: ($) =>
token.immediate(":"), repeat1(choice(token.immediate(/[^@:\s\$]+/), $.expansion)),
repeat1(choice(
token.immediate(/[^@\s\$]+/),
$.expansion,
))
),
image_digest: $ => seq( image_tag: ($) =>
token.immediate("@"), seq(
repeat1(choice( token.immediate(":"),
token.immediate(/[a-zA-Z0-9:]+/), repeat1(choice(token.immediate(/[^@\s\$]+/), $.expansion))
$.expansion, ),
)),
),
param: $ => seq( image_digest: ($) =>
"--", seq(
field("name", token.immediate(/[a-z][-a-z]*/)), token.immediate("@"),
token.immediate("="), repeat1(choice(token.immediate(/[a-zA-Z0-9:]+/), $.expansion))
field("value", token.immediate(/[^\s]+/)), ),
),
image_alias: $ => repeat1(choice( param: ($) =>
/[-a-zA-Z0-9_]+/, seq(
$.expansion, "--",
)), field("name", token.immediate(/[a-z][-a-z]*/)),
token.immediate("="),
field("value", token.immediate(/[^\s]+/))
),
string_array: $ => seq( image_alias: ($) => repeat1(choice(/[-a-zA-Z0-9_]+/, $.expansion)),
"[",
optional(seq(
$.double_quoted_string,
repeat(seq(",", $.double_quoted_string)),
)),
"]",
),
shell_command: $ => seq( string_array: ($) =>
$.shell_fragment, seq(
repeat(seq( "[",
$.line_continuation, optional(
repeat($._comment_line), seq($.double_quoted_string, repeat(seq(",", $.double_quoted_string)))
$.shell_fragment, ),
)), "]"
), ),
shell_fragment: $ => repeat1(choice( shell_command: ($) =>
/[^\\\[\n#\s][^\\\n]*/, seq(
/\\[^\n]/, $.shell_fragment,
)), repeat(
seq($.line_continuation, repeat($._comment_line), $.shell_fragment)
)
),
line_continuation: $ => '\\\n', shell_fragment: ($) => repeat1(choice(/[^\\\[\n#\s][^\\\n]*/, /\\[^\n]/)),
_comment_line: $ => seq( line_continuation: ($) => "\\\n",
alias($._anon_comment, $.comment), '\n'
),
_anon_comment: $ => seq('#', /.*/), _comment_line: ($) => seq(alias($._anon_comment, $.comment), "\n"),
double_quoted_string: $ => seq( _anon_comment: ($) => seq("#", /.*/),
'"',
repeat(choice(
token.immediate(prec(1, /[^"\n\\\$]+/)),
$.escape_sequence,
$.expansion,
)),
'"'
),
unquoted_string: $ => repeat1(choice( double_quoted_string: ($) =>
token.immediate(/[^\s\n\"\\\$]+/), seq(
token.immediate("\\ "), '"',
$.expansion, repeat(
)), choice(
token.immediate(prec(1, /[^"\n\\\$]+/)),
$.escape_sequence,
$.expansion
)
),
'"'
),
escape_sequence: $ => token.immediate(seq( unquoted_string: ($) =>
'\\', repeat1(
choice( choice(
/[^xuU]/, token.immediate(/[^\s\n\"\\\$]+/),
/\d{2,3}/, token.immediate("\\ "),
/x[0-9a-fA-F]{2,}/, $.expansion
/u[0-9a-fA-F]{4}/, )
/U[0-9a-fA-F]{8}/ ),
)
)),
_non_newline_whitespace: $ => /[\t ]+/, escape_sequence: ($) =>
token.immediate(
seq(
"\\",
choice(
/[^xuU]/,
/\d{2,3}/,
/x[0-9a-fA-F]{2,}/,
/u[0-9a-fA-F]{4}/,
/U[0-9a-fA-F]{8}/
)
)
),
comment: $ => seq("#", /.*/), _non_newline_whitespace: ($) => /[\t ]+/,
}
comment: ($) => seq("#", /.*/),
},
}); });