handle embedded script payload

This commit is contained in:
2023-07-04 08:57:44 +00:00
parent 4db0ba409a
commit 28d79d458c
3 changed files with 41 additions and 6 deletions

25
corpus/payload.txt Normal file
View File

@@ -0,0 +1,25 @@
==================
nix-shell with a bash payload
==================
#!/usr/bin/env nix-shell
#!nix-shell -i bash
echo "hello, world!"
---
(source_file
(first_line
(shebang_open)
(opt_ws)
(env)
(ws)
(nix_shell))
(next_line
(newline)
(annotation_line
(shebang_open)
(opt_ws)
(nix_shell)
(nix_shell_args)))
(next_line
(newline)))

View File

@@ -13,7 +13,7 @@ Minimal nix-shell invocation
(env)
(ws)
(nix_shell))
(non_first_line
(next_line
(newline)
(annotation_line
(shebang_open)

View File

@@ -2,18 +2,28 @@ module.exports = grammar({
name: 'nixshell',
rules: {
source_file: $ => seq($.first_line, repeat($.non_first_line)),
source_file: $ => seq($.first_line, repeat($.next_line)),
first_line: $ => seq($.shebang_open, $.opt_ws, optional(seq($.env, $.ws)), $.nix_shell),
non_first_line: $ => seq($.newline, $.annotation_line),
next_line: $ => seq($.newline, choice(
$.annotation_line,
// TODO: capture this
repeat(/./),
)),
annotation_line: $ => seq($.shebang_open, $.opt_ws, $.nix_shell, $.nix_shell_args),
embedded_script_line: $ => /.*/,
// payload_next_line: $ => /\n.*/,
// payload_line: $ => /.*/,
// payload_line: $ => repeat(/./),
shebang_open: $ => /#!/,
// nix_shell_directive_open: $ => '#!nix-shell',
// TODO: actually parse nix-shell arguments
nix_shell_args: $ => ' -i bash',
// TODO: hide these (prefix with underscore)
shebang_open: $ => '#!',
// TODO: allow `usr/bin/env`, `env`
env: $ => '/usr/bin/env',
nix_shell: $ => 'nix-shell',
nix_shell_args: $ => ' -i bash',
ws: $ => / +/,
opt_ws: $ => / */,
newline: $ => '\n',