1998-04-14 00:24:54 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
1998-04-14 00:24:54 +04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MODULE NOTES:
|
|
|
|
* @update gess 4/1/98
|
|
|
|
*
|
|
|
|
* The scanner is a low-level service class that knows
|
|
|
|
* how to consume characters out of an (internal) stream.
|
|
|
|
* This class also offers a series of utility methods
|
2007-06-27 04:21:47 +04:00
|
|
|
* that most tokenizers want, such as readUntil()
|
|
|
|
* and SkipWhitespace().
|
1998-04-14 00:24:54 +04:00
|
|
|
*/
|
|
|
|
|
1998-05-07 11:19:47 +04:00
|
|
|
|
1998-04-14 00:24:54 +04:00
|
|
|
#ifndef SCANNER
|
|
|
|
#define SCANNER
|
|
|
|
|
2003-03-15 04:04:32 +03:00
|
|
|
#include "nsCOMPtr.h"
|
1998-04-14 00:24:54 +04:00
|
|
|
#include "nsString.h"
|
1999-01-09 04:09:02 +03:00
|
|
|
#include "nsIParser.h"
|
2017-04-27 13:27:03 +03:00
|
|
|
#include "mozilla/Encoding.h"
|
2004-02-19 05:44:03 +03:00
|
|
|
#include "nsScannerString.h"
|
2017-02-17 09:44:49 +03:00
|
|
|
#include "mozilla/CheckedInt.h"
|
2000-12-13 00:58:14 +03:00
|
|
|
|
2001-08-16 09:24:17 +04:00
|
|
|
class nsReadEndCondition {
|
|
|
|
public:
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t *mChars;
|
|
|
|
char16_t mFilter;
|
|
|
|
explicit nsReadEndCondition(const char16_t* aTerminateChars);
|
2001-08-16 09:24:17 +04:00
|
|
|
private:
|
|
|
|
nsReadEndCondition(const nsReadEndCondition& aOther); // No copying
|
|
|
|
void operator=(const nsReadEndCondition& aOther); // No assigning
|
|
|
|
};
|
|
|
|
|
2017-06-18 14:37:50 +03:00
|
|
|
class nsScanner final {
|
|
|
|
using Encoding = mozilla::Encoding;
|
|
|
|
template <typename T> using NotNull = mozilla::NotNull<T>;
|
1998-04-14 00:24:54 +04:00
|
|
|
public:
|
1998-05-15 02:19:08 +04:00
|
|
|
|
1998-07-25 01:57:43 +04:00
|
|
|
/**
|
2012-11-06 15:57:51 +04:00
|
|
|
* Use this constructor for the XML fragment parsing case
|
1998-07-25 01:57:43 +04:00
|
|
|
*/
|
2014-09-02 02:04:52 +04:00
|
|
|
explicit nsScanner(const nsAString& anHTMLString);
|
1998-07-25 01:57:43 +04:00
|
|
|
|
1998-05-15 02:19:08 +04:00
|
|
|
/**
|
1998-07-14 01:13:09 +04:00
|
|
|
* Use this constructor if you want i/o to be based on
|
|
|
|
* a file (therefore a stream) or just data you provide via Append().
|
1998-05-15 02:19:08 +04:00
|
|
|
*/
|
2012-11-06 15:57:51 +04:00
|
|
|
nsScanner(nsString& aFilename, bool aCreateStream);
|
1998-05-15 02:19:08 +04:00
|
|
|
|
1999-01-09 04:09:02 +03:00
|
|
|
~nsScanner();
|
1998-05-07 11:19:47 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* retrieve next char from internal input stream
|
|
|
|
*
|
|
|
|
* @update gess 3/25/98
|
|
|
|
* @param ch is the char to accept new value
|
|
|
|
* @return error code reflecting read status
|
|
|
|
*/
|
2014-01-04 19:02:17 +04:00
|
|
|
nsresult GetChar(char16_t& ch);
|
1998-05-07 11:19:47 +04:00
|
|
|
|
1998-05-15 02:19:08 +04:00
|
|
|
/**
|
|
|
|
* Records current offset position in input stream. This allows us
|
|
|
|
* to back up to this point if the need should arise, such as when
|
|
|
|
* tokenization gets interrupted.
|
|
|
|
*
|
|
|
|
* @update gess 5/12/98
|
|
|
|
* @param
|
|
|
|
* @return
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t Mark(void);
|
1998-05-15 02:19:08 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets current offset position of input stream to marked position.
|
|
|
|
* This allows us to back up to this point if the need should arise,
|
|
|
|
* such as when tokenization gets interrupted.
|
|
|
|
* NOTE: IT IS REALLY BAD FORM TO CALL RELEASE WITHOUT CALLING MARK FIRST!
|
|
|
|
*
|
|
|
|
* @update gess 5/12/98
|
|
|
|
* @param
|
|
|
|
* @return
|
|
|
|
*/
|
2000-12-13 00:58:14 +03:00
|
|
|
void RewindToMark(void);
|
1998-05-15 02:19:08 +04:00
|
|
|
|
|
|
|
|
2000-01-15 23:35:57 +03:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @update harishd 01/12/99
|
|
|
|
* @param
|
|
|
|
* @return
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool UngetReadable(const nsAString& aBuffer);
|
2000-01-15 23:35:57 +03:00
|
|
|
|
1998-05-15 02:19:08 +04:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @update gess 5/13/98
|
|
|
|
* @param
|
|
|
|
* @return
|
|
|
|
*/
|
2002-03-24 03:16:18 +03:00
|
|
|
nsresult Append(const nsAString& aBuffer);
|
1998-05-15 02:19:08 +04:00
|
|
|
|
1998-05-22 00:38:32 +04:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @update gess 5/21/98
|
|
|
|
* @param
|
|
|
|
* @return
|
|
|
|
*/
|
2017-02-17 09:44:49 +03:00
|
|
|
nsresult Append(const char* aBuffer, uint32_t aLen);
|
1998-11-06 05:07:17 +03:00
|
|
|
|
1999-02-01 07:24:37 +03:00
|
|
|
/**
|
|
|
|
* Call this to copy bytes out of the scanner that have not yet been consumed
|
|
|
|
* by the tokenization process.
|
|
|
|
*
|
|
|
|
* @update gess 5/12/98
|
|
|
|
* @param aCopyBuffer is where the scanner buffer will be copied to
|
2016-02-09 13:45:59 +03:00
|
|
|
* @return true if OK or false on OOM
|
1999-02-01 07:24:37 +03:00
|
|
|
*/
|
2016-02-09 13:45:59 +03:00
|
|
|
bool CopyUnusedData(nsString& aCopyBuffer);
|
1999-02-01 07:24:37 +03:00
|
|
|
|
1998-07-02 12:14:22 +04:00
|
|
|
/**
|
1998-07-14 01:13:09 +04:00
|
|
|
* Retrieve the name of the file that the scanner is reading from.
|
|
|
|
* In some cases, it's just a given name, because the scanner isn't
|
|
|
|
* really reading from a file.
|
1998-07-02 12:14:22 +04:00
|
|
|
*
|
|
|
|
* @update gess 5/12/98
|
|
|
|
* @return
|
|
|
|
*/
|
1998-07-14 01:13:09 +04:00
|
|
|
nsString& GetFilename(void);
|
1998-07-02 12:14:22 +04:00
|
|
|
|
1998-04-14 00:24:54 +04:00
|
|
|
static void SelfTest();
|
|
|
|
|
1999-02-01 21:23:31 +03:00
|
|
|
/**
|
1999-02-16 21:32:02 +03:00
|
|
|
* Use this setter to change the scanner's unicode decoder
|
|
|
|
*
|
1999-03-08 23:00:23 +03:00
|
|
|
* @update ftang 3/02/99
|
1999-02-16 21:32:02 +03:00
|
|
|
* @param aCharset a normalized (alias resolved) charset name
|
1999-03-08 23:00:23 +03:00
|
|
|
* @param aCharsetSource- where the charset info came from
|
1999-02-16 21:32:02 +03:00
|
|
|
* @return
|
|
|
|
*/
|
2017-06-18 14:37:50 +03:00
|
|
|
nsresult SetDocumentCharset(NotNull<const Encoding*> aEncoding,
|
|
|
|
int32_t aSource);
|
1999-02-01 21:23:31 +03:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
void BindSubstring(nsScannerSubstring& aSubstring, const nsScannerIterator& aStart, const nsScannerIterator& aEnd);
|
|
|
|
void CurrentPosition(nsScannerIterator& aPosition);
|
|
|
|
void EndReading(nsScannerIterator& aPosition);
|
|
|
|
void SetPosition(nsScannerIterator& aPosition,
|
2017-02-17 09:44:49 +03:00
|
|
|
bool aTruncate = false);
|
2000-12-13 00:58:14 +03:00
|
|
|
|
1999-07-25 21:23:24 +04:00
|
|
|
/**
|
|
|
|
* Internal method used to cause the internal buffer to
|
|
|
|
* be filled with data.
|
|
|
|
*
|
|
|
|
* @update gess4/3/98
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool IsIncremental(void) {return mIncremental;}
|
|
|
|
void SetIncremental(bool anIncrValue) {mIncremental=anIncrValue;}
|
1999-07-25 21:23:24 +04:00
|
|
|
|
2005-01-04 01:06:27 +03:00
|
|
|
protected:
|
|
|
|
|
2017-02-17 09:44:49 +03:00
|
|
|
bool AppendToBuffer(nsScannerString::Buffer* aBuffer);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool AppendToBuffer(const nsAString& aStr)
|
2004-11-05 09:50:27 +03:00
|
|
|
{
|
2008-07-14 17:05:15 +04:00
|
|
|
nsScannerString::Buffer* buf = nsScannerString::AllocBufferFromString(aStr);
|
|
|
|
if (!buf)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2017-02-17 09:44:49 +03:00
|
|
|
AppendToBuffer(buf);
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2004-11-05 09:50:27 +03:00
|
|
|
}
|
2000-12-13 00:58:14 +03:00
|
|
|
|
|
|
|
nsScannerString* mSlidingBuffer;
|
2004-02-19 05:44:03 +03:00
|
|
|
nsScannerIterator mCurrentPosition; // The position we will next read from in the scanner buffer
|
|
|
|
nsScannerIterator mMarkPosition; // The position last marked (we may rewind to here)
|
|
|
|
nsScannerIterator mEndPosition; // The current end of the scanner buffer
|
1998-07-14 01:13:09 +04:00
|
|
|
nsString mFilename;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mIncremental;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t mCharsetSource;
|
2003-06-11 22:16:03 +04:00
|
|
|
nsCString mCharset;
|
2017-04-27 13:27:03 +03:00
|
|
|
mozilla::UniquePtr<mozilla::Decoder> mUnicodeDecoder;
|
2008-10-01 10:48:47 +04:00
|
|
|
|
2017-04-27 13:27:03 +03:00
|
|
|
private:
|
2008-10-01 10:48:47 +04:00
|
|
|
nsScanner &operator =(const nsScanner &); // Not implemented.
|
1998-04-14 00:24:54 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
1998-04-25 23:45:14 +04:00
|
|
|
|
|
|
|
|