]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/lexers/LexRebol.cxx
Select the entire spin control contents when changing its value.
[wxWidgets.git] / src / stc / scintilla / lexers / LexRebol.cxx
1 // Scintilla source code edit control
2 /** @file LexRebol.cxx
3 ** Lexer for REBOL.
4 ** Written by Pascal Hurni, inspired from LexLua by Paul Winwood & Marcos E. Wurzius & Philippe Lhoste
5 **
6 ** History:
7 ** 2005-04-07 First release.
8 ** 2005-04-10 Closing parens and brackets go now in default style
9 ** String and comment nesting should be more safe
10 **/
11 // Copyright 2005 by Pascal Hurni <pascal_hurni@fastmail.fm>
12 // The License.txt file describes the conditions under which this software may be distributed.
13
14 #include <stdlib.h>
15 #include <string.h>
16 #include <stdio.h>
17 #include <stdarg.h>
18 #include <assert.h>
19 #include <ctype.h>
20
21 #include "ILexer.h"
22 #include "Scintilla.h"
23 #include "SciLexer.h"
24
25 #include "WordList.h"
26 #include "LexAccessor.h"
27 #include "Accessor.h"
28 #include "StyleContext.h"
29 #include "CharacterSet.h"
30 #include "LexerModule.h"
31
32 #ifdef SCI_NAMESPACE
33 using namespace Scintilla;
34 #endif
35
36 static inline bool IsAWordChar(const int ch) {
37 return (isalnum(ch) || ch == '?' || ch == '!' || ch == '.' || ch == '\'' || ch == '+' || ch == '-' || ch == '*' || ch == '&' || ch == '|' || ch == '=' || ch == '_' || ch == '~');
38 }
39
40 static inline bool IsAWordStart(const int ch, const int ch2) {
41 return ((ch == '+' || ch == '-' || ch == '.') && !isdigit(ch2)) ||
42 (isalpha(ch) || ch == '?' || ch == '!' || ch == '\'' || ch == '*' || ch == '&' || ch == '|' || ch == '=' || ch == '_' || ch == '~');
43 }
44
45 static inline bool IsAnOperator(const int ch, const int ch2, const int ch3) {
46 // One char operators
47 if (IsASpaceOrTab(ch2)) {
48 return ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '<' || ch == '>' || ch == '=' || ch == '?';
49 }
50
51 // Two char operators
52 if (IsASpaceOrTab(ch3)) {
53 return (ch == '*' && ch2 == '*') ||
54 (ch == '/' && ch2 == '/') ||
55 (ch == '<' && (ch2 == '=' || ch2 == '>')) ||
56 (ch == '>' && ch2 == '=') ||
57 (ch == '=' && (ch2 == '=' || ch2 == '?')) ||
58 (ch == '?' && ch2 == '?');
59 }
60
61 return false;
62 }
63
64 static inline bool IsBinaryStart(const int ch, const int ch2, const int ch3, const int ch4) {
65 return (ch == '#' && ch2 == '{') ||
66 (IsADigit(ch) && ch2 == '#' && ch3 == '{' ) ||
67 (IsADigit(ch) && IsADigit(ch2) && ch3 == '#' && ch4 == '{' );
68 }
69
70
71 static void ColouriseRebolDoc(unsigned int startPos, int length, int initStyle, WordList *keywordlists[], Accessor &styler) {
72
73 WordList &keywords = *keywordlists[0];
74 WordList &keywords2 = *keywordlists[1];
75 WordList &keywords3 = *keywordlists[2];
76 WordList &keywords4 = *keywordlists[3];
77 WordList &keywords5 = *keywordlists[4];
78 WordList &keywords6 = *keywordlists[5];
79 WordList &keywords7 = *keywordlists[6];
80 WordList &keywords8 = *keywordlists[7];
81
82 int currentLine = styler.GetLine(startPos);
83 // Initialize the braced string {.. { ... } ..} nesting level, if we are inside such a string.
84 int stringLevel = 0;
85 if (initStyle == SCE_REBOL_BRACEDSTRING || initStyle == SCE_REBOL_COMMENTBLOCK) {
86 stringLevel = styler.GetLineState(currentLine - 1);
87 }
88
89 bool blockComment = initStyle == SCE_REBOL_COMMENTBLOCK;
90 int dotCount = 0;
91
92 // Do not leak onto next line
93 if (initStyle == SCE_REBOL_COMMENTLINE) {
94 initStyle = SCE_REBOL_DEFAULT;
95 }
96
97 StyleContext sc(startPos, length, initStyle, styler);
98 if (startPos == 0) {
99 sc.SetState(SCE_REBOL_PREFACE);
100 }
101 for (; sc.More(); sc.Forward()) {
102
103 //--- What to do at line end ?
104 if (sc.atLineEnd) {
105 // Can be either inside a {} string or simply at eol
106 if (sc.state != SCE_REBOL_BRACEDSTRING && sc.state != SCE_REBOL_COMMENTBLOCK &&
107 sc.state != SCE_REBOL_BINARY && sc.state != SCE_REBOL_PREFACE)
108 sc.SetState(SCE_REBOL_DEFAULT);
109
110 // Update the line state, so it can be seen by next line
111 currentLine = styler.GetLine(sc.currentPos);
112 switch (sc.state) {
113 case SCE_REBOL_BRACEDSTRING:
114 case SCE_REBOL_COMMENTBLOCK:
115 // Inside a braced string, we set the line state
116 styler.SetLineState(currentLine, stringLevel);
117 break;
118 default:
119 // Reset the line state
120 styler.SetLineState(currentLine, 0);
121 break;
122 }
123
124 // continue with next char
125 continue;
126 }
127
128 //--- What to do on white-space ?
129 if (IsASpaceOrTab(sc.ch))
130 {
131 // Return to default if any of these states
132 if (sc.state == SCE_REBOL_OPERATOR || sc.state == SCE_REBOL_CHARACTER ||
133 sc.state == SCE_REBOL_NUMBER || sc.state == SCE_REBOL_PAIR ||
134 sc.state == SCE_REBOL_TUPLE || sc.state == SCE_REBOL_FILE ||
135 sc.state == SCE_REBOL_DATE || sc.state == SCE_REBOL_TIME ||
136 sc.state == SCE_REBOL_MONEY || sc.state == SCE_REBOL_ISSUE ||
137 sc.state == SCE_REBOL_URL || sc.state == SCE_REBOL_EMAIL) {
138 sc.SetState(SCE_REBOL_DEFAULT);
139 }
140 }
141
142 //--- Specialize state ?
143 // URL, Email look like identifier
144 if (sc.state == SCE_REBOL_IDENTIFIER)
145 {
146 if (sc.ch == ':' && !IsASpace(sc.chNext)) {
147 sc.ChangeState(SCE_REBOL_URL);
148 } else if (sc.ch == '@') {
149 sc.ChangeState(SCE_REBOL_EMAIL);
150 } else if (sc.ch == '$') {
151 sc.ChangeState(SCE_REBOL_MONEY);
152 }
153 }
154 // Words look like identifiers
155 if (sc.state == SCE_REBOL_IDENTIFIER || (sc.state >= SCE_REBOL_WORD && sc.state <= SCE_REBOL_WORD8)) {
156 // Keywords ?
157 if (!IsAWordChar(sc.ch) || sc.Match('/')) {
158 char s[100];
159 sc.GetCurrentLowered(s, sizeof(s));
160 blockComment = strcmp(s, "comment") == 0;
161 if (keywords8.InList(s)) {
162 sc.ChangeState(SCE_REBOL_WORD8);
163 } else if (keywords7.InList(s)) {
164 sc.ChangeState(SCE_REBOL_WORD7);
165 } else if (keywords6.InList(s)) {
166 sc.ChangeState(SCE_REBOL_WORD6);
167 } else if (keywords5.InList(s)) {
168 sc.ChangeState(SCE_REBOL_WORD5);
169 } else if (keywords4.InList(s)) {
170 sc.ChangeState(SCE_REBOL_WORD4);
171 } else if (keywords3.InList(s)) {
172 sc.ChangeState(SCE_REBOL_WORD3);
173 } else if (keywords2.InList(s)) {
174 sc.ChangeState(SCE_REBOL_WORD2);
175 } else if (keywords.InList(s)) {
176 sc.ChangeState(SCE_REBOL_WORD);
177 }
178 // Keep same style if there are refinements
179 if (!sc.Match('/')) {
180 sc.SetState(SCE_REBOL_DEFAULT);
181 }
182 }
183 // special numbers
184 } else if (sc.state == SCE_REBOL_NUMBER) {
185 switch (sc.ch) {
186 case 'x': sc.ChangeState(SCE_REBOL_PAIR);
187 break;
188 case ':': sc.ChangeState(SCE_REBOL_TIME);
189 break;
190 case '-':
191 case '/': sc.ChangeState(SCE_REBOL_DATE);
192 break;
193 case '.': if (++dotCount >= 2) sc.ChangeState(SCE_REBOL_TUPLE);
194 break;
195 }
196 }
197
198 //--- Determine if the current state should terminate
199 if (sc.state == SCE_REBOL_QUOTEDSTRING || sc.state == SCE_REBOL_CHARACTER) {
200 if (sc.ch == '^' && sc.chNext == '\"') {
201 sc.Forward();
202 } else if (sc.ch == '\"') {
203 sc.ForwardSetState(SCE_REBOL_DEFAULT);
204 }
205 } else if (sc.state == SCE_REBOL_BRACEDSTRING || sc.state == SCE_REBOL_COMMENTBLOCK) {
206 if (sc.ch == '}') {
207 if (--stringLevel == 0) {
208 sc.ForwardSetState(SCE_REBOL_DEFAULT);
209 }
210 } else if (sc.ch == '{') {
211 stringLevel++;
212 }
213 } else if (sc.state == SCE_REBOL_BINARY) {
214 if (sc.ch == '}') {
215 sc.ForwardSetState(SCE_REBOL_DEFAULT);
216 }
217 } else if (sc.state == SCE_REBOL_TAG) {
218 if (sc.ch == '>') {
219 sc.ForwardSetState(SCE_REBOL_DEFAULT);
220 }
221 } else if (sc.state == SCE_REBOL_PREFACE) {
222 if (sc.MatchIgnoreCase("rebol"))
223 {
224 int i;
225 for (i=5; IsASpaceOrTab(styler.SafeGetCharAt(sc.currentPos+i, 0)); i++);
226 if (sc.GetRelative(i) == '[')
227 sc.SetState(SCE_REBOL_DEFAULT);
228 }
229 }
230
231 //--- Parens and bracket changes to default style when the current is a number
232 if (sc.state == SCE_REBOL_NUMBER || sc.state == SCE_REBOL_PAIR || sc.state == SCE_REBOL_TUPLE ||
233 sc.state == SCE_REBOL_MONEY || sc.state == SCE_REBOL_ISSUE || sc.state == SCE_REBOL_EMAIL ||
234 sc.state == SCE_REBOL_URL || sc.state == SCE_REBOL_DATE || sc.state == SCE_REBOL_TIME) {
235 if (sc.ch == '(' || sc.ch == '[' || sc.ch == ')' || sc.ch == ']') {
236 sc.SetState(SCE_REBOL_DEFAULT);
237 }
238 }
239
240 //--- Determine if a new state should be entered.
241 if (sc.state == SCE_REBOL_DEFAULT) {
242 if (IsAnOperator(sc.ch, sc.chNext, sc.GetRelative(2))) {
243 sc.SetState(SCE_REBOL_OPERATOR);
244 } else if (IsBinaryStart(sc.ch, sc.chNext, sc.GetRelative(2), sc.GetRelative(3))) {
245 sc.SetState(SCE_REBOL_BINARY);
246 } else if (IsAWordStart(sc.ch, sc.chNext)) {
247 sc.SetState(SCE_REBOL_IDENTIFIER);
248 } else if (IsADigit(sc.ch) || sc.ch == '+' || sc.ch == '-' || /*Decimal*/ sc.ch == '.' || sc.ch == ',') {
249 dotCount = 0;
250 sc.SetState(SCE_REBOL_NUMBER);
251 } else if (sc.ch == '\"') {
252 sc.SetState(SCE_REBOL_QUOTEDSTRING);
253 } else if (sc.ch == '{') {
254 sc.SetState(blockComment ? SCE_REBOL_COMMENTBLOCK : SCE_REBOL_BRACEDSTRING);
255 ++stringLevel;
256 } else if (sc.ch == ';') {
257 sc.SetState(SCE_REBOL_COMMENTLINE);
258 } else if (sc.ch == '$') {
259 sc.SetState(SCE_REBOL_MONEY);
260 } else if (sc.ch == '%') {
261 sc.SetState(SCE_REBOL_FILE);
262 } else if (sc.ch == '<') {
263 sc.SetState(SCE_REBOL_TAG);
264 } else if (sc.ch == '#' && sc.chNext == '"') {
265 sc.SetState(SCE_REBOL_CHARACTER);
266 sc.Forward();
267 } else if (sc.ch == '#' && sc.chNext != '"' && sc.chNext != '{' ) {
268 sc.SetState(SCE_REBOL_ISSUE);
269 }
270 }
271 }
272 sc.Complete();
273 }
274
275
276 static void FoldRebolDoc(unsigned int startPos, int length, int /* initStyle */, WordList *[],
277 Accessor &styler) {
278 unsigned int lengthDoc = startPos + length;
279 int visibleChars = 0;
280 int lineCurrent = styler.GetLine(startPos);
281 int levelPrev = styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK;
282 int levelCurrent = levelPrev;
283 char chNext = styler[startPos];
284 int styleNext = styler.StyleAt(startPos);
285 for (unsigned int i = startPos; i < lengthDoc; i++) {
286 char ch = chNext;
287 chNext = styler.SafeGetCharAt(i + 1);
288 int style = styleNext;
289 styleNext = styler.StyleAt(i + 1);
290 bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
291 if (style == SCE_REBOL_DEFAULT) {
292 if (ch == '[') {
293 levelCurrent++;
294 } else if (ch == ']') {
295 levelCurrent--;
296 }
297 }
298 if (atEOL) {
299 int lev = levelPrev;
300 if (visibleChars == 0)
301 lev |= SC_FOLDLEVELWHITEFLAG;
302 if ((levelCurrent > levelPrev) && (visibleChars > 0))
303 lev |= SC_FOLDLEVELHEADERFLAG;
304 if (lev != styler.LevelAt(lineCurrent)) {
305 styler.SetLevel(lineCurrent, lev);
306 }
307 lineCurrent++;
308 levelPrev = levelCurrent;
309 visibleChars = 0;
310 }
311 if (!isspacechar(ch))
312 visibleChars++;
313 }
314 // Fill in the real level of the next line, keeping the current flags as they will be filled in later
315 int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK;
316 styler.SetLevel(lineCurrent, levelPrev | flagsNext);
317 }
318
319 static const char * const rebolWordListDesc[] = {
320 "Keywords",
321 0
322 };
323
324 LexerModule lmREBOL(SCLEX_REBOL, ColouriseRebolDoc, "rebol", FoldRebolDoc, rebolWordListDesc);
325