Debian Codegraph @

scintilla/lexilla/lexers/LexYAML.cxx

raw
1 // Scintilla source code edit control
2 /** @file LexYAML.cxx
3 ** Lexer for YAML.
4 **/
5 // Copyright 2003- by Sean O'Dell <[email protected]>
6 // The License.txt file describes the conditions under which this software may be distributed.
7
8 #include <cstdlib>
9 #include <cassert>
10 #include <cstring>
11 #include <cctype>
12 #include <cstdio>
13 #include <cstdarg>
14
15 #include <string>
16 #include <string_view>
17
18 #include "ILexer.h"
19 #include "Scintilla.h"
20 #include "SciLexer.h"
21
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
29 using namespace Lexilla;
30
31 namespace {
32
33 const char * const yamlWordListDesc[] = {
34 "Keywords",
35 nullptr
36 };
37
38 inline bool AtEOL(Accessor &styler, Sci_PositionU i) {
39 return (styler[i] == '\n') ||
40 ((styler[i] == '\r') && (styler.SafeGetCharAt(i + 1) != '\n'));
41 }
42
43 /**
44 * Check for space, tab, line feed, or carriage return.
45 * See YAML 1.2 spec sections 5.4. Line Break Characters and 5.5. White Space Characters.
46 */
47 constexpr bool IsWhiteSpaceOrEOL(char ch) noexcept {
48 return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r';
49 }
50
51 unsigned int SpaceCount(char* lineBuffer) noexcept {
52 if (lineBuffer == nullptr)
53 return 0;
54
55 char* headBuffer = lineBuffer;
56
57 while (*headBuffer == ' ')
58 headBuffer++;
59
60 return static_cast<unsigned int>(headBuffer - lineBuffer);
61 }
62
63 bool KeywordAtChar(const char* lineBuffer, char* startComment, const WordList &keywords) noexcept {
64 if (lineBuffer == nullptr || startComment <= lineBuffer)
65 return false;
66 char* endValue = startComment - 1;
67 while (endValue >= lineBuffer && *endValue == ' ')
68 endValue--;
69 Sci_PositionU len = static_cast<Sci_PositionU>(endValue - lineBuffer) + 1;
70 char s[100];
71 if (len > (sizeof(s) / sizeof(s[0]) - 1))
72 return false;
73 strncpy(s, lineBuffer, len);
74 s[len] = '\0';
75 return (keywords.InList(s));
76 }
77
78 #define YAML_STATE_BITSIZE 16
79 #define YAML_STATE_MASK (0xFFFF0000)
80 #define YAML_STATE_DOCUMENT (1 << YAML_STATE_BITSIZE)
81 #define YAML_STATE_VALUE (2 << YAML_STATE_BITSIZE)
82 #define YAML_STATE_COMMENT (3 << YAML_STATE_BITSIZE)
83 #define YAML_STATE_TEXT_PARENT (4 << YAML_STATE_BITSIZE)
84 #define YAML_STATE_TEXT (5 << YAML_STATE_BITSIZE)
85
86 void ColouriseYAMLLine(
87 char *lineBuffer,
88 Sci_PositionU currentLine,
89 Sci_PositionU lengthLine,
90 Sci_PositionU startLine,
91 Sci_PositionU endPos,
92 const WordList &keywords,
93 Accessor &styler) {
94
95 Sci_PositionU i = 0;
96 bool bInQuotes = false;
97 const unsigned int indentAmount = SpaceCount(lineBuffer);
98
99 if (currentLine > 0) {
100 const int parentLineState = styler.GetLineState(currentLine - 1);
101
102 if ((parentLineState&YAML_STATE_MASK) == YAML_STATE_TEXT || (parentLineState&YAML_STATE_MASK) == YAML_STATE_TEXT_PARENT) {
103 const unsigned int parentIndentAmount = parentLineState&(~YAML_STATE_MASK);
104 if (indentAmount > parentIndentAmount) {
105 styler.SetLineState(currentLine, YAML_STATE_TEXT | parentIndentAmount);
106 styler.ColourTo(endPos, SCE_YAML_TEXT);
107 return;
108 }
109 }
110 }
111 styler.SetLineState(currentLine, 0);
112 if (strncmp(lineBuffer, "---", 3) == 0 || strncmp(lineBuffer, "...", 3) == 0) { // Document marker
113 styler.SetLineState(currentLine, YAML_STATE_DOCUMENT);
114 styler.ColourTo(endPos, SCE_YAML_DOCUMENT);
115 return;
116 }
117 // Skip initial spaces
118 while ((i < lengthLine) && lineBuffer[i] == ' ') { // YAML always uses space, never TABS or anything else
119 i++;
120 }
121 if (lineBuffer[i] == '\t') { // if we skipped all spaces, and we are NOT inside a text block, this is wrong
122 styler.ColourTo(endPos, SCE_YAML_ERROR);
123 return;
124 }
125 if (lineBuffer[i] == '#') { // Comment
126 styler.SetLineState(currentLine, YAML_STATE_COMMENT);
127 styler.ColourTo(endPos, SCE_YAML_COMMENT);
128 return;
129 }
130 while (i < lengthLine) {
131 if (lineBuffer[i] == '\'' || lineBuffer[i] == '\"') {
132 bInQuotes = !bInQuotes;
133 } else if (lineBuffer[i] == '#' && isspacechar(lineBuffer[i - 1]) && !bInQuotes) {
134 styler.ColourTo(startLine + i - 1, SCE_YAML_DEFAULT);
135 styler.ColourTo(endPos, SCE_YAML_COMMENT);
136 return;
137 } else if (lineBuffer[i] == ':' && !bInQuotes && (IsWhiteSpaceOrEOL(lineBuffer[i + 1]) || i == lengthLine - 1)) {
138 styler.ColourTo(startLine + i - 1, SCE_YAML_IDENTIFIER);
139 styler.ColourTo(startLine + i, SCE_YAML_OPERATOR);
140 // Non-folding scalar
141 i++;
142 while ((i < lengthLine) && isspacechar(lineBuffer[i]))
143 i++;
144 Sci_PositionU endValue = lengthLine - 1;
145 while ((endValue >= i) && isspacechar(lineBuffer[endValue]))
146 endValue--;
147 lineBuffer[endValue + 1] = '\0';
148 if (lineBuffer[i] == '|' || lineBuffer[i] == '>') {
149 i++;
150 if (lineBuffer[i] == '+' || lineBuffer[i] == '-')
151 i++;
152 while ((i < lengthLine) && isspacechar(lineBuffer[i]))
153 i++;
154 if (lineBuffer[i] == '\0') {
155 styler.SetLineState(currentLine, YAML_STATE_TEXT_PARENT | indentAmount);
156 styler.ColourTo(endPos, SCE_YAML_DEFAULT);
157 return;
158 } else if (lineBuffer[i] == '#') {
159 styler.SetLineState(currentLine, YAML_STATE_TEXT_PARENT | indentAmount);
160 styler.ColourTo(startLine + i - 1, SCE_YAML_DEFAULT);
161 styler.ColourTo(endPos, SCE_YAML_COMMENT);
162 return;
163 } else {
164 styler.ColourTo(endPos, SCE_YAML_ERROR);
165 return;
166 }
167 } else if (lineBuffer[i] == '#') {
168 styler.ColourTo(startLine + i - 1, SCE_YAML_DEFAULT);
169 styler.ColourTo(endPos, SCE_YAML_COMMENT);
170 return;
171 }
172 Sci_PositionU startComment = i;
173 bInQuotes = false;
174 while (startComment < lengthLine) { // Comment must be space padded
175 if (lineBuffer[startComment] == '\'' || lineBuffer[startComment] == '\"')
176 bInQuotes = !bInQuotes;
177 if (lineBuffer[startComment] == '#' && isspacechar(lineBuffer[startComment - 1]) && !bInQuotes)
178 break;
179 startComment++;
180 }
181 styler.SetLineState(currentLine, YAML_STATE_VALUE);
182 if (lineBuffer[i] == '&' || lineBuffer[i] == '*') {
183 styler.ColourTo(startLine + startComment - 1, SCE_YAML_REFERENCE);
184 if (startComment < lengthLine)
185 styler.ColourTo(endPos, SCE_YAML_COMMENT);
186 return;
187 }
188 if (KeywordAtChar(&lineBuffer[i], &lineBuffer[startComment], keywords)) { // Convertible value (true/false, etc.)
189 styler.ColourTo(startLine + startComment - 1, SCE_YAML_KEYWORD);
190 if (startComment < lengthLine)
191 styler.ColourTo(endPos, SCE_YAML_COMMENT);
192 return;
193 }
194 const Sci_PositionU i2 = i;
195 while ((i < startComment) && lineBuffer[i]) {
196 if (!(IsASCII(lineBuffer[i]) && isdigit(lineBuffer[i])) && lineBuffer[i] != '-'
197 && lineBuffer[i] != '.' && lineBuffer[i] != ',' && lineBuffer[i] != ' ') {
198 styler.ColourTo(startLine + startComment - 1, SCE_YAML_DEFAULT);
199 if (startComment < lengthLine)
200 styler.ColourTo(endPos, SCE_YAML_COMMENT);
201 return;
202 }
203 i++;
204 }
205 if (i > i2) {
206 styler.ColourTo(startLine + startComment - 1, SCE_YAML_NUMBER);
207 if (startComment < lengthLine)
208 styler.ColourTo(endPos, SCE_YAML_COMMENT);
209 return;
210 }
211 break; // shouldn't get here, but just in case, the rest of the line is coloured the default
212 }
213 i++;
214 }
215 styler.ColourTo(endPos, SCE_YAML_DEFAULT);
216 }
217
218 void ColouriseYAMLDoc(Sci_PositionU startPos, Sci_Position length, int, WordList *keywordLists[], Accessor &styler) {
219 std::string lineBuffer;
220 styler.StartAt(startPos);
221 styler.StartSegment(startPos);
222 Sci_PositionU startLine = startPos;
223 const Sci_PositionU endPos = startPos + length;
224 const Sci_PositionU maxPos = styler.Length();
225 Sci_PositionU lineCurrent = styler.GetLine(startPos);
226
227 for (Sci_PositionU i = startPos; i < maxPos && i < endPos; i++) {
228 lineBuffer.push_back(styler[i]);
229 if (AtEOL(styler, i)) {
230 // End of line (or of line buffer) met, colourise it
231 ColouriseYAMLLine(lineBuffer.data(), lineCurrent, lineBuffer.length(), startLine, i, *keywordLists[0], styler);
232 lineBuffer.clear();
233 startLine = i + 1;
234 lineCurrent++;
235 }
236 }
237 if (!lineBuffer.empty()) { // Last line does not have ending characters
238 ColouriseYAMLLine(lineBuffer.data(), lineCurrent, lineBuffer.length(), startLine, startPos + length - 1, *keywordLists[0], styler);
239 }
240 }
241
242 bool IsCommentLine(Sci_Position line, Accessor &styler) {
243 const Sci_Position pos = styler.LineStart(line);
244 if (styler[pos] == '#')
245 return true;
246 return false;
247 }
248
249 void FoldYAMLDoc(Sci_PositionU startPos, Sci_Position length, int /*initStyle - unused*/,
250 WordList *[], Accessor &styler) {
251 const Sci_Position maxPos = startPos + length;
252 const Sci_Position maxLines = styler.GetLine(maxPos - 1); // Requested last line
253 const Sci_Position docLines = styler.GetLine(styler.Length() - 1); // Available last line
254
255 // property fold.comment.yaml
256 // Set to 1 to allow folding of comment blocks in YAML.
257 const bool foldComment = styler.GetPropertyInt("fold.comment.yaml") != 0;
258
259 // Backtrack to previous non-blank line so we can determine indent level
260 // for any white space lines
261 // and so we can fix any preceding fold level (which is why we go back
262 // at least one line in all cases)
263 int spaceFlags = 0;
264 Sci_Position lineCurrent = styler.GetLine(startPos);
265 int indentCurrent = styler.IndentAmount(lineCurrent, &spaceFlags, nullptr);
266 while (lineCurrent > 0) {
267 lineCurrent--;
268 indentCurrent = styler.IndentAmount(lineCurrent, &spaceFlags, nullptr);
269 if (!(indentCurrent & SC_FOLDLEVELWHITEFLAG) &&
270 (!IsCommentLine(lineCurrent, styler)))
271 break;
272 }
273 int indentCurrentLevel = indentCurrent & SC_FOLDLEVELNUMBERMASK;
274
275 // Set up initial loop state
276 int prevComment = 0;
277 if (lineCurrent >= 1)
278 prevComment = foldComment && IsCommentLine(lineCurrent - 1, styler);
279
280 // Process all characters to end of requested range
281 // or comment that hangs over the end of the range. Cap processing in all cases
282 // to end of document (in case of unclosed comment at end).
283 while ((lineCurrent <= docLines) && ((lineCurrent <= maxLines) || prevComment)) {
284
285 // Gather info
286 int lev = indentCurrent;
287 Sci_Position lineNext = lineCurrent + 1;
288 int indentNext = indentCurrent;
289 if (lineNext <= docLines) {
290 // Information about next line is only available if not at end of document
291 indentNext = styler.IndentAmount(lineNext, &spaceFlags, nullptr);
292 }
293 const int comment = foldComment && IsCommentLine(lineCurrent, styler);
294 const int comment_start = (comment && !prevComment && (lineNext <= docLines) &&
295 IsCommentLine(lineNext, styler) && (lev > SC_FOLDLEVELBASE));
296 const int comment_continue = (comment && prevComment);
297 if (!comment)
298 indentCurrentLevel = indentCurrent & SC_FOLDLEVELNUMBERMASK;
299 if (indentNext & SC_FOLDLEVELWHITEFLAG)
300 indentNext = SC_FOLDLEVELWHITEFLAG | indentCurrentLevel;
301
302 if (comment_start) {
303 // Place fold point at start of a block of comments
304 lev |= SC_FOLDLEVELHEADERFLAG;
305 } else if (comment_continue) {
306 // Add level to rest of lines in the block
307 lev = lev + 1;
308 }
309
310 // Skip past any blank lines for next indent level info; we skip also
311 // comments (all comments, not just those starting in column 0)
312 // which effectively folds them into surrounding code rather
313 // than screwing up folding.
314
315 while ((lineNext < docLines) &&
316 ((indentNext & SC_FOLDLEVELWHITEFLAG) ||
317 (lineNext <= docLines && IsCommentLine(lineNext, styler)))) {
318
319 lineNext++;
320 indentNext = styler.IndentAmount(lineNext, &spaceFlags, nullptr);
321 }
322
323 const int levelAfterComments = indentNext & SC_FOLDLEVELNUMBERMASK;
324 const int levelBeforeComments = Maximum(indentCurrentLevel,levelAfterComments);
325
326 // Now set all the indent levels on the lines we skipped
327 // Do this from end to start. Once we encounter one line
328 // which is indented more than the line after the end of
329 // the comment-block, use the level of the block before
330
331 Sci_Position skipLine = lineNext;
332 int skipLevel = levelAfterComments;
333
334 while (--skipLine > lineCurrent) {
335 const int skipLineIndent = styler.IndentAmount(skipLine, &spaceFlags, nullptr);
336
337 if ((skipLineIndent & SC_FOLDLEVELNUMBERMASK) > levelAfterComments)
338 skipLevel = levelBeforeComments;
339
340 const int whiteFlag = skipLineIndent & SC_FOLDLEVELWHITEFLAG;
341
342 styler.SetLevel(skipLine, skipLevel | whiteFlag);
343 }
344
345 // Set fold header on non-comment line
346 if (!comment && !(indentCurrent & SC_FOLDLEVELWHITEFLAG) ) {
347 if ((indentCurrent & SC_FOLDLEVELNUMBERMASK) < (indentNext & SC_FOLDLEVELNUMBERMASK))
348 lev |= SC_FOLDLEVELHEADERFLAG;
349 }
350
351 // Keep track of block comment state of previous line
352 prevComment = comment_start || comment_continue;
353
354 // Set fold level for this line and move to next line
355 styler.SetLevel(lineCurrent, lev);
356 indentCurrent = indentNext;
357 lineCurrent = lineNext;
358 }
359
360 // NOTE: Cannot set level of last line here because indentCurrent doesn't have
361 // header flag set; the loop above is crafted to take care of this case!
362 //styler.SetLevel(lineCurrent, indentCurrent);
363 }
364
365 }
366
367 extern const LexerModule lmYAML(SCLEX_YAML, ColouriseYAMLDoc, "yaml", FoldYAMLDoc, yamlWordListDesc);