X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fparser.cpp;h=8fccec2f94c208d93f966ec88f4279eeb133b081;hb=3e2ee845788985e552d2a09f4c524e08fada1fdf;hp=8707cedae643d829a32dbf193d2474b03593c133;hpb=5453824394771ca21de32088a2842486b63e6f6d;p=libs%2Fdatafile.git diff --git a/source/parser.cpp b/source/parser.cpp index 8707ced..8fccec2 100644 --- a/source/parser.cpp +++ b/source/parser.cpp @@ -1,21 +1,20 @@ -/* -This file is part of libmspparser -Copyright © 2006 Mikko Rasa, Mikkosoft Productions +/* $Id$ + +This file is part of libmspdatafile +Copyright © 2006 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ #include -#include -#include +#include +#include "error.h" #include "parser.h" #include "statement.h" #include "token.h" using namespace std; -#include - namespace Msp { -namespace Parser { +namespace DataFile { Parser::Parser(istream &i, const string &s): in(i), @@ -27,7 +26,7 @@ Statement Parser::parse() { if(!good) throw Exception("Parser is not good"); - + try { return parse_(0); @@ -44,7 +43,7 @@ Statement Parser::parse_(const Token *t) Statement result; bool sub=false; bool finish=false; - + while(in) { Token token; @@ -55,13 +54,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 +82,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 +97,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 +116,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<='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,8 +371,10 @@ 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<