LexPO.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // Scintilla source code edit control
  2. /** @file LexPO.cxx
  3. ** Lexer for GetText Translation (PO) files.
  4. **/
  5. // Copyright 2012 by Colomban Wendling <ban@herbesfolles.org>
  6. // The License.txt file describes the conditions under which this software may be distributed.
  7. // see https://www.gnu.org/software/gettext/manual/gettext.html#PO-Files for the syntax reference
  8. // some details are taken from the GNU msgfmt behavior (like that indent is allows in front of lines)
  9. // TODO:
  10. // * add keywords for flags (fuzzy, c-format, ...)
  11. // * highlight formats inside c-format strings (%s, %d, etc.)
  12. // * style for previous untranslated string? ("#|" comment)
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <stdio.h>
  16. #include <stdarg.h>
  17. #include <assert.h>
  18. #include <ctype.h>
  19. #include "ILexer.h"
  20. #include "Scintilla.h"
  21. #include "SciLexer.h"
  22. #include "WordList.h"
  23. #include "LexAccessor.h"
  24. #include "Accessor.h"
  25. #include "StyleContext.h"
  26. #include "CharacterSet.h"
  27. #include "LexerModule.h"
  28. #ifdef SCI_NAMESPACE
  29. using namespace Scintilla;
  30. #endif
  31. static void ColourisePODoc(Sci_PositionU startPos, Sci_Position length, int initStyle, WordList *[], Accessor &styler) {
  32. StyleContext sc(startPos, length, initStyle, styler);
  33. bool escaped = false;
  34. Sci_Position curLine = styler.GetLine(startPos);
  35. // the line state holds the last state on or before the line that isn't the default style
  36. int curLineState = curLine > 0 ? styler.GetLineState(curLine - 1) : SCE_PO_DEFAULT;
  37. for (; sc.More(); sc.Forward()) {
  38. // whether we should leave a state
  39. switch (sc.state) {
  40. case SCE_PO_COMMENT:
  41. case SCE_PO_PROGRAMMER_COMMENT:
  42. case SCE_PO_REFERENCE:
  43. case SCE_PO_FLAGS:
  44. case SCE_PO_FUZZY:
  45. if (sc.atLineEnd)
  46. sc.SetState(SCE_PO_DEFAULT);
  47. else if (sc.state == SCE_PO_FLAGS && sc.Match("fuzzy"))
  48. // here we behave like the previous parser, but this should probably be highlighted
  49. // on its own like a keyword rather than changing the whole flags style
  50. sc.ChangeState(SCE_PO_FUZZY);
  51. break;
  52. case SCE_PO_MSGCTXT:
  53. case SCE_PO_MSGID:
  54. case SCE_PO_MSGSTR:
  55. if (isspacechar(sc.ch))
  56. sc.SetState(SCE_PO_DEFAULT);
  57. break;
  58. case SCE_PO_ERROR:
  59. if (sc.atLineEnd)
  60. sc.SetState(SCE_PO_DEFAULT);
  61. break;
  62. case SCE_PO_MSGCTXT_TEXT:
  63. case SCE_PO_MSGID_TEXT:
  64. case SCE_PO_MSGSTR_TEXT:
  65. if (sc.atLineEnd) { // invalid inside a string
  66. if (sc.state == SCE_PO_MSGCTXT_TEXT)
  67. sc.ChangeState(SCE_PO_MSGCTXT_TEXT_EOL);
  68. else if (sc.state == SCE_PO_MSGID_TEXT)
  69. sc.ChangeState(SCE_PO_MSGID_TEXT_EOL);
  70. else if (sc.state == SCE_PO_MSGSTR_TEXT)
  71. sc.ChangeState(SCE_PO_MSGSTR_TEXT_EOL);
  72. sc.SetState(SCE_PO_DEFAULT);
  73. escaped = false;
  74. } else {
  75. if (escaped)
  76. escaped = false;
  77. else if (sc.ch == '\\')
  78. escaped = true;
  79. else if (sc.ch == '"')
  80. sc.ForwardSetState(SCE_PO_DEFAULT);
  81. }
  82. break;
  83. }
  84. // whether we should enter a new state
  85. if (sc.state == SCE_PO_DEFAULT) {
  86. // forward to the first non-white character on the line
  87. bool atLineStart = sc.atLineStart;
  88. if (atLineStart) {
  89. // reset line state if it is set to comment state so empty lines don't get
  90. // comment line state, and the folding code folds comments separately,
  91. // and anyway the styling don't use line state for comments
  92. if (curLineState == SCE_PO_COMMENT)
  93. curLineState = SCE_PO_DEFAULT;
  94. while (sc.More() && ! sc.atLineEnd && isspacechar(sc.ch))
  95. sc.Forward();
  96. }
  97. if (atLineStart && sc.ch == '#') {
  98. if (sc.chNext == '.')
  99. sc.SetState(SCE_PO_PROGRAMMER_COMMENT);
  100. else if (sc.chNext == ':')
  101. sc.SetState(SCE_PO_REFERENCE);
  102. else if (sc.chNext == ',')
  103. sc.SetState(SCE_PO_FLAGS);
  104. else
  105. sc.SetState(SCE_PO_COMMENT);
  106. } else if (atLineStart && sc.Match("msgid")) { // includes msgid_plural
  107. sc.SetState(SCE_PO_MSGID);
  108. } else if (atLineStart && sc.Match("msgstr")) { // includes [] suffixes
  109. sc.SetState(SCE_PO_MSGSTR);
  110. } else if (atLineStart && sc.Match("msgctxt")) {
  111. sc.SetState(SCE_PO_MSGCTXT);
  112. } else if (sc.ch == '"') {
  113. if (curLineState == SCE_PO_MSGCTXT || curLineState == SCE_PO_MSGCTXT_TEXT)
  114. sc.SetState(SCE_PO_MSGCTXT_TEXT);
  115. else if (curLineState == SCE_PO_MSGID || curLineState == SCE_PO_MSGID_TEXT)
  116. sc.SetState(SCE_PO_MSGID_TEXT);
  117. else if (curLineState == SCE_PO_MSGSTR || curLineState == SCE_PO_MSGSTR_TEXT)
  118. sc.SetState(SCE_PO_MSGSTR_TEXT);
  119. else
  120. sc.SetState(SCE_PO_ERROR);
  121. } else if (! isspacechar(sc.ch))
  122. sc.SetState(SCE_PO_ERROR);
  123. if (sc.state != SCE_PO_DEFAULT)
  124. curLineState = sc.state;
  125. }
  126. if (sc.atLineEnd) {
  127. // Update the line state, so it can be seen by next line
  128. curLine = styler.GetLine(sc.currentPos);
  129. styler.SetLineState(curLine, curLineState);
  130. }
  131. }
  132. sc.Complete();
  133. }
  134. static int FindNextNonEmptyLineState(Sci_PositionU startPos, Accessor &styler) {
  135. Sci_PositionU length = styler.Length();
  136. for (Sci_PositionU i = startPos; i < length; i++) {
  137. if (! isspacechar(styler[i])) {
  138. return styler.GetLineState(styler.GetLine(i));
  139. }
  140. }
  141. return 0;
  142. }
  143. static void FoldPODoc(Sci_PositionU startPos, Sci_Position length, int, WordList *[], Accessor &styler) {
  144. if (! styler.GetPropertyInt("fold"))
  145. return;
  146. bool foldCompact = styler.GetPropertyInt("fold.compact") != 0;
  147. bool foldComment = styler.GetPropertyInt("fold.comment") != 0;
  148. Sci_PositionU endPos = startPos + length;
  149. Sci_Position curLine = styler.GetLine(startPos);
  150. int lineState = styler.GetLineState(curLine);
  151. int nextLineState;
  152. int level = styler.LevelAt(curLine) & SC_FOLDLEVELNUMBERMASK;
  153. int nextLevel;
  154. int visible = 0;
  155. int chNext = styler[startPos];
  156. for (Sci_PositionU i = startPos; i < endPos; i++) {
  157. int ch = chNext;
  158. chNext = styler.SafeGetCharAt(i+1);
  159. if (! isspacechar(ch)) {
  160. visible++;
  161. } else if ((ch == '\r' && chNext != '\n') || ch == '\n' || i+1 >= endPos) {
  162. int lvl = level;
  163. Sci_Position nextLine = curLine + 1;
  164. nextLineState = styler.GetLineState(nextLine);
  165. if ((lineState != SCE_PO_COMMENT || foldComment) &&
  166. nextLineState == lineState &&
  167. FindNextNonEmptyLineState(i, styler) == lineState)
  168. nextLevel = SC_FOLDLEVELBASE + 1;
  169. else
  170. nextLevel = SC_FOLDLEVELBASE;
  171. if (nextLevel > level)
  172. lvl |= SC_FOLDLEVELHEADERFLAG;
  173. if (visible == 0 && foldCompact)
  174. lvl |= SC_FOLDLEVELWHITEFLAG;
  175. styler.SetLevel(curLine, lvl);
  176. lineState = nextLineState;
  177. curLine = nextLine;
  178. level = nextLevel;
  179. visible = 0;
  180. }
  181. }
  182. }
  183. static const char *const poWordListDesc[] = {
  184. 0
  185. };
  186. LexerModule lmPO(SCLEX_PO, ColourisePODoc, "po", FoldPODoc, poWordListDesc);