]> git.tdb.fi Git - libs/datafile.git/blobdiff - source/textparser.cpp
Update formatter.h -> format.h
[libs/datafile.git] / source / textparser.cpp
index d8e422ee7ff4d4d710a764a62383955f377305f5..9545cdcdc7f2e77018d23f341f5aeb68e9da8007 100644 (file)
@@ -1,11 +1,4 @@
-/* $Id$
-
-This file is part of libmspdatafile
-Copyright © 2007-2008  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
-#include <msp/strings/formatter.h>
+#include <msp/strings/format.h>
 #include <msp/strings/utils.h>
 #include "input.h"
 #include "textparser.h"
@@ -77,20 +70,19 @@ Statement TextParser::parse_statement(const Token *t)
                else if(token.str==";")
                        break;
                else if(token.type==Token::INTEGER)
-                       result.args.push_back(Value(INTEGER, token.str));
+                       result.append(lexical_cast<IntType::Store>(token.str));
                else if(token.type==Token::FLOAT)
-                       result.args.push_back(Value(FLOAT, token.str));
+                       result.append(lexical_cast<FloatType::Store>(token.str));
                else if(token.type==Token::STRING)
-                       result.args.push_back(Value(STRING, token.str));
+                       result.append(token.str);
                else if(token.type==Token::IDENTIFIER)
                {
                        if(token.str=="true")
-                               result.args.push_back(Value(BOOLEAN, "1"));
+                               result.append(true);
                        else if(token.str=="false")
-                               result.args.push_back(Value(BOOLEAN, "0"));
+                               result.append(false);
                        else
-                               result.args.push_back(Value(ENUM, token.str));
-                       //result.args.push_back(resolve_identifiertoken.str);
+                               result.append(Symbol(token.str));
                }
                else if(token.str=="")
                        throw_at(ParseError("Unexcepted end of input"), get_location());
@@ -122,7 +114,7 @@ Token TextParser::parse_token()
                        comment = 3;
                else if(comment==3)   // Skip the second character of block comment end
                        comment = 0;
-               else if(!isspace(c) && !comment)
+               else if(c!=-1 && !isspace(c) && !comment)
                        comment = -1;
        }
 
@@ -145,6 +137,7 @@ Token TextParser::parse_token()
                OCTAL,
                FLOAT,
                FLOATEXP,
+               STRING_END,
                IDENTIFIER
        };
 
@@ -154,7 +147,7 @@ Token TextParser::parse_token()
                Token::SPECIAL,
                Token::SPECIAL,
                Token::SPECIAL,
-               Token::STRING,
+               Token::SPECIAL,
                Token::SPECIAL,
                Token::INTEGER,
                Token::INTEGER,
@@ -162,6 +155,7 @@ Token TextParser::parse_token()
                Token::INTEGER,
                Token::FLOAT,
                Token::FLOAT,
+               Token::STRING,
                Token::IDENTIFIER
        };
 
@@ -192,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:
@@ -269,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
                                {
@@ -281,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, "");