LexerModule.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Scintilla source code edit control
  2. /** @file LexerModule.cxx
  3. ** Colourise for particular languages.
  4. **/
  5. // Copyright 1998-2010 by Neil Hodgson <neilh@scintilla.org>
  6. // The License.txt file describes the conditions under which this software may be distributed.
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <stdarg.h>
  11. #include <assert.h>
  12. #include <ctype.h>
  13. #include <string>
  14. #include "ILexer.h"
  15. #include "Scintilla.h"
  16. #include "SciLexer.h"
  17. #include "PropSetSimple.h"
  18. #include "WordList.h"
  19. #include "LexAccessor.h"
  20. #include "Accessor.h"
  21. #include "LexerModule.h"
  22. #include "LexerBase.h"
  23. #include "LexerSimple.h"
  24. #ifdef SCI_NAMESPACE
  25. using namespace Scintilla;
  26. #endif
  27. LexerModule::LexerModule(int language_,
  28. LexerFunction fnLexer_,
  29. const char *languageName_,
  30. LexerFunction fnFolder_,
  31. const char *const wordListDescriptions_[]) :
  32. language(language_),
  33. fnLexer(fnLexer_),
  34. fnFolder(fnFolder_),
  35. fnFactory(0),
  36. wordListDescriptions(wordListDescriptions_),
  37. languageName(languageName_) {
  38. }
  39. LexerModule::LexerModule(int language_,
  40. LexerFactoryFunction fnFactory_,
  41. const char *languageName_,
  42. const char * const wordListDescriptions_[]) :
  43. language(language_),
  44. fnLexer(0),
  45. fnFolder(0),
  46. fnFactory(fnFactory_),
  47. wordListDescriptions(wordListDescriptions_),
  48. languageName(languageName_) {
  49. }
  50. int LexerModule::GetNumWordLists() const {
  51. if (wordListDescriptions == NULL) {
  52. return -1;
  53. } else {
  54. int numWordLists = 0;
  55. while (wordListDescriptions[numWordLists]) {
  56. ++numWordLists;
  57. }
  58. return numWordLists;
  59. }
  60. }
  61. const char *LexerModule::GetWordListDescription(int index) const {
  62. assert(index < GetNumWordLists());
  63. if (!wordListDescriptions || (index >= GetNumWordLists())) {
  64. return "";
  65. } else {
  66. return wordListDescriptions[index];
  67. }
  68. }
  69. ILexer *LexerModule::Create() const {
  70. if (fnFactory)
  71. return fnFactory();
  72. else
  73. return new LexerSimple(this);
  74. }
  75. void LexerModule::Lex(Sci_PositionU startPos, Sci_Position lengthDoc, int initStyle,
  76. WordList *keywordlists[], Accessor &styler) const {
  77. if (fnLexer)
  78. fnLexer(startPos, lengthDoc, initStyle, keywordlists, styler);
  79. }
  80. void LexerModule::Fold(Sci_PositionU startPos, Sci_Position lengthDoc, int initStyle,
  81. WordList *keywordlists[], Accessor &styler) const {
  82. if (fnFolder) {
  83. Sci_Position lineCurrent = styler.GetLine(startPos);
  84. // Move back one line in case deletion wrecked current line fold state
  85. if (lineCurrent > 0) {
  86. lineCurrent--;
  87. Sci_Position newStartPos = styler.LineStart(lineCurrent);
  88. lengthDoc += startPos - newStartPos;
  89. startPos = newStartPos;
  90. initStyle = 0;
  91. if (startPos > 0) {
  92. initStyle = styler.StyleAt(startPos - 1);
  93. }
  94. }
  95. fnFolder(startPos, lengthDoc, initStyle, keywordlists, styler);
  96. }
  97. }