From 6aa24e7565dfa5feafd12cd0ec12312df044916e Mon Sep 17 00:00:00 2001 From: Stephan Bergmann Date: Mon, 28 Oct 2013 12:09:28 +0100 Subject: Move implementation details to syntaxhighlight.cxx (and clean up a little) Change-Id: I6e660708d8ca1509b89b306cd428dc38c8b03f2c --- basctl/source/basicide/baside2.hxx | 1 + comphelper/source/misc/syntaxhighlight.cxx | 83 +++++++++++++++++++++--------- include/comphelper/syntaxhighlight.hxx | 55 ++------------------ include/svtools/svmedit.hxx | 1 - 4 files changed, 64 insertions(+), 76 deletions(-) diff --git a/basctl/source/basicide/baside2.hxx b/basctl/source/basicide/baside2.hxx index 85a1d88899da..68b937e9d97a 100644 --- a/basctl/source/basicide/baside2.hxx +++ b/basctl/source/basicide/baside2.hxx @@ -56,6 +56,7 @@ class SvxSearchItem; #include "com/sun/star/reflection/XIdlClass.hpp" #include #include +#include #include #include "com/sun/star/reflection/XIdlReflection.hpp" diff --git a/comphelper/source/misc/syntaxhighlight.cxx b/comphelper/source/misc/syntaxhighlight.cxx index fd8ee2665c41..b886fea0355d 100644 --- a/comphelper/source/misc/syntaxhighlight.cxx +++ b/comphelper/source/misc/syntaxhighlight.cxx @@ -22,6 +22,18 @@ #include #include +// Flags for character properties +#define CHAR_START_IDENTIFIER 0x0001 +#define CHAR_IN_IDENTIFIER 0x0002 +#define CHAR_START_NUMBER 0x0004 +#define CHAR_IN_NUMBER 0x0008 +#define CHAR_IN_HEX_NUMBER 0x0010 +#define CHAR_IN_OCT_NUMBER 0x0020 +#define CHAR_START_STRING 0x0040 +#define CHAR_OPERATOR 0x0080 +#define CHAR_SPACE 0x0100 +#define CHAR_EOL 0x0200 + // ########################################################################## // ATTENTION: all these words need to be in lower case // ########################################################################## @@ -232,7 +244,7 @@ extern "C" int compare_strings( const void *arg1, const void *arg2 ) namespace { - static bool isAlpha(sal_Unicode c) + bool isAlpha(sal_Unicode c) { if (comphelper::string::isalphaAscii(c)) return true; @@ -240,8 +252,40 @@ namespace } } +class SyntaxHighlighter::Tokenizer +{ + HighlighterLanguage aLanguage; + // Character information tables + sal_uInt16 aCharTypeTab[256]; + + const sal_Unicode* mpStringBegin; + const sal_Unicode* mpActualPos; + + sal_Unicode peekChar( void ) { return *mpActualPos; } + sal_Unicode getChar( void ) { return *mpActualPos++; } + + // Auxiliary function: testing of the character flags + sal_Bool testCharFlags( sal_Unicode c, sal_uInt16 nTestFlags ); + + // Get new token, EmptyString == nothing more over there + sal_Bool getNextToken( /*out*/TokenTypes& reType, + /*out*/const sal_Unicode*& rpStartPos, /*out*/const sal_Unicode*& rpEndPos ); + + const char** ppListKeyWords; + sal_uInt16 nKeyWordCount; + +public: + Tokenizer( HighlighterLanguage aLang = HIGHLIGHT_BASIC ); + ~Tokenizer( void ); + + sal_uInt16 parseLine( const OUString* aSource ); + void getHighlightPortions( const OUString& rLine, + /*out*/std::vector& portions ); + void setKeyWords( const char** ppKeyWords, sal_uInt16 nCount ); +}; + // Helper function: test character flag -sal_Bool SimpleTokenizer_Impl::testCharFlags( sal_Unicode c, sal_uInt16 nTestFlags ) +sal_Bool SyntaxHighlighter::Tokenizer::testCharFlags( sal_Unicode c, sal_uInt16 nTestFlags ) { bool bRet = false; if( c != 0 && c <= 255 ) @@ -256,13 +300,13 @@ sal_Bool SimpleTokenizer_Impl::testCharFlags( sal_Unicode c, sal_uInt16 nTestFla return bRet; } -void SimpleTokenizer_Impl::setKeyWords( const char** ppKeyWords, sal_uInt16 nCount ) +void SyntaxHighlighter::Tokenizer::setKeyWords( const char** ppKeyWords, sal_uInt16 nCount ) { ppListKeyWords = ppKeyWords; nKeyWordCount = nCount; } -sal_Bool SimpleTokenizer_Impl::getNextToken( /*out*/TokenTypes& reType, +sal_Bool SyntaxHighlighter::Tokenizer::getNextToken( /*out*/TokenTypes& reType, /*out*/const sal_Unicode*& rpStartPos, /*out*/const sal_Unicode*& rpEndPos ) { reType = TT_UNKNOWN; @@ -539,7 +583,7 @@ sal_Bool SimpleTokenizer_Impl::getNextToken( /*out*/TokenTypes& reType, return sal_True; } -SimpleTokenizer_Impl::SimpleTokenizer_Impl( HighlighterLanguage aLang ): aLanguage(aLang) +SyntaxHighlighter::Tokenizer::Tokenizer( HighlighterLanguage aLang ): aLanguage(aLang) { memset( aCharTypeTab, 0, sizeof( aCharTypeTab ) ); @@ -619,19 +663,11 @@ SimpleTokenizer_Impl::SimpleTokenizer_Impl( HighlighterLanguage aLang ): aLangua ppListKeyWords = NULL; } -SimpleTokenizer_Impl::~SimpleTokenizer_Impl( void ) -{ -} - -SimpleTokenizer_Impl* getSimpleTokenizer( void ) +SyntaxHighlighter::Tokenizer::~Tokenizer( void ) { - static SimpleTokenizer_Impl* pSimpleTokenizer = NULL; - if( !pSimpleTokenizer ) - pSimpleTokenizer = new SimpleTokenizer_Impl(); - return pSimpleTokenizer; } -sal_uInt16 SimpleTokenizer_Impl::parseLine( const OUString* aSource ) +sal_uInt16 SyntaxHighlighter::Tokenizer::parseLine( const OUString* aSource ) { // Set the position to the beginning of the source string mpStringBegin = mpActualPos = aSource->getStr(); @@ -649,7 +685,7 @@ sal_uInt16 SimpleTokenizer_Impl::parseLine( const OUString* aSource ) return nTokenCount; } -void SimpleTokenizer_Impl::getHighlightPortions( const OUString& rLine, +void SyntaxHighlighter::Tokenizer::getHighlightPortions( const OUString& rLine, /*out*/std::vector& portions ) { // Set the position to the beginning of the source string @@ -672,35 +708,32 @@ void SimpleTokenizer_Impl::getHighlightPortions( const OUString& rLine, SyntaxHighlighter::SyntaxHighlighter() { - m_pSimpleTokenizer = 0; m_pKeyWords = NULL; m_nKeyWordCount = 0; } SyntaxHighlighter::~SyntaxHighlighter() { - delete m_pSimpleTokenizer; delete m_pKeyWords; } void SyntaxHighlighter::initialize( HighlighterLanguage eLanguage_ ) { eLanguage = eLanguage_; - delete m_pSimpleTokenizer; - m_pSimpleTokenizer = new SimpleTokenizer_Impl(eLanguage); + m_tokenizer.reset(new SyntaxHighlighter::Tokenizer(eLanguage)); switch (eLanguage) { case HIGHLIGHT_BASIC: - m_pSimpleTokenizer->setKeyWords( strListBasicKeyWords, + m_tokenizer->setKeyWords( strListBasicKeyWords, sizeof( strListBasicKeyWords ) / sizeof( char* )); break; case HIGHLIGHT_SQL: - m_pSimpleTokenizer->setKeyWords( strListSqlKeyWords, + m_tokenizer->setKeyWords( strListSqlKeyWords, sizeof( strListSqlKeyWords ) / sizeof( char* )); break; default: - m_pSimpleTokenizer->setKeyWords( NULL, 0 ); + m_tokenizer->setKeyWords( NULL, 0 ); } } @@ -708,13 +741,13 @@ void SyntaxHighlighter::notifyChange( const OUString* pChangedLines, sal_uInt32 nArrayLength) { for( sal_uInt32 i=0 ; i < nArrayLength ; i++ ) - m_pSimpleTokenizer->parseLine(&pChangedLines[i]); + m_tokenizer->parseLine(&pChangedLines[i]); } void SyntaxHighlighter::getHighlightPortions( const OUString& rLine, /*out*/std::vector& portions ) { - m_pSimpleTokenizer->getHighlightPortions( rLine, portions ); + m_tokenizer->getHighlightPortions( rLine, portions ); } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/comphelper/syntaxhighlight.hxx b/include/comphelper/syntaxhighlight.hxx index cbeb95ee9c14..43c42d00f338 100644 --- a/include/comphelper/syntaxhighlight.hxx +++ b/include/comphelper/syntaxhighlight.hxx @@ -20,6 +20,8 @@ #define INCLUDED_COMPHELPER_SYNTAXHIGHLIGHT_HXX #include + +#include #include #include @@ -55,22 +57,6 @@ struct HighlightPortion { {} }; -///////////////////////////////////////////////////////////////////////// -// Auxiliary class to support JavaScript modules, next to find functions which -// will later will be used for syntax highlighting - -// Flags for character properties -#define CHAR_START_IDENTIFIER 0x0001 -#define CHAR_IN_IDENTIFIER 0x0002 -#define CHAR_START_NUMBER 0x0004 -#define CHAR_IN_NUMBER 0x0008 -#define CHAR_IN_HEX_NUMBER 0x0010 -#define CHAR_IN_OCT_NUMBER 0x0020 -#define CHAR_START_STRING 0x0040 -#define CHAR_OPERATOR 0x0080 -#define CHAR_SPACE 0x0100 -#define CHAR_EOL 0x0200 - // Language mode of the Highlighter (possibly to be refined later with keyword // lists, C comment flags) enum HighlighterLanguage @@ -79,39 +65,6 @@ enum HighlighterLanguage HIGHLIGHT_SQL }; -class SimpleTokenizer_Impl -{ - HighlighterLanguage aLanguage; - // Character information tables - sal_uInt16 aCharTypeTab[256]; - - const sal_Unicode* mpStringBegin; - const sal_Unicode* mpActualPos; - - sal_Unicode peekChar( void ) { return *mpActualPos; } - sal_Unicode getChar( void ) { return *mpActualPos++; } - - // Auxiliary function: testing of the character flags - sal_Bool testCharFlags( sal_Unicode c, sal_uInt16 nTestFlags ); - - // Get new token, EmptyString == nothing more over there - sal_Bool getNextToken( /*out*/TokenTypes& reType, - /*out*/const sal_Unicode*& rpStartPos, /*out*/const sal_Unicode*& rpEndPos ); - - const char** ppListKeyWords; - sal_uInt16 nKeyWordCount; - -public: - SimpleTokenizer_Impl( HighlighterLanguage aLang = HIGHLIGHT_BASIC ); - ~SimpleTokenizer_Impl( void ); - - sal_uInt16 parseLine( const OUString* aSource ); - void getHighlightPortions( const OUString& rLine, - /*out*/std::vector& portions ); - void setKeyWords( const char** ppKeyWords, sal_uInt16 nCount ); -}; - - //*** SyntaxHighlighter Class *** // Concept: the Highlighter will be notified of all changes in the source // (notifyChange) and returns the caller the range of lines, which based on the @@ -119,8 +72,10 @@ public: // lines internally whether or not C comments begin or end. class COMPHELPER_DLLPUBLIC SyntaxHighlighter { + class Tokenizer; + HighlighterLanguage eLanguage; - SimpleTokenizer_Impl* m_pSimpleTokenizer; + boost::scoped_ptr m_tokenizer; char* m_pKeyWords; sal_uInt16 m_nKeyWordCount; diff --git a/include/svtools/svmedit.hxx b/include/svtools/svmedit.hxx index 6999db8d5b51..64d81bd721fe 100644 --- a/include/svtools/svmedit.hxx +++ b/include/svtools/svmedit.hxx @@ -23,7 +23,6 @@ #include #include -#include #include #include -- cgit v1.2.3