Files
carbon-lang/toolchain/lexer/numeric_literal.h
T
Chandler Carruth a857b7ea1a Cleanup or suppress numerous clang-tidy issues. (#577)
This gets us to a nearly clean state across the toolchain. A couple of
these are checks that I don't think we want to try to rigidly use and
I've disabled them completely. Others I've added relevant `NOLINT` style
suppressions or applied the automatic fix suggested by `clang-tidy`.

The implicit conversions that are allowed here with `NOLINT` are
probably worth at least a tiny bit of scrutiny to see if we could
replace the construct with something more direct without undue effort
and no longer need the implicit conversion. But until then, it seemed
fine to suppress.
2021-06-14 19:46:49 -07:00

76 lines
2.3 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef TOOLCHAIN_LEXER_NUMERIC_LITERAL_H_
#define TOOLCHAIN_LEXER_NUMERIC_LITERAL_H_
#include <utility>
#include <variant>
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/StringRef.h"
#include "toolchain/diagnostics/diagnostic_emitter.h"
namespace Carbon {
// A numeric literal token that has been extracted from a source buffer.
class LexedNumericLiteral {
public:
// Get the text corresponding to this literal.
[[nodiscard]] auto Text() const -> llvm::StringRef { return text; }
// Extract a numeric literal from the given text, if it has a suitable form.
//
// The supplied `source_text` must outlive the return value.
static auto Lex(llvm::StringRef source_text)
-> llvm::Optional<LexedNumericLiteral>;
// Value of an integer literal.
struct IntegerValue {
// An unsigned literal value.
llvm::APInt value;
};
// Value of a real literal.
struct RealValue {
// The radix of the exponent, either 2 or 10.
int radix;
// The mantissa, represented as a variable-width unsigned integer.
llvm::APInt mantissa;
// The exponent, represented as a variable-width signed integer..
llvm::APInt exponent;
};
struct UnrecoverableError {};
using Value = std::variant<IntegerValue, RealValue, UnrecoverableError>;
// Compute the value of the token, if possible. Emit diagnostics to the given
// emitter if the token is not valid.
auto ComputeValue(DiagnosticEmitter<const char*>& emitter) const -> Value;
private:
LexedNumericLiteral() = default;
class Parser;
// The text of the token.
llvm::StringRef text;
// The offset of the '.'. Set to text.size() if none is present.
int radix_point;
// The offset of the alphabetical character introducing the exponent. In a
// valid literal, this will be an 'e' or a 'p', and may be followed by a '+'
// or a '-', but for error recovery, this may simply be the last lowercase
// letter in the invalid token. Always greater than or equal to radix_point.
// Set to text.size() if none is present.
int exponent;
};
} // namespace Carbon
#endif // TOOLCHAIN_LEXER_NUMERIC_LITERAL_H_