]> git.tdb.fi Git - libs/datafile.git/blobdiff - source/textparser.cpp
Refactor exceptions
[libs/datafile.git] / source / textparser.cpp
index f7c830d0030ebcf5428c3c0abf06c63ba043ce60..0f17d3f3cc79808a7659e291e406f6b7bb814137 100644 (file)
@@ -1,7 +1,7 @@
 /* $Id$
 
 This file is part of libmspdatafile
-Copyright © 2007  Mikko Rasa, Mikkosoft Productions
+Copyright © 2007-2008  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 
@@ -47,7 +47,7 @@ Statement TextParser::parse_statement(const Token *t)
                        if(token.str.empty())
                                break;
                        else if(token.type!=Token::IDENTIFIER)
-                               throw ParseError(format("%s: Syntax error at token '%s' (expected an identifier)", get_location(), token.str), src, in.get_line_number());
+                               throw_at(ParseError(format("Syntax error at token '%s' (expected an identifier)", token.str)), get_location());
                        result.keyword=token.str;
                        result.valid=true;
                        result.source=src;
@@ -69,7 +69,7 @@ Statement TextParser::parse_statement(const Token *t)
                else if(finish)
                {
                        if(token.str!=";")
-                               throw ParseError(format("%s: Syntax error at token '%s' (Expected a ';')", get_location(), token.str), src, in.get_line_number());
+                               throw_at(ParseError(format("Syntax error at token '%s' (Expected a ';')", token.str)), get_location());
                        break;
                }
                else if(token.str=="{")
@@ -93,9 +93,9 @@ Statement TextParser::parse_statement(const Token *t)
                        //result.args.push_back(resolve_identifiertoken.str);
                }
                else if(token.str=="")
-                       throw ParseError(src+": Unexcepted end of input", src, in.get_line_number());
+                       throw_at(ParseError("Unexcepted end of input"), get_location());
                else
-                       throw ParseError(get_location()+": Syntax error", src, in.get_line_number());
+                       throw_at(ParseError("Syntax error"), get_location());
        }
 
        return result;
@@ -127,7 +127,7 @@ Token TextParser::parse_token()
        }
 
        if(comment>0)  // EOF while in comment
-               throw ParseError(src+": Unfinished comment at end of input", src, in.get_line_number());
+               throw_at(ParseError("Unfinished comment at end of input"), get_location());
        else if(comment==0)  // Didn't hit any non-whitespace
                return Token(Token::SPECIAL, "");
 
@@ -195,7 +195,7 @@ Token TextParser::parse_token()
                        else if(isalpha(c) || c=='_')
                                state=IDENTIFIER;
                        else
-                               parse_error(c, state);
+                               parse_error(c, "0-9A-Za-z_.\"{};+-");
                        break;
 
                case SIGN:
@@ -206,7 +206,7 @@ Token TextParser::parse_token()
                        else if(c=='.')
                                state=FLOAT;
                        else
-                               parse_error(c, state);
+                               parse_error(c, "0-9.");
                        break;
 
                case ZERO:
@@ -217,31 +217,31 @@ Token TextParser::parse_token()
                        else if(c=='.')
                                state=FLOAT;
                        else
-                               parse_error(c, state);
+                               parse_error(c, "0-9A-Fa-f.");
                        break;
 
                case DECIMAL:
                        if(c=='.')
                                state=FLOAT;
                        else if(!isdigit(c))
-                               parse_error(c, state);
+                               parse_error(c, "0-9.");
                        break;
 
                case HEXADECIMAL:
                        if(!isxdigit(c))
-                               parse_error(c, state);
+                               parse_error(c, "0-9A-Fa-f");
                        break;
 
                case OCTAL:
                        if(!isodigit(c))
-                               parse_error(c, state);
+                               parse_error(c, "0-7");
                        break;
 
                case FLOAT:
                        if(c=='e' || c=='E')
                                state=FLOATEXPINIT;
                        else if(!isdigit(c))
-                               parse_error(c, state);
+                               parse_error(c, "0-9Ee");
                        break;
 
                case FLOATEXPINIT:
@@ -250,19 +250,19 @@ Token TextParser::parse_token()
                        else if(isdigit(c))
                                state=FLOATEXP;
                        else
-                               parse_error(c, state);
+                               parse_error(c, "0-9+-");
                        break;
 
                case FLOATEXPSIGN:
                        if(isdigit(c))
                                state=FLOATEXP;
                        else
-                               parse_error(c, state);
+                               parse_error(c, "0-9");
                        break;
 
                case FLOATEXP:
                        if(!isdigit(c))
-                               parse_error(c, state);
+                               parse_error(c, "0-9");
                        break;
 
                case STRING:
@@ -274,9 +274,10 @@ Token TextParser::parse_token()
                                {
                                        return Token(Token::STRING, c_unescape(buf.substr(1, buf.size()-2)));
                                }
-                               catch(const Exception &e)
+                               catch(Exception &e)
                                {
-                                       throw ParseError(format("%s: %s", get_location(), e.what()), src, in.get_line_number());
+                                       e.at(get_location());
+                                       throw;
                                }
                        }
                        else
@@ -285,11 +286,11 @@ Token TextParser::parse_token()
 
                case IDENTIFIER:
                        if(!isalpha(c) && !isdigit(c) && c!='_')
-                               parse_error(c, state);
+                               parse_error(c, "0-9A-Za-z_");
                        break;
 
                default:
-                       throw Exception(get_location()+": Internal error (bad state)");
+                       throw_at(InvalidState("Internal error (bad state)"), get_location());
                }
 
                if(is_delimiter(next) && state>=ACCEPT)
@@ -316,9 +317,9 @@ string TextParser::get_location()
        return ss.str();
 }
 
-void TextParser::parse_error(int c, int state)
+void TextParser::parse_error(int c, const char *e)
 {
-       throw ParseError(format("%s: Parse error at '%c' (state %d)", get_location(), static_cast<char>(c), state), src, in.get_line_number());
+       throw_at(ParseError(format("Parse error at '%c', expected one of \"%s\"", static_cast<char>(c), e)), get_location());
 }
 
 } // namespace DataFile