CodeSpan.hpp
Go to the documentation of this file.
1 // @formatter:off
2 //
3 // Balau core C++ library
4 //
5 // Copyright (C) 2008 Bora Software (contact@borasoftware.com)
6 //
7 // Licensed under the Boost Software License - Version 1.0 - August 17th, 2003.
8 // See the LICENSE file for the full license text.
9 //
10 
16 
17 #ifndef COM_BORA_SOFTWARE__BALAU_LANG__CODE_SPAN
18 #define COM_BORA_SOFTWARE__BALAU_LANG__CODE_SPAN
19 
20 #include <Balau/Type/ToString.hpp>
21 #include <Balau/Resource/Uri.hpp>
22 
23 namespace Balau::Lang {
24 
30 class CodePosition {
34  public: unsigned int line;
35 
39  public: unsigned int column;
40 
44  public: CodePosition() : line(0), column(0) {}
45 
49  public: CodePosition(unsigned int line_, unsigned int column_) : line(line_), column(column_) {}
50 
54  public: CodePosition(const CodePosition & copy) = default;
55 
61  public: CodePosition & operator = (const CodePosition & copy) = default;
62 
66  public: bool operator == (const CodePosition & rhs) const {
67  return line == rhs.line && column == rhs.column;
68  }
69 
73  public: bool operator < (const CodePosition & rhs) const {
74  return line < rhs.line || (line == rhs.line && column < rhs.column);
75  }
76 
80  public: bool operator > (const CodePosition & rhs) const {
81  return line > rhs.line || (line == rhs.line && column > rhs.column);
82  }
83 };
84 
91 class CodeSpan {
96 
100  public: CodePosition end;
101 
111  public: static CodeSpan totalCodeSpan(const CodeSpan & start, const CodeSpan & end) {
112  return CodeSpan(start.start.line, start.start.column, end.end.line, end.end.column);
113  }
114 
121  public: static CodeSpan emptyCodeSpanStart(const CodeSpan & next) {
122  return CodeSpan(next.start.line, next.start.column, next.start.line, next.start.column);
123  }
124 
131  public: static CodeSpan emptyCodeSpanEnd(const CodeSpan & previous) {
132  return CodeSpan(previous.end.line, previous.end.column, previous.end.line, previous.end.column);
133  }
134 
138  public: CodeSpan() : start(0, 0), end(0, 0) {}
139 
143  public: CodeSpan(unsigned int lineStart_,
144  unsigned int columnStart_,
145  unsigned int lineEnd_,
146  unsigned int columnEnd_)
147  : start(lineStart_, columnStart_)
148  , end(lineEnd_, columnEnd_) {}
149 
153  public: CodeSpan(const CodePosition & start_,
154  const CodePosition & end_)
155  : start(start_)
156  , end(end_) {}
157 
161  public: CodeSpan(const CodeSpan & copy) = default;
162 
166  public: CodeSpan( CodeSpan && copy) = default;
167 
174  public: CodeSpan & operator = (const CodeSpan & rhs) = default;
175 
182  public: CodeSpan & operator = (CodeSpan && rhs) = default;
183 
190  public: bool operator == (const CodeSpan & rhs) const {
191  return start == rhs.start && end == rhs.end;
192  }
193 
201  public: bool operator < (const CodeSpan & rhs) const {
202  return start < rhs.start;
203  }
204 
212  public: bool operator > (const CodeSpan & rhs) const {
213  return start > rhs.start;
214  }
215 
225  public: CodeSpan & operator += (const CodeSpan & rhs) {
226  end = rhs.end;
227  return *this;
228  }
229 };
230 
234 template <typename AllocatorT>
236  return ::toString<AllocatorT>(
237  "[L", codeSpan.start.line
238  , "C", codeSpan.start.column
239  , ":L", codeSpan.end.line
240  , "C", codeSpan.end.column
241  , "]"
242  );
243 }
244 
248 inline std::string toString(const CodeSpan & codeSpan) {
250  "[L", codeSpan.start.line
251  , "C", codeSpan.start.column
252  , ":L", codeSpan.end.line
253  , "C", codeSpan.end.column
254  , "]"
255  );
256 }
257 
258 } // namespace Balau::Lang
259 
260 #endif // COM_BORA_SOFTWARE__BALAU_LANG__CODE_SPAN
static CodeSpan emptyCodeSpanEnd(const CodeSpan &previous)
Create an empty code span equal to the end of the supplied code span.
Definition: CodeSpan.hpp:131
static CodeSpan totalCodeSpan(const CodeSpan &start, const CodeSpan &end)
Create a code span equal to the start of the first code span and the end of the end code span...
Definition: CodeSpan.hpp:111
Pre-defined universal to-string functions.
bool operator==(const CodePosition &rhs) const
Is this code position equal to the supplied code position?
Definition: CodeSpan.hpp:66
unsigned int column
The column of the code position (1-indexed).
Definition: CodeSpan.hpp:39
CodePosition(unsigned int line_, unsigned int column_)
Create a code position with the supplied line and colunn values.
Definition: CodeSpan.hpp:49
A position in a piece of multi-line text.
Definition: CodeSpan.hpp:30
unsigned int line
The line of the code position (1-indexed).
Definition: CodeSpan.hpp:34
Information on the span of some source code text.
Definition: CodeSpan.hpp:91
CodeSpan(unsigned int lineStart_, unsigned int columnStart_, unsigned int lineEnd_, unsigned int columnEnd_)
Create a code span equal to the supplied start and end positions.
Definition: CodeSpan.hpp:143
CodePosition & operator=(const CodePosition &copy)=default
Assign the code position by copying the supplied code position line and column values.
CodeSpan(const CodePosition &start_, const CodePosition &end_)
Create a code span equal to the supplied start and end positions.
Definition: CodeSpan.hpp:153
Parsing tools and parser implementations.
Definition: AbstractScanner.hpp:27
Balau::U8String< AllocatorT > toString(const CodeSpan &codeSpan)
Print the supplied code span as a UTF-8 string.
Definition: CodeSpan.hpp:235
CodePosition()
Create a null code position (line and colunn equal to 0).
Definition: CodeSpan.hpp:44
CodePosition end
The end position of the code span.
Definition: CodeSpan.hpp:100
std::basic_string< char, std::char_traits< char >, AllocatorT > U8String
UTF-8 string type with selectable allocator.
Definition: ToStringA.hpp:41
static CodeSpan emptyCodeSpanStart(const CodeSpan &next)
Create an empty code span equal to the start of the supplied code span.
Definition: CodeSpan.hpp:121
CodeSpan()
Create a null code span.
Definition: CodeSpan.hpp:138
The abstract URI base class.
bool operator>(const CodePosition &rhs) const
Does this code position come after the supplied code position?
Definition: CodeSpan.hpp:80
bool operator<(const CodePosition &rhs) const
Does this code position come before the supplied code position?
Definition: CodeSpan.hpp:73
CodePosition start
The start position of the code span.
Definition: CodeSpan.hpp:95