The key thing is that there is an important distinction between syntactic and semantic validity.
For example, something like class class Foo { ]
is not well-formed syntactically. However, class C { protocol P {} }
is valid syntax but doesn't make sense semantically. There is nothing in the language grammar preventing a protocol declaration from being nested inside of a class. Instead, it is the type checker's job to enforce such semantic rules.