From 28d79d458cc4bb1cf9558e3af2589de2c3d89b23 Mon Sep 17 00:00:00 2001 From: Colin Date: Tue, 4 Jul 2023 08:57:44 +0000 Subject: [PATCH] handle embedded script payload --- corpus/payload.txt | 25 +++++++++++++++++++++++++ corpus/shebang_with_sub_interpreter.txt | 2 +- grammar.js | 20 +++++++++++++++----- 3 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 corpus/payload.txt diff --git a/corpus/payload.txt b/corpus/payload.txt new file mode 100644 index 0000000..f9031ac --- /dev/null +++ b/corpus/payload.txt @@ -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))) diff --git a/corpus/shebang_with_sub_interpreter.txt b/corpus/shebang_with_sub_interpreter.txt index 4669fa6..9c75a89 100644 --- a/corpus/shebang_with_sub_interpreter.txt +++ b/corpus/shebang_with_sub_interpreter.txt @@ -13,7 +13,7 @@ Minimal nix-shell invocation (env) (ws) (nix_shell)) - (non_first_line + (next_line (newline) (annotation_line (shebang_open) diff --git a/grammar.js b/grammar.js index 62f2f8d..ded906c 100644 --- a/grammar.js +++ b/grammar.js @@ -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',