aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/LexYAML.cxx
blob: 156ca279d56693f62bf6789d9911c005ebbb9921 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Scintilla source code edit control
/** @file LexYAML.cxx
 ** Lexer for YAML.
 **/
// Copyright 2003- by Sean O'Dell <sean@celsoft.com>
// Release under the same license as Scintilla/SciTE.

#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>

#include "Platform.h"

#include "PropSet.h"
#include "Accessor.h"
#include "KeyWords.h"
#include "Scintilla.h"
#include "SciLexer.h"

static const char * const yamlWordListDesc[] = {
	"Keywords",
	0
};

static inline bool AtEOL(Accessor &styler, unsigned int i) {
	return (styler[i] == '\n') ||
		((styler[i] == '\r') && (styler.SafeGetCharAt(i + 1) != '\n'));
}

static void ColouriseYAMLLine(
    char *lineBuffer,
    unsigned int lengthLine,
    unsigned int startLine,
    unsigned int endPos,
    WordList &keywords,
    Accessor &styler) {

	unsigned int i = 0;
	bool bInQuotes = false;
	unsigned int startValue, endValue, valueLen;
	char scalar[256];
	if (lineBuffer[0] == '#') {	// Comment
		styler.ColourTo(endPos, SCE_YAML_COMMENT);
		return;
	}
	if (strncmp(lineBuffer, "---", 3) == 0) {	// Document marker
		styler.ColourTo(endPos, SCE_YAML_DOCUMENT);
		return;
	}
	// Skip initial spaces
	while ((i < lengthLine) && isspacechar(lineBuffer[i])) {
		i++;
	}
	while (i < lengthLine) {
		if (lineBuffer[i] == '\'' || lineBuffer[i] == '\"') {
			bInQuotes = !bInQuotes;
		} else if (lineBuffer[i] == ':' && !bInQuotes) {
			styler.ColourTo(startLine + i, SCE_YAML_IDENTIFIER);
			// Non-folding scalar
			startValue = i + 1;
			while ((startValue < lengthLine) && isspacechar(lineBuffer[startValue])) {
				startValue++;
			}
			endValue = lengthLine - 1;
			while ((endValue >= startValue) && isspacechar(lineBuffer[endValue])) {
				endValue--;
			}
			valueLen = endValue - startValue + 1;
			if (endValue < startValue || valueLen > sizeof(scalar)) {
				break;
			}
			strncpy(scalar,  &lineBuffer[startValue], valueLen);
			scalar[valueLen] = '\0';
			if (scalar[0] == '&' || scalar[0] == '*') {
				styler.ColourTo(startLine + endValue, SCE_YAML_REFERENCE);
			}
			else if (keywords.InList(scalar)) { // Convertible value (true/false, etc.)
				styler.ColourTo(startLine + endValue, SCE_YAML_KEYWORD);
			} else {
				startValue = 0;
				while (startValue < valueLen) {
					if (!isdigit(scalar[startValue]) && scalar[startValue] != '-' && scalar[startValue] != '.' && scalar[startValue] != ',') {
						break;
					}
					startValue++;
				}
				if (startValue >= valueLen) {
					styler.ColourTo(startLine + endValue, SCE_YAML_NUMBER);
				}
			}
			break; // the rest of the line is coloured the default
		}
		i++;
	}
	styler.ColourTo(endPos, SCE_YAML_DEFAULT);
}

static void ColouriseYAMLDoc(unsigned int startPos, int length, int, WordList *keywordLists[], Accessor &styler) {
	char lineBuffer[1024];
	styler.StartAt(startPos);
	styler.StartSegment(startPos);
	unsigned int linePos = 0;
	unsigned int startLine = startPos;
	for (unsigned int i = startPos; i < startPos + length; i++) {
		lineBuffer[linePos++] = styler[i];
		if (AtEOL(styler, i) || (linePos >= sizeof(lineBuffer) - 1)) {
			// End of line (or of line buffer) met, colourise it
			lineBuffer[linePos] = '\0';
			ColouriseYAMLLine(lineBuffer, linePos, startLine, i, *keywordLists[0], styler);
			linePos = 0;
			startLine = i + 1;
		}
	}
	if (linePos > 0) {	// Last line does not have ending characters
		ColouriseYAMLLine(lineBuffer, linePos, startLine, startPos + length - 1, *keywordLists[0], styler);
	}
}

static int IdentifierLevelYAMLLine(
    char *lineBuffer,
    unsigned int lengthLine) {

	unsigned int i = 0;
	bool bInQuotes = false;
	int level;
	if (lineBuffer[0] == '#') {	// Comment
		return 0xFFFFFFFF;
	}
	if (strncmp(lineBuffer, "---", 3) == 0) {	// Document marker
		return 0;
	}
	// Count initial spaces and '-' character
	while ((i < lengthLine) && (isspacechar(lineBuffer[i]) || lineBuffer[i] == '-')) {
		i++;
	}
	level = i;
	while (i < lengthLine) {
		if (lineBuffer[i] == '\'' || lineBuffer[i] == '\"') {
			bInQuotes = !bInQuotes;
		} else if (lineBuffer[i] == ':' && !bInQuotes) {
			return level;
		}
		i++;
	}
	return -level; // Scalar
}

static void FoldYAMLDoc(unsigned int startPos, int length, int, WordList *[], Accessor &styler) {
	char lineBuffer[1024];
	styler.StartAt(startPos);
	styler.StartSegment(startPos);
	unsigned int linePos = 0;
	int currentLevel;
	int lastLevel = 0xFFFFFFFF;
	unsigned int lineCurrent = 0;
	for (unsigned int i = startPos; i < startPos + length; i++) {
		lineBuffer[linePos++] = styler[i];
		if (AtEOL(styler, i) || (linePos >= sizeof(lineBuffer) - 1)) {
			// End of line (or of line buffer) met, colourise it
			lineBuffer[linePos] = '\0';
			currentLevel = IdentifierLevelYAMLLine(lineBuffer, linePos);
	 		if (currentLevel != static_cast<int>(0xFFFFFFFF)) {
				if (abs(currentLevel) > abs(lastLevel) && lastLevel >= 0) { // indented higher than last, and last was an identifier line
					styler.SetLevel(lineCurrent - 1, SC_FOLDLEVELHEADERFLAG);
				}
				lastLevel = currentLevel;
			}
			linePos = 0;
			lineCurrent++;
		}
	}
	if (linePos > 0) {	// Last line does not have ending characters
		currentLevel = IdentifierLevelYAMLLine(lineBuffer, linePos);
		if (currentLevel != static_cast<int>(0xFFFFFFFF)) {
			if (abs(currentLevel) > abs(lastLevel) && lastLevel >= 0) {
				styler.SetLevel(lineCurrent - 1, SC_FOLDLEVELHEADERFLAG);
			}
		}
	}
}

LexerModule lmYAML(SCLEX_YAML, ColouriseYAMLDoc, "yaml", FoldYAMLDoc, yamlWordListDesc);