initial import
[vuplus_webkit] / Source / JavaScriptCore / runtime / RegExp.h
1 /*
2  *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3  *  Copyright (C) 2007, 2008, 2009 Apple Inc. All rights reserved.
4  *  Copyright (C) 2009 Torch Mobile, Inc.
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public
8  *  License as published by the Free Software Foundation; either
9  *  version 2 of the License, or (at your option) any later version.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this library; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifndef RegExp_h
23 #define RegExp_h
24
25 #include "UString.h"
26 #include "ExecutableAllocator.h"
27 #include "Structure.h"
28 #include "RegExpKey.h"
29 #include "yarr/Yarr.h"
30 #include <wtf/Forward.h>
31 #include <wtf/RefCounted.h>
32
33 namespace JSC {
34
35     struct RegExpRepresentation;
36     class JSGlobalData;
37
38     RegExpFlags regExpFlags(const UString&);
39
40     class RegExp : public JSCell {
41     public:
42         typedef JSCell Base;
43
44         static RegExp* create(JSGlobalData&, const UString& pattern, RegExpFlags);
45         ~RegExp();
46
47         bool global() const { return m_flags & FlagGlobal; }
48         bool ignoreCase() const { return m_flags & FlagIgnoreCase; }
49         bool multiline() const { return m_flags & FlagMultiline; }
50
51         const UString& pattern() const { return m_patternString; }
52
53         bool isValid() const { return !m_constructionError && m_flags != InvalidFlags; }
54         const char* errorMessage() const { return m_constructionError; }
55
56         int match(JSGlobalData&, const UString&, int startOffset, Vector<int, 32>* ovector = 0);
57         unsigned numSubpatterns() const { return m_numSubpatterns; }
58
59         bool hasCode()
60         {
61             return m_representation;
62         }
63
64         void invalidateCode();
65         
66 #if ENABLE(REGEXP_TRACING)
67         void printTraceData();
68 #endif
69
70         static Structure* createStructure(JSGlobalData& globalData, JSGlobalObject* globalObject, JSValue prototype)
71         {
72             return Structure::create(globalData, globalObject, prototype, TypeInfo(LeafType, 0), &s_info);
73         }
74         
75         static JS_EXPORTDATA const ClassInfo s_info;
76
77         RegExpKey key() { return RegExpKey(m_flags, m_patternString); }
78
79     protected:
80         void finishCreation(JSGlobalData&);
81
82     private:
83         friend class RegExpCache;
84         RegExp(JSGlobalData&, const UString&, RegExpFlags);
85
86         static RegExp* createWithoutCaching(JSGlobalData&, const UString&, RegExpFlags);
87
88         enum RegExpState {
89             ParseError,
90             JITCode,
91             ByteCode,
92             NotCompiled
93         } m_state;
94
95         void compile(JSGlobalData*, Yarr::YarrCharSize);
96         void compileIfNecessary(JSGlobalData&, Yarr::YarrCharSize);
97
98 #if ENABLE(YARR_JIT_DEBUG)
99         void matchCompareWithInterpreter(const UString&, int startOffset, int* offsetVector, int jitResult);
100 #endif
101
102         UString m_patternString;
103         RegExpFlags m_flags;
104         const char* m_constructionError;
105         unsigned m_numSubpatterns;
106 #if ENABLE(REGEXP_TRACING)
107         unsigned m_rtMatchCallCount;
108         unsigned m_rtMatchFoundCount;
109 #endif
110
111         OwnPtr<RegExpRepresentation> m_representation;
112     };
113
114 } // namespace JSC
115
116 #endif // RegExp_h