X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Ftextparser.cpp;h=56c9bb2b07d75fbbdac9394b4edf42c314d6fdb2;hb=5c98c1f499413c41fd46ddf71a46b481338d7d6b;hp=0076c1b4bab954f8a893abf9f2e6a4ff7da5039a;hpb=3b70a4d95e414f63fd39b4325d0f639c155a70f5;p=libs%2Fdatafile.git diff --git a/source/textparser.cpp b/source/textparser.cpp index 0076c1b..56c9bb2 100644 --- a/source/textparser.cpp +++ b/source/textparser.cpp @@ -1,11 +1,4 @@ -/* $Id$ - -This file is part of libmspdatafile -Copyright © 2007-2008, 2010 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - -#include +#include #include #include "input.h" #include "textparser.h" @@ -28,8 +21,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) { @@ -144,6 +137,7 @@ Token TextParser::parse_token() OCTAL, FLOAT, FLOATEXP, + STRING_END, IDENTIFIER }; @@ -153,7 +147,7 @@ Token TextParser::parse_token() Token::SPECIAL, Token::SPECIAL, Token::SPECIAL, - Token::STRING, + Token::SPECIAL, Token::SPECIAL, Token::INTEGER, Token::INTEGER, @@ -161,12 +155,13 @@ Token TextParser::parse_token() Token::INTEGER, Token::FLOAT, Token::FLOAT, + Token::STRING, Token::IDENTIFIER }; ParseState state = INIT; - string buf; - bool escape = false; + string buf; + bool escape = false; while(in || state==INIT) { @@ -191,10 +186,10 @@ Token TextParser::parse_token() return Token(Token::SPECIAL, string(1, c)); else if(isdigit(c)) state = DECIMAL; - else if(isalpha(c) || c=='_') + else if(isalpha(c) || c=='_' || c=='\\') state = IDENTIFIER; else - parse_error(c, "0-9A-Za-z_.\"{};+-"); + parse_error(c, "0-9A-Za-z_\\.\"{};+-"); break; case SIGN: @@ -268,6 +263,28 @@ Token TextParser::parse_token() if(c=='\\') escape = !escape; else if(c=='"' && !escape) + state = STRING_END; + else + escape = false; + break; + + case IDENTIFIER: + if(!isalpha(c) && !isdigit(c) && c!='_' && c!='-' && c!='/') + parse_error(c, "0-9A-Za-z_/-"); + break; + + case STRING_END: + throw_at(ParseError("Garbage after string"), get_location()); + + default: + throw_at(InvalidState("Internal error (bad state)"), get_location()); + } + + if(is_delimiter(next) && state>=ACCEPT) + { + if(state==IDENTIFIER && buf[0]=='\\') + return Token(Token::IDENTIFIER, buf.substr(1)); + else if(state==STRING_END) { try { @@ -280,20 +297,8 @@ Token TextParser::parse_token() } } else - escape = false; - break; - - case IDENTIFIER: - if(!isalpha(c) && !isdigit(c) && c!='_') - parse_error(c, "0-9A-Za-z_"); - break; - - default: - throw_at(InvalidState("Internal error (bad state)"), get_location()); + return Token(token_type[state], buf); } - - if(is_delimiter(next) && state>=ACCEPT) - return Token(token_type[state], buf); } return Token(Token::SPECIAL, "");