diff --git a/flang/position.cc b/flang/position.cc new file mode 100644 index 000000000000..b398225b7131 --- /dev/null +++ b/flang/position.cc @@ -0,0 +1,7 @@ +#include "position.h" + +namespace Fortran { +std::ostream &operator<<(std::ostream &o, const Position &x) { + return o << "(at line " << x.lineNumber() << ", column " << x.column() << ')'; +} +} // namespace Fortran diff --git a/flang/position.h b/flang/position.h new file mode 100644 index 000000000000..efe67046c3e2 --- /dev/null +++ b/flang/position.h @@ -0,0 +1,70 @@ +#ifndef FORTRAN_POSITION_H_ +#define FORTRAN_POSITION_H_ + +// Represents a position in a source file. +// TODO: Support multiple source files for inclusion and support contextual +// positions for macro expansions. + +#include + +namespace Fortran { + +class Position { + public: + constexpr Position() = default; + constexpr Position(int ln, int col) + : lineNumber_{ln}, column_{col} {} + + constexpr int lineNumber() const { return lineNumber_; } + constexpr int column() const { return column_; } + + constexpr bool operator<(const Position &that) const { + return lineNumber_ < that.lineNumber_ || + (lineNumber_ == that.lineNumber_ && + column_ < that.column_); + } + + constexpr bool operator<=(const Position &that) const { + return lineNumber_ < that.lineNumber_ || + (lineNumber_ == that.lineNumber_ && + column_ <= that.column_); + } + + constexpr bool operator==(const Position &that) const { + return lineNumber_ == that.lineNumber_ && + column_ == that.column_; + } + + constexpr bool operator!=(const Position &that) const { + return !operator==(that); + } + + constexpr bool operator>(const Position &that) const { + return !operator<=(that); + } + + constexpr bool operator>=(const Position &that) const { + return !operator<(that); + } + + void AdvanceColumn() { + ++column_; + } + + void TabAdvanceColumn() { + column_ = ((column_ + 7) & -8) + 1; + } + + void AdvanceLine() { + ++lineNumber_; + column_ = 1; + } + + private: + int lineNumber_{1}; + int column_{1}; +}; + +std::ostream &operator<<(std::ostream &, const Position &); +} // namespace Fortran +#endif // FORTRAN_POSITION_H_