1/*
2Open Tracker License
3
4Terms and Conditions
5
6Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
7
8Permission is hereby granted, free of charge, to any person obtaining a copy of
9this software and associated documentation files (the "Software"), to deal in
10the Software without restriction, including without limitation the rights to
11use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12of the Software, and to permit persons to whom the Software is furnished to do
13so, subject to the following conditions:
14
15The above copyright notice and this permission notice applies to all licensees
16and shall be included in all copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
20FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
23WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25Except as contained in this notice, the name of Be Incorporated shall not be
26used in advertising or otherwise to promote the sale, use or other dealings in
27this Software without prior written authorization from Be Incorporated.
28
29Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
30of Be Incorporated in the United States and other countries. Other brand product
31names are registered trademarks or trademarks of their respective holders.
32All rights reserved.
33*/
34
35
36// This code is based on regexp.c, v.1.3 by Henry Spencer:
37
38// @(#)regexp.c	1.3 of 18 April 87
39//
40//	Copyright (c) 1986 by University of Toronto.
41//	Written by Henry Spencer.  Not derived from licensed software.
42//
43//	Permission is granted to anyone to use this software for any
44//	purpose on any computer system, and to redistribute it freely,
45//	subject to the following restrictions:
46//
47//	1. The author is not responsible for the consequences of use of
48//		this software, no matter how awful, even if they arise
49//		from defects in it.
50//
51//	2. The origin of this software must not be misrepresented, either
52//		by explicit claim or by omission.
53//
54//	3. Altered versions must be plainly marked as such, and must not
55//		be misrepresented as being the original software.
56//
57// Beware that some of this code is subtly aware of the way operator
58// precedence is structured in regular expressions.  Serious changes in
59// regular-expression syntax might require a total rethink.
60//
61
62// ALTERED VERSION: Adapted to ANSI C and C++ for the OpenTracker
63// project (www.opentracker.org), Jul 11, 2000.
64
65#ifndef _REG_EXP_H
66#define _REG_EXP_H
67
68#include <String.h>
69
70enum {
71	REGEXP_UNMATCHED_PARENTHESIS = B_ERRORS_END,
72	REGEXP_TOO_BIG,
73	REGEXP_TOO_MANY_PARENTHESIS,
74	REGEXP_JUNK_ON_END,
75	REGEXP_STAR_PLUS_OPERAND_EMPTY,
76	REGEXP_NESTED_STAR_QUESTION_PLUS,
77	REGEXP_INVALID_BRACKET_RANGE,
78	REGEXP_UNMATCHED_BRACKET,
79	REGEXP_INTERNAL_ERROR,
80	REGEXP_QUESTION_PLUS_STAR_FOLLOWS_NOTHING,
81	REGEXP_TRAILING_BACKSLASH,
82	REGEXP_CORRUPTED_PROGRAM,
83	REGEXP_MEMORY_CORRUPTION,
84	REGEXP_CORRUPTED_POINTERS,
85	REGEXP_CORRUPTED_OPCODE
86};
87
88const int32 kSubExpressionMax = 10;
89
90struct regexp {
91	const char *startp[kSubExpressionMax];
92	const char *endp[kSubExpressionMax];
93	char regstart;		/* Internal use only. See RegExp.cpp for details. */
94	char reganch;		/* Internal use only. */
95	const char *regmust;/* Internal use only. */
96	int regmlen;		/* Internal use only. */
97	char program[1];	/* Unwarranted chumminess with compiler. */
98};
99
100class RegExp {
101
102public:
103	RegExp();
104	RegExp(const char *);
105	RegExp(const BString &);
106	~RegExp();
107
108	status_t InitCheck() const;
109
110	status_t SetTo(const char*);
111	status_t SetTo(const BString &);
112
113	bool Matches(const char *string) const;
114	bool Matches(const BString &) const;
115
116	int32 RunMatcher(regexp *, const char *) const;
117	regexp *Compile(const char *);
118	regexp *Expression() const;
119	const char *ErrorString() const;
120
121#ifdef DEBUG
122	void Dump();
123#endif
124
125private:
126
127	void SetError(status_t error) const;
128
129	// Working functions for Compile():
130	char *Reg(int32, int32 *);
131	char *Branch(int32 *);
132	char *Piece(int32 *);
133	char *Atom(int32 *);
134	char *Node(char);
135	char *Next(char *);
136	const char *Next(const char *) const;
137	void Char(char);
138	void Insert(char, char *);
139	void Tail(char *, char *);
140	void OpTail(char *, char *);
141
142	// Working functions for RunMatcher():
143	int32 Try(regexp *, const char *) const;
144	int32 Match(const char *) const;
145	int32 Repeat(const char *) const;
146
147	// Utility functions:
148#ifdef DEBUG
149	char *Prop(const char *) const;
150	void RegExpError(const char *) const;
151#endif
152	inline int32 UCharAt(const char *p) const;
153	inline char *Operand(char* p) const;
154	inline const char *Operand(const char* p) const;
155	inline bool	IsMult(char c) const;
156
157// --------- Variables -------------
158
159	mutable status_t fError;
160	regexp *fRegExp;
161
162	// Work variables for Compile().
163
164	const char *fInputScanPointer;
165	int32 fParenthesisCount;
166	char fDummy;
167	char *fCodeEmitPointer;		// &fDummy = don't.
168	long fCodeSize;
169
170	// Work variables for RunMatcher().
171
172	mutable const char *fStringInputPointer;
173	mutable const char *fRegBol;	// Beginning of input, for ^ check.
174	mutable const char **fStartPArrayPointer;
175	mutable const char **fEndPArrayPointer;
176};
177
178
179#endif
180