Skip to content

Commit 3971842

Browse files
authored
Merge pull request #591 from rintaro/rawtokenkind-misc
Add some conventient methods to RawTokenKind
2 parents 59c93fc + b2fa7e6 commit 3971842

File tree

5 files changed

+508
-3
lines changed

5 files changed

+508
-3
lines changed

Sources/SwiftSyntax/SyntaxArena.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ public class SyntaxArena {
143143
///
144144
/// "managed" means it's empty, a part of "source buffer", or in the memory
145145
/// allocated by the underlying arena.
146-
func contains(text: SyntaxText) -> Bool {
146+
@_spi(RawSyntax)
147+
public func contains(text: SyntaxText) -> Bool {
147148
return (text.isEmpty ||
148149
sourceBufferContains(text.baseAddress!) ||
149150
allocator.contains(address: text.baseAddress!))

Sources/SwiftSyntax/TokenKind.swift.gyb

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,64 @@ public enum RawTokenKind {
166166
% if token.text:
167167
case .${token.swift_kind()}: return "${token.text}"
168168
% end
169+
% end
170+
default: return nil
171+
}
172+
}
173+
174+
/// Returns `true` if the token is a Swift keyword.
175+
///
176+
/// Keywords are reserved unconditionally for use by Swift and may not
177+
/// appear as identifiers in any position without being escaped. For example,
178+
/// `class`, `func`, or `import`.
179+
public var isKeyword: Bool {
180+
switch self {
181+
case .eof: return false
182+
% for token in SYNTAX_TOKENS:
183+
% if token.is_keyword:
184+
case .${token.swift_kind()}: return true
185+
% else:
186+
case .${token.swift_kind()}: return false
187+
% end
188+
% end
189+
}
190+
}
191+
192+
/// Returns `true` if the token is a Swift punctuator.
193+
///
194+
/// Punctuation tokens generally separate identifiers from each other. For
195+
/// example, the '<' and '>' characters in a generic parameter list, or the
196+
/// quote characters in a string literal.
197+
public var isPunctuation: Bool {
198+
switch self {
199+
case .eof: return false
200+
% for token in SYNTAX_TOKENS:
201+
% if type(token).__name__ == 'Punctuator':
202+
case .${token.swift_kind()}: return true
203+
% else:
204+
case .${token.swift_kind()}: return false
205+
% end
206+
% end
207+
}
208+
}
209+
210+
@_spi(RawSyntax)
211+
public init?(keyword text: SyntaxText) {
212+
%{
213+
tokens_by_length = {}
214+
for token in SYNTAX_TOKENS:
215+
if token.is_keyword:
216+
tokens_by_length.setdefault(len(token.text), []).append(token)
217+
}%
218+
switch text.count {
219+
% for len, tokens in sorted(tokens_by_length.items()):
220+
case ${len}:
221+
switch text {
222+
% for token in tokens:
223+
case "${token.text}": self = .${token.swift_kind()}
224+
% end
225+
default: return nil
226+
}
169227
% end
170228
default: return nil
171229
}

Sources/SwiftSyntax/Trivia.swift.gyb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ extension TriviaPiece {
236236
/// In contrast to `TriviaPiece`, a `RawTriviaPiece` does not own the source
237237
/// text of a the trivia.
238238
@_spi(RawSyntax)
239-
public enum RawTriviaPiece {
239+
public enum RawTriviaPiece: Equatable {
240240
% for trivia in TRIVIAS:
241241
% if trivia.is_collection():
242242
case ${trivia.lower_name}s(Int)
@@ -329,3 +329,17 @@ extension RawTriviaPiece {
329329
}
330330
}
331331
}
332+
333+
extension RawTriviaPiece {
334+
/// Returns true if the trivia is `.newlines`, `.carriageReturns` or `.carriageReturnLineFeeds`
335+
public var isNewline: Bool {
336+
switch self {
337+
case .newlines,
338+
.carriageReturns,
339+
.carriageReturnLineFeeds:
340+
return true
341+
default:
342+
return false
343+
}
344+
}
345+
}

0 commit comments

Comments
 (0)