00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #define _PARSEFUN_SOURCE_
00028
00029 #include "setup.h"
00030
00031 #include <string.h>
00032
00033 #include "argacces.h"
00034 #include "cstrcpsr.h"
00035 #include "envrnmnt.h"
00036 #include "exprnpsr.h"
00037 #include "extnfunc.h"
00038 #include "memalloc.h"
00039 #include "multifld.h"
00040 #include "prcdrpsr.h"
00041 #include "router.h"
00042 #include "strngrtr.h"
00043 #include "utility.h"
00044
00045 #include "parsefun.h"
00046
00047 #define PARSEFUN_DATA 11
00048
00049 struct parseFunctionData
00050 {
00051 char *ErrorString;
00052 size_t ErrorCurrentPosition;
00053 size_t ErrorMaximumPosition;
00054 char *WarningString;
00055 size_t WarningCurrentPosition;
00056 size_t WarningMaximumPosition;
00057 };
00058
00059 #define ParseFunctionData(theEnv) ((struct parseFunctionData *) GetEnvironmentData(theEnv,PARSEFUN_DATA))
00060
00061
00062
00063
00064
00065 #if (! RUN_TIME) && (! BLOAD_ONLY)
00066 static int FindErrorCapture(void *,char *);
00067 static int PrintErrorCapture(void *,char *,char *);
00068 static void DeactivateErrorCapture(void *);
00069 static void SetErrorCaptureValues(void *,DATA_OBJECT_PTR);
00070 #endif
00071
00072
00073
00074
00075
00076 globle void ParseFunctionDefinitions(
00077 void *theEnv)
00078 {
00079 AllocateEnvironmentData(theEnv,PARSEFUN_DATA,sizeof(struct parseFunctionData),NULL);
00080
00081 #if ! RUN_TIME
00082 EnvDefineFunction2(theEnv,"check-syntax",'u',PTIEF CheckSyntaxFunction,"CheckSyntaxFunction","11s");
00083 #endif
00084 }
00085
00086 #if (! RUN_TIME) && (! BLOAD_ONLY)
00087
00088
00089
00090
00091 globle void CheckSyntaxFunction(
00092 void *theEnv,
00093 DATA_OBJECT *returnValue)
00094 {
00095 DATA_OBJECT theArg;
00096
00097
00098
00099
00100
00101
00102 SetpType(returnValue,SYMBOL);
00103 SetpValue(returnValue,EnvTrueSymbol(theEnv));
00104
00105
00106
00107
00108
00109 if (EnvArgCountCheck(theEnv,"check-syntax",EXACTLY,1) == -1) return;
00110
00111
00112
00113
00114
00115 if (EnvArgTypeCheck(theEnv,"check-syntax",1,STRING,&theArg) == FALSE)
00116 { return; }
00117
00118
00119
00120
00121
00122 CheckSyntax(theEnv,DOToString(theArg),returnValue);
00123 }
00124
00125
00126
00127
00128
00129 globle int CheckSyntax(
00130 void *theEnv,
00131 char *theString,
00132 DATA_OBJECT_PTR returnValue)
00133 {
00134 char *name;
00135 struct token theToken;
00136 struct expr *top;
00137 short rv;
00138
00139
00140
00141
00142
00143
00144 SetpType(returnValue,SYMBOL);
00145 SetpValue(returnValue,EnvTrueSymbol(theEnv));
00146
00147
00148
00149
00150
00151
00152 if (OpenStringSource(theEnv,"check-syntax",theString,0) == 0)
00153 { return(TRUE); }
00154
00155
00156
00157
00158
00159
00160 GetToken(theEnv,"check-syntax",&theToken);
00161
00162 if (theToken.type != LPAREN)
00163 {
00164 CloseStringSource(theEnv,"check-syntax");
00165 SetpValue(returnValue,EnvAddSymbol(theEnv,"MISSING-LEFT-PARENTHESIS"));
00166 return(TRUE);
00167 }
00168
00169
00170
00171
00172
00173
00174 GetToken(theEnv,"check-syntax",&theToken);
00175 if (theToken.type != SYMBOL)
00176 {
00177 CloseStringSource(theEnv,"check-syntax");
00178 SetpValue(returnValue,EnvAddSymbol(theEnv,"EXPECTED-SYMBOL-AFTER-LEFT-PARENTHESIS"));
00179 return(TRUE);
00180 }
00181
00182 name = ValueToString(theToken.value);
00183
00184
00185
00186
00187
00188 EnvAddRouter(theEnv,"error-capture",40,
00189 FindErrorCapture, PrintErrorCapture,
00190 NULL, NULL, NULL);
00191
00192
00193
00194
00195
00196 if (FindConstruct(theEnv,name))
00197 {
00198 ConstructData(theEnv)->CheckSyntaxMode = TRUE;
00199 rv = (short) ParseConstruct(theEnv,name,"check-syntax");
00200 GetToken(theEnv,"check-syntax",&theToken);
00201 ConstructData(theEnv)->CheckSyntaxMode = FALSE;
00202
00203 if (rv)
00204 {
00205 EnvPrintRouter(theEnv,WERROR,"\nERROR:\n");
00206 PrintInChunks(theEnv,WERROR,GetPPBuffer(theEnv));
00207 EnvPrintRouter(theEnv,WERROR,"\n");
00208 }
00209
00210 DestroyPPBuffer(theEnv);
00211
00212 CloseStringSource(theEnv,"check-syntax");
00213
00214 if ((rv != FALSE) || (ParseFunctionData(theEnv)->WarningString != NULL))
00215 {
00216 SetErrorCaptureValues(theEnv,returnValue);
00217 DeactivateErrorCapture(theEnv);
00218 return(TRUE);
00219 }
00220
00221 if (theToken.type != STOP)
00222 {
00223 SetpValue(returnValue,EnvAddSymbol(theEnv,"EXTRANEOUS-INPUT-AFTER-LAST-PARENTHESIS"));
00224 DeactivateErrorCapture(theEnv);
00225 return(TRUE);
00226 }
00227
00228 SetpType(returnValue,SYMBOL);
00229 SetpValue(returnValue,EnvFalseSymbol(theEnv));
00230 DeactivateErrorCapture(theEnv);
00231 return(FALSE);
00232 }
00233
00234
00235
00236
00237
00238 top = Function2Parse(theEnv,"check-syntax",name);
00239 GetToken(theEnv,"check-syntax",&theToken);
00240 ClearParsedBindNames(theEnv);
00241 CloseStringSource(theEnv,"check-syntax");
00242
00243 if (top == NULL)
00244 {
00245 SetErrorCaptureValues(theEnv,returnValue);
00246 DeactivateErrorCapture(theEnv);
00247 return(TRUE);
00248 }
00249
00250 if (theToken.type != STOP)
00251 {
00252 SetpValue(returnValue,EnvAddSymbol(theEnv,"EXTRANEOUS-INPUT-AFTER-LAST-PARENTHESIS"));
00253 DeactivateErrorCapture(theEnv);
00254 ReturnExpression(theEnv,top);
00255 return(TRUE);
00256 }
00257
00258 DeactivateErrorCapture(theEnv);
00259
00260 ReturnExpression(theEnv,top);
00261 SetpType(returnValue,SYMBOL);
00262 SetpValue(returnValue,EnvFalseSymbol(theEnv));
00263 return(FALSE);
00264 }
00265
00266
00267
00268
00269
00270
00271 static void DeactivateErrorCapture(
00272 void *theEnv)
00273 {
00274 if (ParseFunctionData(theEnv)->ErrorString != NULL)
00275 {
00276 rm(theEnv,ParseFunctionData(theEnv)->ErrorString,ParseFunctionData(theEnv)->ErrorMaximumPosition);
00277 ParseFunctionData(theEnv)->ErrorString = NULL;
00278 }
00279
00280 if (ParseFunctionData(theEnv)->WarningString != NULL)
00281 {
00282 rm(theEnv,ParseFunctionData(theEnv)->WarningString,ParseFunctionData(theEnv)->WarningMaximumPosition);
00283 ParseFunctionData(theEnv)->WarningString = NULL;
00284 }
00285
00286 ParseFunctionData(theEnv)->ErrorCurrentPosition = 0;
00287 ParseFunctionData(theEnv)->ErrorMaximumPosition = 0;
00288 ParseFunctionData(theEnv)->WarningCurrentPosition = 0;
00289 ParseFunctionData(theEnv)->WarningMaximumPosition = 0;
00290
00291 EnvDeleteRouter(theEnv,"error-capture");
00292 }
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302 static void SetErrorCaptureValues(
00303 void *theEnv,
00304 DATA_OBJECT_PTR returnValue)
00305 {
00306 struct multifield *theMultifield;
00307
00308 theMultifield = (struct multifield *) EnvCreateMultifield(theEnv,2L);
00309
00310 if (ParseFunctionData(theEnv)->ErrorString != NULL)
00311 {
00312 SetMFType(theMultifield,1,STRING);
00313 SetMFValue(theMultifield,1,EnvAddSymbol(theEnv,ParseFunctionData(theEnv)->ErrorString));
00314 }
00315 else
00316 {
00317 SetMFType(theMultifield,1,SYMBOL);
00318 SetMFValue(theMultifield,1,EnvFalseSymbol(theEnv));
00319 }
00320
00321 if (ParseFunctionData(theEnv)->WarningString != NULL)
00322 {
00323 SetMFType(theMultifield,2,STRING);
00324 SetMFValue(theMultifield,2,EnvAddSymbol(theEnv,ParseFunctionData(theEnv)->WarningString));
00325 }
00326 else
00327 {
00328 SetMFType(theMultifield,2,SYMBOL);
00329 SetMFValue(theMultifield,2,EnvFalseSymbol(theEnv));
00330 }
00331
00332 SetpType(returnValue,MULTIFIELD);
00333 SetpDOBegin(returnValue,1);
00334 SetpDOEnd(returnValue,2);
00335 SetpValue(returnValue,(void *) theMultifield);
00336 }
00337
00338
00339
00340
00341
00342 #if WIN_BTC
00343 #pragma argsused
00344 #endif
00345 static int FindErrorCapture(
00346 void *theEnv,
00347 char *logicalName)
00348 {
00349 #if MAC_MCW || WIN_MCW || MAC_XCD
00350 #pragma unused(theEnv)
00351 #endif
00352
00353 if ((strcmp(logicalName,WERROR) == 0) ||
00354 (strcmp(logicalName,WWARNING) == 0))
00355 { return(TRUE); }
00356
00357 return(FALSE);
00358 }
00359
00360
00361
00362
00363
00364 static int PrintErrorCapture(
00365 void *theEnv,
00366 char *logicalName,
00367 char *str)
00368 {
00369 if (strcmp(logicalName,WERROR) == 0)
00370 {
00371 ParseFunctionData(theEnv)->ErrorString = AppendToString(theEnv,str,ParseFunctionData(theEnv)->ErrorString,
00372 &ParseFunctionData(theEnv)->ErrorCurrentPosition,
00373 &ParseFunctionData(theEnv)->ErrorMaximumPosition);
00374 }
00375 else if (strcmp(logicalName,WWARNING) == 0)
00376 {
00377 ParseFunctionData(theEnv)->WarningString = AppendToString(theEnv,str,ParseFunctionData(theEnv)->WarningString,
00378 &ParseFunctionData(theEnv)->WarningCurrentPosition,
00379 &ParseFunctionData(theEnv)->WarningMaximumPosition);
00380 }
00381
00382 return(1);
00383 }
00384
00385 #else
00386
00387
00388
00389
00390 globle void CheckSyntaxFunction(
00391 void *theEnv,
00392 DATA_OBJECT *returnValue)
00393 {
00394 PrintErrorID(theEnv,"PARSEFUN",1,FALSE);
00395 EnvPrintRouter(theEnv,WERROR,"Function check-syntax does not work in run time modules.\n");
00396 SetpType(returnValue,SYMBOL);
00397 SetpValue(returnValue,EnvTrueSymbol(theEnv));
00398 }
00399
00400
00401
00402
00403
00404 globle int CheckSyntax(
00405 void *theEnv,
00406 char *theString,
00407 DATA_OBJECT_PTR returnValue)
00408 {
00409 #if (MAC_MCW || WIN_MCW) && (RUN_TIME || BLOAD_ONLY)
00410 #pragma unused(theString)
00411 #pragma unused(returnValue)
00412 #endif
00413
00414 PrintErrorID(theEnv,"PARSEFUN",1,FALSE);
00415 EnvPrintRouter(theEnv,WERROR,"Function check-syntax does not work in run time modules.\n");
00416 SetpType(returnValue,SYMBOL);
00417 SetpValue(returnValue,EnvTrueSymbol(theEnv));
00418 return(TRUE);
00419 }
00420
00421 #endif
00422
00423