parser

Написать ответ на текущее сообщение

 

 
   команды управления поиском

Сейчас стандартные опции выглядят так...

Sumo 25.02.2015 23:37

void VRegex::regex_options(const String* options, int* result){
        struct Regex_option {
                const char* key;
                const char* keyAlt;
                int clear;
                int set;
                int *result;
        } regex_option[]={
                {"i", "I", 0, PCRE_CASELESS, result}, // a=A
                {"s", "S", 0, PCRE_DOTALL, result}, // ^\n\n$ [default]
                {"m", "M", PCRE_DOTALL, PCRE_MULTILINE, result}, // ^aaa\n$^bbb\n$
                {"x", 0, 0, PCRE_EXTENDED, result}, // whitespace in regex ignored
                {"U", 0, 0, PCRE_UNGREEDY, result}, // ungreedy patterns (greedy by default)
                {"g", "G", 0, MF_GLOBAL_SEARCH, result+1}, // many rows
                {"'", 0, 0, MF_NEED_PRE_POST_MATCH, result+1},
                {"n", 0, 0, MF_JUST_COUNT_MATCHES, result+1},
                {0, 0, 0, 0, 0}
        };
        result[0]=PCRE_EXTRA /* backslash+non-special char causes error */
                        | PCRE_DOTALL /* dot matches all chars including newline char */
                        | PCRE_DOLLAR_ENDONLY /* dollar matches only end of string, but not newline chars */;
        result[1]=0;

        if(options && !options->is_empty()){
                size_t valid_options=0;
                for(Regex_option *o=regex_option; o->key; o++)
                        if(
                                options->pos(o->key)!=STRING_NOT_FOUND
                                || (o->keyAlt && options->pos(o->keyAlt)!=STRING_NOT_FOUND)
                        ){
                                *o->result &= ~o->clear;
                                *o->result |= o->set;
                                valid_options++;
                        }
                if(options->length()!=valid_options)
                        throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
        }
}

void VRegex::compile(){
        const char* err_ptr;
        int err_offset;
        int options=foptions[0];

        // @todo (for UTF-8): check string & pattern and use PCRE_NO_UTF8_CHECK option
        if(fcharset->isUTF8())
                options |= (PCRE_UTF8 | PCRE_UCP);

        fcode=pcre_compile(fpattern, options,
                &err_ptr, &err_offset,
                fcharset->pcre_tables);

        if(!fcode){
                throw Exception(PCRE_EXCEPTION_TYPE,
                        new String(fpattern+err_offset, String::L_TAINTED),
                        "regular expression syntax error - %s", err_ptr);
        }

}