Skip to content

Add some conventient methods to RawTokenKind #591

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
Aug 16, 2022
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
3 changes: 2 additions & 1 deletion Sources/SwiftSyntax/SyntaxArena.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ public class SyntaxArena {
///
/// "managed" means it's empty, a part of "source buffer", or in the memory
/// allocated by the underlying arena.
func contains(text: SyntaxText) -> Bool {
@_spi(RawSyntax)
public func contains(text: SyntaxText) -> Bool {
return (text.isEmpty ||
sourceBufferContains(text.baseAddress!) ||
allocator.contains(address: text.baseAddress!))
Expand Down
58 changes: 58 additions & 0 deletions Sources/SwiftSyntax/TokenKind.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,64 @@ public enum RawTokenKind {
% if token.text:
case .${token.swift_kind()}: return "${token.text}"
% end
% end
default: return nil
}
}

/// Returns `true` if the token is a Swift keyword.
///
/// Keywords are reserved unconditionally for use by Swift and may not
/// appear as identifiers in any position without being escaped. For example,
/// `class`, `func`, or `import`.
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
}
}

/// Returns `true` if the token is a Swift punctuator.
///
/// Punctuation tokens generally separate identifiers from each other. For
/// example, the '<' and '>' characters in a generic parameter list, or the
/// quote characters in a string literal.
public var isPunctuation: Bool {
switch self {
case .eof: return false
% for token in SYNTAX_TOKENS:
% if type(token).__name__ == 'Punctuator':
case .${token.swift_kind()}: return true
% else:
case .${token.swift_kind()}: return false
% end
% end
}
}

@_spi(RawSyntax)
public init?(keyword text: SyntaxText) {
%{
tokens_by_length = {}
for token in SYNTAX_TOKENS:
if token.is_keyword:
tokens_by_length.setdefault(len(token.text), []).append(token)
}%
switch text.count {
% for len, tokens in sorted(tokens_by_length.items()):
case ${len}:
switch text {
% for token in tokens:
case "${token.text}": self = .${token.swift_kind()}
% end
default: return nil
}
% end
default: return nil
}
Expand Down
16 changes: 15 additions & 1 deletion Sources/SwiftSyntax/Trivia.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ extension TriviaPiece {
/// In contrast to `TriviaPiece`, a `RawTriviaPiece` does not own the source
/// text of a the trivia.
@_spi(RawSyntax)
public enum RawTriviaPiece {
public enum RawTriviaPiece: Equatable {
% for trivia in TRIVIAS:
% if trivia.is_collection():
case ${trivia.lower_name}s(Int)
Expand Down Expand Up @@ -329,3 +329,17 @@ extension RawTriviaPiece {
}
}
}

extension RawTriviaPiece {
/// Returns true if the trivia is `.newlines`, `.carriageReturns` or `.carriageReturnLineFeeds`
public var isNewline: Bool {
switch self {
case .newlines,
.carriageReturns,
.carriageReturnLineFeeds:
return true
default:
return false
}
}
}
Loading