It's not quite the same, but swift -frontend -emit-syntax dumps a JSON representation of the syntax tree, and you could recursively scrape similar information out of objects that have a tokenKind property. Doesn't look like it has the line/column numbers in there directly though.
What are you trying to do? Can it be done with SwiftSyntax instead?
Right now my only use case was for debugging purposes, just to have a concise listing of the tokens that the parser is actually seeing for a particular input. Not particularly compelling, but thought it was interesting that clang has it as an option with no obvious equivalent for swiftc. Does SwiftSyntax/libSyntax offer something like that?
Nice, thanks @allevato! Just curious—does this solution require the program to successfully parse in order to dump all the tokens, or will it make it through the whole file even if parsing bails out at some point?
As far as I know SyntaxParser.parse always returns a "valid" tree (i.e., doesn't throw an error) even for unparsable inputs; it only throws errors if the syntax parser is incompatible or the root node was an unexpected type. There are various nodes like UnknownStmtSyntax, UnknownDeclSyntax, etc., that will be used in the tree if something couldn't be parsed correctly, but the tokens will still be accessible underneath them.
With print)"nope"( as the input, it still prints the following: