15#include "llvm/ADT/StringExtras.h"
16#include "llvm/Support/ErrorHandling.h"
24SMLoc
Token::getLoc()
const {
return SMLoc::getFromPointer(spelling.data()); }
27 return SMLoc::getFromPointer(spelling.data() + spelling.size());
35 bool isHex = spelling.size() > 1 && spelling[1] ==
'x';
38 if (spelling.getAsInteger(isHex ? 0 : 10,
result))
46 bool isHex = spelling.size() > 1 && spelling[1] ==
'x';
49 if (spelling.getAsInteger(isHex ? 0 : 10,
result))
57 unsigned bitwidthStart = (spelling[0] ==
'i' ? 1 : 2);
59 if (spelling.drop_front(bitwidthStart).getAsInteger(10,
result))
66 if (spelling[0] ==
'i')
68 if (spelling[0] ==
's')
70 assert(spelling[0] ==
'u');
82 if (
getKind() != Token::code_complete) {
83 bytes = bytes.drop_back();
85 bytes = bytes.drop_front();
89 result.reserve(bytes.size());
90 for (
unsigned i = 0, e = bytes.size(); i != e;) {
97 assert(i + 1 <= e &&
"invalid string should be caught by lexer");
114 assert(i + 1 <= e &&
"invalid string should be caught by lexer");
115 auto c2 = bytes[i++];
117 assert(llvm::isHexDigit(c1) && llvm::isHexDigit(c2) &&
"invalid escape");
118 result.push_back((llvm::hexDigitValue(c1) << 4) | llvm::hexDigitValue(c2));
130 StringRef bytes =
getSpelling().drop_front().drop_back();
136 if (!bytes.consume_front(
"0x") || (bytes.size() & 1) ||
137 !llvm::tryGetFromHex(bytes, hex))
145 assert(
is(Token::at_identifier) &&
"expected valid @-identifier");
149 if (nameStr.front() ==
'"')
151 return std::string(nameStr);
158 assert(
getKind() == hash_identifier);
160 if (spelling.drop_front().getAsInteger(10,
result))
171 llvm_unreachable(
"This token kind has no fixed spelling");
172#define TOK_PUNCTUATION(NAME, SPELLING) \
175#define TOK_KEYWORD(SPELLING) \
176 case kw_##SPELLING: \
178#include "TokenKinds.def"
187#define TOK_KEYWORD(SPELLING) \
188 case kw_##SPELLING: \
190#include "TokenKinds.def"
199 return spelling[0] ==
'"';
200 case Kind::hash_identifier:
201 return spelling[0] ==
'#';
202 case Kind::percent_identifier:
203 return spelling[0] ==
'%';
204 case Kind::caret_identifier:
205 return spelling[0] ==
'^';
206 case Kind::exclamation_identifier:
207 return spelling[0] ==
'!';
bool isCodeCompletionFor(Kind kind) const
Returns true if the current token represents a code completion for the "normal" token type.
SMRange getLocRange() const
bool isKeyword() const
Return true if this is one of the keyword token kinds (e.g. kw_if).
static StringRef getTokenSpelling(Kind kind)
Given a punctuation or keyword token kind, return the spelling of the token as a string.
std::optional< uint64_t > getUInt64IntegerValue() const
std::string getStringValue() const
Given a token containing a string literal, return its value, including removing the quote characters ...
std::string getSymbolReference() const
Given a token containing a symbol reference, return the unescaped string value.
std::optional< unsigned > getUnsignedIntegerValue() const
For an integer token, return its value as an unsigned.
std::optional< unsigned > getHashIdentifierNumber() const
Given a hash_identifier token like #123, try to parse the number out of the identifier,...
bool isCodeCompletion() const
Returns true if the current token represents a code completion.
StringRef getSpelling() const
std::optional< bool > getIntTypeSignedness() const
For an inttype token, return its signedness semantics: std::nullopt means no signedness semantics; tr...
std::optional< unsigned > getIntTypeBitwidth() const
For an inttype token, return its bitwidth.
std::optional< std::string > getHexStringValue() const
Given a token containing a hex string literal, return its value or std::nullopt if the token does not...
Include the generated interface declarations.