Skip to content

Commit

Permalink
libnixf: line column range (#296)
Browse files Browse the repository at this point in the history
  • Loading branch information
inclyc authored Jan 14, 2024
2 parents fe3fff3 + 9f0a0b1 commit 218e282
Show file tree
Hide file tree
Showing 10 changed files with 372 additions and 269 deletions.
35 changes: 16 additions & 19 deletions libnixf/include/nixf/Basic/Diagnostic.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,20 @@ namespace nixf {
/// 1. Insertions: special `OldRange` that `Begin` == `End`.
/// 2. Removals: empty `NewText`.
class Fix {
OffsetRange OldRange;
RangeTy OldRange;
std::string NewText;

public:
Fix(OffsetRange OldRange, std::string NewText)
Fix(RangeTy OldRange, std::string NewText)
: OldRange(OldRange), NewText(std::move(NewText)) {
assert(OldRange.Begin != OldRange.End || !this->NewText.empty());
assert(OldRange.begin() != OldRange.end() || !this->NewText.empty());
}

static Fix mkInsertion(const char *Loc, std::string NewText) {
return {{Loc, Loc}, std::move(NewText)};
static Fix mkInsertion(Point P, std::string NewText) {
return {{P, P}, std::move(NewText)};
}

static Fix mkRemoval(OffsetRange RemovingRange) {
return {RemovingRange, ""};
}
static Fix mkRemoval(RangeTy RemovingRange) { return {RemovingRange, ""}; }

[[nodiscard]] bool isReplace() const {
return !isRemoval() && !isInsertion();
Expand All @@ -47,10 +45,10 @@ class Fix {
[[nodiscard]] bool isRemoval() const { return NewText.empty(); }

[[nodiscard]] bool isInsertion() const {
return OldRange.Begin == OldRange.End;
return OldRange.begin() == OldRange.end();
}

[[nodiscard]] OffsetRange oldRange() const { return OldRange; }
[[nodiscard]] RangeTy oldRange() const { return OldRange; }
[[nodiscard]] std::string_view newText() const { return NewText; }
};

Expand All @@ -70,7 +68,7 @@ class PartialDiagnostic {
protected:
std::vector<std::string> Args;
/// Location of this diagnostic
OffsetRange Range;
RangeTy Range;
};

class Note : public PartialDiagnostic {
Expand All @@ -82,7 +80,7 @@ class Note : public PartialDiagnostic {
#undef DIAG_NOTE
};

Note(NoteKind Kind, OffsetRange Range) : Kind(Kind), Range(Range) {}
Note(NoteKind Kind, RangeTy Range) : Kind(Kind), Range(Range) {}

template <class T> PartialDiagnostic &operator<<(const T &Var) {
Args.push_back(Var);
Expand All @@ -95,11 +93,11 @@ class Note : public PartialDiagnostic {

[[nodiscard]] const char *message() const override { return message(kind()); }

OffsetRange range() const { return Range; }
RangeTy range() const { return Range; }

private:
NoteKind Kind;
OffsetRange Range;
RangeTy Range;
};

/// The super class for all diagnostics.
Expand All @@ -123,8 +121,7 @@ class Diagnostic : public PartialDiagnostic {
#undef DIAG
};

Diagnostic(DiagnosticKind Kind, OffsetRange Range)
: Kind(Kind), Range(Range) {}
Diagnostic(DiagnosticKind Kind, RangeTy Range) : Kind(Kind), Range(Range) {}

[[nodiscard]] DiagnosticKind kind() const { return Kind; };

Expand All @@ -143,7 +140,7 @@ class Diagnostic : public PartialDiagnostic {

[[nodiscard]] virtual const char *sname() const { return sname(kind()); }

Note &note(Note::NoteKind Kind, OffsetRange Range) {
Note &note(Note::NoteKind Kind, RangeTy Range) {
return *Notes.emplace_back(std::make_unique<Note>(Kind, Range));
}

Expand All @@ -156,12 +153,12 @@ class Diagnostic : public PartialDiagnostic {

const std::vector<Fix> &fixes() { return Fixes; }

OffsetRange range() const { return Range; }
[[nodiscard]] RangeTy range() const { return Range; }

private:
DiagnosticKind Kind;
/// Location of this diagnostic
OffsetRange Range;
RangeTy Range;

std::vector<std::unique_ptr<Note>> Notes;
std::vector<Fix> Fixes;
Expand Down
2 changes: 1 addition & 1 deletion libnixf/include/nixf/Basic/DiagnosticEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace nixf {

class DiagnosticEngine {
public:
Diagnostic &diag(Diagnostic::DiagnosticKind Kind, OffsetRange Range) {
Diagnostic &diag(Diagnostic::DiagnosticKind Kind, RangeTy Range) {
auto Diag = std::make_unique<Diagnostic>(Kind, Range);
Diags.emplace_back(std::move(Diag));
return *Diags.back();
Expand Down
57 changes: 49 additions & 8 deletions libnixf/include/nixf/Basic/Range.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,57 @@

namespace nixf {

struct OffsetRange {
const char *Begin;
const char *End;
/// \brief A point in the source file.
///
/// This class is used to represent a point in the source file. And it shall be
/// constructed by Lexer, to keep Line & Column information correct.
/// \see Lexer::consume(std::size_t)
class Point {
int64_t Line;
int64_t Column;
std::size_t Offset;
friend class Lexer;
Point(int64_t Line, int64_t Column, std::size_t Offset)
: Line(Line), Column(Column), Offset(Offset) {}

OffsetRange() = default;
public:
friend bool operator==(const Point &LHS, const Point &RHS) {
return LHS.Line == RHS.Line && LHS.Column == RHS.Column &&
LHS.Offset == RHS.Offset;
}
Point() = default;

OffsetRange(const char *Begin, const char *End) : Begin(Begin), End(End) {}
explicit OffsetRange(const char *Pos) : Begin(Pos), End(Pos) {}
operator std::string_view() const { return {Begin, End}; }
[[nodiscard]] std::string_view view() const { return {Begin, End}; }
/// \brief Check if the point is at the given position.
[[nodiscard]] bool isAt(int64_t Line, int64_t Column,
std::size_t Offset) const {
return this->Line == Line && this->Column == Column &&
this->Offset == Offset;
}

/// \brief Line number, starting from 0.
///
/// Currently we only accept LF as the line terminator.
[[nodiscard]] int64_t line() const { return Line; }

/// \brief Column number, starting from 0.
[[nodiscard]] int64_t column() const { return Column; }

/// \brief Offset in the source file, starting from 0.
[[nodiscard]] std::size_t offset() const { return Offset; }
};

class RangeTy {
Point Begin;
Point End;

public:
RangeTy() = default;

RangeTy(Point Begin, Point End) : Begin(Begin), End(End) {}
explicit RangeTy(Point Pos) : Begin(Pos), End(Pos) {}

[[nodiscard]] Point begin() const { return Begin; }
[[nodiscard]] Point end() const { return End; }
};

} // namespace nixf
22 changes: 11 additions & 11 deletions libnixf/include/nixf/Parse/Nodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,21 @@ class Node {

private:
NodeKind Kind;
OffsetRange Range;
RangeTy Range;

protected:
explicit Node(NodeKind Kind, OffsetRange Range) : Kind(Kind), Range(Range) {}
explicit Node(NodeKind Kind, RangeTy Range) : Kind(Kind), Range(Range) {}

public:
[[nodiscard]] NodeKind kind() const { return Kind; }
[[nodiscard]] OffsetRange range() const { return Range; }
[[nodiscard]] const char *begin() const { return Range.Begin; }
[[nodiscard]] const char *end() const { return Range.End; }
[[nodiscard]] RangeTy range() const { return Range; }
[[nodiscard]] Point begin() const { return Range.begin(); }
[[nodiscard]] Point end() const { return Range.end(); }
};

class Expr : public Node {
protected:
explicit Expr(NodeKind Kind, OffsetRange Range) : Node(Kind, Range) {
explicit Expr(NodeKind Kind, RangeTy Range) : Node(Kind, Range) {
assert(NK_BeginExpr <= Kind && Kind <= NK_EndExpr);
}
};
Expand All @@ -57,7 +57,7 @@ class ExprInt : public Expr {
NixInt Value;

public:
ExprInt(OffsetRange Range, NixInt Value)
ExprInt(RangeTy Range, NixInt Value)
: Expr(NK_ExprInt, Range), Value(Value) {}
[[nodiscard]] NixInt value() const { return Value; }
};
Expand All @@ -66,7 +66,7 @@ class ExprFloat : public Expr {
NixFloat Value;

public:
ExprFloat(OffsetRange Range, NixFloat Value)
ExprFloat(RangeTy Range, NixFloat Value)
: Expr(NK_ExprFloat, Range), Value(Value) {}
[[nodiscard]] NixFloat value() const { return Value; }
};
Expand Down Expand Up @@ -98,7 +98,7 @@ class InterpolatedParts : public Node {
std::vector<InterpolablePart> Fragments;

public:
InterpolatedParts(OffsetRange Range, std::vector<InterpolablePart> Fragments)
InterpolatedParts(RangeTy Range, std::vector<InterpolablePart> Fragments)
: Node(NK_InterpolableParts, Range), Fragments(std::move(Fragments)) {}

[[nodiscard]] const std::vector<InterpolablePart> &fragments() const {
Expand All @@ -110,7 +110,7 @@ class ExprString : public Expr {
std::shared_ptr<InterpolatedParts> Parts;

public:
ExprString(OffsetRange Range, std::shared_ptr<InterpolatedParts> Parts)
ExprString(RangeTy Range, std::shared_ptr<InterpolatedParts> Parts)
: Expr(NK_ExprString, Range), Parts(std::move(Parts)) {}

[[nodiscard]] const std::shared_ptr<InterpolatedParts> &parts() const {
Expand All @@ -122,7 +122,7 @@ class ExprPath : public Expr {
std::shared_ptr<InterpolatedParts> Parts;

public:
ExprPath(OffsetRange Range, std::shared_ptr<InterpolatedParts> Parts)
ExprPath(RangeTy Range, std::shared_ptr<InterpolatedParts> Parts)
: Expr(NK_ExprPath, Range), Parts(std::move(Parts)) {}

[[nodiscard]] const std::shared_ptr<InterpolatedParts> &parts() const {
Expand Down
Loading

0 comments on commit 218e282

Please sign in to comment.