Rev 1 | Blame | Compare with Previous | Last modification | View Log | RSS feed
#include "bod_parser_base.h"#include "../common/strutils.h"#include "../common/string_builder.h"#include <stdarg.h>#include <stdlib.h>#include <stdio.h>//---------------------------------------------------------------------------------void bod_parser_base::error(const bod_text_parser::token *t, ErrorSeverity severity, const char *format, ...){va_list ap;va_start(ap, format);Error *e=new Error();e->makeCode(F_Compiler, severity, E_Error);if(t){e->line=t->line;e->col=t->col;}else{e->line=-1;e->col=-1;}_vsnprintf(e->text, sizeof(e->text), format, ap);va_end(ap);errors.push_back(e);}//---------------------------------------------------------------------------------bod_text_parser::token* bod_parser_base::loadValue(token_stream& is, const char *context){bod_text_parser::token* t=is.tok();bod_text_parser::token* res;if(t==NULL || t->type!=token::t_text){error(t ? t : is.previous(), S_Error, "%sExpected value after '%s'", context, is.previous()->getText());return NULL;}res=t;t=(++is).tok();if(t==NULL || t->type!=token::t_semicolon){error(is.previous(), S_Error, "%sExpected ';' after '%s'", context, is.previous()->getText());return NULL;}++is;return res;}//---------------------------------------------------------------------------------bool bod_parser_base::ungetValue(token_stream& s){if(s.fail()) return false;if(s.tell() >= 2){--s;if(s.tok()->type==bod_text_parser::token::t_semicolon){--s;if(s.tok()->type==bod_text_parser::token::t_text)return true;}}s.setstate(token_stream::failbit);return false;}//---------------------------------------------------------------------------------bool bod_parser_base::loadIntValue(token_stream& is, int *i, const char *msg){const char *str;token *t=loadValue(is, msg);bool bRes;if(bRes=(t!=NULL)){str=t->text;while(*str!=0 && *str==' ')str++;if(*str=='0' && ((str[1] | 0x20)=='x')){if(bRes=ishexa(t->text))*i=hextoi(t->text);}else{if(bRes=isinteger(t->text))*i=atoi(t->text);}if(bRes==false)error(t, S_Error, "%sExpected integer value", msg);}return bRes;}//---------------------------------------------------------------------------------bool bod_parser_base::loadDoubleValue(token_stream& is, double *d, const char *msg){token *t=loadValue(is, msg);bool bRes;if(bRes=(t!=NULL)){if((bRes=isfloat(t->text))==false)error(t, S_Error, "%sExpected float value", msg);else*d=atof(t->text);}return bRes;}//---------------------------------------------------------------------------------/*load a "bod string" - should only be used when the parser is set to "bod compatibility"*/char * bod_parser_base::loadString(token_stream& is, const char *context){bod_text_parser::token *t, *prev=NULL;string_builder str="";//while((t=is.tok())!=NULL && (t->type==token::t_text || t->type==token::t_colon)){while((t=is.tok())!=NULL && !(t->type==token::t_semicolon || t->type==token::t_openCrBracket || t->type==token::t_closeCrBracket)){if(prev!=NULL && prev->type==token::t_text && t->type==token::t_text){str << ' ';}str << t->getText();prev=t;++is;}if(t==NULL){error(is.previous(), S_Error, "%sUnexpected end of document found while looking for end of string (;)", context);return NULL;}if(t->type!=token::t_semicolon){error(t, S_Error, "%sUnexpected here '%s' while loading string", context, t->getText());return NULL;}++is;return is.fail() ? NULL : str.getbuffer();}//---------------------------------------------------------------------------------