*/
#include <cctype>
#include <sstream>
-#include <msp/error.h>
+#include "error.h"
#include "parser.h"
#include "statement.h"
#include "token.h"
{
if(!good)
throw Exception("Parser is not good");
-
+
try
{
return parse_(0);
Statement result;
bool sub=false;
bool finish=false;
-
+
while(in)
{
Token token;
}
else
token=parse_token();
-
+
if(result.keyword.empty())
{
if(token.str.empty())
break;
else if(token.type!=Token::IDENTIFIER)
- throw DataError(get_location()+": Syntax error at token '"+token.str+"' (expected an identifier)");
+ 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;
else if(finish)
{
if(token.str!=";")
- throw DataError(get_location()+": Syntax error at token '"+token.str+"' (Expected a ';')");
+ throw ParseError(get_location()+": Syntax error at token '"+token.str+"' (Expected a ';')", src, in.get_line_number());
break;
}
else if(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;
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=='*')
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,
if(state!=INIT)
c=in.get();
int next=in.peek();
-
+
buf+=c;
switch(state)
else
parse_error(c, state);
break;
-
+
case SIGN:
if(c=='0')
state=ZERO;
else
escape=false;
break;
-
+
case IDENTIFIER:
if(!isalpha(c) && !isdigit(c) && c!='_')
parse_error(c, state);
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)
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)
{
ostringstream ss;
ss<<get_location()<<": Parse error at '"<<(char)c<<"' (state "<<state<<')';
- throw DataError(ss.str());
+ throw ParseError(ss.str(), src, in.get_line_number());
}
} // namespace Parser