Merge mozilla-central and tracemonkey. (a=blockers)
[mozilla-central.git] / layout / style / nsCSSScanner.h
blob9bd7bdfe6b10c9b7570b16d73015bb1d09c8e13a
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
13 * License.
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.
22 * Contributor(s):
23 * L. David Baron <dbaron@dbaron.org>
24 * Daniel Glazman <glazman@netscape.com>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 /* tokenization of CSS style sheets */
42 #ifndef nsCSSScanner_h___
43 #define nsCSSScanner_h___
45 #include "nsString.h"
46 #include "nsCOMPtr.h"
47 #include "mozilla/css/Loader.h"
48 #include "nsCSSStyleSheet.h"
50 class nsIUnicharInputStream;
52 // XXX turn this off for minimo builds
53 #define CSS_REPORT_PARSE_ERRORS
55 #define CSS_BUFFER_SIZE 256
57 // for #ifdef CSS_REPORT_PARSE_ERRORS
58 #include "nsXPIDLString.h"
59 class nsIURI;
61 // Token types
62 enum nsCSSTokenType {
63 // A css identifier (e.g. foo)
64 eCSSToken_Ident, // mIdent
66 // A css at keyword (e.g. @foo)
67 eCSSToken_AtKeyword, // mIdent
69 // A css number without a percentage or dimension; with percentage;
70 // without percentage but with a dimension
71 eCSSToken_Number, // mNumber
72 eCSSToken_Percentage, // mNumber
73 eCSSToken_Dimension, // mNumber + mIdent
75 // A css string (e.g. "foo" or 'foo')
76 eCSSToken_String, // mSymbol + mIdent + mSymbol
78 // Whitespace (e.g. " " or "/* abc */")
79 eCSSToken_WhiteSpace, // mIdent
81 // A css symbol (e.g. ':', ';', '+', etc.)
82 eCSSToken_Symbol, // mSymbol
84 // A css1 id (e.g. #foo3)
85 eCSSToken_ID, // mIdent
86 // Just like eCSSToken_ID, except the part following the '#' is not
87 // a valid CSS identifier (eg. starts with a digit, is the empty
88 // string, etc).
89 eCSSToken_Ref, // mIdent
91 eCSSToken_Function, // mIdent
93 eCSSToken_URL, // mIdent
94 eCSSToken_InvalidURL, // doesn't matter
96 eCSSToken_HTMLComment, // "<!--" or "-->"
98 eCSSToken_Includes, // "~="
99 eCSSToken_Dashmatch, // "|="
100 eCSSToken_Beginsmatch, // "^="
101 eCSSToken_Endsmatch, // "$="
102 eCSSToken_Containsmatch, // "*="
104 eCSSToken_URange, // Low in mInteger, high in mInteger2;
105 // mIntegerValid is true if the token is a
106 // valid range; mIdent preserves the textual
107 // form of the token for error reporting
109 // A special token indicating that there was an error in tokenization.
110 // It's always an unterminated string.
111 eCSSToken_Error // mSymbol + mIdent
114 struct nsCSSToken {
115 nsAutoString mIdent NS_OKONHEAP;
116 float mNumber;
117 PRInt32 mInteger;
118 PRInt32 mInteger2;
119 nsCSSTokenType mType;
120 PRUnichar mSymbol;
121 PRPackedBool mIntegerValid; // for number, dimension, urange
122 PRPackedBool mHasSign; // for number, percentage, and dimension
124 nsCSSToken();
126 PRBool IsSymbol(PRUnichar aSymbol) {
127 return PRBool((eCSSToken_Symbol == mType) && (mSymbol == aSymbol));
130 void AppendToString(nsString& aBuffer);
133 // CSS Scanner API. Used to tokenize an input stream using the CSS
134 // forward compatible tokenization rules. This implementation is
135 // private to this package and is only used internally by the css
136 // parser.
137 class nsCSSScanner {
138 public:
139 nsCSSScanner();
140 ~nsCSSScanner();
142 // Init the scanner.
143 // |aLineNumber == 1| is the beginning of a file, use |aLineNumber == 0|
144 // when the line number is unknown.
145 // Either aInput or (aBuffer and aCount) must be set.
146 void Init(nsIUnicharInputStream* aInput,
147 const PRUnichar *aBuffer, PRUint32 aCount,
148 nsIURI* aURI, PRUint32 aLineNumber,
149 nsCSSStyleSheet* aSheet, mozilla::css::Loader* aLoader);
150 void Close();
152 static PRBool InitGlobals();
153 static void ReleaseGlobals();
155 // Set whether or not we are processing SVG
156 void SetSVGMode(PRBool aSVGMode) {
157 NS_ASSERTION(aSVGMode == PR_TRUE || aSVGMode == PR_FALSE,
158 "bad PRBool value");
159 mSVGMode = aSVGMode;
161 PRBool IsSVGMode() const {
162 return mSVGMode;
165 #ifdef CSS_REPORT_PARSE_ERRORS
166 void AddToError(const nsSubstring& aErrorText);
167 void OutputError();
168 void ClearError();
170 // aMessage must take no parameters
171 void ReportUnexpected(const char* aMessage);
172 void ReportUnexpectedParams(const char* aMessage,
173 const PRUnichar **aParams,
174 PRUint32 aParamsLength);
175 // aLookingFor is a plain string, not a format string
176 void ReportUnexpectedEOF(const char* aLookingFor);
177 // aLookingFor is a single character
178 void ReportUnexpectedEOF(PRUnichar aLookingFor);
179 // aMessage must take 1 parameter (for the string representation of the
180 // unexpected token)
181 void ReportUnexpectedToken(nsCSSToken& tok, const char *aMessage);
182 // aParams's first entry must be null, and we'll fill in the token
183 void ReportUnexpectedTokenParams(nsCSSToken& tok,
184 const char* aMessage,
185 const PRUnichar **aParams,
186 PRUint32 aParamsLength);
187 #endif
189 PRUint32 GetLineNumber() { return mLineNumber; }
191 // Get the next token. Return PR_FALSE on EOF. aTokenResult
192 // is filled in with the data for the token.
193 PRBool Next(nsCSSToken& aTokenResult);
195 // Get the next token that may be a string or unquoted URL or whitespace
196 PRBool NextURL(nsCSSToken& aTokenResult);
198 // It's really ugly that we have to expose this, but it's the easiest
199 // way to do :nth-child() parsing sanely. (In particular, in
200 // :nth-child(2n-1), "2n-1" is a dimension, and we need to push the
201 // "-1" back so we can read it again as a number.)
202 void Pushback(PRUnichar aChar);
204 // Reports operating-system level errors, e.g. read failures and
205 // out of memory.
206 nsresult GetLowLevelError();
208 // sometimes the parser wants to make note of a low-level error
209 void SetLowLevelError(nsresult aErrorCode);
211 protected:
212 PRBool EnsureData();
213 PRInt32 Read();
214 PRInt32 Peek();
215 PRBool LookAhead(PRUnichar aChar);
216 void EatWhiteSpace();
218 void ParseAndAppendEscape(nsString& aOutput);
219 PRBool ParseIdent(PRInt32 aChar, nsCSSToken& aResult);
220 PRBool ParseAtKeyword(PRInt32 aChar, nsCSSToken& aResult);
221 PRBool ParseNumber(PRInt32 aChar, nsCSSToken& aResult);
222 PRBool ParseRef(PRInt32 aChar, nsCSSToken& aResult);
223 PRBool ParseString(PRInt32 aChar, nsCSSToken& aResult);
224 PRBool ParseURange(PRInt32 aChar, nsCSSToken& aResult);
225 PRBool SkipCComment();
227 PRBool GatherIdent(PRInt32 aChar, nsString& aIdent);
229 // Only used when input is a stream
230 nsCOMPtr<nsIUnicharInputStream> mInputStream;
231 PRUnichar mBuffer[CSS_BUFFER_SIZE];
233 const PRUnichar *mReadPointer;
234 PRUint32 mOffset;
235 PRUint32 mCount;
236 PRUnichar* mPushback;
237 PRInt32 mPushbackCount;
238 PRInt32 mPushbackSize;
239 PRUnichar mLocalPushback[4];
240 nsresult mLowLevelError;
242 PRUint32 mLineNumber;
243 // True if we are in SVG mode; false in "normal" CSS
244 PRPackedBool mSVGMode;
245 #ifdef CSS_REPORT_PARSE_ERRORS
246 nsXPIDLCString mFileName;
247 nsCOMPtr<nsIURI> mURI; // Cached so we know to not refetch mFileName
248 PRUint32 mErrorLineNumber, mColNumber, mErrorColNumber;
249 nsFixedString mError;
250 PRUnichar mErrorBuf[200];
251 PRUint64 mWindowID;
252 PRBool mWindowIDCached;
253 nsCSSStyleSheet* mSheet;
254 mozilla::css::Loader* mLoader;
255 #endif
258 #endif /* nsCSSScanner_h___ */