|
|
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.3 ! misha 11: static const char * const IDENT_VREGEX_H="$Date: 2009-04-22 21:39:19 $";
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: fpattern_len(0),
! 65: fcode(0),
! 66: fextra(0),
! 67: fstudied(false)
! 68: {
! 69: foptions[0]=0;
! 70: foptions[1]=0;
! 71: }
1.1 misha 72:
73: VRegex(Charset& acharset, const String* aregex, const String* aoptions):
1.3 ! misha 74: fextra(0),
1.1 misha 75: fstudied(false)
76: {
77: set(acharset, aregex, aoptions);
78: compile();
79: }
80:
81: ~VRegex(){
82: if(fextra)
83: pcre_free(fextra);
84: if(fcode)
85: pcre_free(fcode);
86: }
87:
88: void set(Charset& acharset, const String* aregex, const String* aoptions);
89:
90: void compile();
91:
92: void study();
93:
94: int exec(const char* string, size_t string_len, int* ovector, int ovector_size, int prestart=0);
95:
96: // size_t info();
97:
98: size_t full_info(int type);
99:
100: size_t get_info_size();
101:
102: size_t get_study_size();
103:
104: size_t get_options();
105:
106: bool is_pre_post_match_needed(){
107: return (foptions[1] & MF_NEED_PRE_POST_MATCH)!=0;
108: }
109:
110: bool is_just_count(){
111: return (foptions[1] & MF_JUST_COUNT_MATCHES)!=0;
112: }
113:
114: bool is_global_search(){
115: return (foptions[1] & MF_GLOBAL_SEARCH)!=0;
116: }
117:
118: private:
119: static void regex_options(const String* options, int* result);
120:
121: private:
122: Charset* fcharset;
123: const char* fpattern;
124: size_t fpattern_len;
125: int foptions[2];
126:
127: pcre* fcode;
128: pcre_extra* fextra;
129: bool fstudied;
130: };
131:
1.2 misha 132:
133: class VRegexCleaner {
134: public:
135: VRegex *vregex;
136:
137: VRegexCleaner(): vregex(0) { }
138:
139: ~VRegexCleaner(){
140: if(vregex) delete vregex;
141: }
142:
143: };
144:
1.1 misha 145:
146: #endif