mupp 1.1.0
Loading...
Searching...
No Matches
PErrorHandler.hpp
Go to the documentation of this file.
1/***************************************************************************
2
3 PErrorHandler.hpp
4
5 Author: Andreas Suter
6 e-mail: andreas.suter@psi.ch
7
8 Based on Joel de Guzman example on calc7,
9 see https://github.com/boostorg/spirit
10
11***************************************************************************/
12
13/***************************************************************************
14 * Copyright (C) 2023 by Andreas Suter *
15 * andreas.suter@psi.ch *
16 * *
17 * This program is free software; you can redistribute it and/or modify *
18 * it under the terms of the GNU General Public License as published by *
19 * the Free Software Foundation; either version 2 of the License, or *
20 * (at your option) any later version. *
21 * *
22 * This program is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
25 * GNU General Public License for more details. *
26 * *
27 * You should have received a copy of the GNU General Public License *
28 * along with this program; if not, write to the *
29 * Free Software Foundation, Inc., *
30 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
31 ***************************************************************************/
32
33#ifndef _PERROR_HANDLER_HPP_
34#define _PERROR_HANDLER_HPP_
35
36#include <iostream>
37#include <fstream>
38#include <string>
39#include <vector>
40
41namespace mupp
42{
44
58 template <typename Iterator>
60 {
61
67 PErrorHandler(Iterator first, Iterator last)
68 : first(first), last(last) {}
69
84 template <typename Message, typename What>
86 Message const& message,
87 What const& what,
88 Iterator err_pos) const
89 {
90 int line;
91 Iterator line_start = get_pos(err_pos, line);
92 const char *homeStr = getenv("HOME");
93 char fln[1024];
94 snprintf(fln, sizeof(fln), "%s/.musrfit/mupp/mupp_err.log", homeStr);
95 std::ofstream fout(fln, std::ofstream::app);
96 if (err_pos != last) {
97 fout << message << what << ':' << std::endl;
98 fout << get_line(line_start) << std::endl;
99 for (; line_start != err_pos; ++line_start)
100 fout << ' ';
101 fout << "^~~" << std::endl;
102 } else {
103 fout << "**ERROR** Unexpected end of file. ";
104 fout << message << what << " line " << line << std::endl;
105 }
106 }
107
118 Iterator get_pos(Iterator err_pos, int& line) const
119 {
120 line = 1;
121 Iterator i = first;
122 Iterator line_start = first;
123 while (i != err_pos) {
124 bool eol = false;
125 if (i != err_pos && *i == '\r') { // CR
126 eol = true;
127 line_start = ++i;
128 }
129 if (i != err_pos && *i == '\n') { // LF
130 eol = true;
131 line_start = ++i;
132 }
133 if (eol)
134 ++line;
135 else
136 ++i;
137 }
138 return line_start;
139 }
140
149 std::string get_line(Iterator err_pos) const
150 {
151 Iterator i = err_pos;
152 // position i to the next EOL
153 while (i != last && (*i != '\r' && *i != '\n'))
154 ++i;
155 return std::string(err_pos, i);
156 }
157
158 Iterator first;
159 Iterator last;
160 std::vector<Iterator> iters;
161 };
162}
163
164#endif //_PERROR_HANDLER_HPP_
165
Iterator get_pos(Iterator err_pos, int &line) const
Finds the start of the line containing an error and computes line number.
PErrorHandler(Iterator first, Iterator last)
Constructor that stores the source code range.
std::vector< Iterator > iters
Vector mapping AST node IDs to source positions (used by PAnnotation)
void operator()(Message const &message, What const &what, Iterator err_pos) const
Function call operator that logs an error to file.
Iterator last
Iterator to the end of the source code.
std::string get_line(Iterator err_pos) const
Extracts the complete line containing an error.
Iterator first
Iterator to the beginning of the source code.