mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-26 09:41:17 +00:00
Lexer diagnostics used a single `const char*` as their source location, which only allowed pointing at a single character rather than a run of characters. This limited the underlining of unrecognized tokens, and also made escape sequence diagnostics particularly strange by stopping at whatever point the lexer discovered the error and pointing only at that location. For example: `"\w"` was reported at the `w`, and `"\x"` at the closing `"`. This change tries to always keep the primary location of a diagnostic the point at which typing (inserting or deleting) might fix the error. So for `"\x"`, `\x` is underlined, but the position is at the `"` as that's where you would type a hex code. The lexer's emitter now takes a special lexing source location that represents either a single position _between_ two characters, a single character, or a range of the source buffer. The lexer diagnostics are updated to construct these for the character range as appropriate. Parser diagnostics for a missing token were located at the _next_ token, which is particularly confusing for terminators like missing `;`s. Now they are located at a position after the previous token, with or without a space depending on the token, but typically on the same line. Diagnostics still sort by how far the parser has read. The parser diagnostics for unclosed groups also now attaches a label to the opening token when it is on a separate line. And the diagnostics for mismatched indent in a multi-line string attaches a label to the closing `'''` that establishes that indent. There are likely more location improvements we should do here, but this seemed like a reasonable incremental improvement and is what Claude found when doing an initial audit. One example where I think we should make further improvements is around suggesting raw identifiers when we find an unexpected `Cpp` or `Core` or `base`. We do this consistently now, but raw identifiers should be rare. A TODO is added for these cases. Assisted-by: Claude Code --------- Co-authored-by: Richard Smith <richard@metafoo.co.uk>
83 lines
2.4 KiB
C++
83 lines
2.4 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 CARBON_TOOLCHAIN_LEX_NUMERIC_LITERAL_H_
|
|
#define CARBON_TOOLCHAIN_LEX_NUMERIC_LITERAL_H_
|
|
|
|
#include <optional>
|
|
#include <variant>
|
|
|
|
#include "llvm/ADT/APInt.h"
|
|
#include "llvm/ADT/StringRef.h"
|
|
#include "toolchain/diagnostics/emitter.h"
|
|
#include "toolchain/lex/source_loc.h"
|
|
|
|
namespace Carbon::Lex {
|
|
|
|
// A numeric literal token that has been extracted from a source buffer.
|
|
class NumericLiteral {
|
|
public:
|
|
enum class Radix : int8_t {
|
|
Binary = 2,
|
|
Octal = 8,
|
|
Decimal = 10,
|
|
Hexadecimal = 16
|
|
};
|
|
|
|
// Value of an integer literal.
|
|
struct IntValue {
|
|
// An unsigned literal value.
|
|
llvm::APInt value;
|
|
};
|
|
|
|
// Value of a real literal.
|
|
struct RealValue {
|
|
// The radix of the exponent, either Binary or Decimal.
|
|
Radix 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<IntValue, RealValue, UnrecoverableError>;
|
|
|
|
// 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, bool can_form_real_literal)
|
|
-> std::optional<NumericLiteral>;
|
|
|
|
// Compute the value of the token, if possible. Emit diagnostics to the given
|
|
// emitter if the token is not valid.
|
|
auto ComputeValue(Diagnostics::Emitter<SourceLoc>& emitter) const -> Value;
|
|
|
|
// Get the text corresponding to this literal.
|
|
auto text() const -> llvm::StringRef { return text_; }
|
|
|
|
private:
|
|
class Parser;
|
|
|
|
NumericLiteral() = default;
|
|
|
|
// 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::Lex
|
|
|
|
#endif // CARBON_TOOLCHAIN_LEX_NUMERIC_LITERAL_H_
|