Skip to content
This repository was archived by the owner on Dec 12, 2024. It is now read-only.

Read XML into attributes. #544

Merged
merged 2 commits into from
Feb 9, 2021
Merged

Read XML into attributes. #544

merged 2 commits into from
Feb 9, 2021

Conversation

stephen-hawley
Copy link
Contributor

Added an AttributeDeclaration type in order to round-trip attributes.

An attribute in swift is simultaneously overly complicated and inflexible.
The syntax is an @ sign followed by a name followed by an optional argument set off by parens.
What can go in the parens is a seamy, inconsistent set of junk that can included nested lists set off by parentheses, brackets, or braces (as long as they balance).
Examples:

@foo
@available(swift *)
@available(iOS 14.*, unavailable, macOS *, available)
@someotherthing(foo: bar, baz: [15 .. 20], "a string because why not")

And the contents of the parentheses is determined entirely by the name of the attribute.

So how should we represent this in C#? We get an AttributeDeclaration which has a name and an optional list of parameters.
A parameter is one of a plain AttributeParameter (should never happen), an AttributeParameterLabel (any non-number), an AttributeParameterLiteral (a number), an AttributeParameterSublist.

The XML consumption code is straightforward, although I'm sure it could be better where the right set of attributes/names would make it just read in on its own (cue "old man shouts at clouds" meme).

I added a List to the BaseDeclaration and therefore attributes are now available on classes, structs, enums, protocols, properties, and functions (take that, anti-inheritance people!).

In addition, the original grammar definition tried to break out a separate rule for platform and version, but it was broken and couldn't handle wild cards, so I got rid of it entirely and got rid of the version parameter. I also made separators and operators get treated as labels. There really isn't any advantage to having those broken out right now (if ever).

@stephen-hawley stephen-hawley changed the title Read into attributes. Read XML into attributes. Feb 9, 2021
@stephen-hawley stephen-hawley merged commit 53a1c36 into main Feb 9, 2021
@stephen-hawley stephen-hawley deleted the attributes-from-xml branch February 9, 2021 19:04
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants