void listfiles(const char *dir, const char *ext, vector<char *> &files, int (__cdecl *sf)(const char **, const char **)) { listdir(dir, ext, files); string s; if(homedir[0]) { formatstring(s)("%s%s", homedir, dir); listdir(s, ext, files); } loopv(packagedirs) { formatstring(s)("%s%s", packagedirs[i], dir); listdir(s, ext, files); } #ifndef STANDALONE listzipfiles(dir, ext, files); #endif if(sf) { // sort and remove doubles files.sort(sf); for(int i = files.length() - 1; i > 0; i--) { if(!strcmp(files[i], files[i - 1])) delstring(files.remove(i)); } } }
void listfilesrecursive(const char *dir, vector<char *> &files, int level) { if(level > 8) return; // 8 levels is insane enough... vector<char *> dirs, thisdir; listsubdirs(dir, dirs, stringsort); loopv(dirs) { if(dirs[i][0] != '.') // ignore "." and ".." (and also other directories starting with '.', like it is unix-convention - and doesn't hurt on windows) { defformatstring(name)("%s/%s", dir, dirs[i]); listfilesrecursive(name, files, level + 1); } delstring(dirs[i]); } listfiles(dir, NULL, thisdir); loopv(thisdir) { defformatstring(name)("%s/%s", dir, thisdir[i]); files.add(newstring(name)); delstring(thisdir[i]); } }
void listsubdirs(const char *dir, vector<char *> &subdirs, int (__cdecl *sf)(const char **, const char **)) { listsubdir(dir, subdirs); string s; if(homedir[0]) { formatstring(s)("%s%s", homedir, dir); listsubdir(s, subdirs); } loopv(packagedirs) { formatstring(s)("%s%s", packagedirs[i], dir); listsubdir(s, subdirs); } #ifndef STANDALONE listzipdirs(dir, subdirs); #endif subdirs.sort(sf); for(int i = subdirs.length() - 1; i > 0; i--) { // remove doubles if(!strcmp(subdirs[i], subdirs[i - 1])) delstring(subdirs.remove(i)); } }
/** displayRecognitionError()함수를 변경 */ void produceError4Parser(pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_UINT8 * tokenNames) { pANTLR3_PARSER parser; pANTLR3_TREE_PARSER tparser; pANTLR3_INT_STREAM is; pANTLR3_STRING ttext; pANTLR3_STRING ftext; pANTLR3_EXCEPTION ex; pANTLR3_COMMON_TOKEN theToken; pANTLR3_BASE_TREE theBaseTree; pANTLR3_COMMON_TREE theCommonTree; //char* p; FILE *out; out = fopen("ParErr.txt", "ab"); // 에러를 저장할 파일 ANTLR3_FPRINTF(out, "\r\n"); // Retrieve some info for easy reading. // ex = recognizer->state->exception; ttext = NULL; // See if there is a 'filename' we can use // if (ex->streamName == NULL) { if (((pANTLR3_COMMON_TOKEN)(ex->token))->type == ANTLR3_TOKEN_EOF) { ANTLR3_FPRINTF(out, "-end of input-("); } else { ANTLR3_FPRINTF(out, "-unknown source-("); } } else { /* rwkim 수정*/ char curPath[200]; getcwd(curPath, 200); ftext = ex->streamName->to8(ex->streamName); delstring(ftext->chars, curPath); //앞의 경로 제거 ANTLR3_FPRINTF(out, "%s(", ftext->chars); } // Next comes the line number // ANTLR3_FPRINTF(out, "%d) ", recognizer->state->exception->line); //ANTLR3_FPRINTF(out, " : error %d : %s", // recognizer->state->exception->type, // (pANTLR3_UINT8) (recognizer->state->exception->message)); //다음과 같아 바꾸었음 ANTLR3_FPRINTF(out, " : %s", (pANTLR3_UINT8)(recognizer->state->exception->message)); // How we determine the next piece is dependent on which thing raised the // error. // switch (recognizer->type) { case ANTLR3_TYPE_PARSER: // Prepare the knowledge we know we have // parser = (pANTLR3_PARSER) (recognizer->super); tparser = NULL; is = parser->tstream->istream; theToken = (pANTLR3_COMMON_TOKEN)(recognizer->state->exception->token); ttext = theToken->toString(theToken); /* rwkim 수정*/ //p = strchr(ttext->chars, '='); //ttext->chars = p; ANTLR3_FPRINTF(out, " at offset %d", recognizer->state->exception->charPositionInLine); if (theToken != NULL) { if (theToken->type == ANTLR3_TOKEN_EOF) { ANTLR3_FPRINTF(out, ", at <EOF>"); } else { // Guard against null text in a token // ANTLR3_FPRINTF(out, "\r\n near %s\r\n ", ttext == NULL ? (pANTLR3_UINT8)"<no text for the token>" : ttext->chars); } } break; case ANTLR3_TYPE_TREE_PARSER: tparser = (pANTLR3_TREE_PARSER) (recognizer->super); parser = NULL; is = tparser->ctnstream->tnstream->istream; theBaseTree = (pANTLR3_BASE_TREE)(recognizer->state->exception->token); ttext = theBaseTree->toStringTree(theBaseTree); if (theBaseTree != NULL) { theCommonTree = (pANTLR3_COMMON_TREE) theBaseTree->super; if (theCommonTree != NULL) { theToken = (pANTLR3_COMMON_TOKEN) theBaseTree->getToken(theBaseTree); } ANTLR3_FPRINTF(out, ", at offset %d", theBaseTree->getCharPositionInLine(theBaseTree)); ANTLR3_FPRINTF(out, ", near %s", ttext->chars); } break; default: ANTLR3_FPRINTF(out, "Base recognizer function displayRecognitionError called by unknown parser type - provide override for this function\r\n"); return; break; } // Although this function should generally be provided by the implementation, this one // should be as helpful as possible for grammar developers and serve as an example // of what you can do with each exception type. In general, when you make up your // 'real' handler, you should debug the routine with all possible errors you expect // which will then let you be as specific as possible about all circumstances. // // Note that in the general case, errors thrown by tree parsers indicate a problem // with the output of the parser or with the tree grammar itself. The job of the parser // is to produce a perfect (in traversal terms) syntactically correct tree, so errors // at that stage should really be semantic errors that your own code determines and handles // in whatever way is appropriate. // switch (ex->type) { case ANTLR3_UNWANTED_TOKEN_EXCEPTION: // Indicates that the recognizer was fed a token which seesm to be // spurious input. We can detect this when the token that follows // this unwanted token would normally be part of the syntactically // correct stream. Then we can see that the token we are looking at // is just something that should not be there and throw this exception. // if (tokenNames == NULL) { ANTLR3_FPRINTF(out, " : Extraneous input..."); } else { if (ex->expecting == ANTLR3_TOKEN_EOF) { ANTLR3_FPRINTF(out, " : Extraneous input - expected <EOF>\r\n"); } else { ANTLR3_FPRINTF(out, " : Extraneous input - expected %s ...\r\n", tokenNames[ex->expecting]); } } break; case ANTLR3_MISSING_TOKEN_EXCEPTION: // Indicates that the recognizer detected that the token we just // hit would be valid syntactically if preceeded by a particular // token. Perhaps a missing ';' at line end or a missing ',' in an // expression list, and such like. // if (tokenNames == NULL) { ANTLR3_FPRINTF(out, " : Missing token (%d)...\r\n", ex->expecting); } else { if (ex->expecting == ANTLR3_TOKEN_EOF) { ANTLR3_FPRINTF(out, " : Missing <EOF>\r\n"); } else { ANTLR3_FPRINTF(out, " : Missing %s \r\n", tokenNames[ex->expecting]); } } break; case ANTLR3_RECOGNITION_EXCEPTION: // Indicates that the recognizer received a token // in the input that was not predicted. This is the basic exception type // from which all others are derived. So we assume it was a syntax error. // You may get this if there are not more tokens and more are needed // to complete a parse for instance. // ANTLR3_FPRINTF(out, " : syntax error...\r\n"); break; case ANTLR3_MISMATCHED_TOKEN_EXCEPTION: // We were expecting to see one thing and got another. This is the // most common error if we coudl not detect a missing or unwanted token. // Here you can spend your efforts to // derive more useful error messages based on the expected // token set and the last token and so on. The error following // bitmaps do a good job of reducing the set that we were looking // for down to something small. Knowing what you are parsing may be // able to allow you to be even more specific about an error. // if (tokenNames == NULL) { ANTLR3_FPRINTF(out, " : syntax error...\r\n"); } else { if (ex->expecting == ANTLR3_TOKEN_EOF) { ANTLR3_FPRINTF(out, " : expected <EOF>\r\n"); } else { ANTLR3_FPRINTF(out, " : expected %s ...\r\n", tokenNames[ex->expecting]); } } break; case ANTLR3_NO_VIABLE_ALT_EXCEPTION: // We could not pick any alt decision from the input given // so god knows what happened - however when you examine your grammar, // you should. It means that at the point where the current token occurred // that the DFA indicates nowhere to go from here. // ANTLR3_FPRINTF(out, " : cannot match to any predicted input...\r\n"); break; case ANTLR3_MISMATCHED_SET_EXCEPTION: { ANTLR3_UINT32 count; ANTLR3_UINT32 bit; ANTLR3_UINT32 size; ANTLR3_UINT32 numbits; pANTLR3_BITSET errBits; // This means we were able to deal with one of a set of // possible tokens at this point, but we did not see any // member of that set. // ANTLR3_FPRINTF(out, " : unexpected input...\r\n expected one of : "); // What tokens could we have accepted at this point in the // parse? // count = 0; errBits = antlr3BitsetLoad (ex->expectingSet); numbits = errBits->numBits (errBits); size = errBits->size (errBits); if (size > 0) { // However many tokens we could have dealt with here, it is usually // not useful to print ALL of the set here. I arbitrarily chose 8 // here, but you should do whatever makes sense for you of course. // No token number 0, so look for bit 1 and on. // for (bit = 1; bit < numbits && count < 8 && count < size; bit++) { // TODO: This doesn;t look right - should be asking if the bit is set!! // if (tokenNames[bit]) { ANTLR3_FPRINTF(out, "%s%s", count > 0 ? ", " : "", tokenNames[bit]); count++; } } ANTLR3_FPRINTF(out, "\r\n"); } else { ANTLR3_FPRINTF(out, "Actually dude, we didn't seem to be expecting anything here, or at least\r\n"); ANTLR3_FPRINTF(out, "I could not work out what I was expecting, like so many of us these days!\r\n"); } } break; case ANTLR3_EARLY_EXIT_EXCEPTION: // We entered a loop requiring a number of token sequences // but found a token that ended that sequence earlier than // we should have done. // ANTLR3_FPRINTF(out, " : missing elements...\r\n"); break; default: // We don't handle any other exceptions here, but you can // if you wish. If we get an exception that hits this point // then we are just going to report what we know about the // token. // ANTLR3_FPRINTF(out, " : syntax not recognized...\r\n"); break; } // Here you have the token that was in error which if this is // the standard implementation will tell you the line and offset // and also record the address of the start of the line in the // input stream. You could therefore print the source line and so on. // Generally though, I would expect that your lexer/parser will keep // its own map of lines and source pointers or whatever as there // are a lot of specific things you need to know about the input // to do something like that. // Here is where you do it though :-). // fclose(out); }