RegExp.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #ifndef RegExp_h
  22. #define RegExp_h
  23. #include "ExecutableAllocator.h"
  24. #include "MatchResult.h"
  25. #include "RegExpKey.h"
  26. #include "Structure.h"
  27. #include "yarr/Yarr.h"
  28. #include <wtf/Forward.h>
  29. #include <wtf/RefCounted.h>
  30. #include <wtf/text/WTFString.h>
  31. #if ENABLE(YARR_JIT)
  32. #include "yarr/YarrJIT.h"
  33. #endif
  34. namespace JSC {
  35. struct RegExpRepresentation;
  36. class VM;
  37. JS_EXPORT_PRIVATE RegExpFlags regExpFlags(const String&);
  38. class RegExp : public JSCell {
  39. public:
  40. typedef JSCell Base;
  41. JS_EXPORT_PRIVATE static RegExp* create(VM&, const String& pattern, RegExpFlags);
  42. static const bool needsDestruction = true;
  43. static const bool hasImmortalStructure = true;
  44. static void destroy(JSCell*);
  45. bool global() const { return m_flags & FlagGlobal; }
  46. bool ignoreCase() const { return m_flags & FlagIgnoreCase; }
  47. bool multiline() const { return m_flags & FlagMultiline; }
  48. const String& pattern() const { return m_patternString; }
  49. bool isValid() const { return !m_constructionError && m_flags != InvalidFlags; }
  50. const char* errorMessage() const { return m_constructionError; }
  51. JS_EXPORT_PRIVATE int match(VM&, const String&, unsigned startOffset, Vector<int, 32>& ovector);
  52. MatchResult match(VM&, const String&, unsigned startOffset);
  53. unsigned numSubpatterns() const { return m_numSubpatterns; }
  54. bool hasCode()
  55. {
  56. return m_state != NotCompiled;
  57. }
  58. void invalidateCode();
  59. #if ENABLE(REGEXP_TRACING)
  60. void printTraceData();
  61. #endif
  62. static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
  63. {
  64. return Structure::create(vm, globalObject, prototype, TypeInfo(LeafType, 0), &s_info);
  65. }
  66. static const ClassInfo s_info;
  67. RegExpKey key() { return RegExpKey(m_flags, m_patternString); }
  68. protected:
  69. void finishCreation(VM&);
  70. private:
  71. friend class RegExpCache;
  72. RegExp(VM&, const String&, RegExpFlags);
  73. static RegExp* createWithoutCaching(VM&, const String&, RegExpFlags);
  74. enum RegExpState {
  75. ParseError,
  76. JITCode,
  77. ByteCode,
  78. NotCompiled
  79. } m_state;
  80. void compile(VM*, Yarr::YarrCharSize);
  81. void compileIfNecessary(VM&, Yarr::YarrCharSize);
  82. void compileMatchOnly(VM*, Yarr::YarrCharSize);
  83. void compileIfNecessaryMatchOnly(VM&, Yarr::YarrCharSize);
  84. #if ENABLE(YARR_JIT_DEBUG)
  85. void matchCompareWithInterpreter(const String&, int startOffset, int* offsetVector, int jitResult);
  86. #endif
  87. String m_patternString;
  88. RegExpFlags m_flags;
  89. const char* m_constructionError;
  90. unsigned m_numSubpatterns;
  91. #if ENABLE(REGEXP_TRACING)
  92. unsigned m_rtMatchCallCount;
  93. unsigned m_rtMatchFoundCount;
  94. #endif
  95. #if ENABLE(YARR_JIT)
  96. #if ENABLE(DETACHED_JIT)
  97. OwnPtr<Yarr::YarrCodeBlock> m_regExpJITCode_shared;
  98. Yarr::YarrCodeBlock & m_regExpJITCode;
  99. #else
  100. Yarr::YarrCodeBlock m_regExpJITCode;
  101. #endif
  102. #endif
  103. OwnPtr<Yarr::BytecodePattern> m_regExpBytecode;
  104. };
  105. } // namespace JSC
  106. #endif // RegExp_h