Skip to content

Commit 75cb871

Browse files
committed
re-do of description support
uses blockstrings2 re-do and matches closer to canonical js approach. cleaned up test names. update tests.
1 parent 35a3f70 commit 75cb871

File tree

5 files changed

+122
-108
lines changed

5 files changed

+122
-108
lines changed

Sources/GraphQL/Language/AST.swift

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ final public class Token {
5757
case string = "String"
5858
case blockstring = "BlockString"
5959
case comment = "Comment"
60-
case description = "Description" // string in a description position
6160

6261
public var description: String {
6362
return rawValue
@@ -134,19 +133,6 @@ extension Token : CustomStringConvertible {
134133
}
135134
}
136135

137-
extension Token {
138-
convenience init(from token: Token, as kind: Kind ) {
139-
self.init(kind: kind,
140-
start: token.start,
141-
end: token.end,
142-
line: token.line,
143-
column: token.column,
144-
value: token.value,
145-
prev: token.prev,
146-
next: token.next)
147-
}
148-
}
149-
150136
public enum NodeResult {
151137
case node(Node)
152138
case array([Node])
@@ -825,16 +811,18 @@ public final class StringValue {
825811
public let kind: Kind = .stringValue
826812
public let loc: Location?
827813
public let value: String
814+
public let block: Bool?
828815

829-
init(loc: Location? = nil, value: String) {
816+
init(loc: Location? = nil, value: String, block: Bool? = nil) {
830817
self.loc = loc
831818
self.value = value
819+
self.block = block
832820
}
833821
}
834822

835823
extension StringValue : Equatable {
836824
public static func == (lhs: StringValue, rhs: StringValue) -> Bool {
837-
return lhs.value == rhs.value
825+
return lhs.value == rhs.value && lhs.block == rhs.block
838826
}
839827
}
840828

@@ -1090,11 +1078,11 @@ public func == (lhs: TypeSystemDefinition, rhs: TypeSystemDefinition) -> Bool {
10901078
public final class SchemaDefinition {
10911079
public let kind: Kind = .schemaDefinition
10921080
public let loc: Location?
1093-
public let description: String?
1081+
public let description: StringValue?
10941082
public let directives: [Directive]
10951083
public let operationTypes: [OperationTypeDefinition]
10961084

1097-
init(loc: Location? = nil, description: String? = nil, directives: [Directive], operationTypes: [OperationTypeDefinition]) {
1085+
init(loc: Location? = nil, description: StringValue? = nil, directives: [Directive], operationTypes: [OperationTypeDefinition]) {
10981086
self.loc = loc
10991087
self.description = description
11001088
self.directives = directives
@@ -1174,11 +1162,11 @@ public func == (lhs: TypeDefinition, rhs: TypeDefinition) -> Bool {
11741162
public final class ScalarTypeDefinition {
11751163
public let kind: Kind = .scalarTypeDefinition
11761164
public let loc: Location?
1177-
public let description: String?
1165+
public let description: StringValue?
11781166
public let name: Name
11791167
public let directives: [Directive]
11801168

1181-
init(loc: Location? = nil, description: String? = nil, name: Name, directives: [Directive] = []) {
1169+
init(loc: Location? = nil, description: StringValue? = nil, name: Name, directives: [Directive] = []) {
11821170
self.loc = loc
11831171
self.description = description
11841172
self.name = name
@@ -1197,13 +1185,13 @@ extension ScalarTypeDefinition : Equatable {
11971185
public final class ObjectTypeDefinition {
11981186
public let kind: Kind = .objectTypeDefinition
11991187
public let loc: Location?
1200-
public let description: String?
1188+
public let description: StringValue?
12011189
public let name: Name
12021190
public let interfaces: [NamedType]
12031191
public let directives: [Directive]
12041192
public let fields: [FieldDefinition]
12051193

1206-
init(loc: Location? = nil, description: String? = nil, name: Name, interfaces: [NamedType] = [], directives: [Directive] = [], fields: [FieldDefinition] = []) {
1194+
init(loc: Location? = nil, description: StringValue? = nil, name: Name, interfaces: [NamedType] = [], directives: [Directive] = [], fields: [FieldDefinition] = []) {
12071195
self.loc = loc
12081196
self.description = description
12091197
self.name = name
@@ -1226,13 +1214,13 @@ extension ObjectTypeDefinition : Equatable {
12261214
public final class FieldDefinition {
12271215
public let kind: Kind = .fieldDefinition
12281216
public let loc: Location?
1229-
public let description: String?
1217+
public let description: StringValue?
12301218
public let name: Name
12311219
public let arguments: [InputValueDefinition]
12321220
public let type: Type
12331221
public let directives: [Directive]
12341222

1235-
init(loc: Location? = nil, description: String? = nil, name: Name, arguments: [InputValueDefinition] = [], type: Type, directives: [Directive] = []) {
1223+
init(loc: Location? = nil, description: StringValue? = nil, name: Name, arguments: [InputValueDefinition] = [], type: Type, directives: [Directive] = []) {
12361224
self.loc = loc
12371225
self.description = description
12381226
self.name = name
@@ -1255,13 +1243,13 @@ extension FieldDefinition : Equatable {
12551243
public final class InputValueDefinition {
12561244
public let kind: Kind = .inputValueDefinition
12571245
public let loc: Location?
1258-
public let description: String?
1246+
public let description: StringValue?
12591247
public let name: Name
12601248
public let type: Type
12611249
public let defaultValue: Value?
12621250
public let directives: [Directive]
12631251

1264-
init(loc: Location? = nil, description: String? = nil, name: Name, type: Type, defaultValue: Value? = nil, directives: [Directive] = []) {
1252+
init(loc: Location? = nil, description: StringValue? = nil, name: Name, type: Type, defaultValue: Value? = nil, directives: [Directive] = []) {
12651253
self.loc = loc
12661254
self.description = description
12671255
self.name = name
@@ -1300,15 +1288,15 @@ extension InputValueDefinition : Equatable {
13001288
public final class InterfaceTypeDefinition {
13011289
public let kind: Kind = .interfaceTypeDefinition
13021290
public let loc: Location?
1303-
public let description: String?
1291+
public let description: StringValue?
13041292
public let name: Name
13051293
public let interfaces: [NamedType]
13061294
public let directives: [Directive]
13071295
public let fields: [FieldDefinition]
13081296

13091297
init(
13101298
loc: Location? = nil,
1311-
description: String? = nil,
1299+
description: StringValue? = nil,
13121300
name: Name,
13131301
interfaces: [NamedType] = [],
13141302
directives: [Directive] = [],
@@ -1335,12 +1323,12 @@ extension InterfaceTypeDefinition : Equatable {
13351323
public final class UnionTypeDefinition {
13361324
public let kind: Kind = .unionTypeDefinition
13371325
public let loc: Location?
1338-
public let description: String?
1326+
public let description: StringValue?
13391327
public let name: Name
13401328
public let directives: [Directive]
13411329
public let types: [NamedType]
13421330

1343-
init(loc: Location? = nil, description: String? = nil, name: Name, directives: [Directive] = [], types: [NamedType]) {
1331+
init(loc: Location? = nil, description: StringValue? = nil, name: Name, directives: [Directive] = [], types: [NamedType]) {
13441332
self.loc = loc
13451333
self.description = description
13461334
self.name = name
@@ -1361,12 +1349,12 @@ extension UnionTypeDefinition : Equatable {
13611349
public final class EnumTypeDefinition {
13621350
public let kind: Kind = .enumTypeDefinition
13631351
public let loc: Location?
1364-
public let description: String?
1352+
public let description: StringValue?
13651353
public let name: Name
13661354
public let directives: [Directive]
13671355
public let values: [EnumValueDefinition]
13681356

1369-
init(loc: Location? = nil, description: String? = nil, name: Name, directives: [Directive] = [], values: [EnumValueDefinition]) {
1357+
init(loc: Location? = nil, description: StringValue? = nil, name: Name, directives: [Directive] = [], values: [EnumValueDefinition]) {
13701358
self.loc = loc
13711359
self.description = description
13721360
self.name = name
@@ -1387,11 +1375,11 @@ extension EnumTypeDefinition : Equatable {
13871375
public final class EnumValueDefinition {
13881376
public let kind: Kind = .enumValueDefinition
13891377
public let loc: Location?
1390-
public let description: String?
1378+
public let description: StringValue?
13911379
public let name: Name
13921380
public let directives: [Directive]
13931381

1394-
init(loc: Location? = nil, description: String? = nil, name: Name, directives: [Directive] = []) {
1382+
init(loc: Location? = nil, description: StringValue? = nil, name: Name, directives: [Directive] = []) {
13951383
self.loc = loc
13961384
self.description = description
13971385
self.name = name
@@ -1410,12 +1398,12 @@ extension EnumValueDefinition : Equatable {
14101398
public final class InputObjectTypeDefinition {
14111399
public let kind: Kind = .inputObjectTypeDefinition
14121400
public let loc: Location?
1413-
public let description: String?
1401+
public let description: StringValue?
14141402
public let name: Name
14151403
public let directives: [Directive]
14161404
public let fields: [InputValueDefinition]
14171405

1418-
init(loc: Location? = nil, description: String? = nil, name: Name, directives: [Directive] = [], fields: [InputValueDefinition]) {
1406+
init(loc: Location? = nil, description: StringValue? = nil, name: Name, directives: [Directive] = [], fields: [InputValueDefinition]) {
14191407
self.loc = loc
14201408
self.description = description
14211409
self.name = name
@@ -1453,12 +1441,12 @@ extension TypeExtensionDefinition : Equatable {
14531441
public final class DirectiveDefinition {
14541442
public let kind: Kind = .directiveDefinition
14551443
public let loc: Location?
1456-
public let description: String?
1444+
public let description: StringValue?
14571445
public let name: Name
14581446
public let arguments: [InputValueDefinition]
14591447
public let locations: [Name]
14601448

1461-
init(loc: Location? = nil, description: String? = nil, name: Name, arguments: [InputValueDefinition] = [], locations: [Name]) {
1449+
init(loc: Location? = nil, description: StringValue? = nil, name: Name, arguments: [InputValueDefinition] = [], locations: [Name]) {
14621450
self.loc = loc
14631451
self.name = name
14641452
self.description = description

Sources/GraphQL/Language/Lexer.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,33 @@ final class Lexer {
9191
func advance() throws -> Token {
9292
return try advanceFunction(self)
9393
}
94+
95+
/**
96+
* Looks ahead and returns the next non-ignored token, but does not change
97+
* the state of Lexer.
98+
*/
99+
func lookahead() throws -> Token {
100+
var startToken = token
101+
let savedLine = self.line
102+
let savedLineStart = self.lineStart
103+
104+
guard startToken.kind != .eof else { return startToken }
105+
repeat {
106+
startToken = try startToken.next ??
107+
{
108+
startToken.next = try readToken(lexer: self, prev: startToken)
109+
return startToken.next!
110+
}()
111+
} while startToken.kind == .comment
112+
113+
// restore these since both `positionAfterWhitespace` & `readBlockString`
114+
// can potentially modify them and commment for `lookahead` says no lexer modification.
115+
// (the latter is true in the canonical js lexer also and is likely a bug)
116+
self.line = savedLine
117+
self.lineStart = savedLineStart
118+
119+
return startToken
120+
}
94121
}
95122

96123
/**

0 commit comments

Comments
 (0)