Skip to content

[Proposal] Ignored Directives Support #3507

@333fred

Description

@333fred

Ignored Directives

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

  1. 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:
    1. 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.
    2. Less restrictive than 1, but forbid ignored directives inside conditional directives.
    3. 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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions