X-Git-Url: http://git.tdb.fi/?p=libs%2Fdatafile.git;a=blobdiff_plain;f=source%2Ftextparser.cpp;h=a7e8ee9d6abaf4d5198824fde47e811651996777;hp=c4eb03c7d91ef21e2b1af8d707e2f1363cc7fc4e;hb=4edbe0eb078c4e480682862ccb68ebc8cb284045;hpb=d14b7ddd81404b909a4c4763a36a23b94998a089 diff --git a/source/textparser.cpp b/source/textparser.cpp index c4eb03c..a7e8ee9 100644 --- a/source/textparser.cpp +++ b/source/textparser.cpp @@ -1,5 +1,6 @@ #include #include +#include "except.h" #include "input.h" #include "textparser.h" #include "token.h" @@ -9,28 +10,6 @@ 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) { } @@ -43,8 +22,7 @@ Statement TextParser::parse() Statement TextParser::parse_statement(const Token *t) { Statement result; - bool sub = false; - bool finish = false; + unsigned sub = 0; while(in) { @@ -68,27 +46,24 @@ Statement TextParser::parse_statement(const Token *t) result.source = src; result.line = in.get_line_number(); } - else if(sub) + else if(sub==1) { if(token.str=="}") - { - sub = false; - finish = true; - } + sub = 2; else { Statement ss = parse_statement(&token); result.sub.push_back(ss); } } - else if(finish) + else if(sub==2) { if(token.str!=";") throw syntax_error(token.str); break; } else if(token.str=="{") - sub = true; + sub = 1; else if(token.str==";") break; else if(token.type==Token::INTEGER) @@ -124,9 +99,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; @@ -150,6 +125,7 @@ Token TextParser::parse_token() FLOATEXPINIT, FLOATEXPSIGN, STRING, + STRING_ESCAPE, ACCEPT, ZERO, DECIMAL, @@ -169,6 +145,7 @@ Token TextParser::parse_token() Token::SPECIAL, Token::SPECIAL, Token::SPECIAL, + Token::SPECIAL, Token::INTEGER, Token::INTEGER, Token::INTEGER, @@ -181,7 +158,6 @@ Token TextParser::parse_token() ParseState state = INIT; string buf; - bool escape = false; while(in || state==INIT) { @@ -237,6 +213,8 @@ Token TextParser::parse_token() case DECIMAL: if(c=='.') state = FLOAT; + else if(c=='e' || c=='E') + state = FLOATEXPINIT; else if(!isdigit(c)) throw parse_error(buf); break; @@ -281,11 +259,13 @@ Token TextParser::parse_token() case STRING: if(c=='\\') - escape = !escape; - else if(c=='"' && !escape) + state = STRING_ESCAPE; + else if(c=='"') state = STRING_END; - else - escape = false; + break; + + case STRING_ESCAPE: + state = STRING; break; case IDENTIFIER: