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