LexPO.cpp 6.5 KB

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