Where can I get the full list of Swift keywords?

I have noticed that Swift 5.10 contains keywords borrowing and consuming. However these are missing in Documentation .

Where can I get the full list of actual keywords? Am I missing more keywords than the two mentioned?

1 Like

Can this be considered a "source of truth"?

1 Like

this might be a better reference, but yes, i would consider that reliable

3 Likes

Hmm... At first I tried to use Documentation and found that it was missing some (many?) keywords. Then I tried to use the suggested .swift file, and now found that it is missing some keywords from the documentation:

  • Keywords used in patterns: _
  • Keywords that begin with a number sign (#): #available, #colorLiteral, #else, #elseif, #endif, #fileLiteral, #if, #imageLiteral, #keyPath, #selector, #sourceLocation, #unavailable
  • Keywords reserved in particular contexts: precedence

Now I have to combine keywords from both sources :man_shrugging: Not ideal, but should work.

To give some context on why I need keywords: I'm trying to make a syntax highlighter for the Swift language.

2 Likes

Thanks for pointing out this issue in the documentation. The list in TSPL is meant to be complete, but sometimes gets out of date. The borrowing and consuming keywords are fairly new — it looks like the PR that added reference for them didn't add the keywords. I open an issue to track these. If you see more, please comment there or open another issue.

The markdown source files for the docs include comments about where in the compiler we based the lists on:

https://github.com/apple/swift-book/blob/main/TSPL.docc/ReferenceManual/LexicalStructure.md?plain=1#L173
The main list of keywords comes from swift/include/swift/Parse/Tokens.def and from utils/gyb_syntax_support/Token.py (which generates TokenKinds.def)

https://github.com/apple/swift-book/blob/main/TSPL.docc/ReferenceManual/LexicalStructure.md?plain=1#L371
Context sensitive keywords are marked CONTEXTUAL_SIMPLE_DECL_ATTR in swift/include/swift/AST/Attr.def — however, not all of them appear there.

5 Likes

are you trying to do some sort of client-side syntax highlighting? i can’t think of many other reasons why i would write such a thing instead of just using SwiftSyntax.

Yes, it is a desktop app, not written in Swift, that should be able to do syntax highlighting without installing Swift.

I counted 232 keywords in Swift. Quick googling on how many keywords in C++ revealed that C++ has 63. And C++ considered a complex language. Is Swift still clean and simple?

Out of those 232 keywords, how many of them start with the underscore character?

1 Like

54 keywords start with the underscore character, we still have 178 without them.

1 Like

C++ try to avoid introducing new keyword as much as possible to not break existing code.
They are going very far to do as much as possible using stdlib constructs instead of keyword. For instance, async/await is using unary operators. std::move is a function, etc.

So, comparing the number of keywords is not a really good metric to compare language complexity.

And Swift is not meant to be a simple language, but being able to cover simples cases using only simples languages features (progressive disclosure).

4 Likes

The number of reserved keywords is not a good measure of conceptual complexity. You can replace all keywords in the grammar with a single reserved keyword X that is followed by a string literal, so X "class", etc. The resulting language is even more complex but now it only has one keyword.

8 Likes

If you're writing a custom highlighter, one thing you probably care about is that a subset of those keywords are contextual and should only be highlighted as keywords in certain positions, and some of the rules around them are tricky to get right; sometimes even line breaks are significant.

For example, copy is only treated as a keyword if it's immediately followed by another identifier on the same line:

let x = copy y    // copy is a keyword

let x = copy (y)  // copy is an identifier being called with y passed to it

let x = copy      // copy is an identifier
  y               // this is a separate statement

As you can see, the syntax highlighter on Discourse is already getting this wrong in the last two cases.

5 Likes

TeX has entered the chat

1 Like

I don't think that's fair... X"abracadabra" would be a compilation error and there will be only N valid X"..." sequences — that's the number of "keywords" even though you've constructed them differently. We could also count operators like "<=", "&&" etc as "keywords". The number of keywords approximately corresponds to language complexity. Indeed, C++ tries to not introduce many keywords. And yes, Swift is a more complex language than C++, and has been such for quite a while now. All IMHO.

3 Likes

On the other hand, C++ cannot even be unambiguously parsed without intertwined name lookup, while Swift has a straightforward (but large) “true” grammar. It’s all subjective though.

2 Likes

:open_mouth:

Can you provide a list of those one starting with an underscore character please?

Here's the list
_BridgeObject
_Class
_NativeClass
_NativeRefCountedObject
_PackageDescription
_RefCountedObject
_Trivial
_TrivialAtMost
_TrivialStride
_UnknownLayout
__consuming
__owned
__setter_access
__shared
_alignment
_backDeploy
_borrow
_borrowing
_cdecl
_compilerInitialized
_const
_consuming
_documentation
_dynamicReplacement
_effects
_expose
_forward
_implements
_linear
_local
_modify
_move
_mutating
_noMetadata
_nonSendable
_objcImplementation
_objcRuntimeName
_opaqueReturnTypeOf
_optimize
_originallyDefinedIn
_private
_projectedValueProperty
_read
_resultDependsOn
_resultDependsOnSelf
_semantics
_specialize
_spi
_spi_available
_swift_native_objc_runtime_base
_typeEraser
_unavailableFromAsync
_underlyingVersion
_version
1 Like

Thanks a bunch @dima_kozhinov