]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/LexPB.cxx
775b5587257d750d34c214e05f6e1332b6b8f0d1
[wxWidgets.git] / src / stc / scintilla / src / LexPB.cxx
1 // Scintilla source code edit control
2 // @file LexPB.cxx
3 // Lexer for PowerBasic by Roland Walter, roland@rowalt.de (for PowerBasic see www.powerbasic.com)
4 //
5 // Changes:
6 // 17.10.2003 Toggling of subs/functions now until next sub/function - this gives better results
7 // 29.10.2003 1. Bug: Toggling didn't work for subs/functions added in editor
8 // 2. Own colors for PB constants and Inline Assembler SCE_B_CONSTANT and SCE_B_ASM
9 // 3. Several smaller syntax coloring improvements and speed optimizations
10 //
11 // Copyright for Scintilla: 1998-2001 by Neil Hodgson <neilh@scintilla.org>
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 <ctype.h>
17 #include <stdio.h>
18 #include <stdarg.h>
19
20 #include "Platform.h"
21
22 #include "PropSet.h"
23 #include "Accessor.h"
24 #include "StyleContext.h"
25 #include "KeyWords.h"
26 #include "Scintilla.h"
27 #include "SciLexer.h"
28
29 static inline bool IsTypeCharacter(const int ch)
30 {
31 return ch == '%' || ch == '&' || ch == '@' || ch == '!' || ch == '#' || ch == '$' || ch == '?';
32 }
33
34 static inline bool IsAWordChar(const int ch)
35 {
36 return (ch < 0x80) && (isalnum(ch) || ch == '.' || ch == '_');
37 }
38
39 static inline bool IsAWordStart(const int ch)
40 {
41 return (ch < 0x80) && (isalnum(ch) || ch == '_');
42 }
43
44 bool MatchUpperCase(Accessor &styler, int pos, const char *s) //Same as styler.Match() but uppercase comparison (a-z,A-Z and space only)
45 {
46 char ch;
47 for (int i=0; *s; i++)
48 {
49 ch=styler.SafeGetCharAt(pos+i);
50 if (ch > 0x60) ch -= '\x20';
51 if (*s != ch) return false;
52 s++;
53 }
54 return true;
55 }
56
57 static void ColourisePBDoc(unsigned int startPos, int length, int initStyle,WordList *keywordlists[],Accessor &styler) {
58
59 WordList &keywords = *keywordlists[0];
60
61 styler.StartAt(startPos);
62
63 StyleContext sc(startPos, length, initStyle, styler);
64
65 for (; sc.More(); sc.Forward()) {
66 switch (sc.state)
67 {
68 case SCE_B_OPERATOR:
69 {
70 sc.SetState(SCE_B_DEFAULT);
71 break;
72 }
73 case SCE_B_KEYWORD:
74 {
75 if (!IsAWordChar(sc.ch))
76 {
77 if (!IsTypeCharacter(sc.ch))
78 {
79 char s[100];
80 sc.GetCurrentLowered(s, sizeof(s));
81 if (keywords.InList(s))
82 {
83 if (strcmp(s, "rem") == 0)
84 {
85 sc.ChangeState(SCE_B_COMMENT);
86 if (sc.atLineEnd) {sc.SetState(SCE_B_DEFAULT);}
87 }
88 else if (strcmp(s, "asm") == 0)
89 {
90 sc.ChangeState(SCE_B_ASM);
91 if (sc.atLineEnd) {sc.SetState(SCE_B_DEFAULT);}
92 }
93 else
94 {
95 sc.SetState(SCE_B_DEFAULT);
96 }
97 }
98 else
99 {
100 sc.ChangeState(SCE_B_IDENTIFIER);
101 sc.SetState(SCE_B_DEFAULT);
102 }
103 }
104 }
105 break;
106 }
107 case SCE_B_NUMBER:
108 {
109 if (!IsAWordChar(sc.ch)) {sc.SetState(SCE_B_DEFAULT);}
110 break;
111 }
112 case SCE_B_STRING:
113 {
114 if (sc.ch == '\"'){sc.ForwardSetState(SCE_B_DEFAULT);}
115 break;
116 }
117 case SCE_B_CONSTANT:
118 {
119 if (!IsAWordChar(sc.ch)) {sc.SetState(SCE_B_DEFAULT);}
120 break;
121 }
122 case SCE_B_COMMENT:
123 {
124 if (sc.atLineEnd) {sc.SetState(SCE_B_DEFAULT);}
125 break;
126 }
127 case SCE_B_ASM:
128 {
129 if (sc.atLineEnd) {sc.SetState(SCE_B_DEFAULT);}
130 break;
131 }
132 } //switch (sc.state)
133
134 // Determine if a new state should be entered:
135 if (sc.state == SCE_B_DEFAULT)
136 {
137 if (sc.ch == '\'') {sc.SetState(SCE_B_COMMENT);}
138 else if (sc.ch == '\"') {sc.SetState(SCE_B_STRING);}
139 else if (sc.ch == '&' && tolower(sc.chNext) == 'h') {sc.SetState(SCE_B_NUMBER);}
140 else if (sc.ch == '&' && tolower(sc.chNext) == 'b') {sc.SetState(SCE_B_NUMBER);}
141 else if (sc.ch == '&' && tolower(sc.chNext) == 'o') {sc.SetState(SCE_B_NUMBER);}
142 else if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {sc.SetState(SCE_B_NUMBER);}
143 else if (IsAWordStart(sc.ch)) {sc.SetState(SCE_B_KEYWORD);}
144 else if (sc.ch == '%') {sc.SetState(SCE_B_CONSTANT);}
145 else if (sc.ch == '$') {sc.SetState(SCE_B_CONSTANT);}
146 else if (sc.ch == '#') {sc.SetState(SCE_B_KEYWORD);}
147 else if (sc.ch == '!') {sc.SetState(SCE_B_ASM);}
148 else if (isoperator(static_cast<char>(sc.ch)) || (sc.ch == '\\')) {sc.SetState(SCE_B_OPERATOR);}
149 }
150 } //for (; sc.More(); sc.Forward())
151 sc.Complete();
152 }
153
154 //The folding routine for PowerBasic toggles SUBs and FUNCTIONs only. This was exactly what I wanted,
155 //nothing more. I had worked with this kind of toggling for several years when I used the great good old
156 //GFA Basic which is dead now. After testing the feature of toggling FOR-NEXT loops, WHILE-WEND loops
157 //and so on too I found this is more disturbing then helping (for me). So if You think in another way
158 //you can (or must) write Your own toggling routine ;-)
159 static void FoldPBDoc(unsigned int startPos, int length, int, WordList *[], Accessor &styler)
160 {
161 // No folding enabled, no reason to continue...
162 if( styler.GetPropertyInt("fold") == 0 )
163 return;
164
165 unsigned int endPos = startPos + length;
166 int lineCurrent = styler.GetLine(startPos);
167 int levelCurrent = SC_FOLDLEVELBASE;
168 if (lineCurrent > 0)
169 levelCurrent = styler.LevelAt(lineCurrent-1) >> 16;
170 int levelNext = levelCurrent;
171 char chNext = styler[startPos];
172
173 bool fNewLine=true;
174 for (unsigned int i = startPos; i < endPos; i++)
175 {
176 char ch = chNext;
177 chNext = styler.SafeGetCharAt(i + 1);
178
179 if (fNewLine) //Begin of a new line (The Sub/Function/Macro keywords may occur at begin of line only)
180 {
181 fNewLine=false;
182
183 switch (ch)
184 {
185 case ' ': //Most lines start with space - so check this first
186 {
187 int levelUse = levelCurrent;
188 int lev = levelUse | levelNext << 16;
189 styler.SetLevel(lineCurrent, lev);
190 break;
191 }
192 case 'F':
193 case 'S':
194 case 'C':
195 case 'f':
196 case 's':
197 case 'c':
198 {
199 if( MatchUpperCase(styler,i,"FUNCTION") )
200 {
201 styler.SetLevel(lineCurrent, (SC_FOLDLEVELBASE << 16) | SC_FOLDLEVELHEADERFLAG);
202 levelNext=SC_FOLDLEVELBASE+1;
203 }
204 else if( MatchUpperCase(styler,i,"SUB") )
205 {
206 styler.SetLevel(lineCurrent, (SC_FOLDLEVELBASE << 16) | SC_FOLDLEVELHEADERFLAG);
207 levelNext=SC_FOLDLEVELBASE+1;
208 }
209 else if( MatchUpperCase(styler,i,"CALLBACK FUNCTION") )
210 {
211 styler.SetLevel(lineCurrent, (SC_FOLDLEVELBASE << 16) | SC_FOLDLEVELHEADERFLAG);
212 levelNext=SC_FOLDLEVELBASE+1;
213 }
214 else if( MatchUpperCase(styler,i,"STATIC FUNCTION") )
215 {
216 styler.SetLevel(lineCurrent, (SC_FOLDLEVELBASE << 16) | SC_FOLDLEVELHEADERFLAG);
217 levelNext=SC_FOLDLEVELBASE+1;
218 }
219 else if( MatchUpperCase(styler,i,"STATIC SUB") )
220 {
221 styler.SetLevel(lineCurrent, (SC_FOLDLEVELBASE << 16) | SC_FOLDLEVELHEADERFLAG);
222 levelNext=SC_FOLDLEVELBASE+1;
223 }
224 break;
225 }
226 default:
227 {
228 int levelUse = levelCurrent;
229 int lev = levelUse | levelNext << 16;
230 styler.SetLevel(lineCurrent, lev);
231 break;
232 }
233 } //switch (ch)
234 } //if( fNewLine )
235
236 switch (ch)
237 {
238 case '\n':
239 {
240 lineCurrent++;
241 levelCurrent = levelNext;
242 fNewLine=true;
243 break;
244 }
245 case '\r':
246 {
247 if (chNext != '\n')
248 {
249 lineCurrent++;
250 levelCurrent = levelNext;
251 fNewLine=true;
252 }
253 break;
254 }
255 } //switch (ch)
256 } //for (unsigned int i = startPos; i < endPos; i++)
257 }
258
259 static const char * const pbWordListDesc[] = {
260 "Keywords",
261 0
262 };
263
264 LexerModule lmPB(SCLEX_POWERBASIC, ColourisePBDoc, "powerbasic", FoldPBDoc, pbWordListDesc);