X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Ftextparser.cpp;h=c87a32df666924e163ee83e8d53f06d7d181b2e3;hb=ed78b585cfc4ecb44972e346857e887b183fd7a7;hp=930ce875d782a58a43f6063ce30a4c3aab0b66ef;hpb=7df5e45c7f414f6a07681dc4ec2abb63b091a309;p=libs%2Fdatafile.git diff --git a/source/textparser.cpp b/source/textparser.cpp index 930ce87..c87a32d 100644 --- a/source/textparser.cpp +++ b/source/textparser.cpp @@ -1,5 +1,6 @@ -#include +#include #include +#include "except.h" #include "input.h" #include "textparser.h" #include "token.h" @@ -21,8 +22,8 @@ Statement TextParser::parse() Statement TextParser::parse_statement(const Token *t) { Statement result; - bool sub = false; - bool finish = false; + bool sub = false; + bool finish = false; while(in) { @@ -40,7 +41,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 +63,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 +85,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; @@ -104,9 +103,9 @@ Token TextParser::parse_token() c = in.get(); int next = in.peek(); - if(c=='/' && next=='/') + if(c=='/' && next=='/' && !comment) comment = 1; - else if(c=='/' && next=='*') + else if(c=='/' && next=='*' && !comment) comment = 2; else if(c=='\n' && comment==1) comment = 0; @@ -119,7 +118,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, ""); @@ -160,8 +159,8 @@ Token TextParser::parse_token() }; ParseState state = INIT; - string buf; - bool escape = false; + string buf; + bool escape = false; while(in || state==INIT) { @@ -189,7 +188,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 +199,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 +210,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 +245,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 +271,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 +286,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 +305,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