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