-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[Proposal] Ignored Directives Support #3507
Description
Ignored Directives
- Proposed
- Prototype: Complete
- Implementation: In Progress
- Specification: Not Started
Summary
As we move to allow C# statements in the top level of a file, it's reasonable to assume that we might someday get support for dotnet runing a .cs file. For further use of C# as a scripting language in this vein, I propose that we introduce a new preprocessor directive who's purpose is to exist solely to be ignored by the language: the hashbang directive. Linux and Unix scripts will often use #!<path to interpreter> in order to direct the program loader to the interpreter that can run them. We should add a similar ignore to the C# language.
Detailed design
We introduce a new directive:
pp_directive
: ... // Existing directives
| pp_hashbang
| pp_reference
;
pp_hashbang
: whitespace? '#' '!' input_character*
;
pp_reference
: whitespace? '#' 'r' whitespace input_character*
;The language ignores the contents of the hashbang and reference directives and moves to the next line.
Drawbacks
Any change adds complication to the language. If we implement support for this and it starts appearing in files without actual scripting support, then it could be confusing for users expecting it to work.
Alternatives
N/A
Unresolved questions
- If we use these directives to support tooling such as nuget references, we will need to consider how they interact with other pragmas, such as
#if. We have a few options for this:- Require that ignored directives are the first directives in a file. We could even go further and require they're all at the beginning of the file.
- Less restrictive than 1, but forbid ignored directives inside conditional directives.
- Least restrictive, require a true preprocessing step in the compiler. Today, we can process directives up front without having to do a full multi-step preprocessing pass before lexing, but if we want to support nuget directives inside conditional directives we may need the full two-step process.
Design meetings
- https://github.com/dotnet/csharplang/blob/main/meetings/2021/LDM-2021-05-12.md#simple-c-programs
- https://github.com/dotnet/csharplang/blob/main/meetings/2022/LDM-2022-08-31.md#ignored-directives-support
- https://github.com/dotnet/csharplang/blob/main/meetings/2022/LDM-2022-09-26.md#ungrouped