Skip to content

Commit 28b699a

Browse files
committed
More style fixes.
1 parent 266533d commit 28b699a

File tree

10 files changed

+87
-66
lines changed

10 files changed

+87
-66
lines changed

Sources/ast.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
protocol ASTNode: CustomStringConvertible {
2+
23
}
34

45
protocol Statement: ASTNode {
@@ -24,6 +25,7 @@ protocol Declaration: ASTNode {
2425
}
2526

2627
class BaseDeclaration: Declaration {
28+
2729
let name: String
2830

2931
init(_ name: String) {
@@ -35,6 +37,7 @@ class BaseDeclaration: Declaration {
3537
preconditionFailure("This method must be overridden")
3638
}
3739
}
40+
3841
}
3942

4043
protocol OperatorDeclaration: Declaration {
@@ -58,6 +61,7 @@ class PostfixOperatorDeclaration: BaseDeclaration, OperatorDeclaration {
5861
}
5962

6063
class InfixOperatorDeclaration: BaseDeclaration, OperatorDeclaration {
64+
6165
let precedenceGroupName: String?
6266

6367
init(_ name: String, precedenceGroup: String? = nil) {
@@ -71,13 +75,15 @@ class InfixOperatorDeclaration: BaseDeclaration, OperatorDeclaration {
7175
}
7276
return "infix operator \(name)"
7377
}
78+
7479
}
7580

7681
enum Associativity {
7782
case Left, Right, None
7883
}
7984

8085
class PrecedenceGroupDeclaration: BaseDeclaration {
86+
8187
let `higherThan`: [Identifier]
8288
let `lowerThan`: [Identifier]
8389
let `associativity`: Associativity

Sources/diagnostics.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ class Diagnose: Error, CustomStringConvertible {
104104
description += "^" * max(count, 1)
105105
if !fixIts.isEmpty {
106106
description += "\n\nFix:\n"
107-
description += fixIts.map{String(describing: $0)}.joined(separator: "\n")
107+
description += fixIts.map { String(describing: $0) }.joined(separator: "\n")
108108
}
109109
if !related.isEmpty {
110-
description += "\n\n" + related.map{String(describing: $0)}.joined(separator: "\n")
110+
description += "\n\n" + related.map { String(describing: $0) }.joined(separator: "\n")
111111
}
112112
return description
113113
}

Sources/helpers.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,11 @@ extension String {
199199
return self.characters.count
200200
}
201201

202-
subscript (i: Int) -> Character {
202+
subscript(i: Int) -> Character {
203203
return self.characters[index(startIndex, offsetBy: i)]
204204
}
205205

206-
subscript (range: Range<Int>) -> String {
206+
subscript(range: Range<Int>) -> String {
207207
let startOffset = clamp(range.lowerBound, lower: 0, upper: length)
208208
let endOffset = clamp(range.upperBound, lower: 0, upper: length)
209209
let start = index(startIndex, offsetBy: startOffset)

Sources/lexer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ class Lexer: Sequence {
540540
break
541541
}
542542

543-
let type: TokenTypev = leftBound == rightBound ? .BinaryOperator(content) : leftBound ? .PostfixOperator(content) : .PrefixOperator(content)
543+
let type: TokenType = leftBound == rightBound ? .BinaryOperator(content) : leftBound ? .PostfixOperator(content) : .PrefixOperator(content)
544544

545545
return makeToken(type: type, range: start..<self.index)
546546
}

Sources/parser.swift

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Parser {
1818
func parseType() -> Type {
1919
var type: Type = .Unknown
2020
let token = self.lexer.lexNextToken()
21-
type_switch: switch token.type {
21+
typeSwitch: switch token.type {
2222
case .Punctuator(.LeftSquare):
2323
let key = self.parseType()
2424
var value: Type? = nil
@@ -31,18 +31,18 @@ class Parser {
3131
self.diagnose("Expected ], found \(after.content)")
3232
break
3333
}
34-
if let actual_value = value {
35-
type = .DictionaryType(key, actual_value)
34+
if let actualValue = value {
35+
type = .DictionaryType(key, actualValue)
3636
} else {
3737
type = .ArrayType(key)
3838
}
3939
case .Punctuator(.LeftParenthesis):
4040
var after: Token
41-
var tuple_args = [Type]()
41+
var tupleArgs = [Type]()
4242
repeat {
4343
let inner = self.parseType()
4444
after = self.lexer.lexNextToken()
45-
tuple_args.append(inner)
45+
tupleArgs.append(inner)
4646
} while after.type == .Punctuator(.Comma)
4747
guard after.type == .Punctuator(.RightParenthesis) else {
4848
self.diagnose("Expected ), found \(after.content)")
@@ -51,24 +51,24 @@ class Parser {
5151
after = self.lexer.peekNextToken()
5252
if after.type == .Punctuator(.Arrow) {
5353
_ = self.lexer.lexNextToken()
54-
let return_type = self.parseType()
55-
type = .FunctionType(tuple_args, return_type)
54+
let returnType = self.parseType()
55+
type = .FunctionType(tupleArgs, returnType)
5656
} else {
57-
type = .TupleType(tuple_args)
57+
type = .TupleType(tupleArgs)
5858
}
5959
case .Identifier(_):
60-
var parts = [((Identifier, [Type]))]()
61-
var ident_token = token
62-
identifier_loop: repeat {
63-
let identifier = Identifier(ident_token.content)
60+
var parts = [(Identifier, [Type])]()
61+
var identToken = token
62+
identifierLoop: repeat {
63+
let identifier = Identifier(identToken.content)
6464
switch self.lexer.peekNextToken().type {
6565
case .Punctuator(.Period):
6666
parts.append((identifier, []))
6767
let dot = self.lexer.lexNextToken()
68-
ident_token = self.lexer.lexNextToken()
69-
if ident_token.type == .Identifier(false) && (ident_token.content == "Type" || ident_token.content == "Protocol") {
68+
identToken = self.lexer.lexNextToken()
69+
if identToken.type == .Identifier(false) && (identToken.content == "Type" || identToken.content == "Protocol") {
7070
self.lexer.resetToBeginning(of: dot)
71-
break identifier_loop
71+
break identifierLoop
7272
}
7373
case .BinaryOperator("<"):
7474
var generics = [Type]()
@@ -79,22 +79,22 @@ class Parser {
7979
let after = self.lexer.lexNextToken()
8080
if case let .PostfixOperator(op) = after.type, op.hasPrefix(">") {
8181
if op != ">" {
82-
let next_index = self.lexer.getIndex(after: after.range.range.lowerBound)
83-
self.lexer.resetIndex(to: next_index)
82+
let nextIndex = self.lexer.getIndex(after: after.range.range.lowerBound)
83+
self.lexer.resetIndex(to: nextIndex)
8484
}
8585
} else {
8686
self.diagnose("Expected >, found \(after.content)")
87-
break type_switch
87+
break typeSwitch
8888
}
8989
parts.append((identifier, generics))
9090
guard self.lexer.peekNextToken().type == .Punctuator(.Period) else {
91-
break identifier_loop
91+
break identifierLoop
9292
}
9393
_ = self.lexer.lexNextToken()
94-
ident_token = self.lexer.lexNextToken()
94+
identToken = self.lexer.lexNextToken()
9595
default:
9696
parts.append((identifier, []))
97-
break identifier_loop
97+
break identifierLoop
9898
}
9999
} while true
100100
type = .TypeIdentifier(parts)
@@ -104,7 +104,7 @@ class Parser {
104104
print("Error: " + String(describing: token))
105105
}
106106

107-
postfix_loop: while true {
107+
postfixLoop: while true {
108108
let nextToken = self.lexer.peekNextToken()
109109
switch nextToken.type {
110110
case .Punctuator(.PostfixExclaimationMark):
@@ -124,11 +124,11 @@ class Parser {
124124
type = .MetaProtocol(type)
125125
default:
126126
self.lexer.resetToBeginning(of: nextToken)
127-
break postfix_loop
127+
break postfixLoop
128128
}
129129
}
130130
default:
131-
break postfix_loop
131+
break postfixLoop
132132
}
133133
}
134134

@@ -144,18 +144,18 @@ class Parser {
144144
guard kwtoken.type == .DeclarationKeyword(.PrecedenceGroup) else {
145145
preconditionFailure("Should only parsePrecedenceGroup at the beginning of a precedencegroup declaration")
146146
}
147-
let ident_token = lexer.lexNextToken()
148-
guard case .Identifier(_) = ident_token.type else {
147+
let identToken = lexer.lexNextToken()
148+
guard case .Identifier(_) = identToken.type else {
149149
let error = self.diagnose("Expected identifier after 'precedencegroup'")
150-
if ident_token.type == .Punctuator(.LeftBrace) || lexer.peekNextToken().type == .Punctuator(.LeftBrace) {
150+
if identToken.type == .Punctuator(.LeftBrace) || lexer.peekNextToken().type == .Punctuator(.LeftBrace) {
151151
skipWhile { $0.type != .Punctuator(.RightBrace) }
152152
}
153153
consumeIf(type: .Punctuator(.RightBrace))
154154
throw error
155155
}
156156

157157
var valid = true
158-
let name = ident_token.content
158+
let name = identToken.content
159159
var higherThan: [Identifier]? = nil
160160
var lowerThan: [Identifier]? = nil
161161
var associativity: Associativity? = nil
@@ -167,13 +167,13 @@ class Parser {
167167
consumeIf(type: .Punctuator(.RightBrace))
168168
}
169169

170-
let open_brace = lexer.lexNextToken()
171-
guard open_brace.type == .Punctuator(.LeftBrace) else {
170+
let openBrace = lexer.lexNextToken()
171+
guard openBrace.type == .Punctuator(.LeftBrace) else {
172172
throw self.diagnose("Expected '{' after name of precedence group")
173173
}
174-
var attribute_name_token: Token = lexer.lexNextToken()
175-
while attribute_name_token.type == .Identifier(false) {
176-
let name = attribute_name_token.content
174+
var attributeNameToken: Token = lexer.lexNextToken()
175+
while attributeNameToken.type == .Identifier(false) {
176+
let name = attributeNameToken.content
177177
let colon = lexer.lexNextToken()
178178
if colon.type != .Punctuator(.Colon) {
179179
self.diagnose("Expected colon after attribute name in precedence group")
@@ -222,20 +222,20 @@ class Parser {
222222
throw error
223223
}
224224
groups.append(group.content)
225-
} while (consumeIf(type: .Punctuator(.Comma)))
225+
} while consumeIf(type: .Punctuator(.Comma))
226226
if name == "higherThan" {
227227
higherThan = groups.map { Identifier($0) }
228228
} else {
229229
lowerThan = groups.map { Identifier($0) }
230230
}
231231
default:
232-
let error = self.diagnose("'\(name)' is not a valid precedence group attribute", at: attribute_name_token)
232+
let error = self.diagnose("'\(name)' is not a valid precedence group attribute", at: attributeNameToken)
233233
abortBlock()
234234
throw error
235235
}
236-
attribute_name_token = lexer.lexNextToken()
236+
attributeNameToken = lexer.lexNextToken()
237237
}
238-
guard attribute_name_token.type == .Punctuator(.RightBrace) else {
238+
guard attributeNameToken.type == .Punctuator(.RightBrace) else {
239239
throw self.diagnose("Expected operator attribute identifier in precedence group body")
240240
}
241241
return PrecedenceGroupDeclaration(
@@ -298,4 +298,5 @@ class Parser {
298298
diagnoses.append(diag)
299299
return diag
300300
}
301+
301302
}

Sources/source.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ class Source {
178178
}
179179

180180
struct SourceLocation: CustomStringConvertible {
181+
181182
let source: Source
182183
let index: Source.Index
183184

@@ -195,12 +196,15 @@ struct SourceLocation: CustomStringConvertible {
195196
}
196197

197198
extension SourceLocation: Equatable {
199+
198200
static func == (lhs: SourceLocation, rhs: SourceLocation) -> Bool {
199201
return lhs.source === rhs.source && lhs.index == rhs.index
200202
}
203+
201204
}
202205

203206
struct SourceRange: CustomStringConvertible {
207+
204208
let source: Source
205209
let range: Range<Source.Index>
206210

@@ -224,19 +228,25 @@ struct SourceRange: CustomStringConvertible {
224228
}
225229
return "\(source.identifier):\(startLine):\(start.column)-\(endLine):\(end.column)"
226230
}
231+
227232
}
228233

229234
extension SourceRange: Equatable {
235+
230236
static func == (lhs: SourceRange, rhs: SourceRange) -> Bool {
231237
return lhs.source === rhs.source && lhs.range == rhs.range
232238
}
239+
233240
}
234241

235242
extension SourceRange {
243+
236244
init(source: Source, index: Source.Index) {
237245
self.init(source: source, range: index..<index)
238246
}
247+
239248
init(location: SourceLocation) {
240249
self.init(source: location.source, index: location.index)
241250
}
251+
242252
}

Sources/token.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ public enum TokenType {
353353
}
354354

355355
var isWhitespace: Bool {
356-
switch (self) {
356+
switch self {
357357
case .Whitespace, .Comment(_): return true
358358
default: return false
359359
}

Sources/types.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
indirect enum Type: CustomStringConvertible {
2+
23
typealias IdentifierWithGeneric = (Identifier, [Type])
34

45
case ArrayType(Type)
@@ -21,8 +22,8 @@ indirect enum Type: CustomStringConvertible {
2122
return "[\(inner)]"
2223
case let .DictionaryType(key, value):
2324
return "[\(key): \(value)]"
24-
case let .FunctionType(parameters, return_type):
25-
return "(\((parameters.map { $0.description }).joined(separator: ", "))) -> \(return_type)"
25+
case let .FunctionType(parameters, returnType):
26+
return "(\((parameters.map { $0.description }).joined(separator: ", "))) -> \(returnType)"
2627
case let .TupleType(types):
2728
return "(\((types.map { $0.description }).joined(separator: ", ")))"
2829
case let .TypeIdentifier(parts):
@@ -56,9 +57,11 @@ indirect enum Type: CustomStringConvertible {
5657
static func Identifier(_ name: String) -> Type {
5758
return .TypeIdentifier([(OriginalIdentifier(name), [])])
5859
}
60+
5961
}
6062

6163
class Identifier: CustomStringConvertible {
64+
6265
var name: String
6366

6467
init(_ name: String) {
@@ -68,6 +71,7 @@ class Identifier: CustomStringConvertible {
6871
var description: String {
6972
return self.name
7073
}
74+
7175
}
7276

7377
typealias OriginalIdentifier = Identifier

Tests/SwiftParserTests/ParsePrecedenceGroupTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,10 @@ class ParsePrecedenceGroupTests: ParserTestCase {
148148
line: UInt = #line
149149
) -> (PrecedenceGroupDeclaration) -> () {
150150
return { group in
151-
let actual_higher = Set(group.higherThan.map { $0.name })
152-
XCTAssertEqual(actual_higher, higherThan, "Wrong 'higherThan' for precedence group.", file: file, line: line)
153-
let actual_lower = Set(group.lowerThan.map { $0.name })
154-
XCTAssertEqual(actual_lower, lowerThan, "Wrong 'lowerThan' for precedence group.", file: file, line: line)
151+
let actualHigher = Set(group.higherThan.map { $0.name })
152+
XCTAssertEqual(actualHigher, higherThan, "Wrong 'higherThan' for precedence group.", file: file, line: line)
153+
let actualLower = Set(group.lowerThan.map { $0.name })
154+
XCTAssertEqual(actualLower, lowerThan, "Wrong 'lowerThan' for precedence group.", file: file, line: line)
155155
XCTAssertEqual(group.associativity, associativity, "Wrong 'associativity' for precedence group.", file: file, line: line)
156156
XCTAssertEqual(group.assignment, assignment, "Wrong 'assignment' for precedence group.", file: file, line: line)
157157
}

0 commit comments

Comments
 (0)