]>
git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/lexers/LexRebol.cxx
1 // Scintilla source code edit control
4 ** Written by Pascal Hurni, inspired from LexLua by Paul Winwood & Marcos E. Wurzius & Philippe Lhoste
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
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.
22 #include "Scintilla.h"
26 #include "LexAccessor.h"
28 #include "StyleContext.h"
29 #include "CharacterSet.h"
30 #include "LexerModule.h"
33 using namespace Scintilla
;
36 static inline bool IsAWordChar(const int ch
) {
37 return (isalnum(ch
) || ch
== '?' || ch
== '!' || ch
== '.' || ch
== '\'' || ch
== '+' || ch
== '-' || ch
== '*' || ch
== '&' || ch
== '|' || ch
== '=' || ch
== '_' || ch
== '~');
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
== '~');
45 static inline bool IsAnOperator(const int ch
, const int ch2
, const int ch3
) {
47 if (IsASpaceOrTab(ch2
)) {
48 return ch
== '+' || ch
== '-' || ch
== '*' || ch
== '/' || ch
== '<' || ch
== '>' || ch
== '=' || ch
== '?';
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
== '?');
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
== '{' );
71 static void ColouriseRebolDoc(unsigned int startPos
, int length
, int initStyle
, WordList
*keywordlists
[], Accessor
&styler
) {
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];
82 int currentLine
= styler
.GetLine(startPos
);
83 // Initialize the braced string {.. { ... } ..} nesting level, if we are inside such a string.
85 if (initStyle
== SCE_REBOL_BRACEDSTRING
|| initStyle
== SCE_REBOL_COMMENTBLOCK
) {
86 stringLevel
= styler
.GetLineState(currentLine
- 1);
89 bool blockComment
= initStyle
== SCE_REBOL_COMMENTBLOCK
;
92 // Do not leak onto next line
93 if (initStyle
== SCE_REBOL_COMMENTLINE
) {
94 initStyle
= SCE_REBOL_DEFAULT
;
97 StyleContext
sc(startPos
, length
, initStyle
, styler
);
99 sc
.SetState(SCE_REBOL_PREFACE
);
101 for (; sc
.More(); sc
.Forward()) {
103 //--- What to do at line end ?
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
);
110 // Update the line state, so it can be seen by next line
111 currentLine
= styler
.GetLine(sc
.currentPos
);
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
);
119 // Reset the line state
120 styler
.SetLineState(currentLine
, 0);
124 // continue with next char
128 //--- What to do on white-space ?
129 if (IsASpaceOrTab(sc
.ch
))
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
);
142 //--- Specialize state ?
143 // URL, Email look like identifier
144 if (sc
.state
== SCE_REBOL_IDENTIFIER
)
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
);
154 // Words look like identifiers
155 if (sc
.state
== SCE_REBOL_IDENTIFIER
|| (sc
.state
>= SCE_REBOL_WORD
&& sc
.state
<= SCE_REBOL_WORD8
)) {
157 if (!IsAWordChar(sc
.ch
) || sc
.Match('/')) {
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
);
178 // Keep same style if there are refinements
179 if (!sc
.Match('/')) {
180 sc
.SetState(SCE_REBOL_DEFAULT
);
184 } else if (sc
.state
== SCE_REBOL_NUMBER
) {
186 case 'x': sc
.ChangeState(SCE_REBOL_PAIR
);
188 case ':': sc
.ChangeState(SCE_REBOL_TIME
);
191 case '/': sc
.ChangeState(SCE_REBOL_DATE
);
193 case '.': if (++dotCount
>= 2) sc
.ChangeState(SCE_REBOL_TUPLE
);
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
== '\"') {
202 } else if (sc
.ch
== '\"') {
203 sc
.ForwardSetState(SCE_REBOL_DEFAULT
);
205 } else if (sc
.state
== SCE_REBOL_BRACEDSTRING
|| sc
.state
== SCE_REBOL_COMMENTBLOCK
) {
207 if (--stringLevel
== 0) {
208 sc
.ForwardSetState(SCE_REBOL_DEFAULT
);
210 } else if (sc
.ch
== '{') {
213 } else if (sc
.state
== SCE_REBOL_BINARY
) {
215 sc
.ForwardSetState(SCE_REBOL_DEFAULT
);
217 } else if (sc
.state
== SCE_REBOL_TAG
) {
219 sc
.ForwardSetState(SCE_REBOL_DEFAULT
);
221 } else if (sc
.state
== SCE_REBOL_PREFACE
) {
222 if (sc
.MatchIgnoreCase("rebol"))
225 for (i
=5; IsASpaceOrTab(styler
.SafeGetCharAt(sc
.currentPos
+i
, 0)); i
++);
226 if (sc
.GetRelative(i
) == '[')
227 sc
.SetState(SCE_REBOL_DEFAULT
);
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
);
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
== ',') {
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
);
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
);
267 } else if (sc
.ch
== '#' && sc
.chNext
!= '"' && sc
.chNext
!= '{' ) {
268 sc
.SetState(SCE_REBOL_ISSUE
);
276 static void FoldRebolDoc(unsigned int startPos
, int length
, int /* initStyle */, WordList
*[],
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
++) {
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
) {
294 } else if (ch
== ']') {
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
);
308 levelPrev
= levelCurrent
;
311 if (!isspacechar(ch
))
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
);
319 static const char * const rebolWordListDesc
[] = {
324 LexerModule
lmREBOL(SCLEX_REBOL
, ColouriseRebolDoc
, "rebol", FoldRebolDoc
, rebolWordListDesc
);