Skip to content

Allow TokenKind to check whether it's a keyword. #10

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

Merged
merged 1 commit into from
Sep 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Sources/SwiftSyntax/TokenKind.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ public enum TokenKind: Codable {
}
}

public var isKeyword: Bool {
switch self {
case .eof: return false
% for token in SYNTAX_TOKENS:
% if token.is_keyword:
case .${token.swift_kind()}: return true
% else:
case .${token.swift_kind()}: return false
% end
% end
}
}

/// Keys for serializing and deserializing token kinds.
enum CodingKeys: String, CodingKey {
case kind, text
Expand Down
11 changes: 11 additions & 0 deletions Tests/SwiftSyntaxTest/TokenTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import XCTest
import SwiftSyntax

public class TokenSyntaxTestCase: XCTestCase {
public func testKeywordKinds() {
XCTAssertTrue(TokenKind.operatorKeyword.isKeyword)
XCTAssertTrue(TokenKind.funcKeyword.isKeyword)
XCTAssertFalse(TokenKind.leftAngle.isKeyword)
XCTAssertFalse(TokenKind.rightAngle.isKeyword)
}
}