1// -*- c-basic-offset: 2 -*-
2/*
3 * This file is part of the KDE libraries
4 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
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_OBJECT_H_
23#define _REGEXP_OBJECT_H_
24
25#include "internal.h"
26#include "function_object.h"
27#include "regexp.h"
28#include <wtf/OwnArrayPtr.h>
29
30namespace KJS {
31 class ExecState;
32 class RegExpPrototype : public JSObject {
33 public:
34 RegExpPrototype(ExecState *exec,
35 ObjectPrototype *objProto,
36 FunctionPrototype *funcProto);
37 virtual const ClassInfo *classInfo() const { return &info; }
38 static const ClassInfo info;
39 };
40
41 class RegExpProtoFunc : public InternalFunctionImp {
42 public:
43 RegExpProtoFunc(ExecState*, FunctionPrototype*, int i, int len, const Identifier&);
44
45 virtual JSValue *callAsFunction(ExecState *exec, JSObject *thisObj, const List &args);
46
47 enum { Exec, Test, ToString, Compile };
48 private:
49 int id;
50 };
51
52 class RegExpImp : public JSObject {
53 public:
54 RegExpImp(RegExpPrototype *regexpProto);
55 ~RegExpImp();
56 void setRegExp(ExecState* exec, RegExp* r);
57 RegExp* regExp() const { return reg; }
58
59 virtual const ClassInfo *classInfo() const { return &info; }
60 static const ClassInfo info;
61
62 virtual JSObject* valueClone(Interpreter* targetCtx) const;
63 private:
64 RegExp *reg;
65 };
66
67 struct RegExpObjectImpPrivate;
68
69 class RegExpObjectImp : public InternalFunctionImp {
70 public:
71 enum { Dollar1, Dollar2, Dollar3, Dollar4, Dollar5, Dollar6, Dollar7, Dollar8, Dollar9,
72 Input, Multiline, LastMatch, LastParen, LeftContext, RightContext };
73
74 RegExpObjectImp(ExecState *exec,
75 FunctionPrototype *funcProto,
76 RegExpPrototype *regProto);
77 virtual bool implementsConstruct() const;
78 using KJS::JSObject::construct;
79 virtual JSObject *construct(ExecState *exec, const List &args);
80 virtual JSValue *callAsFunction(ExecState *exec, JSObject *thisObj, const List &args);
81
82 using KJS::JSObject::put;
83 virtual void put(ExecState *, const Identifier &, JSValue *, int attr = None);
84 void putValueProperty(ExecState *, int token, JSValue *, int attr);
85 using KJS::JSObject::getOwnPropertySlot;
86 virtual bool getOwnPropertySlot(ExecState *, const Identifier&, PropertySlot&);
87 JSValue *getValueProperty(ExecState *, int token) const;
88
89 // If resources are exhaused during a match, exec parameter will have an exception
90 // set, and endOffset will be -1
91 UString performMatch(RegExp *, ExecState *, const RegExpStringContext&, const UString&,
92 int startOffset = 0, int *endOffset = 0, int **ovector = 0);
93 JSObject *arrayOfMatches(ExecState *exec, const UString &result) const;
94
95 static void throwRegExpError(ExecState *);
96
97 virtual const ClassInfo *classInfo() const { return &info; }
98
99 /*
100 Attempts to create a new regular expression engine for the string p
101 and the flags stored in flagsInput. If this succeeds, it returns the
102 engine. If not, it returns 0, and raises an exception in exec
103 */
104 static RegExp* makeEngine(ExecState *exec, const UString &p, JSValue *flagsInput);
105 private:
106 JSValue *getBackref(int) const;
107 JSValue *getLastMatch() const;
108 JSValue *getLastParen() const;
109 JSValue *getLeftContext() const;
110 JSValue *getRightContext() const;
111
112 OwnPtr<RegExpObjectImpPrivate> d;
113
114 static const ClassInfo info;
115 };
116
117} // namespace
118
119#endif
120