MLIR 24.0.0git
Token.cpp
Go to the documentation of this file.
1//===- Token.cpp - MLIR Token Implementation ------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the Token class for the MLIR textual form.
10//
11//===----------------------------------------------------------------------===//
12
13#include "Token.h"
14#include "mlir/Support/LLVM.h"
15#include "llvm/ADT/StringExtras.h"
16#include "llvm/Support/ErrorHandling.h"
17#include <cassert>
18#include <cstdint>
19#include <optional>
20#include <string>
21
22using namespace mlir;
23
24SMLoc Token::getLoc() const { return SMLoc::getFromPointer(spelling.data()); }
25
26SMLoc Token::getEndLoc() const {
27 return SMLoc::getFromPointer(spelling.data() + spelling.size());
28}
29
30SMRange Token::getLocRange() const { return SMRange(getLoc(), getEndLoc()); }
31
32/// For an integer token, return its value as an unsigned. If it doesn't fit,
33/// return std::nullopt.
34std::optional<unsigned> Token::getUnsignedIntegerValue() const {
35 bool isHex = spelling.size() > 1 && spelling[1] == 'x';
36
37 unsigned result = 0;
38 if (spelling.getAsInteger(isHex ? 0 : 10, result))
39 return std::nullopt;
40 return result;
41}
42
43/// For an integer token, return its value as a uint64_t. If it doesn't fit,
44/// return std::nullopt.
45std::optional<uint64_t> Token::getUInt64IntegerValue(StringRef spelling) {
46 bool isHex = spelling.size() > 1 && spelling[1] == 'x';
47
48 uint64_t result = 0;
49 if (spelling.getAsInteger(isHex ? 0 : 10, result))
50 return std::nullopt;
51 return result;
52}
53
54/// For an inttype token, return its bitwidth.
55std::optional<unsigned> Token::getIntTypeBitwidth() const {
56 assert(getKind() == inttype);
57 unsigned bitwidthStart = (spelling[0] == 'i' ? 1 : 2);
58 unsigned result = 0;
59 if (spelling.drop_front(bitwidthStart).getAsInteger(10, result))
60 return std::nullopt;
61 return result;
62}
63
64std::optional<bool> Token::getIntTypeSignedness() const {
65 assert(getKind() == inttype);
66 if (spelling[0] == 'i')
67 return std::nullopt;
68 if (spelling[0] == 's')
69 return true;
70 assert(spelling[0] == 'u');
71 return false;
72}
73
74/// Given a token containing a string literal, return its value, including
75/// removing the quote characters and unescaping the contents of the string. The
76/// lexer has already verified that this token is valid.
77std::string Token::getStringValue() const {
78 assert(getKind() == string || getKind() == code_complete ||
79 (getKind() == at_identifier && getSpelling()[1] == '"'));
80 // Start by dropping the quotes.
81 StringRef bytes = getSpelling().drop_front();
82 if (getKind() != Token::code_complete) {
83 bytes = bytes.drop_back();
84 if (getKind() == at_identifier)
85 bytes = bytes.drop_front();
86 }
87
88 std::string result;
89 result.reserve(bytes.size());
90 for (unsigned i = 0, e = bytes.size(); i != e;) {
91 auto c = bytes[i++];
92 if (c != '\\') {
93 result.push_back(c);
94 continue;
95 }
96
97 assert(i + 1 <= e && "invalid string should be caught by lexer");
98 auto c1 = bytes[i++];
99 switch (c1) {
100 case '"':
101 case '\\':
102 result.push_back(c1);
103 continue;
104 case 'n':
105 result.push_back('\n');
106 continue;
107 case 't':
108 result.push_back('\t');
109 continue;
110 default:
111 break;
112 }
113
114 assert(i + 1 <= e && "invalid string should be caught by lexer");
115 auto c2 = bytes[i++];
116
117 assert(llvm::isHexDigit(c1) && llvm::isHexDigit(c2) && "invalid escape");
118 result.push_back((llvm::hexDigitValue(c1) << 4) | llvm::hexDigitValue(c2));
119 }
120
121 return result;
122}
123
124/// Given a token containing a hex string literal, return its value or
125/// std::nullopt if the token does not contain a valid hex string.
126std::optional<std::string> Token::getHexStringValue() const {
127 assert(getKind() == string);
128
129 // Get the internal string data, without the quotes.
130 StringRef bytes = getSpelling().drop_front().drop_back();
131
132 // Try to extract the binary data from the hex string. We expect the hex
133 // string to start with `0x` and have an even number of hex nibbles (nibbles
134 // should come in pairs).
135 std::string hex;
136 if (!bytes.consume_front("0x") || (bytes.size() & 1) ||
137 !llvm::tryGetFromHex(bytes, hex))
138 return std::nullopt;
139 return hex;
140}
141
142/// Given a token containing a symbol reference, return the unescaped string
143/// value.
144std::string Token::getSymbolReference() const {
145 assert(is(Token::at_identifier) && "expected valid @-identifier");
146 StringRef nameStr = getSpelling().drop_front();
147
148 // Check to see if the reference is a string literal, or a bare identifier.
149 if (nameStr.front() == '"')
150 return getStringValue();
151 return std::string(nameStr);
152}
153
154/// Given a hash_identifier token like #123, try to parse the number out of
155/// the identifier, returning std::nullopt if it is a named identifier like #x
156/// or if the integer doesn't fit.
157std::optional<unsigned> Token::getHashIdentifierNumber() const {
158 assert(getKind() == hash_identifier);
159 unsigned result = 0;
160 if (spelling.drop_front().getAsInteger(10, result))
161 return std::nullopt;
162 return result;
163}
164
165/// Given a punctuation or keyword token kind, return the spelling of the
166/// token as a string. Warning: This will abort on markers, identifiers and
167/// literal tokens since they have no fixed spelling.
169 switch (kind) {
170 default:
171 llvm_unreachable("This token kind has no fixed spelling");
172#define TOK_PUNCTUATION(NAME, SPELLING) \
173 case NAME: \
174 return SPELLING;
175#define TOK_KEYWORD(SPELLING) \
176 case kw_##SPELLING: \
177 return #SPELLING;
178#include "TokenKinds.def"
179 }
180}
181
182/// Return true if this is one of the keyword token kinds (e.g. kw_if).
183bool Token::isKeyword() const {
184 switch (kind) {
185 default:
186 return false;
187#define TOK_KEYWORD(SPELLING) \
188 case kw_##SPELLING: \
189 return true;
190#include "TokenKinds.def"
191 }
192}
193
195 if (!isCodeCompletion() || spelling.empty())
196 return false;
197 switch (kind) {
198 case Kind::string:
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] == '!';
208 default:
209 return false;
210 }
211}
bool isCodeCompletionFor(Kind kind) const
Returns true if the current token represents a code completion for the "normal" token type.
Definition Token.cpp:194
SMRange getLocRange() const
Definition Token.cpp:30
bool isKeyword() const
Return true if this is one of the keyword token kinds (e.g. kw_if).
Definition Token.cpp:183
static StringRef getTokenSpelling(Kind kind)
Given a punctuation or keyword token kind, return the spelling of the token as a string.
Definition Token.cpp:168
SMLoc getLoc() const
Definition Token.cpp:24
bool is(Kind k) const
Definition Token.h:38
std::optional< uint64_t > getUInt64IntegerValue() const
Definition Token.h:83
std::string getStringValue() const
Given a token containing a string literal, return its value, including removing the quote characters ...
Definition Token.cpp:77
std::string getSymbolReference() const
Given a token containing a symbol reference, return the unescaped string value.
Definition Token.cpp:144
std::optional< unsigned > getUnsignedIntegerValue() const
For an integer token, return its value as an unsigned.
Definition Token.cpp:34
Kind getKind() const
Definition Token.h:37
SMLoc getEndLoc() const
Definition Token.cpp:26
std::optional< unsigned > getHashIdentifierNumber() const
Given a hash_identifier token like #123, try to parse the number out of the identifier,...
Definition Token.cpp:157
bool isCodeCompletion() const
Returns true if the current token represents a code completion.
Definition Token.h:62
StringRef getSpelling() const
Definition Token.h:34
std::optional< bool > getIntTypeSignedness() const
For an inttype token, return its signedness semantics: std::nullopt means no signedness semantics; tr...
Definition Token.cpp:64
std::optional< unsigned > getIntTypeBitwidth() const
For an inttype token, return its bitwidth.
Definition Token.cpp:55
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...
Definition Token.cpp:126
Include the generated interface declarations.