PageRenderTime 20ms CodeModel.GetById 10ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llui/llkeywords.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 159 lines | 94 code | 19 blank | 46 comment | 0 complexity | cf659585ff31c931d019cf7f4cef5a6c MD5 | raw file
  1/** 
  2 * @file llkeywords.h
  3 * @brief Keyword list for LSL
  4 *
  5 * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26
 27#ifndef LL_LLKEYWORDS_H
 28#define LL_LLKEYWORDS_H
 29
 30
 31#include "llstring.h"
 32#include "v3color.h"
 33#include <map>
 34#include <list>
 35#include <deque>
 36#include "llpointer.h"
 37
 38class LLTextSegment;
 39typedef LLPointer<LLTextSegment> LLTextSegmentPtr;
 40
 41class LLKeywordToken
 42{
 43public:
 44	/** 
 45	 * @brief Types of tokens/delimters being parsed.
 46	 *
 47	 * @desc Tokens/delimiters that need to be identified/highlighted. All are terminated if an EOF is encountered.
 48	 * - WORD are keywords in the normal sense, i.e. constants, events, etc.
 49	 * - LINE are for entire lines (currently only flow control labels use this).
 50	 * - ONE_SIDED_DELIMITER are for open-ended delimiters which are terminated by EOL.
 51	 * - TWO_SIDED_DELIMITER are for delimiters that end with a different delimiter than they open with.
 52	 * - DOUBLE_QUOTATION_MARKS are for delimiting areas using the same delimiter to open and close.
 53	 */
 54	enum TOKEN_TYPE
 55	{
 56		WORD,
 57		LINE,
 58		TWO_SIDED_DELIMITER,
 59		ONE_SIDED_DELIMITER,
 60		DOUBLE_QUOTATION_MARKS
 61	};
 62
 63	LLKeywordToken( TOKEN_TYPE type, const LLColor3& color, const LLWString& token, const LLWString& tool_tip, const LLWString& delimiter  ) 
 64		:
 65		mType( type ),
 66		mToken( token ),
 67		mColor( color ),
 68		mToolTip( tool_tip ),
 69		mDelimiter( delimiter )		// right delimiter
 70	{
 71	}
 72
 73	S32					getLengthHead() const	{ return mToken.size(); }
 74	S32					getLengthTail() const	{ return mDelimiter.size(); }
 75	BOOL				isHead(const llwchar* s) const;
 76	BOOL				isTail(const llwchar* s) const;
 77	const LLWString&	getToken() const		{ return mToken; }
 78	const LLColor3&		getColor() const		{ return mColor; }
 79	TOKEN_TYPE			getType()  const		{ return mType; }
 80	const LLWString&	getToolTip() const		{ return mToolTip; }
 81	const LLWString&	getDelimiter() const	{ return mDelimiter; }
 82
 83#ifdef _DEBUG
 84	void		dump();
 85#endif
 86
 87private:
 88	TOKEN_TYPE	mType;
 89	LLWString	mToken;
 90	LLColor3	mColor;
 91	LLWString	mToolTip;
 92	LLWString	mDelimiter;
 93};
 94
 95class LLKeywords
 96{
 97public:
 98	LLKeywords();
 99	~LLKeywords();
100
101	BOOL		loadFromFile(const std::string& filename);
102	BOOL		isLoaded() const	{ return mLoaded; }
103
104	void		findSegments(std::vector<LLTextSegmentPtr> *seg_list, const LLWString& text, const LLColor4 &defaultColor, class LLTextEditor& editor );
105
106	// Add the token as described
107	void addToken(LLKeywordToken::TOKEN_TYPE type,
108					const std::string& key,
109					const LLColor3& color,
110					const std::string& tool_tip = LLStringUtil::null,
111					const std::string& delimiter = LLStringUtil::null);
112	
113	// This class is here as a performance optimization.
114	// The word token map used to be defined as std::map<LLWString, LLKeywordToken*>.
115	// This worked, but caused a performance bottleneck due to memory allocation and string copies
116	//  because it's not possible to search such a map without creating an LLWString.
117	// Using this class as the map index instead allows us to search using segments of an existing
118	//  text run without copying them first, which greatly reduces overhead in LLKeywords::findSegments().
119	class WStringMapIndex
120	{
121	public:
122		// copy constructor
123		WStringMapIndex(const WStringMapIndex& other);
124		// constructor from a string (copies the string's data into the new object)
125		WStringMapIndex(const LLWString& str);
126		// constructor from pointer and length
127		// NOTE: does NOT copy data, caller must ensure that the lifetime of the pointer exceeds that of the new object!
128		WStringMapIndex(const llwchar *start, size_t length);
129		~WStringMapIndex();
130		bool operator<(const WStringMapIndex &other) const;
131	private:
132		void copyData(const llwchar *start, size_t length);
133		const llwchar *mData;
134		size_t mLength;
135		bool mOwner;
136	};
137
138	typedef std::map<WStringMapIndex, LLKeywordToken*> word_token_map_t;
139	typedef word_token_map_t::const_iterator keyword_iterator_t;
140	keyword_iterator_t begin() const { return mWordTokenMap.begin(); }
141	keyword_iterator_t end() const { return mWordTokenMap.end(); }
142
143#ifdef _DEBUG
144	void		dump();
145#endif
146
147private:
148	LLColor3	readColor(const std::string& s);
149	void		insertSegment(std::vector<LLTextSegmentPtr>& seg_list, LLTextSegmentPtr new_segment, S32 text_len, const LLColor4 &defaultColor, class LLTextEditor& editor);
150	void		insertSegments(const LLWString& wtext, std::vector<LLTextSegmentPtr>& seg_list, LLKeywordToken* token, S32 text_len, S32 seg_start, S32 seg_end, const LLColor4 &defaultColor, LLTextEditor& editor);
151
152	BOOL		mLoaded;
153	word_token_map_t mWordTokenMap;
154	typedef std::deque<LLKeywordToken*> token_list_t;
155	token_list_t mLineTokenList;
156	token_list_t mDelimiterTokenList;
157};
158
159#endif  // LL_LLKEYWORDS_H