00001 #include "parser.h"
00002 #include "refs.h"
00003 #include "vars.h"
00004 #include "value.h"
00005 #include "text.h"
00006 #include "ranges.h"
00007 #include "refs.h"
00008
00009 ParseText::ParseText(QString text)
00010 {
00011 fulltext=NULL;
00012 currptr = NULL;
00013 expression = NULL;
00014 parse(text);
00015 };
00016
00017 void ParseText::parse(QString text)
00018 {
00019 initText(text);
00020 expression = parse();
00021 }
00022
00023 Value * ParseText::properVarType(QString tokentext)
00024 {
00025
00026 int c,r;
00027 bool ac,ar;
00028 int i;
00029 if ((i=tokentext.find(':'))>=0)
00030 {
00031 printf("%s\n",(const char*)tokentext.left(i));
00032 printf("%s\n",(const char*)tokentext.mid(i+1));
00033 bool res = getLocation(tokentext.left(i),c,r,ac,ar);
00034 assert(res);
00035 Reference * a= new Reference(c,r,ac,ar);
00036 res = getLocation(tokentext.mid(i+1),c,r,ac,ar);
00037 assert(res);
00038 Reference * b= new Reference(c,r,ac,ar);
00039 return new Range(a,b);
00040 }
00041 if (getLocation(tokentext,c,r,ac,ar))
00042 return new Reference(c,r,ac,ar);
00043 return new Variable(tokentext);
00044 }
00045
00046 bool ParseText::getLocation(QString text, int &C, int &R, bool &absolute_column, bool& absolute_row)
00047 {
00048 const char * t = text;
00049 const char * c = t;
00050 int column;
00051 int row;
00052
00053 if (absolute_column=(*c=='$')) c++;
00054 column = 0;
00055 while(*c && !isdigit(*c) && *c!='$')
00056 {
00057 column=column*26+toupper(*c)-'A';
00058 c++;
00059 }
00060
00061 if (absolute_row=(*c=='$')) c++;
00062 row = 0;
00063 while(*c && isdigit(*c) && *c!='$')
00064 {
00065 row=row*10+toupper(*c)-'0';
00066 c++;
00067 }
00068
00069 C=column;
00070 R=row;
00071 return true;
00072 }
00073
00074 Value * ParseText::parse()
00075 {
00076 Token t = token();
00077 printf("%d\n",t);
00078 switch (t)
00079 {
00080 case text: return new TextValue(tokentext);
00081 case var: return properVarType(tokentext);
00082 case openexpr:
00083 {
00084 t = token();
00085 if (t!=var)
00086 throw new ParseError("Operand is no operator");
00087 Expression * result = new Expression(tokentext);
00088 Value * arg=NULL;
00089 while((arg=parse()))
00090 result->add(arg);
00091 return result;
00092 }
00093 case closeexpr: return NULL;
00094 case eof: throw new ParseError("Unexpected end of line");
00095 };
00096 throw new ParseError("Unexpected token");
00097 return NULL;
00098 }
00099
00100 void ParseText::initText(QString text)
00101 {
00102 if (fulltext) free(fulltext);
00103 fulltext=strdup(text);
00104 currptr = fulltext;
00105 printf("To parse: %s\n",fulltext);
00106 expression = NULL;
00107 }
00108
00109 ParseText::Token ParseText::token()
00110 {
00111 if (!currptr) return eof;
00112
00113 while(*currptr && isspace(*currptr)) currptr++;
00114 if (!*currptr) return eof;
00115
00116 switch(*currptr)
00117 {
00118 case '(': currptr++; return openexpr;
00119 case ')': currptr++; return closeexpr;
00120 case '"': currptr++; return string_to('"');
00121 case '\'': currptr++; return string_to('\'');
00122 }
00123 bool d = isdigit(*currptr);
00124 text_to();
00125 if (d) return text;
00126 return var;
00127 }
00128
00129 bool ParseText::seperator(unsigned char c)
00130 {
00131 return c==0 || c==' ' || c=='(' || c=='\'' || c=='"' || c==')';
00132 }
00133
00134 ParseText::Token ParseText::string_to(unsigned char c)
00135 {
00136 char result[500];
00137 int i = 0;
00138 while(*currptr && *currptr!=c)
00139 result[i++]=*(currptr++);
00140 if(*currptr==c) currptr++;
00141 result[i]=0;
00142 tokentext=result;
00143 return text;
00144 }
00145
00146 void ParseText::text_to()
00147 {
00148 char result[500];
00149 int i = 0;
00150 while(!seperator(*currptr))
00151 result[i++]=*(currptr++);
00152 result[i]=0;
00153 tokentext=result;
00154 }
00155
00156