llkeywords.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. /**
  2. * @file llkeywords.cpp
  3. * @brief Keyword list for LSL
  4. *
  5. * $LicenseInfo:firstyear=2000&license=viewergpl$
  6. *
  7. * Copyright (c) 2000-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "linden_common.h"
  33. #include <iostream>
  34. #include <fstream>
  35. #include "boost/tokenizer.hpp"
  36. #include "llkeywords.h"
  37. #include "llstl.h"
  38. #include "lltexteditor.h"
  39. constexpr U32 KEYWORD_FILE_CURRENT_VERSION = 2;
  40. LL_INLINE bool LLKeywordToken::isHead(const llwchar* s,
  41. bool search_end_c_comment) const
  42. {
  43. // strncmp is much faster than string compare
  44. const llwchar* t = mToken.c_str();
  45. S32 len = mToken.size();
  46. if (search_end_c_comment && len == 2 && t[0] == '/' && t[1] == '*')
  47. {
  48. // Special case for C-like */ end comment token
  49. return s[0] == '*' && s[1] == '/';
  50. }
  51. for (S32 i = 0; i < len; ++i)
  52. {
  53. if (s[i] != t[i])
  54. {
  55. return false;
  56. }
  57. }
  58. return true;
  59. }
  60. LLKeywords::LLKeywords()
  61. : mLoaded(false)
  62. {
  63. }
  64. LLKeywords::~LLKeywords()
  65. {
  66. std::for_each(mWordTokenMap.begin(), mWordTokenMap.end(),
  67. DeletePairedPointer());
  68. mWordTokenMap.clear();
  69. std::for_each(mLineTokenList.begin(), mLineTokenList.end(),
  70. DeletePointer());
  71. mLineTokenList.clear();
  72. std::for_each(mDelimiterTokenList.begin(), mDelimiterTokenList.end(),
  73. DeletePointer());
  74. mDelimiterTokenList.clear();
  75. }
  76. bool LLKeywords::loadFromFile(const std::string& filename)
  77. {
  78. mLoaded = false;
  79. ////////////////////////////////////////////////////////////
  80. // File header
  81. constexpr S32 BUFFER_SIZE = 1024;
  82. char buffer[BUFFER_SIZE];
  83. llifstream file(filename.c_str());
  84. if (!file.is_open())
  85. {
  86. llwarns << "Unable to open file: " << filename << llendl;
  87. return mLoaded;
  88. }
  89. // Identifying string
  90. file >> buffer;
  91. if (strcmp(buffer, "llkeywords"))
  92. {
  93. llwarns << filename << " does not appear to be a keyword file"
  94. << llendl;
  95. return mLoaded;
  96. }
  97. // Check file version
  98. file >> buffer;
  99. U32 version_num;
  100. file >> version_num;
  101. if (strcmp(buffer, "version") ||
  102. version_num != (U32)KEYWORD_FILE_CURRENT_VERSION)
  103. {
  104. llwarns << filename << " does not appear to be a version "
  105. << KEYWORD_FILE_CURRENT_VERSION << " keyword file" << llendl;
  106. return mLoaded;
  107. }
  108. // Start of line (SOL)
  109. static const std::string SOL_COMMENT = ";";
  110. static const std::string SOL_WORD = "[word ";
  111. static const std::string SOL_LINE = "[line ";
  112. static const std::string SOL_ONE_SIDED_DELIMITER = "[one_sided_delimiter ";
  113. static const std::string SOL_TWO_SIDED_DELIMITER = "[two_sided_delimiter ";
  114. LLColor3 cur_color(1.f, 0.f, 0.f);
  115. LLKeywordToken::TOKEN_TYPE cur_type = LLKeywordToken::WORD;
  116. while (!file.eof())
  117. {
  118. buffer[0] = 0;
  119. file.getline(buffer, BUFFER_SIZE);
  120. std::string line(buffer);
  121. if (line.find(SOL_COMMENT) == 0)
  122. {
  123. continue;
  124. }
  125. else if (line.find(SOL_WORD) == 0)
  126. {
  127. cur_color = readColor(line.substr(SOL_WORD.size()));
  128. cur_type = LLKeywordToken::WORD;
  129. continue;
  130. }
  131. else if (line.find(SOL_LINE) == 0)
  132. {
  133. cur_color = readColor(line.substr(SOL_LINE.size()));
  134. cur_type = LLKeywordToken::LINE;
  135. continue;
  136. }
  137. else if (line.find(SOL_TWO_SIDED_DELIMITER) == 0)
  138. {
  139. cur_color = readColor(line.substr(SOL_TWO_SIDED_DELIMITER.size()));
  140. cur_type = LLKeywordToken::TWO_SIDED_DELIMITER;
  141. continue;
  142. }
  143. else if (line.find(SOL_ONE_SIDED_DELIMITER) == 0)
  144. {
  145. cur_color = readColor(line.substr(SOL_ONE_SIDED_DELIMITER.size()));
  146. cur_type = LLKeywordToken::ONE_SIDED_DELIMITER;
  147. continue;
  148. }
  149. std::string token_buffer(line);
  150. LLStringUtil::trim(token_buffer);
  151. typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
  152. boost::char_separator<char> sep_word("", " \t");
  153. tokenizer word_tokens(token_buffer, sep_word);
  154. tokenizer::iterator token_word_iter = word_tokens.begin();
  155. if (!token_buffer.empty() && token_word_iter != word_tokens.end())
  156. {
  157. // First word is keyword
  158. std::string keyword = *token_word_iter;
  159. LLStringUtil::trim(keyword);
  160. // Following words are tooltip
  161. std::string tool_tip;
  162. while (++token_word_iter != word_tokens.end())
  163. {
  164. tool_tip += *token_word_iter;
  165. }
  166. LLStringUtil::trim(tool_tip);
  167. if (!tool_tip.empty())
  168. {
  169. // Replace : with \n for multi-line tool tips.
  170. LLStringUtil::replaceChar(tool_tip, ':', '\n');
  171. addToken(cur_type, keyword, cur_color, tool_tip);
  172. }
  173. else
  174. {
  175. addToken(cur_type, keyword, cur_color, LLStringUtil::null);
  176. }
  177. }
  178. }
  179. file.close();
  180. mLoaded = true;
  181. return mLoaded;
  182. }
  183. // Add the token as described
  184. void LLKeywords::addToken(LLKeywordToken::TOKEN_TYPE type,
  185. const std::string& key_in, const LLColor3& color,
  186. const std::string& tool_tip_in)
  187. {
  188. LLWString key = utf8str_to_wstring(key_in);
  189. LLWString tool_tip = utf8str_to_wstring(tool_tip_in);
  190. switch(type)
  191. {
  192. case LLKeywordToken::WORD:
  193. mWordTokenMap[key] = new LLKeywordToken(type, color, key,
  194. tool_tip);
  195. break;
  196. case LLKeywordToken::LINE:
  197. mLineTokenList.push_front(new LLKeywordToken(type, color, key,
  198. tool_tip));
  199. break;
  200. case LLKeywordToken::TWO_SIDED_DELIMITER:
  201. case LLKeywordToken::ONE_SIDED_DELIMITER:
  202. mDelimiterTokenList.push_front(new LLKeywordToken(type, color, key,
  203. tool_tip));
  204. break;
  205. default:
  206. llassert(false);
  207. }
  208. }
  209. LLColor3 LLKeywords::readColor(const std::string& s)
  210. {
  211. F32 r, g, b;
  212. r = g = b = 0.0f;
  213. if (sscanf(s.c_str(), "%f, %f, %f]", &r, &g, &b) == 3)
  214. {
  215. return LLColor3(r, g, b);
  216. }
  217. size_t i = s.rfind(']');
  218. if (i != std::string::npos)
  219. {
  220. std::string color_name = s.substr(0, i);
  221. i = color_name.rfind(' ');
  222. if (i != std::string::npos)
  223. {
  224. color_name = color_name.substr(i + 1);
  225. }
  226. if (LLUI::sColorsGroup &&
  227. LLUI::sColorsGroup->getControl(color_name.c_str()))
  228. {
  229. return LLColor3(LLUI::sColorsGroup->getColor(color_name.c_str()));
  230. }
  231. }
  232. llwarns << "Poorly formed color (" << s << ") in keyword file." << llendl;
  233. return LLColor3(r, g, b);
  234. }
  235. // Walk through a string, applying the rules specified by the keyword token
  236. // list and create a list of color segments.
  237. void LLKeywords::findSegments(std::vector<LLTextSegment*>* seg_list,
  238. const LLWString& wtext,
  239. const LLColor4 &default_color)
  240. {
  241. std::for_each(seg_list->begin(), seg_list->end(), DeletePointer());
  242. seg_list->clear();
  243. if (wtext.empty())
  244. {
  245. return;
  246. }
  247. S32 text_len = wtext.size();
  248. seg_list->push_back(new LLTextSegment(LLColor3(default_color), 0,
  249. text_len));
  250. const llwchar* base = wtext.c_str();
  251. const llwchar* cur = base;
  252. while (*cur)
  253. {
  254. if (*cur == '\n' || cur == base)
  255. {
  256. if (*cur == '\n')
  257. {
  258. ++cur;
  259. if (!*cur || *cur == '\n')
  260. {
  261. continue;
  262. }
  263. }
  264. // Skip white space
  265. while (*cur && isspace(*cur) && *cur != '\n')
  266. {
  267. ++cur;
  268. }
  269. if (!*cur || *cur == '\n')
  270. {
  271. continue;
  272. }
  273. // cur is now at the first non-whitespace character of a new line
  274. // Line start tokens
  275. {
  276. bool line_done = false;
  277. for (token_list_t::iterator iter = mLineTokenList.begin(),
  278. end = mLineTokenList.end();
  279. iter != end; ++iter)
  280. {
  281. LLKeywordToken* cur_token = *iter;
  282. if (cur_token->isHead(cur))
  283. {
  284. S32 seg_start = cur - base;
  285. while (*cur && *cur != '\n')
  286. {
  287. // skip the rest of the line
  288. ++cur;
  289. }
  290. S32 seg_end = cur - base;
  291. LLTextSegment* text_segment =
  292. new LLTextSegment(cur_token->getColor(), seg_start,
  293. seg_end);
  294. text_segment->setToken(cur_token);
  295. insertSegment(seg_list, text_segment, text_len,
  296. default_color);
  297. line_done = true; // To break out of second loop.
  298. break;
  299. }
  300. }
  301. if (line_done)
  302. {
  303. continue;
  304. }
  305. }
  306. }
  307. // Skip white space
  308. while (*cur && isspace(*cur) && *cur != '\n')
  309. {
  310. ++cur;
  311. }
  312. while (*cur && *cur != '\n')
  313. {
  314. // Check against delimiters
  315. {
  316. S32 seg_start = 0;
  317. LLKeywordToken* cur_delimiter = NULL;
  318. for (token_list_t::iterator iter = mDelimiterTokenList.begin();
  319. iter != mDelimiterTokenList.end(); ++iter)
  320. {
  321. LLKeywordToken* delimiter = *iter;
  322. if (delimiter->isHead(cur))
  323. {
  324. cur_delimiter = delimiter;
  325. break;
  326. }
  327. }
  328. if (cur_delimiter)
  329. {
  330. S32 between_delimiters = 0;
  331. S32 seg_end = 0;
  332. seg_start = cur - base;
  333. cur += cur_delimiter->getLength();
  334. if (cur_delimiter->getType() ==
  335. LLKeywordToken::TWO_SIDED_DELIMITER)
  336. {
  337. while (*cur && !cur_delimiter->isHead(cur, true))
  338. {
  339. // Check for an escape sequence.
  340. if (*cur == '\\')
  341. {
  342. // Count the number of backslashes.
  343. S32 num_backslashes = 0;
  344. while (*cur == '\\')
  345. {
  346. ++num_backslashes;
  347. ++between_delimiters;
  348. ++cur;
  349. }
  350. // Is the next character the end delimiter ?
  351. if (cur_delimiter->isHead(cur, true))
  352. {
  353. // Is there was an odd number of
  354. // backslashes, then this delimiter does
  355. // not end the sequence.
  356. if (num_backslashes % 2 == 1)
  357. {
  358. ++between_delimiters;
  359. ++cur;
  360. }
  361. else
  362. {
  363. // This is an end delimiter.
  364. break;
  365. }
  366. }
  367. }
  368. else
  369. {
  370. ++between_delimiters;
  371. ++cur;
  372. }
  373. }
  374. if (*cur)
  375. {
  376. cur += cur_delimiter->getLength();
  377. seg_end = seg_start + between_delimiters +
  378. 2 * cur_delimiter->getLength();
  379. }
  380. else
  381. {
  382. // eof
  383. seg_end = seg_start + between_delimiters +
  384. cur_delimiter->getLength();
  385. }
  386. }
  387. else
  388. {
  389. llassert(cur_delimiter->getType() ==
  390. LLKeywordToken::ONE_SIDED_DELIMITER);
  391. // Left side is the delimiter. Right side is eol or eof
  392. while (*cur && *cur != '\n')
  393. {
  394. ++between_delimiters;
  395. ++cur;
  396. }
  397. seg_end = seg_start + between_delimiters +
  398. cur_delimiter->getLength();
  399. }
  400. LLTextSegment* text_segment =
  401. new LLTextSegment(cur_delimiter->getColor(), seg_start,
  402. seg_end);
  403. text_segment->setToken(cur_delimiter);
  404. insertSegment(seg_list, text_segment, text_len,
  405. default_color);
  406. // Note: we do not increment cur, since the end of one
  407. // delimited seg may be immediately followed by the start
  408. // of another one.
  409. continue;
  410. }
  411. }
  412. // Check against words
  413. llwchar prev = cur > base ? *(cur - 1) : 0;
  414. if (!isalnum(prev) && prev != '_')
  415. {
  416. const llwchar* p = cur;
  417. #if 0 // Works for "#define", but not for "# define", so...
  418. if (*p == '#') // For preprocessor #directive tokens
  419. {
  420. ++p;
  421. }
  422. #endif
  423. while (isalnum(*p) || *p == '_')
  424. {
  425. ++p;
  426. }
  427. S32 seg_len = p - cur;
  428. if (seg_len > 0)
  429. {
  430. LLWString word(cur, 0, seg_len);
  431. word_token_map_t::iterator map_iter =
  432. mWordTokenMap.find(word);
  433. if (map_iter != mWordTokenMap.end())
  434. {
  435. LLKeywordToken* cur_token = map_iter->second;
  436. S32 seg_start = cur - base;
  437. S32 seg_end = seg_start + seg_len;
  438. LLTextSegment* text_segment =
  439. new LLTextSegment(cur_token->getColor(), seg_start,
  440. seg_end);
  441. text_segment->setToken(cur_token);
  442. insertSegment(seg_list, text_segment, text_len,
  443. default_color);
  444. }
  445. cur += seg_len;
  446. continue;
  447. }
  448. }
  449. if (*cur && *cur != '\n')
  450. {
  451. ++cur;
  452. }
  453. }
  454. }
  455. }
  456. void LLKeywords::insertSegment(std::vector<LLTextSegment*>* seg_list,
  457. LLTextSegment* new_segment, S32 text_len,
  458. const LLColor4& default_color)
  459. {
  460. LLTextSegment* last = seg_list->back();
  461. S32 new_seg_end = new_segment->getEnd();
  462. if (new_segment->getStart() == last->getStart())
  463. {
  464. *last = *new_segment;
  465. delete new_segment;
  466. }
  467. else
  468. {
  469. last->setEnd(new_segment->getStart());
  470. seg_list->push_back(new_segment);
  471. }
  472. if (new_seg_end < text_len)
  473. {
  474. seg_list->push_back(new LLTextSegment(default_color, new_seg_end,
  475. text_len));
  476. }
  477. }
  478. #if LL_DEBUG
  479. void LLKeywords::dump()
  480. {
  481. llinfos << "LLKeywords" << llendl;
  482. llinfos << "LLKeywords::sWordTokenMap" << llendl;
  483. word_token_map_t::iterator word_token_iter = mWordTokenMap.begin();
  484. while (word_token_iter != mWordTokenMap.end())
  485. {
  486. LLKeywordToken* word_token = word_token_iter->second;
  487. word_token->dump();
  488. ++word_token_iter;
  489. }
  490. llinfos << "LLKeywords::sLineTokenList" << llendl;
  491. for (token_list_t::iterator iter = mLineTokenList.begin();
  492. iter != mLineTokenList.end(); ++iter)
  493. {
  494. LLKeywordToken* line_token = *iter;
  495. line_token->dump();
  496. }
  497. llinfos << "LLKeywords::sDelimiterTokenList" << llendl;
  498. for (token_list_t::iterator iter = mDelimiterTokenList.begin();
  499. iter != mDelimiterTokenList.end(); ++iter)
  500. {
  501. LLKeywordToken* delimiter_token = *iter;
  502. delimiter_token->dump();
  503. }
  504. }
  505. void LLKeywordToken::dump()
  506. {
  507. llinfos << "[" << mColor.mV[VX] << ", " << mColor.mV[VY] << ", "
  508. << mColor.mV[VZ] << "] [" << wstring_to_utf8str(mToken) << "]"
  509. << llendl;
  510. }
  511. #endif // DEBUG