module.exports = grammar({ name: 'nixshell', rules: { source_file: $ => seq($.first_line, repeat($.next_line)), first_line: $ => seq($._shebang_open, $._opt_ws, optional(seq($._env, $._ws)), $._nix_shell), next_line: $ => seq($._newline, choice( $.annotation_line, repeat(/./), )), annotation_line: $ => seq($._shebang_open, $._opt_ws, $._nix_shell, $.nix_shell_args), // TODO: actually parse nix-shell arguments nix_shell_args: $ => ' -i bash', _shebang_open: $ => '#!', // TODO: allow `usr/bin/env`, `env` _env: $ => '/usr/bin/env', _nix_shell: $ => 'nix-shell', _ws: $ => / +/, _opt_ws: $ => / */, _newline: $ => '\n', } });