struct regexp *regexp_new (char *regex, int flags) { struct regexp* result = malloc(sizeof(struct regexp)); RegexpTokenType* token_string; struct Expression* parsed_regexp; int i; if (result == NULL) goto failed; token_string = lex_regexp(regex); if (token_string == NULL) goto failed; parsed_regexp = parse_regexp(token_string); if (parsed_regexp == NULL) goto failed; result->nfa = regexp_to_nfa(parsed_regexp, 1, 0); /* Count groups */ result->num_groups = 0; for(i=0; token_string[i] != '\0'; i++) { if (token_string[i] == GROUP_OPEN) { result->num_groups++; } } /* Success if we get here*/ destroy_expression(parsed_regexp); free(token_string); return result; failed: free(result); return NULL; }
/** * Compile regular expression * * @param __regexp - regular expression to compile * @return descriptor of compiled regular expression * @sideeffect allocate memory for return value. Use regexp_free() to free. */ regexp_t* regexp_compile (const char *__regexp) { regexp_t *result = NULL; int modifiers; char *parsed_regexp = malloc (strlen (__regexp) + 1); if (!parse_regexp (__regexp, &parsed_regexp, &modifiers)) { void *re; const char *err; int pos; /* Clear non-pcre-based modifiers */ int pcre_options = modifiers & ~REGEXP_REPLACE_GLOBAL; re = pcre_compile (parsed_regexp, pcre_options, &err, &pos, NULL); /* Error in regexp */ if (!re) { free (parsed_regexp); return NULL; } /* Create descriptor */ MALLOC_ZERO (result, sizeof (struct regexp)); result->handle = re; result->modifiers = modifiers; } free (parsed_regexp); return result; }
static token lexer_next_token_private (void) { ecma_char_t c = LA (0); JERRY_ASSERT (token_start == NULL); if (isalpha (c) || c == '$' || c == '_') { return parse_name (); } if (isdigit (c) || (c == '.' && isdigit (LA (1)))) { return parse_number (); } if (c == '\n') { consume_char (); return create_token (TOK_NEWLINE, 0); } if (c == '\0') { return create_token (TOK_EOF, 0); } if (c == '\'' || c == '"') { return parse_string (); } if (isspace (c)) { grobble_whitespaces (); return lexer_next_token_private (); } if (c == '/' && LA (1) == '*') { if (replace_comment_by_newline ()) { token ret; ret.type = TOK_NEWLINE; ret.uid = 0; return ret; } else { return lexer_next_token_private (); } } if (c == '/') { if (LA (1) == '/') { replace_comment_by_newline (); return lexer_next_token_private (); } else if (!(sent_token.type == TOK_NAME || sent_token.type == TOK_NULL || sent_token.type == TOK_BOOL || sent_token.type == TOK_CLOSE_BRACE || sent_token.type == TOK_CLOSE_SQUARE || sent_token.type == TOK_CLOSE_PAREN || sent_token.type == TOK_SMALL_INT || sent_token.type == TOK_NUMBER || sent_token.type == TOK_STRING || sent_token.type == TOK_REGEXP)) { return parse_regexp (); } } switch (c) { case '{': RETURN_PUNC (TOK_OPEN_BRACE); break; case '}': RETURN_PUNC (TOK_CLOSE_BRACE); break; case '(': RETURN_PUNC (TOK_OPEN_PAREN); break; case ')': RETURN_PUNC (TOK_CLOSE_PAREN); break; case '[': RETURN_PUNC (TOK_OPEN_SQUARE); break; case ']': RETURN_PUNC (TOK_CLOSE_SQUARE); break; case '.': RETURN_PUNC (TOK_DOT); break; case ';': RETURN_PUNC (TOK_SEMICOLON); break; case ',': RETURN_PUNC (TOK_COMMA); break; case '~': RETURN_PUNC (TOK_COMPL); break; case ':': RETURN_PUNC (TOK_COLON); break; case '?': RETURN_PUNC (TOK_QUERY); break; case '*': IF_LA_IS ('=', TOK_MULT_EQ, TOK_MULT); break; case '/': IF_LA_IS ('=', TOK_DIV_EQ, TOK_DIV); break; case '^': IF_LA_IS ('=', TOK_XOR_EQ, TOK_XOR); break; case '%': IF_LA_IS ('=', TOK_MOD_EQ, TOK_MOD); break; case '+': IF_LA_IS_OR ('+', TOK_DOUBLE_PLUS, '=', TOK_PLUS_EQ, TOK_PLUS); break; case '-': IF_LA_IS_OR ('-', TOK_DOUBLE_MINUS, '=', TOK_MINUS_EQ, TOK_MINUS); break; case '&': IF_LA_IS_OR ('&', TOK_DOUBLE_AND, '=', TOK_AND_EQ, TOK_AND); break; case '|': IF_LA_IS_OR ('|', TOK_DOUBLE_OR, '=', TOK_OR_EQ, TOK_OR); break; case '<': { switch (LA (1)) { case '<': IF_LA_N_IS ('=', TOK_LSHIFT_EQ, TOK_LSHIFT, 2); break; case '=': RETURN_PUNC_EX (TOK_LESS_EQ, 2); break; default: RETURN_PUNC (TOK_LESS); } break; } case '>': { switch (LA (1)) { case '>': { switch (LA (2)) { case '>': IF_LA_N_IS ('=', TOK_RSHIFT_EX_EQ, TOK_RSHIFT_EX, 3); break; case '=': RETURN_PUNC_EX (TOK_RSHIFT_EQ, 3); break; default: RETURN_PUNC_EX (TOK_RSHIFT, 2); } break; } case '=': RETURN_PUNC_EX (TOK_GREATER_EQ, 2); break; default: RETURN_PUNC (TOK_GREATER); } break; } case '=': { if (LA (1) == '=') { IF_LA_N_IS ('=', TOK_TRIPLE_EQ, TOK_DOUBLE_EQ, 2); } else { RETURN_PUNC (TOK_EQ); } break; } case '!': { if (LA (1) == '=') { IF_LA_N_IS ('=', TOK_NOT_DOUBLE_EQ, TOK_NOT_EQ, 2); } else { RETURN_PUNC (TOK_NOT); } break; } default: PARSE_SORRY ("Unknown character", buffer - buffer_start); } PARSE_SORRY ("Unknown character", buffer - buffer_start); }
static void parseaction(int argc, char * const argv[], bool havelogfile) { int i, j, opt; static const struct option long_options[] = { { "stdout-to-log", no_argument, NULL, 1}, { "stderr-to-log", no_argument, NULL, 2}, { "regexp", required_argument, NULL, 'r'}, { "prefix", required_argument, NULL, 'p'}, { "suffix", required_argument, NULL, 's'}, { "chdir", required_argument, NULL, 'c'}, { NULL, 0, NULL, 0} }; struct lookfor *l = calloc(1, sizeof(struct lookfor)); if (l == NULL) { fprintf(stderr, "%s: Out of memory.\n", program); exit(EXIT_FAILURE); } l->next = lookfor; lookfor = l; while ((opt = getopt_long(argc, argv, "+p:s:r:c:", long_options, NULL)) >= 0) { switch (opt) { case 1: if (!havelogfile) logerror_die( "Action's --stderr-to-log needs global --logfile option!\n"); l->logstdout = true; break; case 2: if (!havelogfile) logerror_die( "Action's --stderr-to-log needs global --logfile option!\n"); l->logstderr = true; break; case 'r': parse_regexp(lookfor, optarg); break; case 'p': if (lookfor->prefix != NULL) logerror_die( "Multiple --prefix options!\n"); lookfor->prefix = xstrdup(optarg); lookfor->prefixlen = strlen(lookfor->prefix); break; case 's': if (lookfor->suffix != NULL) logerror_die( "Multiple --suffix options!\n"); lookfor->suffix = xstrdup(optarg); lookfor->suffixlen = strlen(lookfor->suffix); break; case 'c': if (lookfor->dir != NULL) logerror_die( "Multiple --chdir options!\n"); lookfor->dir = xstrdup(optarg); break; case '?': if (optopt == '\0') logerror_die( "Unknown/ambiguous action option '%s'!\n", argv[optind-1]); else if (optopt == 'p' || optopt == 'l') logerror_die( "Action option '%s' needs an argument!\n", argv[optind-1]); else logerror_die( "Unknown action option '%c'!\n", (char)optopt); break; } } if (optind >= argc) logerror_die( "Unexpected end of command line (action expected)!\n"); l->action = xstrdup(argv[optind]); optind++; if (l->action == NULL) logerror_die("Out of memory!\n"); i = optind; while (i < argc && strcmp(argv[i], ";") != 0) i++; if (i >= argc) logerror_die( "Missing ';' at end of action!\n" "(Don't forget to escape it (\\;) if you use a shell)\n"); l->num_arguments = i-optind; l->arguments = calloc(l->num_arguments, sizeof(struct argument)); if (l->arguments == NULL) logerror_die("Out of memory!\n"); for (j = optind; j < i ; j++) { parse_argument(&l->arguments[j-optind], argv[j]); } optind = i+1; }