libqasm
library for handling cQASM files
cqasm-annotations.cpp
Go to the documentation of this file.
1 
5 #include <iostream>
6 #include "cqasm-annotations.hpp"
7 
8 namespace cqasm {
9 namespace annotations {
10 
15  const std::string &filename,
16  uint32_t first_line,
17  uint32_t first_column,
18  uint32_t last_line,
19  uint32_t last_column
20 ) :
21  filename(filename),
22  first_line(first_line),
23  first_column(first_column),
24  last_line(last_line),
25  last_column(last_column)
26 {
27  if (last_line < first_line) {
28  last_line = first_line;
29  }
30  if (last_line == first_line && last_column < first_column) {
31  last_column = first_column;
32  }
33 }
34 
39 void SourceLocation::expand_to_include(uint32_t line, uint32_t column) {
40  if (line < first_line) {
41  first_line = line;
42  }
43  if (line == first_line && column < first_column) {
44  first_column = column;
45  }
46  if (line > last_line) {
47  last_line = line;
48  }
49  if (line == last_line && column > last_column) {
50  last_column = column;
51  }
52 }
53 
57 std::ostream &operator<<(std::ostream &os, const SourceLocation &object) {
58 
59  // Print filename.
60  os << object.filename;
61 
62  // Special case for when only the source filename is known.
63  if (!object.first_line) {
64  return os;
65  }
66 
67  // Print line number.
68  os << ":" << object.first_line;
69 
70  // Special case for when only line numbers are known.
71  if (!object.first_column) {
72 
73  // Print last line too, if greater.
74  if (object.last_line > object.first_line) {
75  os << ".." << object.last_line;
76  }
77 
78  return os;
79  }
80 
81  // Print column.
82  os << ":" << object.first_column;
83 
84  if (object.last_line == object.first_line) {
85 
86  // Range is on a single line. Only repeat the column number.
87  if (object.last_column > object.first_column) {
88  os << ".." << object.last_column;
89  }
90 
91  } else if (object.last_line > object.first_line) {
92 
93  // Range is on multiple lines. Repeat both line and column number.
94  os << ".." << object.last_line << ":" << object.last_column;
95 
96  }
97 
98  return os;
99 }
100 
101 } // namespace annotations
102 } // namespace cqasm
uint32_t last_line
The last line of the range, or 0 if unknown.
uint32_t first_column
The first column of the range, or 0 if unknown.
uint32_t last_column
The last column of the range, or 0 if unknown.
Contains annotation objects used within the trees by libqasm.
Toplevel namespace with entry points for the new API.
uint32_t first_line
The first line of the range, or 0 if unknown.
Source location annotation object, containing source file line numbers etc.
void expand_to_include(uint32_t line, uint32_t column=1)
Expands the location range to contain the given location in the source file.
SourceLocation(const std::string &filename, uint32_t first_line=0, uint32_t first_column=0, uint32_t last_line=0, uint32_t last_column=0)
Constructs a source location object.
std::ostream & operator<<(std::ostream &os, const SourceLocation &object)
Stream << overload for source location objects.