tree-sitter-bash/grammar.js

131 lines
2.4 KiB
JavaScript
Raw Normal View History

2017-07-14 19:28:54 +00:00
module.exports = grammar({
name: 'bash',
inline: $ => [$.control_operator],
2017-07-14 20:54:05 +00:00
externals: $ => [
$._simple_heredoc,
$._heredoc_beginning,
$._heredoc_middle,
$._heredoc_end
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: {
program: $ => repeat($.command),
command: $ => seq(
choice(
$.simple_command,
$.pipeline,
$.list
),
$.control_operator
),
simple_command: $ => seq(
2017-07-14 19:43:42 +00:00
repeat(choice(
$.environment_variable_assignment,
$.file_redirect
)),
2017-07-14 19:28:54 +00:00
rename($.leading_word, 'command_name'),
optional(seq(
/\s+/,
2017-07-14 20:00:41 +00:00
repeat(choice(
rename($.word, 'argument'),
$.expansion,
$.operator_expansion
))
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
),
pipeline: $ => prec.left(seq(
$.simple_command,
choice('|', '|&'),
$.simple_command
)),
list: $ => prec.left(seq(
choice(
$.simple_command,
$.list,
$.pipeline
),
choice('&&', ';'),
choice(
$.simple_command,
$.pipeline
)
)),
environment_variable_assignment: $ => seq(
rename($.leading_word, 'variable_name'),
'=',
rename($.word, 'argument')
),
2017-07-14 20:00:41 +00:00
expansion: $ => seq(
'$',
rename($.word, 'variable_name')
),
operator_expansion: $ => seq(
'${',
rename($.leading_word, 'variable_name'),
optional(seq(
choice(':', ':?', '='),
rename($.word, 'argument')
)),
'}'
),
2017-07-14 19:43:42 +00:00
file_redirect: $ => seq(
optional($.file_descriptor),
choice('<', '>', '<&', '>&'),
choice(
$.file_descriptor,
rename($.word, 'file_name')
)
),
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
)
),
2017-07-14 19:43:42 +00:00
file_descriptor: $ => token(prec(1, /\d+/)),
2017-07-14 21:39:28 +00:00
leading_word: $ => /[^\\\s#=|;:{}]+/,
2017-07-14 19:28:54 +00:00
2017-07-14 21:39:28 +00:00
word: $ => /[^#\\\s$<>{}&]+/,
2017-07-14 19:28:54 +00:00
control_operator: $ => choice(
'\n',
';;'
2017-07-14 21:34:49 +00:00
),
comment: $ => /#.*/,
2017-07-14 19:28:54 +00:00
}
});