1 | //
|
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
|
---|
3 | //
|
---|
4 | // The contents of this file are covered under the licence agreement in the
|
---|
5 | // file "LICENCE" distributed with Cforall.
|
---|
6 | //
|
---|
7 | // SemanticError.cc --
|
---|
8 | //
|
---|
9 | // Author : Richard C. Bilson
|
---|
10 | // Created On : Mon May 18 07:44:20 2015
|
---|
11 | // Last Modified By : Peter A. Buhr
|
---|
12 | // Last Modified On : Tue Aug 29 18:17:35 2017
|
---|
13 | // Update Count : 3
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include <cstdio> // for fileno, stderr
|
---|
17 | #include <unistd.h> // for isatty
|
---|
18 | #include <iostream> // for basic_ostream, operator<<, ostream
|
---|
19 | #include <list> // for list, _List_iterator
|
---|
20 | #include <string> // for string, operator<<, operator+, to_string
|
---|
21 |
|
---|
22 | #include "Common/utility.h" // for to_string, CodeLocation (ptr only)
|
---|
23 | #include "SemanticError.h"
|
---|
24 |
|
---|
25 | SemanticErrorException::SemanticErrorException( CodeLocation location, std::string error ) {
|
---|
26 | append( location, error );
|
---|
27 | }
|
---|
28 |
|
---|
29 | void SemanticErrorException::append( SemanticErrorException &other ) {
|
---|
30 | errors.splice( errors.end(), other.errors );
|
---|
31 | }
|
---|
32 |
|
---|
33 | void SemanticErrorException::append( CodeLocation location, const std::string & msg ) {
|
---|
34 | errors.emplace_back( location, msg );
|
---|
35 | }
|
---|
36 |
|
---|
37 | bool SemanticErrorException::isEmpty() const {
|
---|
38 | return errors.empty();
|
---|
39 | }
|
---|
40 |
|
---|
41 | void SemanticErrorException::print() {
|
---|
42 | using std::to_string;
|
---|
43 | for( auto err : errors ) {
|
---|
44 | std::cerr << ErrorHelpers::bold() << err.location << ErrorHelpers::error_str() << ErrorHelpers::reset_font() << err.description << std::endl;
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | void SemanticError( CodeLocation location, std::string error ) {
|
---|
49 | throw SemanticErrorException(location, error);
|
---|
50 | }
|
---|
51 |
|
---|
52 | void SemanticWarningImpl( CodeLocation location, std::string msg ) {
|
---|
53 | std::cerr << ErrorHelpers::bold() << location << ErrorHelpers::warning_str() << ErrorHelpers::reset_font() << msg << std::endl;
|
---|
54 | }
|
---|
55 |
|
---|
56 | //-----------------------------------------------------------------------------
|
---|
57 | // Helpers
|
---|
58 | namespace ErrorHelpers {
|
---|
59 | const std::string & error_str() {
|
---|
60 | static std::string str = isatty( STDERR_FILENO ) ? "\e[31merror:\e[39m " : "error: ";
|
---|
61 | return str;
|
---|
62 | }
|
---|
63 |
|
---|
64 | const std::string & warning_str() {
|
---|
65 | static std::string str = isatty( STDERR_FILENO ) ? "\e[95mwarning:\e[39m " : "warning: ";
|
---|
66 | return str;
|
---|
67 | }
|
---|
68 |
|
---|
69 | const std::string & bold_ttycode() {
|
---|
70 | static std::string str = isatty( STDERR_FILENO ) ? "\e[1m" : "";
|
---|
71 | return str;
|
---|
72 | }
|
---|
73 |
|
---|
74 | const std::string & reset_font_ttycode() {
|
---|
75 | static std::string str = isatty( STDERR_FILENO ) ? "\e[0m" : "";
|
---|
76 | return str;
|
---|
77 | }
|
---|
78 |
|
---|
79 | std::string make_bold( const std::string & str ) {
|
---|
80 | return bold_ttycode() + str + reset_font_ttycode();
|
---|
81 | }
|
---|
82 |
|
---|
83 | std::ostream & operator<<(std::ostream & os, bold) {
|
---|
84 | os << bold_ttycode();
|
---|
85 | return os;
|
---|
86 | }
|
---|
87 |
|
---|
88 | std::ostream & operator<<(std::ostream & os, reset_font) {
|
---|
89 | os << reset_font_ttycode();
|
---|
90 | return os;
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | // Local Variables: //
|
---|
95 | // tab-width: 4 //
|
---|
96 | // mode: c++ //
|
---|
97 | // compile-command: "make install" //
|
---|
98 | // End: //
|
---|