File: astexception.cpp

package info (click to toggle)
minizinc 2.9.3%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,620 kB
  • sloc: cpp: 74,682; ansic: 8,541; python: 3,322; sh: 79; makefile: 13
file content (106 lines) | stat: -rw-r--r-- 3,127 bytes parent folder | download | duplicates (2)
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
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */

/*
 *  Main authors:
 *     Guido Tack <guido.tack@monash.edu>
 */

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <minizinc/astexception.hh>
#include <minizinc/flatten_internal.hh>

namespace MiniZinc {

void SyntaxError::print(std::ostream& os) const {
  for (const auto& filename : _includeStack) {
    os << "(included from file '" << filename << "')\n";
  }
  os << loc() << ":\n";
  if (!_currentLine.empty()) {
    os << _currentLine << "\n";
  }
  os << "Error: " << msg() << std::endl;
}

void SyntaxError::json(std::ostream& os) const {
  os << "{\"type\": \"error\", \"what\": \"" << Printer::escapeStringLit(std::string(what()))
     << "\", \"location\": " << loc().toJSON() << ", ";
  if (!_includeStack.empty()) {
    os << "\"includedFrom\": [";
    bool first = true;
    for (const auto& filename : _includeStack) {
      if (first) {
        first = false;
      } else {
        os << ", ";
      }
      os << "\"" << Printer::escapeStringLit(filename) << "\"";
    }
    os << "], ";
  }
  os << "\"message\": \"" << Printer::escapeStringLit(msg()) << "\"}" << std::endl;
}

void CyclicIncludeError::print(std::ostream& os) const {
  Exception::print(os);
  for (const auto& filename : _cycle) {
    os << "  " << filename << "\n";
  }
}

void CyclicIncludeError::json(std::ostream& os) const {
  os << "{\"type\": \"error\", \"what\": \"" << Printer::escapeStringLit(std::string(what()))
     << "\", \"cycle\": [";
  bool first = true;
  for (const auto& filename : _cycle) {
    if (first) {
      first = false;
    } else {
      os << ", ";
    }
    os << "\"" << Printer::escapeStringLit(filename) << "\"";
  }
  os << "]}\n";
}

LocationException::LocationException(EnvI& env, const Location& loc, const std::string& msg)
    : Exception(msg), _stack(env), _loc(loc) {}

LocationException::LocationException(const Location& loc, const std::string& msg)
    : Exception(msg), _loc(loc) {}

void LocationException::print(std::ostream& os) const {
  Exception::print(os);
  if (_dumpStack && !_stack.empty()) {
    _stack.print(os);
  } else {
    os << loc() << "\n";
  }
}

void LocationException::json(std::ostream& os) const {
  os << "{\"type\": \"error\", \"what\": \"" << Printer::escapeStringLit(std::string(what()))
     << "\", \"location\": " << loc().toJSON() << ", \"message\": \""
     << Printer::escapeStringLit(msg()) << "\"";
  if (_dumpStack && !_stack.empty()) {
    os << ", \"stack\": ";
    _stack.json(os);
  }
  os << "}" << std::endl;
}

ResultUndefinedError::ResultUndefinedError(EnvI& env, const Location& loc, const std::string& msg)
    : LocationException(env, loc, msg) {
  if (env.inMaybePartial == 0) {
    std::string warning = "undefined result becomes false in Boolean context";
    if (!msg.empty()) {
      warning += "\n  (" + msg + ")";
    }
    _warningIdx = env.addWarning(loc, warning);
  }
}

}  // namespace MiniZinc