Skip to content

FCS SyntaxTree: store preprocessor directives #12815

@nojaf

Description

@nojaf

Is your feature request related to a problem? Please describe.

Currently, compiler directives like #if ..., #else and #endif are not captured in the untyped syntax tree.
It would be extremely valuable if these were present.

Describe the solution you'd like

Given the nature of these directives, I don't think we can store them as part of any existing node.
They can occur in SynExpr, SynPat, SynType and so on.
I would propose to add them to the top-level ParsedImplFileInput or ParsedSigFileInput nodes.

With the data type being something along the lines of:

[<RequireQualifiedAccess; NoEquality; NoComparison>]
type ConditionalHashDirective =
    | IfExpr of expr: SynExpr * range: range
    | Else of range: range
    | Endif of range: range 

For the IfExpr case the boolean logic could perhaps be captured differently.

Describe alternatives you've considered

We currently need to check the source code and parse the found defines ourselves.
The tokenizer from FCS requires you to pass defines in order to get the tokens, so we don't use this API because we need to figure out first what defines to use first.

Another interesting hurdle we encounter while doing this custom parsing in Fantomas, is that we need to keep track when we are entering a multiline string or code comment.

Some example:

let meh = """
#if FOO
""" // FOO is not a define that bears any relevance to this file.

(*
#if BAR
#endif
*)
// BAR is also not relevant in this file

Additional context

This information would be significant to have in Fantomas.
I don't have a crystal view of how this could be implemented. I would assume some additional state in the lexer might do the trick, but that is speculation. If you see any opportunities @dsyme, I'm all ears.

related: #11481

Metadata

Metadata

Assignees

No one assigned

    Labels

    Feature RequestImpact-Low(Internal MS Team use only) Describes an issue with limited impact on existing code.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions