tree-sitter-bash/grammar.js

250 lines
4.5 KiB
JavaScript
Raw Normal View History

2017-07-14 19:28:54 +00:00
module.exports = grammar({
name: 'bash',
inline: $ => [$.statement, $.terminator, $.value],
2017-07-14 19:28:54 +00:00
2017-07-14 20:54:05 +00:00
externals: $ => [
$._simple_heredoc,
$._heredoc_beginning,
$._heredoc_middle,
$._heredoc_end,
2017-07-15 00:41:14 +00:00
$.file_descriptor,
$._empty_value
2017-07-14 20:54:05 +00:00
],
2017-07-14 21:34:49 +00:00
extras: $ => [
$.comment,
2017-07-14 21:39:28 +00:00
token(choice(/\s/, '\\\n')),
2017-07-14 21:34:49 +00:00
],
2017-07-14 19:28:54 +00:00
rules: {
2017-07-14 23:11:35 +00:00
program: $ => repeat($._terminated_statement),
2017-07-14 19:28:54 +00:00
2017-07-14 23:11:35 +00:00
_terminated_statement: $ => seq(
$.statement,
2017-07-14 23:29:28 +00:00
$.terminator
2017-07-14 23:11:35 +00:00
),
statement: $ => choice(
$.environment_variable_assignment,
2017-07-14 23:11:35 +00:00
$.command,
$.bracket_command,
2017-07-15 00:51:06 +00:00
$.for_statement,
2017-07-14 23:11:35 +00:00
$.while_statement,
2017-07-14 23:18:46 +00:00
$.if_statement,
2017-07-14 23:29:28 +00:00
$.case_statement,
2017-07-14 23:11:35 +00:00
$.pipeline,
2017-07-15 00:32:55 +00:00
$.list,
2017-07-15 00:35:51 +00:00
$.subshell,
$.function_definition
2017-07-14 23:11:35 +00:00
),
2017-07-15 00:51:06 +00:00
for_statement: $ => seq(
'for',
rename($.word, 'argument'),
'in',
$._terminated_statement,
$.do_group
),
2017-07-14 23:11:35 +00:00
while_statement: $ => seq(
'while',
$._terminated_statement,
$.do_group
),
2017-07-14 23:29:28 +00:00
do_group: $ => seq(
'do',
repeat($._terminated_statement),
'done'
),
2017-07-14 23:18:46 +00:00
if_statement: $ => seq(
'if',
$._terminated_statement,
'then',
repeat($._terminated_statement),
repeat($.elif_clause),
optional($.else_clause),
'fi'
),
elif_clause: $ => seq(
'elif',
$._terminated_statement,
'then',
repeat($._terminated_statement)
),
else_clause: $ => seq(
'else',
repeat($._terminated_statement)
),
2017-07-14 23:29:28 +00:00
case_statement: $ => seq(
'case',
$.value,
2017-07-14 23:29:28 +00:00
optional($.terminator),
'in',
$.terminator,
repeat($.case_item),
'esac'
),
case_item: $ => seq(
$.value,
2017-07-14 23:29:28 +00:00
')',
2017-07-14 23:11:35 +00:00
repeat($._terminated_statement),
2017-07-15 00:32:55 +00:00
';;'
2017-07-14 19:28:54 +00:00
),
2017-07-15 00:35:51 +00:00
function_definition: $ => seq(
optional('function'),
rename($.leading_word, 'command_name'),
'(',
')',
$.compound_command
),
compound_command: $ => seq(
'{',
repeat($._terminated_statement),
'}'
),
bracket_command: $ => choice(
seq('[', repeat1($.value), ']'),
seq('[[', repeat1($.value), ']]')
),
2017-07-14 23:11:35 +00:00
command: $ => seq(
2017-07-14 19:43:42 +00:00
repeat(choice(
$.environment_variable_assignment,
$.file_redirect
)),
2017-07-15 00:32:55 +00:00
choice(
rename(choice($.leading_word), 'command_name'),
':',
$.quoted_argument,
2017-07-15 00:51:06 +00:00
$.single_quoted_argument,
$.command_substitution
2017-07-15 00:32:55 +00:00
),
2017-07-14 19:28:54 +00:00
optional(seq(
/\s+/,
repeat($.value)
2017-07-14 19:43:42 +00:00
)),
2017-07-14 20:54:05 +00:00
repeat(choice(
$.file_redirect,
$.heredoc_redirect
))
2017-07-14 19:28:54 +00:00
),
2017-07-14 23:11:35 +00:00
pipeline: $ => prec.left(1, seq(
$.statement,
2017-07-14 19:28:54 +00:00
choice('|', '|&'),
2017-07-14 23:11:35 +00:00
$.statement
2017-07-14 19:28:54 +00:00
)),
list: $ => prec.left(seq(
2017-07-14 23:11:35 +00:00
$.statement,
choice('&&', '||'),
$.statement
2017-07-14 19:28:54 +00:00
)),
2017-07-15 00:32:55 +00:00
subshell: $ => seq(
'(',
repeat($._terminated_statement),
')'
),
2017-07-14 19:28:54 +00:00
environment_variable_assignment: $ => seq(
rename($.leading_word, 'variable_name'),
'=',
2017-07-15 00:41:14 +00:00
choice(
$.value,
$._empty_value
)
),
value: $ => choice(
rename($.word, 'argument'),
$.command_substitution,
$.quoted_argument,
$.single_quoted_argument,
$.expansion,
$.operator_expansion
2017-07-14 19:28:54 +00:00
),
quoted_argument: $ => seq(
'"',
repeat(choice(
$._quoted_chars,
$.expansion,
$.operator_expansion,
$.command_substitution
)),
'"'
),
_quoted_chars: $ => /[^"$]+/,
single_quoted_argument: $ => /'[^']*'/,
2017-07-14 20:00:41 +00:00
expansion: $ => seq(
'$',
2017-07-15 00:32:55 +00:00
choice(
rename($.word, 'variable_name'),
'$'
)
2017-07-14 20:00:41 +00:00
),
operator_expansion: $ => seq(
'${',
rename($.leading_word, 'variable_name'),
optional(seq(
2017-07-15 00:32:55 +00:00
choice(':', ':?', '=', ':-'),
$.value
2017-07-14 20:00:41 +00:00
)),
'}'
),
command_substitution: $ => seq(
'$(',
$.command,
')'
),
2017-07-14 19:43:42 +00:00
file_redirect: $ => seq(
optional($.file_descriptor),
choice('<', '>', '>>', '&>', '&>>', '<&', '>&'),
$.value
2017-07-14 19:43:42 +00:00
),
2017-07-14 20:54:05 +00:00
heredoc_redirect: $ => seq(
choice('<<', '<<-'),
$.heredoc
),
heredoc: $ => choice(
$._simple_heredoc,
seq(
$._heredoc_beginning,
repeat(choice(
$.expansion,
$.operator_expansion,
$._heredoc_middle
)),
$._heredoc_end
)
),
leading_word: $ => /[^"\\\s#=|;:{}()]+/,
2017-07-14 19:28:54 +00:00
word: $ => /[^"#\\\s$<>{}&;()]+/,
2017-07-14 21:34:49 +00:00
comment: $ => /#.*/,
2017-07-14 23:29:28 +00:00
2017-07-15 00:32:55 +00:00
terminator: $ => choice(';', ';;', '\n', '&'),
2017-07-14 19:28:54 +00:00
}
});