]> git.tdb.fi Git - libs/datafile.git/blobdiff - source/parser.cpp
Remove old build info
[libs/datafile.git] / source / parser.cpp
index 8707cedae643d829a32dbf193d2474b03593c133..c47969bb2f3f30c5e194f534fbae6ff72884a3ad 100644 (file)
@@ -1,19 +1,17 @@
 /*
 This file is part of libmspparser
-Copyright © 2006 Mikko Rasa, Mikkosoft Productions
+Copyright © 2006  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 #include <cctype>
-#include <msp/error.h>
-#include <msp/streams/format.h>
+#include <sstream>
+#include "error.h"
 #include "parser.h"
 #include "statement.h"
 #include "token.h"
 
 using namespace std;
 
-#include <iostream>
-
 namespace Msp {
 namespace Parser {
 
@@ -27,7 +25,7 @@ Statement Parser::parse()
 {
        if(!good)
                throw Exception("Parser is not good");
-       
+
        try
        {
                return parse_(0);
@@ -44,7 +42,7 @@ Statement Parser::parse_(const Token *t)
        Statement result;
        bool      sub=false;
        bool      finish=false;
-       
+
        while(in)
        {
                Token token;
@@ -55,13 +53,13 @@ Statement Parser::parse_(const Token *t)
                }
                else
                        token=parse_token();
-               
+
                if(result.keyword.empty())
                {
                        if(token.str.empty())
                                break;
                        else if(token.type!=Token::IDENTIFIER)
-                               throw DataError(get_location()+format(": Syntax error at token '%S' (expected an identifier)", &token.str).str());
+                               throw ParseError(get_location()+": Syntax error at token '"+token.str+"' (expected an identifier)", src, in.get_line_number());
                        result.keyword=token.str;
                        result.valid=true;
                        result.source=src;
@@ -83,7 +81,7 @@ Statement Parser::parse_(const Token *t)
                else if(finish)
                {
                        if(token.str!=";")
-                               throw DataError(get_location()+format(": Syntax error at token '%S' (Expected a ';')", &token.str).str());
+                               throw ParseError(get_location()+": Syntax error at token '"+token.str+"' (Expected a ';')", src, in.get_line_number());
                        break;
                }
                else if(token.str=="{")
@@ -98,12 +96,18 @@ Statement Parser::parse_(const Token *t)
                        result.args.push_back(Value(Value::STRING, token.str));
                else if(token.type==Token::IDENTIFIER)
                {
+                       if(token.str=="true")
+                               result.args.push_back(Value(Value::BOOLEAN, "1"));
+                       else if(token.str=="false")
+                               result.args.push_back(Value(Value::BOOLEAN, "0"));
+                       else
+                               result.args.push_back(Value(Value::ENUM, token.str));
                        //result.args.push_back(resolve_identifiertoken.str);
                }
                else if(token.str=="")
-                       throw DataError(src+": Unexcepted EOF");
+                       throw ParseError(src+": Unexcepted EOF", src, in.get_line_number());
                else
-                       throw DataError(get_location()+": Syntax error");
+                       throw ParseError(get_location()+": Syntax error", src, in.get_line_number());
        }
 
        return result;
@@ -111,15 +115,15 @@ Statement Parser::parse_(const Token *t)
 
 Token Parser::parse_token()
 {
-       int c;
+       int c=0;
        unsigned comment=0;
+
+       // Skip over comments and whitespace
        while(in)
        {
                c=in.get();
                int next=in.peek();
 
-               //cout<<c<<' '<<next<<'\n';
-
                if(c=='/' && next=='/')
                        comment=1;
                else if(c=='/' && next=='*')
@@ -133,13 +137,16 @@ Token Parser::parse_token()
                else if(!isspace(c) && !comment)
                        break;
        }
-       if(comment)
-               throw DataError(src+": Unfinished comment");
-       
+
+       if(comment)  // Didn't hit any non-whitespace
+               throw ParseError(src+": Unfinished comment", src, in.get_line_number());
+
        enum ParseState
        {
                INIT,
-               NEGATIVE,
+               SIGN,
+               FLOATEXPINIT,
+               FLOATEXPSIGN,
                STRING,
                ACCEPT,
                ZERO,
@@ -147,11 +154,14 @@ Token Parser::parse_token()
                HEXADECIMAL,
                OCTAL,
                FLOAT,
+               FLOATEXP,
                IDENTIFIER
        };
 
        static Token::Type token_type[]=
        {
+               Token::SPECIAL,
+               Token::SPECIAL,
                Token::SPECIAL,
                Token::SPECIAL,
                Token::STRING,
@@ -161,6 +171,7 @@ Token Parser::parse_token()
                Token::INTEGER,
                Token::INTEGER,
                Token::FLOAT,
+               Token::FLOAT,
                Token::IDENTIFIER
        };
 
@@ -173,7 +184,7 @@ Token Parser::parse_token()
                if(state!=INIT)
                        c=in.get();
                int next=in.peek();
-               
+
                buf+=c;
 
                switch(state)
@@ -181,8 +192,8 @@ Token Parser::parse_token()
                case INIT:
                        if(c=='0')
                                state=ZERO;
-                       else if(c=='-')
-                               state=NEGATIVE;
+                       else if(c=='-' || c=='+')
+                               state=SIGN;
                        else if(c=='.')
                                state=FLOAT;
                        else if(c=='"')
@@ -196,8 +207,8 @@ Token Parser::parse_token()
                        else
                                parse_error(c, state);
                        break;
-               
-               case NEGATIVE:
+
+               case SIGN:
                        if(c=='0')
                                state=ZERO;
                        else if(isdigit(c))
@@ -237,6 +248,29 @@ Token Parser::parse_token()
                        break;
 
                case FLOAT:
+                       if(c=='e' || c=='E')
+                               state=FLOATEXPINIT;
+                       else if(!isdigit(c))
+                               parse_error(c, state);
+                       break;
+
+               case FLOATEXPINIT:
+                       if(c=='+' || c=='-')
+                               state=FLOATEXPSIGN;
+                       else if(isdigit(c))
+                               state=FLOATEXP;
+                       else
+                               parse_error(c, state);
+                       break;
+
+               case FLOATEXPSIGN:
+                       if(isdigit(c))
+                               state=FLOATEXP;
+                       else
+                               parse_error(c, state);
+                       break;
+
+               case FLOATEXP:
                        if(!isdigit(c))
                                parse_error(c, state);
                        break;
@@ -249,7 +283,7 @@ Token Parser::parse_token()
                        else
                                escape=false;
                        break;
-               
+
                case IDENTIFIER:
                        if(!isalpha(c) && !isdigit(c) && c!='_')
                                parse_error(c, state);
@@ -296,7 +330,7 @@ string Parser::unescape_string(const string &str)
                        else if(*i=='x')
                                hexcape=0x100;
                        else
-                               throw DataError("Invalid escape");
+                               throw ParseError("Invalid escape", src, in.get_line_number());
                        escape=false;
                }
                else if(hexcape)
@@ -309,7 +343,7 @@ string Parser::unescape_string(const string &str)
                        else if(*i>='A' && *i<='F')
                                digit=*i-'A'+10;
                        else
-                               throw DataError("Invalid hex digit");
+                               throw ParseError("Invalid hex digit", src, in.get_line_number());
 
                        hexcape=(hexcape<<4)|digit;
                        if(hexcape&0x10000)
@@ -336,7 +370,9 @@ string Parser::get_location()
 
 void Parser::parse_error(int c, int state)
 {
-       throw DataError(get_location()+format(": Parse error at '%c' (state %d)", c, state).str());
+       ostringstream ss;
+       ss<<get_location()<<": Parse error at '"<<(char)c<<"' (state "<<state<<')';
+       throw ParseError(ss.str(), src, in.get_line_number());
 }
 
 } // namespace Parser