Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrectly parsed directives #298

Open
ketkarameya opened this issue Jun 21, 2023 · 1 comment
Open

Incorrectly parsed directives #298

ketkarameya opened this issue Jun 21, 2023 · 1 comment

Comments

@ketkarameya
Copy link
Contributor

ketkarameya commented Jun 21, 2023

I observed that the directives (https://docs.swift.org/swift-book/documentation/the-swift-programming-language/statements/#Compiler-Control-Statements) do not parse well.

class Sample {


    public static func foobar() -> Bool {
        #if DEBUG
            return true
        #else
            return false
        #endif
    }

}

Tree:

body: [function_body][3, 40] - [9, 5]
          [directive][4, 8] - [4, 17]
          [statements][5, 12] - [8, 8]
            [control_transfer_statement][5, 12] - [5, 23]
              result: [boolean_literal][5, 19] - [5, 23]
            [directive][6, 8] - [6, 13]
            [control_transfer_statement][7, 12] - [7, 24]
              result: [boolean_literal][7, 19] - [7, 24]
          [directive][8, 8] - [8, 14]

I think the [statements][5, 12] ... [directive][8, 8] - [8, 14] should be nested under [directive][4, 8] - [4, 17] ?
And in some sense the we need the entire if-elseif like parsing here.

@ketkarameya ketkarameya changed the title Incorrectly parsed derivatives Incorrectly parsed directives Jun 21, 2023
@alex-pinkus
Copy link
Owner

Yep, we parse directives like extras, but Swift is actually restrictive enough with its macro system that we can do better. That is, I was leaving open the possibility that there could be something like:

class MyClass
#if DEBUG
}
#else
}
class MyOtherClass
#endif
}

But Swift doesn't allow this case (it says "Unexpected '}' in conditional compilation block"), and I suspect they disallow any such cases. It probably makes sense to just add this into the choice in _local_statement and _top_level_statement, and add the structure for the if/else like you say.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants