X-Git-Url: http://git.tdb.fi/?p=libs%2Fdatafile.git;a=blobdiff_plain;f=source%2Ftextparser.cpp;h=9bed08ac9e0ea5a154208b24cda76dc75925267b;hp=56c9bb2b07d75fbbdac9394b4edf42c314d6fdb2;hb=eec12aaf350ef69b79d149adbc3585cf784552ad;hpb=5c98c1f499413c41fd46ddf71a46b481338d7d6b diff --git a/source/textparser.cpp b/source/textparser.cpp index 56c9bb2..9bed08a 100644 --- a/source/textparser.cpp +++ b/source/textparser.cpp @@ -9,6 +9,28 @@ using namespace std; namespace Msp { namespace DataFile { +class parse_error: public runtime_error +{ +public: + parse_error(const std::string &t): + runtime_error(t.empty() ? "at end of input" : format("after '%s'", t)) + { } + + virtual ~parse_error() throw() { } +}; + + +class syntax_error: public runtime_error +{ +public: + syntax_error(const std::string &t): + runtime_error(t.empty() ? "at end of input" : format("at '%s'", t)) + { } + + virtual ~syntax_error() throw() { } +}; + + TextParser::TextParser(Input &i, const string &s): ParserMode(i, s) { } @@ -40,7 +62,7 @@ Statement TextParser::parse_statement(const Token *t) if(token.str.empty()) break; else if(token.type!=Token::IDENTIFIER) - throw_at(ParseError(format("Syntax error at token '%s' (expected an identifier)", token.str)), get_location()); + throw syntax_error(token.str); result.keyword = token.str; result.valid = true; result.source = src; @@ -62,7 +84,7 @@ Statement TextParser::parse_statement(const Token *t) else if(finish) { if(token.str!=";") - throw_at(ParseError(format("Syntax error at token '%s' (Expected a ';')", token.str)), get_location()); + throw syntax_error(token.str); break; } else if(token.str=="{") @@ -84,10 +106,8 @@ Statement TextParser::parse_statement(const Token *t) else result.append(Symbol(token.str)); } - else if(token.str=="") - throw_at(ParseError("Unexcepted end of input"), get_location()); else - throw_at(ParseError("Syntax error"), get_location()); + throw syntax_error(token.str); } return result; @@ -119,7 +139,7 @@ Token TextParser::parse_token() } if(comment>0) // EOF while in comment - throw_at(ParseError("Unfinished comment at end of input"), get_location()); + throw parse_error(string()); else if(comment==0) // Didn't hit any non-whitespace return Token(Token::SPECIAL, ""); @@ -189,7 +209,7 @@ Token TextParser::parse_token() else if(isalpha(c) || c=='_' || c=='\\') state = IDENTIFIER; else - parse_error(c, "0-9A-Za-z_\\.\"{};+-"); + throw parse_error(buf); break; case SIGN: @@ -200,7 +220,7 @@ Token TextParser::parse_token() else if(c=='.') state = FLOAT; else - parse_error(c, "0-9."); + throw parse_error(buf); break; case ZERO: @@ -211,31 +231,33 @@ Token TextParser::parse_token() else if(c=='.') state = FLOAT; else - parse_error(c, "0-9A-Fa-f."); + throw parse_error(buf); break; case DECIMAL: if(c=='.') state = FLOAT; + else if(c=='e' || c=='E') + state = FLOATEXPINIT; else if(!isdigit(c)) - parse_error(c, "0-9."); + throw parse_error(buf); break; case HEXADECIMAL: if(!isxdigit(c)) - parse_error(c, "0-9A-Fa-f"); + throw parse_error(buf); break; case OCTAL: if(!isodigit(c)) - parse_error(c, "0-7"); + throw parse_error(buf); break; case FLOAT: if(c=='e' || c=='E') state = FLOATEXPINIT; else if(!isdigit(c)) - parse_error(c, "0-9Ee"); + throw parse_error(buf); break; case FLOATEXPINIT: @@ -244,19 +266,19 @@ Token TextParser::parse_token() else if(isdigit(c)) state = FLOATEXP; else - parse_error(c, "0-9+-"); + throw parse_error(buf); break; case FLOATEXPSIGN: if(isdigit(c)) state = FLOATEXP; else - parse_error(c, "0-9"); + throw parse_error(buf); break; case FLOATEXP: if(!isdigit(c)) - parse_error(c, "0-9"); + throw parse_error(buf); break; case STRING: @@ -270,14 +292,14 @@ Token TextParser::parse_token() case IDENTIFIER: if(!isalpha(c) && !isdigit(c) && c!='_' && c!='-' && c!='/') - parse_error(c, "0-9A-Za-z_/-"); + throw parse_error(buf); break; case STRING_END: - throw_at(ParseError("Garbage after string"), get_location()); + throw parse_error(buf); default: - throw_at(InvalidState("Internal error (bad state)"), get_location()); + throw logic_error("bad parser state"); } if(is_delimiter(next) && state>=ACCEPT) @@ -285,17 +307,7 @@ Token TextParser::parse_token() if(state==IDENTIFIER && buf[0]=='\\') return Token(Token::IDENTIFIER, buf.substr(1)); else if(state==STRING_END) - { - try - { - return Token(Token::STRING, c_unescape(buf.substr(1, buf.size()-2))); - } - catch(Exception &e) - { - e.at(get_location()); - throw; - } - } + return Token(Token::STRING, c_unescape(buf.substr(1, buf.size()-2))); else return Token(token_type[state], buf); } @@ -314,17 +326,5 @@ bool TextParser::isodigit(int c) return (c>='0' && c<='7'); } -string TextParser::get_location() -{ - ostringstream ss; - ss<(c), e)), get_location()); -} - } // namespace DataFile } // namespace Msp