Skip to content

Commit 0f3614a

Browse files
committed
back out use of .blockquote as a new lexer token type. Just a .string
1 parent 6fb9d52 commit 0f3614a

File tree

3 files changed

+27
-28
lines changed

3 files changed

+27
-28
lines changed

Sources/GraphQL/Language/AST.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ final public class Token {
5555
case int = "Int"
5656
case float = "Float"
5757
case string = "String"
58-
case blockString = "BlockString"
5958
case comment = "Comment"
6059

6160
public var description: String {

Sources/GraphQL/Language/Lexer.swift

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -547,9 +547,9 @@ func readDigits(source: Source, start: Int, firstCode: UInt8) throws -> Int {
547547
* augmented to support blockstrings """ """ and return `.blockString` token if found.
548548
*/
549549
func readString(source: Source, start: Int, line: Int, col: Int, prev: Token) throws -> Token {
550-
let token = try readRawString(source: source, start: start, line: line, col: col, prev: prev)
550+
let (token, isBlockString) = try readRawString(source: source, start: start, line: line, col: col, prev: prev)
551551

552-
if token.kind == .blockString,
552+
if isBlockString,
553553
let rawString = token.value {
554554
let valueString = blockStringValue(rawValue: rawString)
555555
return Token(kind: token.kind,
@@ -569,9 +569,9 @@ func readString(source: Source, start: Int, line: Int, col: Int, prev: Token) th
569569
* Doesn't do any clean up of leading indentations or trailing whitespace for blockstring lines;
570570
* so if `token.kind` == `.blockString`, call `blockStringValue` with `token.value` for that.
571571
*
572-
* returns: Token of kind `.string` or `.blockString`
572+
* returns: tuple of Token of kind `.string and Bool of true if it was a block string or not
573573
*/
574-
func readRawString(source: Source, start: Int, line: Int, col: Int, prev: Token) throws -> Token {
574+
func readRawString(source: Source, start: Int, line: Int, col: Int, prev: Token) throws -> (token: Token, isBlockString: Bool) {
575575
let body = source.body
576576
var positionIndex = body.utf8.index(body.utf8.startIndex, offsetBy: start + 1)
577577
var chunkStartIndex = positionIndex
@@ -586,7 +586,7 @@ func readRawString(source: Source, start: Int, line: Int, col: Int, prev: Token)
586586
body.charCode(at: body.utf8.index(after: positionIndex)) == 34 {
587587
blockString = true
588588
positionIndex = body.utf8.index(positionIndex, offsetBy: 2)
589-
589+
590590
// if the first character after the """ is a newline, then it is not included in the value
591591
if let code = body.charCode(at: positionIndex),
592592
(code == 0x000A || code == 0x000D) {
@@ -705,14 +705,15 @@ func readRawString(source: Source, start: Int, line: Int, col: Int, prev: Token)
705705
} else {
706706
value += String(body.utf8[chunkStartIndex ..< positionIndex])!
707707
}
708-
709-
return Token(kind: blockString ? .blockString : .string,
710-
start: start,
711-
end: body.offset(of: positionIndex) + 1,
712-
line: line,
713-
column: col,
714-
value: value,
715-
prev: prev)
708+
709+
return (token: Token(kind: .string,
710+
start: start,
711+
end: body.offset(of: positionIndex) + 1,
712+
line: line,
713+
column: col,
714+
value: value,
715+
prev: prev),
716+
isBlockString: blockString)
716717
}
717718

718719
/**

Tests/GraphQLTests/LanguageTests/LexerTests.swift

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ class LexerTests : XCTestCase {
758758
"""
759759
"""#
760760

761-
let expected = Token(kind: .blockString,
761+
let expected = Token(kind: .string,
762762
start: 0,
763763
end: 66,
764764
line: 1,
@@ -767,14 +767,13 @@ class LexerTests : XCTestCase {
767767
prev: nil, next: nil)
768768

769769
let source = Source(body: sourceStr, name: "TestSource")
770-
let token = try readRawString(source: source,
771-
start: 0,
772-
line: 1,
773-
col: 1,
774-
prev: Token(kind: .sof, start: 0, end: 0, line: 1, column: 1))
775-
770+
let (token, isBlockString) = try readRawString(source: source,
771+
start: 0,
772+
line: 1,
773+
col: 1,
774+
prev: Token(kind: .sof, start: 0, end: 0, line: 1, column: 1))
775+
XCTAssert(isBlockString)
776776
XCTAssertEqual(token, expected, "\n\(dump(expected))\n\(dump(token))\n")
777-
print(String(describing: token.value))
778777
}
779778

780779
func testBlockStringIndentationAndBlankLine() throws {
@@ -803,7 +802,7 @@ class LexerTests : XCTestCase {
803802
func testMultiLineStrings() throws {
804803
let token = try lexOne(#" """ Multi-line string\n With Inner "foo" \nshould be Valid """ "#)
805804
let expected = Token(
806-
kind: .blockString,
805+
kind: .string,
807806
start: 1,
808807
end: 63,
809808
line: 1,
@@ -817,7 +816,7 @@ class LexerTests : XCTestCase {
817816
func testMultiLineStringsSingleSpaceIndent() throws {
818817
let token = try lexOne(#" """ Multi-line string\n With Inner "foo" \n should be Valid """ "#)
819818
let expected = Token(
820-
kind: .blockString,
819+
kind: .string,
821820
start: 1,
822821
end: 64,
823822
line: 1,
@@ -838,7 +837,7 @@ class LexerTests : XCTestCase {
838837
"""#)
839838

840839
let expected = Token(
841-
kind: .blockString,
840+
kind: .string,
842841
start: 0,
843842
end: 59,
844843
line: 1,
@@ -860,7 +859,7 @@ class LexerTests : XCTestCase {
860859
"""#)
861860

862861
let expected = Token(
863-
kind: .blockString,
862+
kind: .string,
864863
start: 0,
865864
end: 79,
866865
line: 1,
@@ -881,7 +880,7 @@ class LexerTests : XCTestCase {
881880

882881
func testEmptySimpleMultilineBlockQuote() throws {
883882
let token = try lexOne(#" """""" "#)
884-
let expected = Token(kind: .blockString, start: 1, end: 7, line: 1, column: 2, value: "")
883+
let expected = Token(kind: .string, start: 1, end: 7, line: 1, column: 2, value: "")
885884
XCTAssertEqual(token, expected, "\n\(dump(expected))\n\(dump(token))\n")
886885
}
887886

@@ -890,7 +889,7 @@ class LexerTests : XCTestCase {
890889
"""
891890
"""
892891
"""#)
893-
let expected = Token(kind: .blockString, start: 0, end: 7, line: 1, column: 1, value: "")
892+
let expected = Token(kind: .string, start: 0, end: 7, line: 1, column: 1, value: "")
894893
XCTAssertEqual(token, expected, "\n\(dump(expected))\n\(dump(token))\n")
895894
}
896895
}

0 commit comments

Comments
 (0)