1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
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.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
43 * This class is defines the basic notion of a token
44 * within our system. All other tokens are derived from
45 * this one. It offers a few basic interfaces, but the
46 * most important is consume(). The consume() method gets
47 * called during the tokenization process when an instance
48 * of that particular token type gets detected in the
51 * CToken objects that are allocated from the heap _must_ be allocated
52 * using the nsTokenAllocator: the nsTokenAllocator object uses an
53 * arena to manage the tokens.
55 * The nsTokenAllocator object's arena implementation requires
56 * object size at destruction time to properly recycle the object;
57 * therefore, CToken::operator delete() is not public. Instead,
58 * heap-allocated tokens should be destroyed using the static
59 * Destroy() method, which accepts a token and the arena from which
60 * the token was allocated.
62 * Leaf classes (that are actually instantiated from the heap) must
63 * implement the SizeOf() method, which Destroy() uses to determine
64 * the size of the token in order to properly recycle it.
74 #include "nsFixedSizeAllocator.h"
76 #define NS_HTMLTOKENS_NOT_AN_ENTITY \
77 NS_ERROR_GENERATE_SUCCESS(NS_ERROR_MODULE_HTMLPARSER,2000)
80 class nsTokenAllocator
;
89 * Implement the SizeOf() method; leaf classes derived from CToken
92 #define CTOKEN_IMPL_SIZEOF \
94 virtual size_t SizeOf() const { return sizeof(*this); } \
98 * Token objects represent sequences of characters as they
99 * are consumed from the input stream (URL). While they're
100 * pretty general in nature, we use subclasses (found in
101 * nsHTMLTokens.h) to define <start>, </end>, <text>,
102 * <comment>, <&entity>, <newline>, and <whitespace> tokens.
104 * @update gess 3/25/98
109 enum eTokenOrigin
{eSource
,eResidualStyle
};
113 // nsTokenAllocator should be the only class that tries to
114 // allocate tokens from the heap.
115 friend class nsTokenAllocator
;
119 * @update harishd 08/01/00
121 * @param aArena - Allocate memory from this pool.
123 static void * operator new (size_t aSize
,nsFixedSizeAllocator
& anArena
) CPP_THROW_NEW
125 return anArena
.Alloc(aSize
);
129 * Hide operator delete; clients should use Destroy() instead.
131 static void operator delete (void*,size_t) {}
136 * @update gess5/11/98
144 static void Destroy(CToken
* aToken
,nsFixedSizeAllocator
& aArenaPool
)
146 size_t sz
= aToken
->SizeOf();
148 aArenaPool
.Free(aToken
, sz
);
153 * Make a note on number of times you have been referenced
154 * @update harishd 08/02/00
158 NS_LOG_ADDREF(this, mUseCount
, "CToken", sizeof(*this));
162 * Free yourself if no one is holding you.
163 * @update harishd 08/02/00
165 void Release(nsFixedSizeAllocator
& aArenaPool
) {
167 NS_LOG_RELEASE(this, mUseCount
, "CToken");
169 Destroy(this, aArenaPool
);
173 * Default constructor
174 * @update gess7/21/98
176 CToken(PRInt32 aTag
=0);
179 * Retrieve string value of the token
180 * @update gess5/11/98
181 * @return reference to string containing string value
183 virtual const nsSubstring
& GetStringValue(void) = 0;
186 * Get string of full contents, suitable for debug dump.
187 * It should look exactly like the input source.
188 * @update gess5/11/98
189 * @return reference to string containing string value
191 virtual void GetSource(nsString
& anOutputString
);
193 /** @update harishd 03/23/00
194 * @return reference to string containing string value
196 virtual void AppendSourceTo(nsAString
& anOutputString
);
199 * Sets the ordinal value of this token (not currently used)
200 * @update gess5/11/98
201 * @param value is the new ord value for this token
203 void SetTypeID(PRInt32 aValue
) {
208 * Getter which retrieves the current ordinal value for this token
209 * @update gess5/11/98
210 * @return current ordinal value
212 virtual PRInt32
GetTypeID(void);
215 * Getter which retrieves the current attribute count for this token
216 * @update gess5/11/98
217 * @return current attribute count
219 virtual PRInt16
GetAttributeCount(void);
222 * Causes token to consume data from given scanner.
223 * Note that behavior varies wildly between CToken subclasses.
224 * @update gess5/11/98
225 * @param aChar -- most recent char consumed
226 * @param aScanner -- input source where token should get data
227 * @return error code (0 means ok)
229 virtual nsresult
Consume(PRUnichar aChar
,nsScanner
& aScanner
,PRInt32 aMode
);
232 * Getter which retrieves type of token
233 * @update gess5/11/98
234 * @return int containing token type
236 virtual PRInt32
GetTokenType(void);
239 * For tokens who care, this can tell us whether the token is
240 * well formed or not.
242 * @update gess 8/30/00
243 * @return PR_FALSE; subclasses MUST override if they care.
245 virtual PRBool
IsWellFormed(void) const {return PR_FALSE
;}
247 virtual PRBool
IsEmpty(void) { return PR_FALSE
; }
250 * If aValue is TRUE then the token represents a short-hand tag
252 virtual void SetEmpty(PRBool aValue
) { return ; }
254 PRInt32
GetNewlineCount()
256 return mNewlineCount
;
259 void SetNewlineCount(PRInt32 aCount
)
261 mNewlineCount
= aCount
;
264 PRInt32
GetLineNumber()
269 void SetLineNumber(PRInt32 aLineNumber
)
271 mLineNumber
= mLineNumber
== 0 ? aLineNumber
: mLineNumber
;
274 void SetInError(PRBool aInError
)
284 void SetAttributeCount(PRInt16 aValue
) { mAttrCount
= aValue
; }
288 * @update gess5/11/98
290 virtual void SelfTest(void);
292 static int GetTokenCount();
298 * Returns the size of the token object.
300 virtual size_t SizeOf() const = 0;
304 PRInt32 mNewlineCount
;
305 PRUint32 mLineNumber
: 31;
306 PRUint32 mInError
: 1;