FE 0.6.1
A header-only C++ library for writing frontends
Loading...
Searching...
No Matches
loc.h
Go to the documentation of this file.
1#pragma once
2
3#include <filesystem>
4
5#include "fe/sym.h"
6
7namespace fe {
8
9/// Pos%ition in a source file; pass around as value.
10struct Pos {
11 Pos() = default; ///< Creates an invalid Pos%ition.
12 Pos(uint16_t row)
13 : row(row) {}
14 Pos(uint16_t row, uint16_t col)
15 : row(row)
16 , col(col) {}
17
18 explicit operator bool() const { return row != 0; } ///< Is a valid Pos%ition?
19 auto operator<=>(const Pos&) const = default;
20 void dump() const;
21
22 uint16_t row = 0;
23 uint16_t col = 0;
24
25 /// `fe/loc.h` only declares the stream output and dump helpers.
26 /// Include `fe/loc.cpp.h` in exactly one translation unit for the default implementation,
27 /// or provide your own definitions instead.
28 friend std::ostream& operator<<(std::ostream& os, Pos pos);
29};
30
31/// Loc%ation in a File.
32/// It's only two machine words on a 64 bit arch, so pass around as value.
33/// @warning Loc::path is only a pointer and it is your job to guarantee
34/// that the underlying `std::filesystem::path` outlives this Loc%ation.
35struct Loc {
36 Loc() = default; ///< Creates an invalid Loc%ation.
37 Loc(const std::filesystem::path* path, Pos begin, Pos finis)
38 : path(path)
39 , begin(begin)
40 , finis(finis) {}
41 Loc(const std::filesystem::path* file, Pos pos)
42 : Loc(file, pos, pos) {}
44 : Loc(nullptr, begin, finis) {}
45 Loc(Pos pos)
46 : Loc(nullptr, pos, pos) {}
47
48 Loc anew_begin() const { return {path, begin, begin}; }
49 Loc anew_finis() const { return {path, finis, finis}; }
50 Loc operator+(Pos pos) const { return {path, begin, pos}; }
51 Loc operator+(Loc loc) const { return {path, begin, loc.finis}; }
52 explicit operator bool() const { return (bool)begin; } ///< Is a valid Loc%ation?
53 /// @note Loc::path is only checked via pointer equality.
54 bool operator==(Loc other) const { return begin == other.begin && finis == other.finis && path == other.path; }
55 void dump() const;
56
57 const std::filesystem::path* path = {};
58 Pos begin = {};
59 Pos finis = {};
60 ///< It's called `finis` because it refers to the **last** character within this Loc%ation.
61 /// In the STL the word `end` refers to the position of something that is one element **past** the end.
62
63 /// `fe/loc.h` only declares the stream output and dump helpers.
64 /// Include `fe/loc.cpp.h` in exactly one translation unit for the default implementation,
65 /// or provide your own definitions instead.
66 friend std::ostream& operator<<(std::ostream& os, Loc loc);
67};
68
69} // namespace fe
Default Pos/Loc stream output and dump helpers.
Definition arena.h:13
Location in a File.
Definition loc.h:35
Loc(Pos begin, Pos finis)
Definition loc.h:43
void dump() const
Definition loc.cpp.h:28
Pos finis
It's called finis because it refers to the last character within this Location.
Definition loc.h:59
Loc()=default
Creates an invalid Location.
Loc(const std::filesystem::path *path, Pos begin, Pos finis)
Definition loc.h:37
const std::filesystem::path * path
Definition loc.h:57
Loc operator+(Loc loc) const
Definition loc.h:51
friend std::ostream & operator<<(std::ostream &os, Loc loc)
fe/loc.h only declares the stream output and dump helpers.
Definition loc.cpp.h:18
bool operator==(Loc other) const
Definition loc.h:54
Loc(const std::filesystem::path *file, Pos pos)
Definition loc.h:41
Pos begin
Definition loc.h:58
Loc(Pos pos)
Definition loc.h:45
Loc anew_begin() const
Definition loc.h:48
Loc operator+(Pos pos) const
Definition loc.h:50
Loc anew_finis() const
Definition loc.h:49
Position in a source file; pass around as value.
Definition loc.h:10
uint16_t col
Definition loc.h:23
Pos(uint16_t row)
Definition loc.h:12
auto operator<=>(const Pos &) const =default
Pos(uint16_t row, uint16_t col)
Definition loc.h:14
Pos()=default
Creates an invalid Position.
friend std::ostream & operator<<(std::ostream &os, Pos pos)
fe/loc.h only declares the stream output and dump helpers.
Definition loc.cpp.h:10
uint16_t row
Definition loc.h:22
void dump() const
Definition loc.cpp.h:27