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

[SR-472] Grammar Inconsistencies #43089

Closed
swift-ci opened this issue Jan 5, 2016 · 2 comments
Closed

[SR-472] Grammar Inconsistencies #43089

swift-ci opened this issue Jan 5, 2016 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. documentation

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Jan 5, 2016

Previous ID SR-472
Radar rdar://problem/72801680
Original Reporter YZheng (JIRA User)
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s
Labels Bug, Documentation
Assignee None
Priority Medium

md5: 39cca24b6ed0d109d568f49955652efe

Issue Description:

The followings are some inconsistencies between the grammar and the swift programs accepted by XCode or seen in the framework modules.

In grammar, "-" means the rules on in the grammar summary page. "+" shows modifications that work for me.


(1) Label allowed before do-statement in Xcode but not in grammar

Grammar:

  labeled-statement :
+   | statement-label   do-statement

Example:

var i = 0
label1: do {
    print("i = \(i++)")
    if i < 3 {
        continue label1
    }
}

(2) Optional "initializer-body" in "initializer-declaration"

Grammar:

  initializer-declaration:
-     initializer-head   generic-parameter-clause?   parameter-clause   WORD-THROWS?    initializer-body
+     initializer-head   generic-parameter-clause?   parameter-clause   WORD-THROWS?    initializer-body?
-   | initializer-head   generic-parameter-clause?   parameter-clause   WORD-RETHROWS   initializer-body
+   | initializer-head   generic-parameter-clause?   parameter-clause   WORD-RETHROWS   initializer-body?

Example:

// In framework Module "Darwin"
public struct DarwinBoolean : BooleanType, BooleanLiteralConvertible {
    public init(_ value: Bool)
    /// Create an instance initialized to `value`.
    public init(booleanLiteral value: Bool)
}

(3) compiler-control statement can appear in declaration

Grammar:

  declaration: 
+   | compiler-control-statement (SEMICOLON)*

Example:

/* 
 * class-declaration : ... class-body
 * class-body : { declarations? }
 * declarations : declaration+
 */
class CCC {
    #if DEBUG
        func foo() {print("foo-debug")}
    #else
        func foo() {print("foo-normal")}
    #endif
}
CCC().foo()

(4) "nonmutating?" or "declaration-modifier" needed before "set" in "setter-keyword-clause"

Grammar:

  setter-keyword-clause:
-     attributes?   set
+     attributes?   nonmutating?   set

Example:

// In framework module "Swift"
public struct AutoreleasingUnsafeMutablePointer<Memory> : Equatable, NilLiteralConvertible, _PointerType {
    ...
    public var memory: Memory { get nonmutating set }
    ...
}

(5) "nonmutating?" or "declaration-modifier" needed before "set" in "setter-clause"
Similar to (4)

(6) "attributes?" needed after "=" in "typealias-assignment"

Grammar:

  typealias-assignment 
-   :  =    type
+   :  =    attributes?    type

Example:

// In framework module "CFNetwork.CFHost"
public typealias CFHostClientCallBack = @convention(c) (CFHost, CFHostInfoType, UnsafePointer<CFStreamError>, UnsafeMutablePointer<Void>) -> Void

(7) "type-annotation" has to be optional in some rules of "parameter"

Grammar:

  parameter:
-     let?   external-parameter-name?   local-parameter-name   type-annotation  default-argument-clause?
+     let?   external-parameter-name?   local-parameter-name   type-annotation?  default-argument-clause?
-   | var    external-parameter-name?   local-parameter-name   type-annotation   default-argument-clause?
+   | var    external-parameter-name?   local-parameter-name   type-annotation?   default-argument-clause?

Example:

var names = ["a", "b", "c"]
var reversed = names.sort( { (s1, s2) -> Bool in return s1 > s2 } )
reversed = names.sort( { (s1, var s2) -> Bool in return s1 > s2 } )

(8) "attributes" needed in "parameter"

Grammar:

  parameter:
-     let?   external-parameter-name?   local-parameter-name   type-annotation?   default-argument-clause?
+     attributes?   let?   external-parameter-name?   local-parameter-name   type-annotation?   default-argument-clause?
-   | var    external-parameter-name?   local-parameter-name   type-annotation?   default-argument-clause?
+   | attributes?   var    external-parameter-name?   local-parameter-name   type-annotation?   default-argument-clause?

Example:

public class CC {
    func kkk (lhs: Bool, @autoclosure rhs: () throws -> Bool) rethrows -> Bool {return true}
}

(9) "= default" in "default-argument-clause"

Grammar:

  default_argument_clause:   
+   |  =  default

Example:

// In framework module "Swift"
extension ContiguousArray : _ArrayType {
    public mutating func removeAll(keepCapacity keepCapacity: Bool = default)
}
/*
 * According to subsection "Keywords and Punctuation", "default" is a keyword and cannot be used as identifiers.
 * So, "default" cannot be matched by the "expression" rule
 */

(10) "access-level-modifier" should be "declaration-modifiers" in "class-declaration"

Grammar:

  class-declaration: 
-     attributes­?   ­access-level-modifier­?   ­class   ­class-name   ­generic-parameter-clause­?   ­type-inheritance-clause­?   ­class-body­
+     attributes­?   ­declaration-modifiers?   ­class   ­class-name   ­generic-parameter-clause­?   ­type-inheritance-clause­?   ­class-body­

Example:

/*
 * "final public" cannot be matched by "access-level-modifier"
 */
final public class CC {
}

(11) "attributes" in "extension-declaration"

Grammar:

  extension-declaration: 
-     access-level-modifier­?   ­extension   ­type-identifier   ­type-inheritance-clause?   ­­extension-body­
+     attributes?   access-level-modifier­?   ­extension   ­type-identifier   ­type-inheritance-clause?   ­­extension-body­ 

Example:

@available(OSX 10.10, *)
extension TT  {
    static var _NSErrorDomain: String {
        get {return "_NSErrorDomain"}
    }
    typealias RawValue = Int
}

(12) "where" can appear in extension declarations

Grammar:

  extension-declaration:
-     attributes?   access-level-modifier­?   ­extension   ­type-identifier   ­type-inheritance-clause?   ­­extension-body­
+     attributes?   access-level-modifier­?   ­extension   ­type-identifier   ­type-inheritance-clause?   requirement_clause?   ­­extension-body­

Example:

// In framework module "Swift.swift"
extension BidirectionalIndexType where Self : ReverseIndexType {
   ...
}

(13) "subscript" may not have the code-body

Grammar:

  subscript-declaration:  
-      subscript-head­   subscript-result   ­code-block­
+      subscript-head­   subscript-result   ­code-block­? 

Example:

// In framework module "Swift"
public struct ContiguousArray<Element> : CollectionType, MutableCollectionType, _DestructorSafeContainer {
    ...
    public subscript (index: Int) -> Element
    public subscript (subRange: Range<Int>) -> ArraySlice<Element>
}

(14) "assignment" in "infix-operator-attributes"

Grammar:

  infix-operator-attributes: 
-     precedence-clause­?   ­associativity-clause?
+     precedence-clause­?   ­associativity-clause?   assignment?

Example:

infix operator %**= {
  precedence 90
  associativity right
  assignment
}

(15) The order of sub-rules in "infix-operator-attributes"

Grammar:

  infix-operator-attributes: 
-     precedence-clause­?   ­associativity-clause?
+     precedence-clause­?   ­associativity-clause?   assignment?
+     precedence-clause­?   ­assignment?   associativity-clause?
+     assignment?   precedence-clause­?   ­associativity-clause?
+     assignment?   ­associativity-clause?   precedence-clause­?
+     associativity-clause?   assignment?   ­precedence-clause­?
+     associativity-clause?   ­precedence-clause­?   assignment?

Example:

infix operator %**= {
  associativity right
  precedence 90
  assignment
}

(16) "." in "enum-case-pattern" should be optional

Grammar:

  enum-case-pattern: 
-     type-identifier­?   ­.   ­enum-case-name   ­tuple-pattern­?
+     type-identifier­?   ­.?   ­enum-case-name   ­tuple-pattern­?

Example:

public enum car{
    case SUV
    case UNKNOWN   
    public func printCar() {
        switch self {
            case SUV:
                print("SUV")
            case .SUV:        // Warning: Case will never be executed
                print("SUV-2")
            default:
                print("default")
        }
    }
}

(17) "statements" in "closure" can be optional

Grammar:

  closure-expression: 
-     {  closure-signature?   statements   }
+     {  closure-signature?   statements?   }

Example:

func foo(f:Int->()) {}
foo({(x:Int) in })

(18) "operator" should be allowed in "expression-element"

Grammar:

  expression-element:
+      operator

Example:

// In Operator Functions in Language Guide
// ">" cannot be matched by "expression"
var names = ["a", "b", "c"]
var reversed = names.sort(>)
@typesanitizer
Copy link

@swift-ci create

@xymus
Copy link
Contributor

xymus commented Feb 17, 2021

According to a duplicate report via feedback assistant I believe this was fixed, but the grammar also changed a lot since then. Please let us know if we missed something.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. documentation
Projects
None yet
Development

No branches or pull requests

3 participants