bool PatternList_match(const PatternList *patternList, const String string, PatternMatchModes patternMatchMode ) { bool matchFlag; PatternNode *patternNode; assert(patternList != NULL); assert(string != NULL); matchFlag = FALSE; patternNode = patternList->head; while ((patternNode != NULL) && !matchFlag) { matchFlag = Pattern_match(&patternNode->pattern,string,patternMatchMode); patternNode = patternNode->next; } return matchFlag; }
bool EntryList_match(const EntryList *entryList, const String string, PatternMatchModes patternMatchMode ) { bool matchFlag; EntryNode *entryNode; assert(entryList != NULL); assert(string != NULL); matchFlag = FALSE; entryNode = entryList->head; while ((entryNode != NULL) && !matchFlag) { matchFlag = Pattern_match(&entryNode->pattern,string,patternMatchMode); entryNode = entryNode->next; } return matchFlag; }
static long _Scanner_scan(Scanner *self, Pattern *restrictions, int restrictions_sz) { Token best_token, *p_token; Restriction *p_restriction; Pattern *regex; int j, k, max, skip; #ifdef DEBUG fprintf(stderr, "%s\n", __PRETTY_FUNCTION__); #endif while (1) { best_token.regex = NULL; /* Search the patterns for a match, with earlier tokens in the list having preference */ for (j = 0; j < Pattern_patterns_sz; j++) { Pattern *regex = &Pattern_patterns[j]; #ifdef DEBUG fprintf(stderr, "\tTrying %s: %s at pos %d -> %s\n", repr(regex->tok), repr(regex->expr), self->pos, repr(self->input)); #endif /* First check to see if we're restricting to this token */ skip = restrictions_sz; if (skip) { max = (restrictions_sz > self->ignore_sz) ? restrictions_sz : self->ignore_sz; for (k = 0; k < max; k++) { if (k < restrictions_sz && strcmp(regex->tok, restrictions[k].tok) == 0) { skip = 0; break; } if (k < self->ignore_sz && regex == self->ignore[k]) { skip = 0; break; } } if (skip) { continue; #ifdef DEBUG fprintf(stderr, "\tSkipping!\n"); #endif } } if (Pattern_match( regex, self->input, self->input_sz, self->pos, &best_token )) { #ifdef DEBUG fprintf(stderr, "\tMatch OK! %s: %s at pos %d\n", repr(regex->tok), repr(regex->expr), self->pos); #endif break; } } /* If we didn't find anything, raise an error */ if (best_token.regex == NULL) { if (restrictions_sz) { sprintf(self->exc, "SyntaxError[@ char %d: Trying to find one of the %d restricted tokens!]", self->pos, restrictions_sz); return SCANNER_EXC_RESTRICTED; } sprintf(self->exc, "SyntaxError[@ char %d: Bad Token!]", self->pos); return SCANNER_EXC_BAD_TOKEN; } /* If we found something that isn't to be ignored, return it */ skip = 0; for (k = 0; k < self->ignore_sz; k++) { if (best_token.regex == self->ignore[k]) { /* This token should be ignored... */ self->pos += best_token.string_sz; skip = 1; break; } } if (!skip) { break; } } if (best_token.regex) { self->pos = (int)(best_token.string - self->input + best_token.string_sz); /* Only add this token if it's not in the list (to prevent looping) */ p_token = &self->tokens[self->tokens_sz - 1]; if (self->tokens_sz == 0 || p_token->regex != best_token.regex || p_token->string != best_token.string || p_token->string_sz != best_token.string_sz ) { if (self->tokens_sz >= self->tokens_bsz) { /* Needs to expand block */ self->tokens_bsz = self->tokens_bsz + BLOCK_SIZE_PATTERNS; PyMem_Resize(self->tokens, Token, self->tokens_bsz); PyMem_Resize(self->restrictions, Restriction, self->tokens_bsz); } memcpy(&self->tokens[self->tokens_sz], &best_token, sizeof(Token)); p_restriction = &self->restrictions[self->tokens_sz]; if (restrictions_sz) { p_restriction->patterns = PyMem_New(Pattern *, restrictions_sz); p_restriction->patterns_sz = 0; for (j = 0; j < restrictions_sz; j++) { regex = Pattern_regex(restrictions[j].tok, restrictions[j].expr); if (regex) { p_restriction->patterns[p_restriction->patterns_sz++] = regex; } } } else { p_restriction->patterns = NULL; p_restriction->patterns_sz = 0; } self->tokens_sz++; return 1; }