summaryrefslogtreecommitdiff
path: root/graphics/asymptote/errormsg.h
blob: 22e7f79f1fe3244e5ec15a2c9b4edeba3b1568f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*****
 * errormsg.h
 * Andy Hammerlindl 2002/06/17
 *
 * Used in all phases of the compiler to give error messages.
 *****/

#ifndef ERRORMSG_H
#define ERRORMSG_H

#include <iostream>
#include "common.h"
#include "settings.h"

using std::ostream;

struct handled_error {}; // Exception to process next file.
struct interrupted {};   // Exception to interrupt execution.
struct quit {};          // Exception to quit current operation.
struct eof {};           // Exception to exit interactive mode.

class fileinfo : public gc {
  string filename;
  size_t lineNum;

public:
  fileinfo(string filename, size_t lineNum=1)
    : filename(filename), lineNum(lineNum) {}

  size_t line() const
  {
    return lineNum;
  }
  
  string name() const {
    return filename;
  }

  // The filename without the directory and without the '.asy' suffix.
  // Note that this assumes name are separated by a forward slash.
  string moduleName() const {
    size_t start = filename.rfind('/');
    if (start == filename.npos)
      start = 0;
    else
      // Step over slash.
      ++start;

    size_t end = filename.rfind(".asy");
    if (end != filename.size() - 4)
      end = filename.size();

    return filename.substr(start, end-start);
  }
  
  // Specifies a newline symbol at the character position given.
  void newline() {
    ++lineNum;
  }
  
};

inline bool operator == (const fileinfo& a, const fileinfo& b)
{
  return a.line() == b.line() && a.name() == b.name();
}

class position : public gc {
  fileinfo *file;
  size_t line;
  size_t column;

public:
  void init(fileinfo *f, Int p) {
    file = f;
    if (file) {
      line = file->line();
      column = p;
    } else {
      line = column = 0;
    }
  }

  string filename() const
  {
    return file ? file->name() : "";
  }
  
  size_t Line() const
  {
    return line;
  }
  
  size_t Column() const
  {
    return column;
  }
  
  std::pair<size_t,size_t>LineColumn() const {
    return std::pair<size_t,size_t>(line,column);
  }

  bool match(const string& s) {
    return file && file->name() == s;
  }
  
  bool match(size_t l) {
    return line == l;
  }
  
  bool matchColumn(size_t c) {
    return column == c;
  }
  
  bool operator! () const
  {
    return (file == 0);
  }
  
  friend ostream& operator << (ostream& out, const position& pos);

  // Write out just the module name and line number.
  void printTerse(ostream& out) const
  {
    if (file) {
      out << file->moduleName() << ":" << line;
    }
  }
};

extern position nullPos;

struct nullPosInitializer {
  nullPosInitializer() {nullPos.init(NULL,0);}
};
  
inline bool operator == (const position& a, const position& b)
{
  return a.Line() == b.Line() && a.Column() == b.Column() && 
    a.filename() == b.filename(); 
}

string warning(string s);
    
class errorstream {
  ostream& out;
  bool anyErrors;
  bool anyWarnings;
  bool floating;        // Was a message output without a terminating newline?
  
  // Is there an error that warrants the asy process to return 1 instead of 0?
  bool anyStatusErrors;

public:
  static bool interrupt; // Is there a pending interrupt?
  
  errorstream(ostream& out = cerr)
    : out(out), anyErrors(false), anyWarnings(false), floating(false),
      anyStatusErrors(false) {}


  void clear();

  void message(position pos, const string& s);
  
  void Interrupt(bool b) {
    interrupt=b;
  }
  
  // An error is encountered, not in the user's code, but in the way the
  // compiler works!  This may be augmented in the future with a message
  // to contact the compiler writers.
  void compiler();
  void compiler(position pos);

  // An error encountered when running compiled code.  This method does
  // not stop the executable, but the executable should be stopped
  // shortly after calling this method.
  void runtime(position pos);

  // Errors encountered when compiling making it impossible to run the code.
  void error(position pos);

  // Indicate potential problems in the code, but the code is still usable.
  void warning(position pos);
  void warning(position pos, string s);
  
  // Single a fatal error and execute the main process.
  void fatal(position pos);
  
  // Print out position in code to aid debugging.
  void trace(position pos);
  
  // Sends stuff to out to print.
  // NOTE: May later make it do automatic line breaking for long messages.
  template<class T>
  errorstream& operator << (const T& x) {
    flush(out);
    out << x;
    return *this;
  }

  // Reporting errors to the stream may be incomplete.  This draws the
  // appropriate newlines or file excerpts that may be needed at the end.
  void sync();

  void cont();
  
  bool errors() const {
    return anyErrors;
  }
  
  bool warnings() const {
    return anyWarnings || errors();
  }

  void statusError() {
    anyStatusErrors=true;
  }

  // Returns true if no errors have occured that should be reported by the
  // return value of the process.
  bool processStatus() const {
    return !anyStatusErrors;
  }
};

extern errorstream em;
void outOfMemory();

GC_DECLARE_PTRFREE(nullPosInitializer);

#endif