[51587aa] | 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 | //
|
---|
[3906301] | 7 | // SemanticError.h --
|
---|
[51587aa] | 8 | //
|
---|
| 9 | // Author : Richard C. Bilson
|
---|
| 10 | // Created On : Mon May 18 07:44:20 2015
|
---|
[9ed4f94] | 11 | // Last Modified By : Peter A. Buhr
|
---|
| 12 | // Last Modified On : Tue Aug 29 22:03:36 2017
|
---|
| 13 | // Update Count : 17
|
---|
[51587aa] | 14 | //
|
---|
[51b73452] | 15 |
|
---|
[6b0b624] | 16 | #pragma once
|
---|
[51b73452] | 17 |
|
---|
[9ed4f94] | 18 | #include <exception> // for exception
|
---|
| 19 | #include <iostream> // for ostream
|
---|
| 20 | #include <list> // for list
|
---|
| 21 | #include <string> // for string
|
---|
| 22 | #include <unistd.h> // for isatty
|
---|
[51b73452] | 23 |
|
---|
[9ed4f94] | 24 | #include "CodeLocation.h" // for CodeLocation, toString
|
---|
[294647b] | 25 |
|
---|
[d55d7a6] | 26 | //-----------------------------------------------------------------------------
|
---|
| 27 | // Errors
|
---|
[138e29e] | 28 | struct error {
|
---|
| 29 | CodeLocation location;
|
---|
[d55d7a6] | 30 | std::string description;
|
---|
[138e29e] | 31 |
|
---|
| 32 | error() = default;
|
---|
[d55d7a6] | 33 | error( CodeLocation loc, const std::string & str ) : location( loc ), description( str ) {}
|
---|
[138e29e] | 34 | };
|
---|
| 35 |
|
---|
[01aeade] | 36 | class SemanticError : public std::exception {
|
---|
| 37 | public:
|
---|
[d55d7a6] | 38 | SemanticError() = default;
|
---|
| 39 | SemanticError( CodeLocation location, std::string error );
|
---|
[01aeade] | 40 | ~SemanticError() throw() {}
|
---|
| 41 |
|
---|
[d55d7a6] | 42 | // constructs an exception using the given message and the printed representation of the obj (T must have a print method)
|
---|
| 43 | template< typename T > SemanticError(const T * obj, const std::string & error);
|
---|
| 44 | template< typename T > SemanticError( CodeLocation location, const T * obj, const std::string & error);
|
---|
| 45 |
|
---|
[9ed4f94] | 46 | static inline const std::string & error_str() {
|
---|
| 47 | static std::string str = isatty( STDERR_FILENO ) ? "\e[31merror:\e[39m " : "error: ";
|
---|
| 48 | return str;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | void append( SemanticError & other );
|
---|
[d55d7a6] | 52 | void append( CodeLocation location, const std::string & );
|
---|
[01aeade] | 53 | bool isEmpty() const;
|
---|
[d55d7a6] | 54 | void print();
|
---|
[01aeade] | 55 | private:
|
---|
[138e29e] | 56 | std::list< error > errors;
|
---|
[51b73452] | 57 | };
|
---|
| 58 |
|
---|
| 59 | template< typename T >
|
---|
[d55d7a6] | 60 | SemanticError::SemanticError( const T * obj, const std::string & error )
|
---|
| 61 | : SemanticError( obj->location, toString( error, obj ) )
|
---|
| 62 | {}
|
---|
| 63 |
|
---|
| 64 | template< typename T >
|
---|
| 65 | SemanticError::SemanticError( CodeLocation location, const T * obj, const std::string & error )
|
---|
| 66 | : SemanticError( location, toString( error, obj ) )
|
---|
| 67 | {}
|
---|
| 68 |
|
---|
| 69 | //-----------------------------------------------------------------------------
|
---|
| 70 | // Warnings
|
---|
| 71 | class SemanticWarning {
|
---|
| 72 | public:
|
---|
| 73 | SemanticWarning( CodeLocation location, std::string error );
|
---|
| 74 | ~SemanticWarning() throw() {}
|
---|
| 75 |
|
---|
| 76 | // constructs an exception using the given message and the printed representation of the obj (T must have a print method)
|
---|
| 77 | template< typename T > SemanticWarning(const T * obj, const std::string & error);
|
---|
| 78 | template< typename T > SemanticWarning( CodeLocation location, const T * obj, const std::string & error);
|
---|
| 79 |
|
---|
| 80 | static inline const std::string & warning_str() {
|
---|
| 81 | static std::string str = isatty( STDERR_FILENO ) ? "\e[95mwarning:\e[39m " : "warning: ";
|
---|
| 82 | return str;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | private:
|
---|
| 86 | };
|
---|
| 87 |
|
---|
| 88 | template< typename T >
|
---|
| 89 | SemanticWarning::SemanticWarning( const T * obj, const std::string & error )
|
---|
| 90 | : SemanticWarning( obj->location, toString( error, obj ) )
|
---|
| 91 | {}
|
---|
| 92 |
|
---|
| 93 | template< typename T >
|
---|
| 94 | SemanticWarning::SemanticWarning( CodeLocation location, const T * obj, const std::string & error )
|
---|
| 95 | : SemanticWarning( location, toString( error, obj ) )
|
---|
| 96 | {}
|
---|
| 97 |
|
---|
| 98 | //-----------------------------------------------------------------------------
|
---|
| 99 | // Helpers
|
---|
| 100 | static inline const std::string & bold_ttycode() {
|
---|
| 101 | static std::string str = isatty( STDERR_FILENO ) ? "\e[1m" : "";
|
---|
| 102 | return str;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | static inline const std::string & reset_font_ttycode() {
|
---|
| 106 | static std::string str = isatty( STDERR_FILENO ) ? "\e[0m" : "";
|
---|
| 107 | return str;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | static inline std::string make_bold( const std::string & str ) {
|
---|
| 111 | return bold_ttycode() + str + reset_font_ttycode();
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | struct bold {};
|
---|
| 115 | static inline std::ostream & operator<<(std::ostream & os, bold) {
|
---|
| 116 | os << bold_ttycode();
|
---|
| 117 | return os;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | struct reset_font {};
|
---|
| 121 | static inline std::ostream & operator<<(std::ostream & os, reset_font) {
|
---|
| 122 | os << reset_font_ttycode();
|
---|
| 123 | return os;
|
---|
[51b73452] | 124 | }
|
---|
| 125 |
|
---|
[51587aa] | 126 | // Local Variables: //
|
---|
| 127 | // tab-width: 4 //
|
---|
| 128 | // mode: c++ //
|
---|
| 129 | // compile-command: "make install" //
|
---|
| 130 | // End: //
|
---|