15 #include "llvm/ADT/StringExtras.h"
16 #include "llvm/Support/ErrorHandling.h"
24 SMLoc
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))
58 if (spelling.getAsDouble(result))
66 unsigned bitwidthStart = (spelling[0] ==
'i' ? 1 : 2);
68 if (spelling.drop_front(bitwidthStart).getAsInteger(10, result))
75 if (spelling[0] ==
'i')
77 if (spelling[0] ==
's')
79 assert(spelling[0] ==
'u');
91 if (
getKind() != Token::code_complete) {
92 bytes = bytes.drop_back();
94 bytes = bytes.drop_front();
98 result.reserve(bytes.size());
99 for (
unsigned i = 0, e = bytes.size(); i != e;) {
106 assert(i + 1 <= e &&
"invalid string should be caught by lexer");
107 auto c1 = bytes[i++];
111 result.push_back(c1);
114 result.push_back(
'\n');
117 result.push_back(
'\t');
123 assert(i + 1 <= e &&
"invalid string should be caught by lexer");
124 auto c2 = bytes[i++];
126 assert(llvm::isHexDigit(c1) && llvm::isHexDigit(c2) &&
"invalid escape");
127 result.push_back((llvm::hexDigitValue(c1) << 4) | llvm::hexDigitValue(c2));
139 StringRef bytes =
getSpelling().drop_front().drop_back();
145 if (!bytes.consume_front(
"0x") || (bytes.size() & 1) ||
146 !llvm::tryGetFromHex(bytes, hex))
154 assert(
is(Token::at_identifier) &&
"expected valid @-identifier");
158 if (nameStr.front() ==
'"')
160 return std::string(nameStr);
167 assert(
getKind() == hash_identifier);
169 if (spelling.drop_front().getAsInteger(10, result))
180 llvm_unreachable(
"This token kind has no fixed spelling");
181 #define TOK_PUNCTUATION(NAME, SPELLING) \
184 #define TOK_KEYWORD(SPELLING) \
185 case kw_##SPELLING: \
187 #include "TokenKinds.def"
196 #define TOK_KEYWORD(SPELLING) \
197 case kw_##SPELLING: \
199 #include "TokenKinds.def"
208 return spelling[0] ==
'"';
209 case Kind::hash_identifier:
210 return spelling[0] ==
'#';
211 case Kind::percent_identifier:
212 return spelling[0] ==
'%';
213 case Kind::caret_identifier:
214 return spelling[0] ==
'^';
215 case Kind::exclamation_identifier:
216 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::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< uint64_t > getUInt64IntegerValue() const
std::optional< double > getFloatingPointValue() const
For a floatliteral token, return its value as a double.
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.