Location and Position

Interface

class Location

Represents the location in a parsed document.

Tested:

LocationTest

Public Functions

Location() = default

Creates an undefined location.

Can be tested with isUndefined().

inline explicit Location(SourceIdentifierPtr sourceIdentifier, const Position position = {}) noexcept

Create a new location object.

Parameters:
  • sourceIdentifier – The source identifier.

  • position – The position in the document.

Location(const Location&) = default

Default copy constructor.

Location(Location&&) = default

Default move constructor.

Location &operator=(const Location&) = default

Default copy assignment.

Location &operator=(Location&&) = default

Default move assignment.

inline bool operator==(const Location &other) const noexcept

Compare this location to another for equality.

Parameters:

other – The location to compare.

Returns:

true if both the source identifier and position are equal.

inline bool operator!=(const Location &other) const noexcept

Compare this location to another for inequality.

Parameters:

other – The location to compare.

Returns:

true if the locations are not equal, false otherwise.

inline bool isUndefined() const noexcept

Test if this location is undefined.

Returns:

true if undefined, false otherwise.

inline const SourceIdentifierPtr &sourceIdentifier() const noexcept

The source identifier for this location.

inline constexpr Position position() const noexcept

Get the position.

String toText() const

Get this location as a text.

The location is formatted as: (source identifier):(line):(column). If no source identifier is specified, it is replaced by the text <unknown>.

Returns:

A string with this location information.

class Position

A position inside a document.

Starting with line 1 and column 1. If the line or column are undefined, they are set to -1.

Tested:

PositionTest

Public Functions

inline constexpr Position(int line, int column)

Create a new position with the given line and column.

Parameters:
  • line – The line.

  • column – The column.

Position() = default

Default constructor.

~Position() = default

Default destructor.

Position(const Position&) = default

Default copy constructor.

Position(Position&&) = default

Default move constructor.

Position &operator=(const Position&) = default

Default copy assignment.

Position &operator=(Position&&) = default

Default move assignment.

inline bool operator==(const Position &other) const noexcept

Compare this position to another for equality.

Parameters:

other – The other position to compare.

Returns:

true if both line and column are identical.

inline bool operator!=(const Position &other) const noexcept

Compare this position to another for inequality.

Parameters:

other – The other position to compare.

Returns:

true if positions differ, false otherwise.

inline int line() const noexcept

The line, starting from 1

inline int column() const noexcept

The column, starting from 1

inline bool isUndefined() const noexcept

Test if this position is undefined.

inline void nextLine() noexcept

Increase the line and reset the column.

inline void nextColumn() noexcept

Increase the column.

String toText() const noexcept

Convert this position into a string.

Returns:

The string representation of this position.

Public Members

int _line = {-1}

The line.

int _column = {-1}

The column.