|
|
1.1 misha 1: /** @file
2: Parser: @b regex class decls.
3:
4: Copyright (c) 2001-2009 ArtLebedev Group (http://www.artlebedev.com)
5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
6: */
7:
8: #ifndef PA_VREGEX_H
9: #define PA_VREGEX_H
10:
1.4 ! misha 11: static const char * const IDENT_VREGEX_H="$Date: 2009-04-27 09:39:00 $";
1.1 misha 12:
13: // include
14:
15: #include "classes.h"
16: #include "pa_common.h"
17: #include "pa_vstateless_object.h"
18: #include "pa_charset.h"
19: #include "pcre.h"
20:
21: // defines
22:
23: #define VREGEX_TYPE "regex"
24:
25: enum Match_feature {
26: MF_GLOBAL_SEARCH = 0x01,
27: MF_NEED_PRE_POST_MATCH = 0x02,
28: MF_JUST_COUNT_MATCHES = 0x04
29: };
30:
31: extern Methoded* regex_class;
32:
33: // VRegex
34: class VRegex: public VStateless_object {
35:
36: public: // Value
37:
38: override const char* type() const { return VREGEX_TYPE; }
39: override VStateless_class *get_class() { return regex_class; }
40:
1.2 misha 41: /// VRegex: count
42: override int as_int() { return get_info_size(); }
43:
44: /// VRegex: count
45: override double as_double() { return as_int(); }
1.1 misha 46:
47: /// VRegex: true
48: override bool is_evaluated_expr() const { return true; }
49:
50: /// VRegex: scalar
51: override Value& as_expr_result(bool/*return_string_as_is=false*/);
52:
53: /// VRegex: true
1.2 misha 54: virtual bool is_defined() const { return as_bool(); }
1.1 misha 55:
56: /// VRegex: true
57: override bool as_bool() const { return true; }
58:
59: public: // usage
60:
61: VRegex():
1.3 misha 62: fcharset(0),
63: fpattern(0),
64: fcode(0),
65: fextra(0),
66: fstudied(false)
67: {
68: foptions[0]=0;
69: foptions[1]=0;
70: }
1.1 misha 71:
72: VRegex(Charset& acharset, const String* aregex, const String* aoptions):
1.3 misha 73: fextra(0),
1.1 misha 74: fstudied(false)
75: {
76: set(acharset, aregex, aoptions);
77: compile();
78: }
79:
80: ~VRegex(){
81: if(fextra)
82: pcre_free(fextra);
83: if(fcode)
84: pcre_free(fcode);
85: }
86:
87: void set(Charset& acharset, const String* aregex, const String* aoptions);
88:
89: void compile();
90:
91: void study();
92:
93: int exec(const char* string, size_t string_len, int* ovector, int ovector_size, int prestart=0);
94:
95: // size_t info();
96:
97: size_t full_info(int type);
98:
99: size_t get_info_size();
100:
101: size_t get_study_size();
102:
103: size_t get_options();
104:
105: bool is_pre_post_match_needed(){
106: return (foptions[1] & MF_NEED_PRE_POST_MATCH)!=0;
107: }
108:
109: bool is_just_count(){
110: return (foptions[1] & MF_JUST_COUNT_MATCHES)!=0;
111: }
112:
113: bool is_global_search(){
114: return (foptions[1] & MF_GLOBAL_SEARCH)!=0;
115: }
116:
117: private:
118: static void regex_options(const String* options, int* result);
119:
120: private:
121: Charset* fcharset;
122: const char* fpattern;
123: int foptions[2];
124:
125: pcre* fcode;
126: pcre_extra* fextra;
127: bool fstudied;
128: };
129:
1.2 misha 130:
131: class VRegexCleaner {
132: public:
133: VRegex *vregex;
134:
135: VRegexCleaner(): vregex(0) { }
136:
137: ~VRegexCleaner(){
138: if(vregex) delete vregex;
139: }
140:
141: };
142:
1.1 misha 143:
144: #endif