]> git.tdb.fi Git - libs/datafile.git/commitdiff
Some refactoring of TextParser logic
authorMikko Rasa <tdb@tdb.fi>
Fri, 2 Sep 2016 11:44:08 +0000 (14:44 +0300)
committerMikko Rasa <tdb@tdb.fi>
Fri, 2 Sep 2016 11:44:08 +0000 (14:44 +0300)
source/textparser.cpp

index c87a32df666924e163ee83e8d53f06d7d181b2e3..a7e8ee9d6abaf4d5198824fde47e811651996777 100644 (file)
@@ -22,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)
        {
@@ -47,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)
@@ -129,6 +125,7 @@ Token TextParser::parse_token()
                FLOATEXPINIT,
                FLOATEXPSIGN,
                STRING,
+               STRING_ESCAPE,
                ACCEPT,
                ZERO,
                DECIMAL,
@@ -148,6 +145,7 @@ Token TextParser::parse_token()
                Token::SPECIAL,
                Token::SPECIAL,
                Token::SPECIAL,
+               Token::SPECIAL,
                Token::INTEGER,
                Token::INTEGER,
                Token::INTEGER,
@@ -160,7 +158,6 @@ Token TextParser::parse_token()
 
        ParseState state = INIT;
        string buf;
-       bool escape = false;
 
        while(in || state==INIT)
        {
@@ -262,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: