LexConf.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // Scintilla source code edit control
  2. /** @file LexConf.cxx
  3. ** Lexer for Apache Configuration Files.
  4. **
  5. ** First working version contributed by Ahmad Zawawi <ahmad.zawawi@gmail.com> on October 28, 2000.
  6. ** i created this lexer because i needed something pretty when dealing
  7. ** when Apache Configuration files...
  8. **/
  9. // Copyright 1998-2001 by Neil Hodgson <neilh@scintilla.org>
  10. // The License.txt file describes the conditions under which this software may be distributed.
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <stdio.h>
  14. #include <stdarg.h>
  15. #include <assert.h>
  16. #include <ctype.h>
  17. #include "ILexer.h"
  18. #include "Scintilla.h"
  19. #include "SciLexer.h"
  20. #include "WordList.h"
  21. #include "LexAccessor.h"
  22. #include "Accessor.h"
  23. #include "StyleContext.h"
  24. #include "CharacterSet.h"
  25. #include "LexerModule.h"
  26. #ifdef SCI_NAMESPACE
  27. using namespace Scintilla;
  28. #endif
  29. static void ColouriseConfDoc(Sci_PositionU startPos, Sci_Position length, int, WordList *keywordLists[], Accessor &styler)
  30. {
  31. int state = SCE_CONF_DEFAULT;
  32. char chNext = styler[startPos];
  33. Sci_Position lengthDoc = startPos + length;
  34. // create a buffer large enough to take the largest chunk...
  35. char *buffer = new char[length+1];
  36. Sci_Position bufferCount = 0;
  37. // this assumes that we have 2 keyword list in conf.properties
  38. WordList &directives = *keywordLists[0];
  39. WordList &params = *keywordLists[1];
  40. // go through all provided text segment
  41. // using the hand-written state machine shown below
  42. styler.StartAt(startPos);
  43. styler.StartSegment(startPos);
  44. for (Sci_Position i = startPos; i < lengthDoc; i++) {
  45. char ch = chNext;
  46. chNext = styler.SafeGetCharAt(i + 1);
  47. if (styler.IsLeadByte(ch)) {
  48. chNext = styler.SafeGetCharAt(i + 2);
  49. i++;
  50. continue;
  51. }
  52. switch(state) {
  53. case SCE_CONF_DEFAULT:
  54. if( ch == '\n' || ch == '\r' || ch == '\t' || ch == ' ') {
  55. // whitespace is simply ignored here...
  56. styler.ColourTo(i,SCE_CONF_DEFAULT);
  57. break;
  58. } else if( ch == '#' ) {
  59. // signals the start of a comment...
  60. state = SCE_CONF_COMMENT;
  61. styler.ColourTo(i,SCE_CONF_COMMENT);
  62. } else if( ch == '.' /*|| ch == '/'*/) {
  63. // signals the start of a file...
  64. state = SCE_CONF_EXTENSION;
  65. styler.ColourTo(i,SCE_CONF_EXTENSION);
  66. } else if( ch == '"') {
  67. state = SCE_CONF_STRING;
  68. styler.ColourTo(i,SCE_CONF_STRING);
  69. } else if( IsASCII(ch) && ispunct(ch) ) {
  70. // signals an operator...
  71. // no state jump necessary for this
  72. // simple case...
  73. styler.ColourTo(i,SCE_CONF_OPERATOR);
  74. } else if( IsASCII(ch) && isalpha(ch) ) {
  75. // signals the start of an identifier
  76. bufferCount = 0;
  77. buffer[bufferCount++] = static_cast<char>(tolower(ch));
  78. state = SCE_CONF_IDENTIFIER;
  79. } else if( IsASCII(ch) && isdigit(ch) ) {
  80. // signals the start of a number
  81. bufferCount = 0;
  82. buffer[bufferCount++] = ch;
  83. //styler.ColourTo(i,SCE_CONF_NUMBER);
  84. state = SCE_CONF_NUMBER;
  85. } else {
  86. // style it the default style..
  87. styler.ColourTo(i,SCE_CONF_DEFAULT);
  88. }
  89. break;
  90. case SCE_CONF_COMMENT:
  91. // if we find a newline here,
  92. // we simply go to default state
  93. // else continue to work on it...
  94. if( ch == '\n' || ch == '\r' ) {
  95. state = SCE_CONF_DEFAULT;
  96. } else {
  97. styler.ColourTo(i,SCE_CONF_COMMENT);
  98. }
  99. break;
  100. case SCE_CONF_EXTENSION:
  101. // if we find a non-alphanumeric char,
  102. // we simply go to default state
  103. // else we're still dealing with an extension...
  104. if( (IsASCII(ch) && isalnum(ch)) || (ch == '_') ||
  105. (ch == '-') || (ch == '$') ||
  106. (ch == '/') || (ch == '.') || (ch == '*') )
  107. {
  108. styler.ColourTo(i,SCE_CONF_EXTENSION);
  109. } else {
  110. state = SCE_CONF_DEFAULT;
  111. chNext = styler[i--];
  112. }
  113. break;
  114. case SCE_CONF_STRING:
  115. // if we find the end of a string char, we simply go to default state
  116. // else we're still dealing with an string...
  117. if( (ch == '"' && styler.SafeGetCharAt(i-1)!='\\') || (ch == '\n') || (ch == '\r') ) {
  118. state = SCE_CONF_DEFAULT;
  119. }
  120. styler.ColourTo(i,SCE_CONF_STRING);
  121. break;
  122. case SCE_CONF_IDENTIFIER:
  123. // stay in CONF_IDENTIFIER state until we find a non-alphanumeric
  124. if( (IsASCII(ch) && isalnum(ch)) || (ch == '_') || (ch == '-') || (ch == '/') || (ch == '$') || (ch == '.') || (ch == '*')) {
  125. buffer[bufferCount++] = static_cast<char>(tolower(ch));
  126. } else {
  127. state = SCE_CONF_DEFAULT;
  128. buffer[bufferCount] = '\0';
  129. // check if the buffer contains a keyword, and highlight it if it is a keyword...
  130. if(directives.InList(buffer)) {
  131. styler.ColourTo(i-1,SCE_CONF_DIRECTIVE );
  132. } else if(params.InList(buffer)) {
  133. styler.ColourTo(i-1,SCE_CONF_PARAMETER );
  134. } else if(strchr(buffer,'/') || strchr(buffer,'.')) {
  135. styler.ColourTo(i-1,SCE_CONF_EXTENSION);
  136. } else {
  137. styler.ColourTo(i-1,SCE_CONF_DEFAULT);
  138. }
  139. // push back the faulty character
  140. chNext = styler[i--];
  141. }
  142. break;
  143. case SCE_CONF_NUMBER:
  144. // stay in CONF_NUMBER state until we find a non-numeric
  145. if( (IsASCII(ch) && isdigit(ch)) || ch == '.') {
  146. buffer[bufferCount++] = ch;
  147. } else {
  148. state = SCE_CONF_DEFAULT;
  149. buffer[bufferCount] = '\0';
  150. // Colourize here...
  151. if( strchr(buffer,'.') ) {
  152. // it is an IP address...
  153. styler.ColourTo(i-1,SCE_CONF_IP);
  154. } else {
  155. // normal number
  156. styler.ColourTo(i-1,SCE_CONF_NUMBER);
  157. }
  158. // push back a character
  159. chNext = styler[i--];
  160. }
  161. break;
  162. }
  163. }
  164. delete []buffer;
  165. }
  166. static const char * const confWordListDesc[] = {
  167. "Directives",
  168. "Parameters",
  169. 0
  170. };
  171. LexerModule lmConf(SCLEX_CONF, ColouriseConfDoc, "conf", 0, confWordListDesc);