FE 0.6.0
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 /// Write your own implementation or include fe/loc.cpp.h somewhere for a default one.
26 friend std::ostream& operator<<(std::ostream& os, Pos pos);
27};
28
29/// Loc%ation in a File.
30/// It's only two machine words on a 64 bit arch, so pass around as value.
31/// @warning Loc::path is only a pointer and it is your job to guarantee
32/// that the underlying `std::filesystem::path` outlives this Loc%ation.
33struct Loc {
34 Loc() = default; ///< Creates an invalid Loc%ation.
35 Loc(const std::filesystem::path* path, Pos begin, Pos finis)
36 : path(path)
37 , begin(begin)
38 , finis(finis) {}
39 Loc(const std::filesystem::path* file, Pos pos)
40 : Loc(file, pos, pos) {}
42 : Loc(nullptr, begin, finis) {}
43 Loc(Pos pos)
44 : Loc(nullptr, pos, pos) {}
45
46 Loc anew_begin() const { return {path, begin, begin}; }
47 Loc anew_finis() const { return {path, finis, finis}; }
48 Loc operator+(Pos pos) const { return {path, begin, pos}; }
49 Loc operator+(Loc loc) const { return {path, begin, loc.finis}; }
50 explicit operator bool() const { return (bool)begin; } ///< Is a valid Loc%ation?
51 /// @note Loc::path is only checked via pointer equality.
52 bool operator==(Loc other) const { return begin == other.begin && finis == other.finis && path == other.path; }
53 void dump() const;
54
55 const std::filesystem::path* path = {};
56 Pos begin = {};
57 Pos finis = {};
58 ///< It's called `finis` because it refers to the **last** character within this Loc%ation.
59 /// In the STL the word `end` refers to the position of something that is one element **past** the end.
60
61 /// Write your own implementation or include fe/loc.cpp.h somewhere for a default one.
62 friend std::ostream& operator<<(std::ostream& os, Loc loc);
63};
64
65} // namespace fe
Definition arena.h:9
Location in a File.
Definition loc.h:33
Loc(Pos begin, Pos finis)
Definition loc.h:41
void dump() const
Definition loc.cpp.h:24
Pos finis
It's called finis because it refers to the last character within this Location.
Definition loc.h:57
Loc()=default
Creates an invalid Location.
Loc(const std::filesystem::path *path, Pos begin, Pos finis)
Definition loc.h:35
const std::filesystem::path * path
Definition loc.h:55
Loc operator+(Loc loc) const
Definition loc.h:49
friend std::ostream & operator<<(std::ostream &os, Loc loc)
Write your own implementation or include fe/loc.cpp.h somewhere for a default one.
Definition loc.cpp.h:14
bool operator==(Loc other) const
Definition loc.h:52
Loc(const std::filesystem::path *file, Pos pos)
Definition loc.h:39
Pos begin
Definition loc.h:56
Loc(Pos pos)
Definition loc.h:43
Loc anew_begin() const
Definition loc.h:46
Loc operator+(Pos pos) const
Definition loc.h:48
Loc anew_finis() const
Definition loc.h:47
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)
Write your own implementation or include fe/loc.cpp.h somewhere for a default one.
Definition loc.cpp.h:6
uint16_t row
Definition loc.h:22
void dump() const
Definition loc.cpp.h:23