+static dictionary_t defines;
+static int defines_initialized = 0;
+static mem_t define_values;
+
+static double parseNameOrTwip(char*s)
+{
+ int l = 0;
+ double v;
+ if(defines_initialized) {
+ l = (int)dictionary_lookup(&defines, s);
+ }
+ if(l) {
+ return *(int*)&define_values.buffer[l-1];
+ } else {
+ return parseRawTwip(s);
+ }
+}
+
+/* automatically generated by yiyiyacc, http://www.quiss.org/yiyiyacc/ */
+static double parseExpression(char*s)
+{
+ int chr2index[256];
+ memset(chr2index, -1, sizeof(chr2index));
+ chr2index['+'] = 0;
+ chr2index['-'] = 1;
+ chr2index['*'] = 2;
+ chr2index['/'] = 3;
+ chr2index['('] = 5;
+ chr2index[')'] = 6;
+ chr2index['\0'] = 7;
+
+ int stackpos = 1;
+ int stack[256];
+ double values[256];
+ stack[0]=0;
+ values[0]=0;
+ int accept = 18;
+ int left[10]={11,8,8,8,8,9,9,9,10,10}; //production left side
+ int plen[10]={1,3,2,3,1,3,3,1,1,3}; //production size
+ int table[18][12] = {
+ {0, 4, 0, 0, 5, 6, 0, 0, 1, 2, 3, 0},
+ {7, 8, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0},
+ {-4, -4, 9, 10, 0, 0, -4, -4, 0, 0, 0, 0},
+ {-7, -7, -7, -7, 0, 0, -7, -7, 0, 0, 0, 0},
+ {0, 0, 0, 0, 5, 6, 0, 0, 0, 11, 3, 0},
+ {-8, -8, -8, -8, 0, 0, -8, -8, 0, 0, 0, 0},
+ {0, 4, 0, 0, 5, 6, 0, 0, 12, 2, 3, 0},
+ {0, 0, 0, 0, 5, 6, 0, 0, 0, 13, 3, 0},
+ {0, 0, 0, 0, 5, 6, 0, 0, 0, 14, 3, 0},
+ {0, 0, 0, 0, 5, 6, 0, 0, 0, 0, 15, 0},
+ {0, 0, 0, 0, 5, 6, 0, 0, 0, 0, 16, 0},
+ {-2, -2, 9, 10, 0, 0, -2, -2, 0, 0, 0, 0},
+ {7, 8, 0, 0, 0, 0, 17, 0, 0, 0, 0, 0},
+ {-1, -1, 9, 10, 0, 0, -1, -1, 0, 0, 0, 0},
+ {-3, -3, 9, 10, 0, 0, -3, -3, 0, 0, 0, 0},
+ {-5, -5, -5, -5, 0, 0, -5, -5, 0, 0, 0, 0},
+ {-6, -6, -6, -6, 0, 0, -6, -6, 0, 0, 0, 0},
+ {-9, -9, -9, -9, 0, 0, -9, -9, 0, 0, 0, 0}};
+
+ char*p = s;
+ while(1) {
+ char*pnext = p+1;
+ int action;
+ double value = 0;
+ if(!stackpos) {
+ fprintf(stderr, "Error in expression\n");
+ return 0.0;
+ }
+
+ if(chr2index[*p]<0) {
+ action = table[stack[stackpos-1]][4];
+ if(action>0) {
+ while(chr2index[*pnext]<0)
+ pnext++;
+ char save = *pnext;
+ *pnext = 0;
+ value = parseNameOrTwip(p);
+ *pnext = save;
+ }
+ } else {
+ action = table[stack[stackpos-1]][chr2index[*p]];
+ }
+
+ if(action == accept) {
+ return values[stack[stackpos-1]];
+ } else if(action>0) { // shift
+ if(stackpos>254) {
+ fprintf(stderr, "Stack overflow while parsing expression\n");
+ return 0.0;
+ }
+ values[stackpos]=value;
+ stack[stackpos++]=action;
+ p=pnext;
+ } else if(action<0) { // reduce
+ stackpos-=plen[-action];
+ stack[stackpos] = table[stack[stackpos-1]][left[-action]];
+ switch(-action) {
+ case 1:
+ values[stackpos] = values[stackpos+0] + values[stackpos+2];
+ break;
+ case 2:
+ values[stackpos] = 0 - values[stackpos+1];
+ break;
+ case 3:
+ values[stackpos] = values[stackpos+0] - values[stackpos+2];
+ break;
+ case 5:
+ values[stackpos] = values[stackpos+0] * values[stackpos+2];
+ break;
+ case 6:
+ values[stackpos] = values[stackpos+0] / values[stackpos+2];
+ break;
+ case 9:
+ values[stackpos] = values[stackpos+1];
+ break;
+ }
+ stackpos++;
+ } else {
+ fprintf(stderr, "Syntax error in expression\n");
+ return 0.0;
+ }
+ }
+}
+
+int parseTwip(char*str)
+{
+ int v = (int)(parseExpression(str)*20);
+ return v;
+}
+
+int parseArc(char* str)
+{
+ if (!strcmp(str, "short"))
+ return 1;
+ if (!strcmp(str, "long"))
+ return 0;
+ syntaxerror("invalid value for the arc parameter: %s", str);
+ return 1;
+}
+
+int parseDir(char* str)
+{
+ if (!strcmp(str, "clockwise"))
+ return 1;
+ if (!strcmp(str, "counterclockwise"))
+ return 0;
+ syntaxerror("invalid value for the dir parameter: %s", str);
+ return 1;
+}
+