1 // Scintilla source code edit control
3 ** Lexer for GetText Translation (PO) files.
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)
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)
24 #include "Scintilla.h"
28 #include "LexAccessor.h"
30 #include "StyleContext.h"
31 #include "CharacterSet.h"
32 #include "LexerModule.h"
35 using namespace Scintilla
;
38 static void ColourisePODoc(Sci_PositionU startPos
, Sci_Position length
, int initStyle
, WordList
*[], Accessor
&styler
) {
39 StyleContext
sc(startPos
, length
, initStyle
, styler
);
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
49 case SCE_PO_PROGRAMMER_COMMENT
:
50 case SCE_PO_REFERENCE
:
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
);
64 if (isspacechar(sc
.ch
))
65 sc
.SetState(SCE_PO_DEFAULT
);
70 sc
.SetState(SCE_PO_DEFAULT
);
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
);
88 else if (sc
.ch
== '\\')
90 else if (sc
.ch
== '"')
91 sc
.ForwardSetState(SCE_PO_DEFAULT
);
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
;
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
))
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
);
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
);
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
;
143 // Update the line state, so it can be seen by next line
144 curLine
= styler
.GetLine(sc
.currentPos
);
145 styler
.SetLineState(curLine
, curLineState
);
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
));
161 static void FoldPODoc(Sci_PositionU startPos
, Sci_Position length
, int, WordList
*[], Accessor
&styler
) {
162 if (! styler
.GetPropertyInt("fold"))
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
);
171 int level
= styler
.LevelAt(curLine
) & SC_FOLDLEVELNUMBERMASK
;
174 int chNext
= styler
[startPos
];
176 for (Sci_PositionU i
= startPos
; i
< endPos
; i
++) {
178 chNext
= styler
.SafeGetCharAt(i
+1);
180 if (! isspacechar(ch
)) {
182 } else if ((ch
== '\r' && chNext
!= '\n') || ch
== '\n' || i
+1 >= endPos
) {
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;
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
;
209 static const char *const poWordListDesc
[] = {
213 LexerModule
lmPO(SCLEX_PO
, ColourisePODoc
, "po", FoldPODoc
, poWordListDesc
);