]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/RegExp.h
JavaScriptCore-1097.13.tar.gz
[apple/javascriptcore.git] / 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 "ExecutableAllocator.h"
26 #include "MatchResult.h"
27 #include "RegExpKey.h"
28 #include "Structure.h"
29 #include "UString.h"
30 #include "yarr/Yarr.h"
31 #include <wtf/Forward.h>
32 #include <wtf/RefCounted.h>
33
34 #if ENABLE(YARR_JIT)
35 #include "yarr/YarrJIT.h"
36 #endif
37
38 namespace JSC {
39
40 struct RegExpRepresentation;
41 class JSGlobalData;
42
43 JS_EXPORT_PRIVATE RegExpFlags regExpFlags(const UString&);
44
45 class RegExp : public JSCell {
46 public:
47 typedef JSCell Base;
48
49 JS_EXPORT_PRIVATE static RegExp* create(JSGlobalData&, const UString& pattern, RegExpFlags);
50 static void destroy(JSCell*);
51
52 bool global() const { return m_flags & FlagGlobal; }
53 bool ignoreCase() const { return m_flags & FlagIgnoreCase; }
54 bool multiline() const { return m_flags & FlagMultiline; }
55
56 const UString& pattern() const { return m_patternString; }
57
58 bool isValid() const { return !m_constructionError && m_flags != InvalidFlags; }
59 const char* errorMessage() const { return m_constructionError; }
60
61 JS_EXPORT_PRIVATE int match(JSGlobalData&, const UString&, unsigned startOffset, Vector<int, 32>& ovector);
62 MatchResult match(JSGlobalData&, const UString&, unsigned startOffset);
63 unsigned numSubpatterns() const { return m_numSubpatterns; }
64
65 bool hasCode()
66 {
67 return m_state != NotCompiled;
68 }
69
70 void invalidateCode();
71
72 #if ENABLE(REGEXP_TRACING)
73 void printTraceData();
74 #endif
75
76 static Structure* createStructure(JSGlobalData& globalData, JSGlobalObject* globalObject, JSValue prototype)
77 {
78 return Structure::create(globalData, globalObject, prototype, TypeInfo(LeafType, 0), &s_info);
79 }
80
81 static const ClassInfo s_info;
82
83 RegExpKey key() { return RegExpKey(m_flags, m_patternString); }
84
85 protected:
86 void finishCreation(JSGlobalData&);
87
88 private:
89 friend class RegExpCache;
90 RegExp(JSGlobalData&, const UString&, RegExpFlags);
91
92 static RegExp* createWithoutCaching(JSGlobalData&, const UString&, RegExpFlags);
93
94 enum RegExpState {
95 ParseError,
96 JITCode,
97 ByteCode,
98 NotCompiled
99 } m_state;
100
101 void compile(JSGlobalData*, Yarr::YarrCharSize);
102 void compileIfNecessary(JSGlobalData&, Yarr::YarrCharSize);
103
104 void compileMatchOnly(JSGlobalData*, Yarr::YarrCharSize);
105 void compileIfNecessaryMatchOnly(JSGlobalData&, Yarr::YarrCharSize);
106
107 #if ENABLE(YARR_JIT_DEBUG)
108 void matchCompareWithInterpreter(const UString&, int startOffset, int* offsetVector, int jitResult);
109 #endif
110
111 UString m_patternString;
112 RegExpFlags m_flags;
113 const char* m_constructionError;
114 unsigned m_numSubpatterns;
115 #if ENABLE(REGEXP_TRACING)
116 unsigned m_rtMatchCallCount;
117 unsigned m_rtMatchFoundCount;
118 #endif
119
120 #if ENABLE(YARR_JIT)
121 Yarr::YarrCodeBlock m_regExpJITCode;
122 #endif
123 OwnPtr<Yarr::BytecodePattern> m_regExpBytecode;
124 };
125
126 } // namespace JSC
127
128 #endif // RegExp_h