PageRenderTime 70ms CodeModel.GetById 20ms 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
Possible License(s): LGPL-2.1
  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. #ifndef LL_LLKEYWORDS_H
  27. #define LL_LLKEYWORDS_H
  28. #include "llstring.h"
  29. #include "v3color.h"
  30. #include <map>
  31. #include <list>
  32. #include <deque>
  33. #include "llpointer.h"
  34. class LLTextSegment;
  35. typedef LLPointer<LLTextSegment> LLTextSegmentPtr;
  36. class LLKeywordToken
  37. {
  38. public:
  39. /**
  40. * @brief Types of tokens/delimters being parsed.
  41. *
  42. * @desc Tokens/delimiters that need to be identified/highlighted. All are terminated if an EOF is encountered.
  43. * - WORD are keywords in the normal sense, i.e. constants, events, etc.
  44. * - LINE are for entire lines (currently only flow control labels use this).
  45. * - ONE_SIDED_DELIMITER are for open-ended delimiters which are terminated by EOL.
  46. * - TWO_SIDED_DELIMITER are for delimiters that end with a different delimiter than they open with.
  47. * - DOUBLE_QUOTATION_MARKS are for delimiting areas using the same delimiter to open and close.
  48. */
  49. enum TOKEN_TYPE
  50. {
  51. WORD,
  52. LINE,
  53. TWO_SIDED_DELIMITER,
  54. ONE_SIDED_DELIMITER,
  55. DOUBLE_QUOTATION_MARKS
  56. };
  57. LLKeywordToken( TOKEN_TYPE type, const LLColor3& color, const LLWString& token, const LLWString& tool_tip, const LLWString& delimiter )
  58. :
  59. mType( type ),
  60. mToken( token ),
  61. mColor( color ),
  62. mToolTip( tool_tip ),
  63. mDelimiter( delimiter ) // right delimiter
  64. {
  65. }
  66. S32 getLengthHead() const { return mToken.size(); }
  67. S32 getLengthTail() const { return mDelimiter.size(); }
  68. BOOL isHead(const llwchar* s) const;
  69. BOOL isTail(const llwchar* s) const;
  70. const LLWString& getToken() const { return mToken; }
  71. const LLColor3& getColor() const { return mColor; }
  72. TOKEN_TYPE getType() const { return mType; }
  73. const LLWString& getToolTip() const { return mToolTip; }
  74. const LLWString& getDelimiter() const { return mDelimiter; }
  75. #ifdef _DEBUG
  76. void dump();
  77. #endif
  78. private:
  79. TOKEN_TYPE mType;
  80. LLWString mToken;
  81. LLColor3 mColor;
  82. LLWString mToolTip;
  83. LLWString mDelimiter;
  84. };
  85. class LLKeywords
  86. {
  87. public:
  88. LLKeywords();
  89. ~LLKeywords();
  90. BOOL loadFromFile(const std::string& filename);
  91. BOOL isLoaded() const { return mLoaded; }
  92. void findSegments(std::vector<LLTextSegmentPtr> *seg_list, const LLWString& text, const LLColor4 &defaultColor, class LLTextEditor& editor );
  93. // Add the token as described
  94. void addToken(LLKeywordToken::TOKEN_TYPE type,
  95. const std::string& key,
  96. const LLColor3& color,
  97. const std::string& tool_tip = LLStringUtil::null,
  98. const std::string& delimiter = LLStringUtil::null);
  99. // This class is here as a performance optimization.
  100. // The word token map used to be defined as std::map<LLWString, LLKeywordToken*>.
  101. // This worked, but caused a performance bottleneck due to memory allocation and string copies
  102. // because it's not possible to search such a map without creating an LLWString.
  103. // Using this class as the map index instead allows us to search using segments of an existing
  104. // text run without copying them first, which greatly reduces overhead in LLKeywords::findSegments().
  105. class WStringMapIndex
  106. {
  107. public:
  108. // copy constructor
  109. WStringMapIndex(const WStringMapIndex& other);
  110. // constructor from a string (copies the string's data into the new object)
  111. WStringMapIndex(const LLWString& str);
  112. // constructor from pointer and length
  113. // NOTE: does NOT copy data, caller must ensure that the lifetime of the pointer exceeds that of the new object!
  114. WStringMapIndex(const llwchar *start, size_t length);
  115. ~WStringMapIndex();
  116. bool operator<(const WStringMapIndex &other) const;
  117. private:
  118. void copyData(const llwchar *start, size_t length);
  119. const llwchar *mData;
  120. size_t mLength;
  121. bool mOwner;
  122. };
  123. typedef std::map<WStringMapIndex, LLKeywordToken*> word_token_map_t;
  124. typedef word_token_map_t::const_iterator keyword_iterator_t;
  125. keyword_iterator_t begin() const { return mWordTokenMap.begin(); }
  126. keyword_iterator_t end() const { return mWordTokenMap.end(); }
  127. #ifdef _DEBUG
  128. void dump();
  129. #endif
  130. private:
  131. LLColor3 readColor(const std::string& s);
  132. void insertSegment(std::vector<LLTextSegmentPtr>& seg_list, LLTextSegmentPtr new_segment, S32 text_len, const LLColor4 &defaultColor, class LLTextEditor& editor);
  133. 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);
  134. BOOL mLoaded;
  135. word_token_map_t mWordTokenMap;
  136. typedef std::deque<LLKeywordToken*> token_list_t;
  137. token_list_t mLineTokenList;
  138. token_list_t mDelimiterTokenList;
  139. };
  140. #endif // LL_LLKEYWORDS_H