1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Netscape Public License
6
* Version 1.1 (the "License"); you may not use this file except in
7
* compliance with the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/NPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is mozilla.org code.
17
* The Initial Developer of the Original Code is
18
* Netscape Communications Corporation.
19
* Portions created by the Initial Developer are Copyright (C) 1998
20
* the Initial Developer. All Rights Reserved.
25
* Alternatively, the contents of this file may be used under the terms of
26
* either the GNU General Public License Version 2 or later (the "GPL"), or
27
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28
* in which case the provisions of the GPL or the LGPL are applicable instead
29
* of those above. If you wish to allow use of your version of this file only
30
* under the terms of either the GPL or the LGPL, and not to allow others to
31
* use your version of this file under the terms of the NPL, indicate your
32
* decision by deleting the provisions above and replace them with the notice
33
* and other provisions required by the GPL or the LGPL. If you do not delete
34
* the provisions above, a recipient may use your version of this file under
35
* the terms of any one of the NPL, the GPL or the LGPL.
37
* ***** END LICENSE BLOCK ***** */
44
* This class is defines the basic notion of a token
45
* within our system. All other tokens are derived from
46
* this one. It offers a few basic interfaces, but the
47
* most important is consume(). The consume() method gets
48
* called during the tokenization process when an instance
49
* of that particular token type gets detected in the
52
* CToken objects that are allocated from the heap _must_ be allocated
53
* using the nsTokenAllocator: the nsTokenAllocator object uses an
54
* arena to manage the tokens.
56
* The nsTokenAllocator object's arena implementation requires
57
* object size at destruction time to properly recycle the object;
58
* therefore, CToken::operator delete() is not public. Instead,
59
* heap-allocated tokens should be destroyed using the static
60
* Destroy() method, which accepts a token and the arena from which
61
* the token was allocated.
63
* Leaf classes (that are actually instantiated from the heap) must
64
* implement the SizeOf() method, which Destroy() uses to determine
65
* the size of the token in order to properly recycle it.
75
#include "nsFixedSizeAllocator.h"
77
#define NS_HTMLTOKENS_NOT_AN_ENTITY \
78
NS_ERROR_GENERATE_SUCCESS(NS_ERROR_MODULE_HTMLPARSER,2000)
81
class nsTokenAllocator;
90
* Implement the SizeOf() method; leaf classes derived from CToken
93
#define CTOKEN_IMPL_SIZEOF \
95
virtual size_t SizeOf() const { return sizeof(*this); } \
99
* Token objects represent sequences of characters as they
100
* are consumed from the input stream (URL). While they're
101
* pretty general in nature, we use subclasses (found in
102
* nsHTMLTokens.h) to define <start>, </end>, <text>,
103
* <comment>, <&entity>, <newline>, and <whitespace> tokens.
105
* @update gess 3/25/98
110
enum eTokenOrigin {eSource,eResidualStyle};
114
// nsTokenAllocator should be the only class that tries to
115
// allocate tokens from the heap.
116
friend class nsTokenAllocator;
120
* @update harishd 08/01/00
122
* @param aArena - Allocate memory from this pool.
124
static void * operator new (size_t aSize,nsFixedSizeAllocator& anArena) CPP_THROW_NEW
126
return anArena.Alloc(aSize);
130
* Hide operator delete; clients should use Destroy() instead.
132
static void operator delete (void*,size_t) {}
137
* @update gess5/11/98
144
static void Destroy(CToken* aToken,nsFixedSizeAllocator& aArenaPool)
146
size_t sz = aToken->SizeOf();
148
aArenaPool.Free(aToken, sz);
152
* Make a note on number of times you have been referenced
153
* @update harishd 08/02/00
155
void AddRef() { ++mUseCount; }
158
* Free yourself if no one is holding you.
159
* @update harishd 08/02/00
161
void Release(nsFixedSizeAllocator& aArenaPool) {
163
Destroy(this, aArenaPool);
167
* Default constructor
168
* @update gess7/21/98
170
CToken(PRInt32 aTag=0);
173
* Retrieve string value of the token
174
* @update gess5/11/98
175
* @return reference to string containing string value
177
virtual const nsAString& GetStringValue(void) = 0;
180
* Get string of full contents, suitable for debug dump.
181
* It should look exactly like the input source.
182
* @update gess5/11/98
183
* @return reference to string containing string value
185
virtual void GetSource(nsString& anOutputString);
187
/** @update harishd 03/23/00
188
* @return reference to string containing string value
190
virtual void AppendSourceTo(nsAString& anOutputString);
193
* Sets the ordinal value of this token (not currently used)
194
* @update gess5/11/98
195
* @param value is the new ord value for this token
197
virtual void SetTypeID(PRInt32 aValue);
200
* Getter which retrieves the current ordinal value for this token
201
* @update gess5/11/98
202
* @return current ordinal value
204
virtual PRInt32 GetTypeID(void);
207
* Getter which retrieves the current attribute count for this token
208
* @update gess5/11/98
209
* @return current attribute count
211
virtual PRInt16 GetAttributeCount(void);
214
* Causes token to consume data from given scanner.
215
* Note that behavior varies wildly between CToken subclasses.
216
* @update gess5/11/98
217
* @param aChar -- most recent char consumed
218
* @param aScanner -- input source where token should get data
219
* @return error code (0 means ok)
221
virtual nsresult Consume(PRUnichar aChar,nsScanner& aScanner,PRInt32 aMode);
224
* Getter which retrieves type of token
225
* @update gess5/11/98
226
* @return int containing token type
228
virtual PRInt32 GetTokenType(void);
231
* Getter which retrieves the class name for this token
232
* This method is only used for debug purposes.
233
* @update gess5/11/98
234
* @return const char* containing class name
236
virtual const char* GetClassName(void);
240
* For tokens who care, this can tell us whether the token is
241
* well formed or not.
243
* @update gess 8/30/00
244
* @return PR_FALSE; subclasses MUST override if they care.
246
virtual PRBool IsWellFormed(void) const {return PR_FALSE;}
248
virtual PRBool IsEmpty(void) { return PR_FALSE; }
251
* If aValue is TRUE then the token represents a short-hand tag
253
virtual void SetEmpty(PRBool aValue) { return ; }
255
PRInt32 GetNewlineCount()
257
return mNewlineCount;
260
void SetNewlineCount(PRInt32 aCount)
262
mNewlineCount = aCount;
265
PRInt32 GetLineNumber()
270
void SetLineNumber(PRInt32 aLineNumber)
272
mLineNumber = mLineNumber == 0 ? aLineNumber : mLineNumber;
275
void SetAttributeCount(PRInt16 aValue) { mAttrCount = aValue; }
279
* @update gess5/11/98
281
virtual void SelfTest(void);
283
static int GetTokenCount();
289
* Returns the size of the token object.
291
virtual size_t SizeOf() const = 0;
295
PRInt32 mNewlineCount;