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