diff options
author | Kevin Newton <[email protected]> | 2024-08-14 13:26:00 -0400 |
---|---|---|
committer | git <[email protected]> | 2024-08-14 17:40:43 +0000 |
commit | a952ea243f7be95d479848a28959f8d29cd967ed (patch) | |
tree | 8001577697faed53bc1e3ba62335ae93b865bf2c /prism/options.h | |
parent | 88954a0e9a199156aadc472c4795133e5ac7651b (diff) |
[ruby/prism] Callback on shebang switches
Add the ability to receive a callback when the parser encounters a
shebang that contains additional switches after the Ruby engine.
This is necessary because some command-line flags may be present
there that will alter the parse.
https://github.com/ruby/prism/commit/afc5000331
Diffstat (limited to 'prism/options.h')
-rw-r--r-- | prism/options.h | 41 |
1 files changed, 40 insertions, 1 deletions
diff --git a/prism/options.h b/prism/options.h index 784769f880..52b5380965 100644 --- a/prism/options.h +++ b/prism/options.h @@ -40,6 +40,23 @@ typedef struct pm_options_scope { pm_string_t *locals; } pm_options_scope_t; +// Forward declaration needed by the callback typedef. +struct pm_options; + +/** + * The callback called when additional switches are found in a shebang comment + * that need to be processed by the runtime. + * + * @param options The options struct that may be updated by this callback. + * Certain fields will be checked for changes, specifically encoding, + * command_line, and frozen_string_literal. + * @param source The source of the shebang comment. + * @param length The length of the source. + * @param shebang_callback_data Any additional data that should be passed along + * to the callback. + */ +typedef void (*pm_options_shebang_callback_t)(struct pm_options *options, const uint8_t *source, size_t length, void *shebang_callback_data); + /** * The version of Ruby syntax that we should be parsing with. This is used to * allow consumers to specify which behavior they want in case they need to @@ -56,7 +73,19 @@ typedef enum { /** * The options that can be passed to the parser. */ -typedef struct { +typedef struct pm_options { + /** + * The callback to call when additional switches are found in a shebang + * comment. + */ + pm_options_shebang_callback_t shebang_callback; + + /** + * Any additional data that should be passed along to the shebang callback + * if one was set. + */ + void *shebang_callback_data; + /** The name of the file that is currently being parsed. */ pm_string_t filepath; @@ -150,6 +179,16 @@ static const uint8_t PM_OPTIONS_COMMAND_LINE_P = 0x10; static const uint8_t PM_OPTIONS_COMMAND_LINE_X = 0x20; /** + * Set the shebang callback option on the given options struct. + * + * @param options The options struct to set the shebang callback on. + * @param shebang_callback The shebang callback to set. + * @param shebang_callback_data Any additional data that should be passed along + * to the callback. + */ +PRISM_EXPORTED_FUNCTION void pm_options_shebang_callback_set(pm_options_t *options, pm_options_shebang_callback_t shebang_callback, void *shebang_callback_data); + +/** * Set the filepath option on the given options struct. * * @param options The options struct to set the filepath on. |