Check-in [6f09ea15f1]

Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
SHA1 Hash:6f09ea15f1bde15c18abae6ec9bcabb50dc2954a
Date: 2008-09-18 18:56:54
User: stephan
Comment:minor cleanups to the error reporting
Tags And Properties
Changes
hide diffs unified diffs patch

Changes to parsepp_err.hpp

Old (c8dbcf42b9e25c15) New (94a3ca983236d91c)
1 #ifndef s11n_net_PARSEPP_ERRORS_HPP_INCLUDED 1 #ifndef s11n_net_PARSEPP_ERRORS_HPP_INCLUDED
2 #define s11n_net_PARSEPP_ERRORS_HPP_INCLUDED 2 #define s11n_net_PARSEPP_ERRORS_HPP_INCLUDED
3 /************************************************************************ 3 /************************************************************************
4 This file contains supplemental error-handling-related code for the 4 This file contains supplemental error-handling-related code for the
5 parsepp toolkit. 5 parsepp toolkit.
6 6
7 Author: Stephan Beal (http://wanderinghorse.net/home/stephan) 7 Author: Stephan Beal (http://wanderinghorse.net/home/stephan)
8 License: Public Domain 8 License: Public Domain
9 ************************************************************************/ 9 ************************************************************************/
10 #include <map> <
11 #include <string> 10 #include <string>
12 #include <cassert> <
13 #include <iostream> <
14 #include <sstream> 11 #include <sstream>
15 #include <stdexcept> 12 #include <stdexcept>
16 #include <vector> <
17 #include <list> <
18 #include <set> <
19 13
20 #include "parsepp.hpp" 14 #include "parsepp.hpp"
21 #include "parsepp_typelist.hpp" 15 #include "parsepp_typelist.hpp"
22 16
23 namespace Ps { 17 namespace Ps {
48 hidden lines
72 UnexpectedEOF, 66 UnexpectedEOF,
73 UnclosedComment, 67 UnclosedComment,
74 UserErrorBegin = 1000 /* client-side IDs should start here. */ 68 UserErrorBegin = 1000 /* client-side IDs should start here. */
75 }; 69 };
76 }; 70 };
> 71
> 72 /**
> 73 A template for mapping parser error codes to strings.
> 74
> 75 Specializations may provide custom handling. There is no need
> 76 to calculate an error's position because the r_error handlers
> 77 do that. Specializations may want, e.g., to collect a range
> 78 of characters around the error point (the current position
> 79 of the parser_state object).
> 80
> 81 Design note: another alternative to solve the
> 82 error-number-to-string problem without requiring template
> 83 specializations would be to use a static map<int,string>, but
> 84 then we'd need to provide .cpp files along with the .hpp files
> 85 for this lib, and i don't wanna do that.
> 86 */
77 template <int ErrorNumber = Errors::Unknown> 87 template <int ErrorNumber = Errors::Unknown>
78 struct error_msg 88 struct error_msg
79 { 89 {
80 template <typename State> 90 template <typename State>
81 static std::string message( parser_state &, State & ) 91 static std::string message( parser_state &, State & )
82 { 92 {
83 return "Unknown (or unspecified) parsing error"; | 93 std::ostringstream os;
| 94 os << "Unknown (or unspecified) parsing error #" << ErrorNumber;
| 95 return os.str();
84 } 96 }
85 }; 97 };
86 98
87 /** 99 /**
88 Specialization for UnexpectedCharacter errors. 100 Specialization for UnexpectedCharacter errors.
9 hidden lines
98 else msg.push_back(*ps.pos()); 110 else msg.push_back(*ps.pos());
99 msg.push_back('\''); 111 msg.push_back('\'');
100 return msg; 112 return msg;
101 } 113 }
102 }; 114 };
> 115
> 116 /**
> 117 Specialization for UnexpectedEOF errors.
> 118 */
103 template <> 119 template <>
104 struct error_msg<Errors::UnexpectedEOF> 120 struct error_msg<Errors::UnexpectedEOF>
105 { 121 {
106 template <typename State> 122 template <typename State>
107 static std::string message( parser_state & ps, State & ) 123 static std::string message( parser_state & ps, State & )
108 { 124 {
109 return "Unexpected end of input"; 125 return "Unexpected end of input";
110 } 126 }
111 }; 127 };
> 128
> 129 /**
> 130 Specialization for UnclosedComment errors.
> 131 */
112 template <> 132 template <>
113 struct error_msg<Errors::UnclosedComment> 133 struct error_msg<Errors::UnclosedComment>
114 { 134 {
115 template <typename State> 135 template <typename State>
116 static std::string message( parser_state & ps, State & ) 136 static std::string message( parser_state & ps, State & )
117 { 137 {
118 return "Reached EOF inside of a multi-line comment"; 138 return "Reached EOF inside of a multi-line comment";
119 } 139 }
120 }; 140 };
> 141
121 /** 142 /**
122 Specialization for IllegalCharacter errors. 143 Specialization for IllegalCharacter errors.
123 */ 144 */
124 template <> 145 template <>
125 struct error_msg<Errors::IllegalCharacter> 146 struct error_msg<Errors::IllegalCharacter>
11 hidden lines
137 /** 158 /**
138 Similar to r_throw, this rule throws a parse_error 159 Similar to r_throw, this rule throws a parse_error
139 exception. The what() text of the exception is the text of 160 exception. The what() text of the exception is the text of
140 error_msg<ErrorNumber>, allowing one to specialize error_msg to 161 error_msg<ErrorNumber>, allowing one to specialize error_msg to
141 create custom error messages. The where() part of the exception 162 create custom error messages. The where() part of the exception
142 <
143 Design note: another alternative to solve this problem would be <
144 to use a static map<int,string>, but then we'd need to provide <
145 .cpp files along with the .hpp files for this lib, and i don't <
146 wanna do that. <
147 */ 163 */
148 template <int ErrorNumber = Errors::Unknown> 164 template <int ErrorNumber = Errors::Unknown>
149 struct r_error 165 struct r_error
150 { 166 {
151 typedef r_error type; 167 typedef r_error type;
152 template <typename State> 168 template <typename State>
153 static bool matches( parser_state & in, State & st ) 169 static bool matches( parser_state & in, State & st )
154 { 170 {
155 throw parse_error( in, error_msg<ErrorNumber>::message(in,st) ); | 171 std::ostringstream os;
| 172 os << "Parse error #" << ErrorNumber
| 173 << ": " << error_msg<ErrorNumber>::message(in,st);
| 174 throw parse_error( in, os.str() );
156 return false; 175 return false;
157 } 176 }
158 }; 177 };
159 178
160 /** 179 /**
65 hidden lines
226 }; 245 };
227 246
228 } // namespace 247 } // namespace
229 248
230 #endif // s11n_net_PARSEPP_ERRORS_HPP_INCLUDED 249 #endif // s11n_net_PARSEPP_ERRORS_HPP_INCLUDED