Bash++
Bash++ compiler internal documentation
ParserPosition.h
Go to the documentation of this file.
1
7#pragma once
8
9#include <cstdint>
10#include <string>
11#include <iostream>
12
23 const std::string* filename;
24 uint32_t line = 0;
25 uint32_t column = 0;
26
27 explicit ParserPosition(const std::string* f = nullptr, uint32_t l = 0, uint32_t c = 0)
28 : filename(f), line(l), column(c) {}
29
30 inline void initialize(const std::string* fn = nullptr, uint32_t l = 0, uint32_t c = 0) {
31 filename = fn;
32 line = l;
33 column = c;
34 }
35
41 inline void lines(uint32_t count = 1) {
42 if (count) {
43 column = 0;
44 line += count;
45 }
46 }
47
53 inline void columns(uint32_t count = 1) {
54 column += count;
55 }
56};
57
58inline ParserPosition& operator+=(ParserPosition& lhs, uint32_t rhs) {
59 lhs.columns(rhs);
60 return lhs;
61}
62
63inline ParserPosition operator+(ParserPosition lhs, uint32_t rhs) {
64 return lhs += rhs;
65}
66
67inline std::ostream& operator<<(std::ostream& os, const ParserPosition& pos) {
68 if (pos.filename) {
69 os << *(pos.filename) << ':';
70 }
71 return os << pos.line << '.' << pos.column;
72}
73
82
89 explicit ParserLocation(const ParserPosition& b, const ParserPosition& e)
90 : begin(b), end(e) {}
91
98 : begin(p), end(p) {}
99
108 explicit ParserLocation(const std::string* f, uint32_t l = 0, uint32_t c = 0)
109 : begin(f, l, c), end(f, l, c) {}
110
111 inline void initialize(const std::string* f = nullptr, uint32_t l = 0, uint32_t c = 0) {
112 begin.initialize(f, l, c);
113 end = begin;
114 }
115
120 inline void step() {
121 begin = end;
122 }
123
124 inline void columns(uint32_t count = 1) {
125 end.columns(count);
126 }
127
128 inline void lines(uint32_t count = 1) {
129 end.lines(count);
130 }
131};
132
133inline ParserLocation& operator+=(ParserLocation& lhs, uint32_t rhs) {
134 lhs.columns(rhs);
135 return lhs;
136}
137
138inline ParserLocation operator+(ParserLocation lhs, uint32_t rhs) {
139 return lhs += rhs;
140}
141
142inline std::ostream& operator<<(std::ostream& os, const ParserLocation& loc) {
143 uint32_t end_col = 0 < loc.end.column ? loc.end.column - 1 : 0;
144 os << loc.begin;
145 if (loc.end.filename && (!loc.begin.filename || *loc.begin.filename != *loc.end.filename)) {
146 os << '-' << *(loc.end.filename) << ':' << loc.end.line << '.' << end_col;
147 } else if (loc.begin.line < loc.end.line) {
148 os << '-' << loc.end.line << '.' << end_col;
149 } else if (loc.begin.column < end_col) {
150 os << '-' << end_col;
151 }
152 return os;
153}
std::ostream & operator<<(std::ostream &os, const ParserPosition &pos)
Definition ParserPosition.h:67
ParserPosition & operator+=(ParserPosition &lhs, uint32_t rhs)
Definition ParserPosition.h:58
ParserPosition operator+(ParserPosition lhs, uint32_t rhs)
Definition ParserPosition.h:63
Represents a range in a source file, from a start position to an end position.
Definition ParserPosition.h:79
void step()
Reset initial position to the end position.
Definition ParserPosition.h:120
ParserPosition begin
Definition ParserPosition.h:80
void initialize(const std::string *f=nullptr, uint32_t l=0, uint32_t c=0)
Definition ParserPosition.h:111
ParserLocation(const ParserPosition &b, const ParserPosition &e)
Construct location from 'begin' to 'end' positions.
Definition ParserPosition.h:89
ParserLocation(const ParserPosition &p=ParserPosition())
Construct a 0-width location at position 'p'.
Definition ParserPosition.h:97
void columns(uint32_t count=1)
Definition ParserPosition.h:124
ParserPosition end
Definition ParserPosition.h:81
ParserLocation(const std::string *f, uint32_t l=0, uint32_t c=0)
Construct a 0-width location at file 'f', line 'l', column 'c'.
Definition ParserPosition.h:108
void lines(uint32_t count=1)
Definition ParserPosition.h:128
Represents a single point in a source file.
Definition ParserPosition.h:22
void initialize(const std::string *fn=nullptr, uint32_t l=0, uint32_t c=0)
Definition ParserPosition.h:30
uint32_t column
Definition ParserPosition.h:25
const std::string * filename
Definition ParserPosition.h:23
uint32_t line
Definition ParserPosition.h:24
ParserPosition(const std::string *f=nullptr, uint32_t l=0, uint32_t c=0)
Definition ParserPosition.h:27
void lines(uint32_t count=1)
Increment the line number by count, resetting the column to 0.
Definition ParserPosition.h:41
void columns(uint32_t count=1)
Increment the column number by count.
Definition ParserPosition.h:53