]>
git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/LexForth.cxx
f097b0e00a1e2b9febac6cc8341e9b1a40d35537
1 // Scintilla source code edit control
2 /** @file LexCrontab.cxx
3 ** Lexer to use with extended crontab files used by a powerful
4 ** Windows scheduler/event monitor/automation manager nnCron.
5 ** (http://nemtsev.eserv.ru/)
7 // Copyright 1998-2001 by Neil Hodgson <neilh@scintilla.org>
8 // The License.txt file describes the conditions under which this software may be distributed.
21 #include "Scintilla.h"
25 using namespace Scintilla
;
28 bool is_whitespace(int ch
){
29 return ch
== '\n' || ch
== '\r' || ch
== '\t' || ch
== ' ';
32 bool is_blank(int ch
){
33 return ch
== '\t' || ch
== ' ';
38 #define log(x) fputs(f_debug,x);
47 static int cur_pos
,pos1
,pos2
,pos0
,lengthDoc
;
50 char getChar(bool is_bl
){
51 char ch
=st
->SafeGetCharAt(cur_pos
);
52 if(is_bl
) if(is_whitespace(ch
)) ch
=BL
;
57 char ch
=st
->SafeGetCharAt(cur_pos
);
61 return ch
=='\n' || ch
=='\r';
64 int parse(char ch
, bool skip_eol
){
65 // pos1 - start pos of word
66 // pos2 - pos after of word
71 pos0
=pos1
=pos2
=cur_pos
;
72 for(;cur_pos
<lengthDoc
&& (c
=getChar(is_bl
))==ch
; cur_pos
++){
73 if(is_eol(c
) && !skip_eol
){
80 if(cur_pos
==lengthDoc
) return 0;
81 for(len
=0;cur_pos
<lengthDoc
&& (c
=getChar(is_bl
))!=ch
; cur_pos
++){
82 if(is_eol(c
) && !skip_eol
) break;
89 fprintf(f_debug
,"parse: %c %s\n",ch
,buffer
);
94 bool _is_number(char *s
,int base
){
96 int digit
=((int)*s
)-(int)'0';
98 fprintf(f_debug
,"digit: %c %d\n",*s
,digit
);
100 if(digit
>9 && base
>10) digit
-=7;
101 if(digit
<0) return false;
102 if(digit
>=base
) return false;
107 bool is_number(char *s
){
108 if(strncmp(s
,"0x",2)==0) return _is_number(s
+2,16);
109 return _is_number(s
,10);
112 static void ColouriseForthDoc(unsigned int startPos
, int length
, int, WordList
*keywordLists
[], Accessor
&styler
)
116 lengthDoc
= startPos
+ length
;
117 buffer
= new char[length
];
120 f_debug
=fopen("c:\\sci.log","at");
123 WordList
&control
= *keywordLists
[0];
124 WordList
&keyword
= *keywordLists
[1];
125 WordList
&defword
= *keywordLists
[2];
126 WordList
&preword1
= *keywordLists
[3];
127 WordList
&preword2
= *keywordLists
[4];
128 WordList
&strings
= *keywordLists
[5];
130 // go through all provided text segment
131 // using the hand-written state machine shown below
132 styler
.StartAt(startPos
);
133 styler
.StartSegment(startPos
);
134 while(parse(BL
,true)!=0){
136 styler
.ColourTo(pos0
,SCE_FORTH_DEFAULT
);
137 styler
.ColourTo(pos1
-1,SCE_FORTH_DEFAULT
);
139 if(strcmp("\\",buffer
)==0){
140 styler
.ColourTo(pos1
,SCE_FORTH_COMMENT
);
142 styler
.ColourTo(pos2
,SCE_FORTH_COMMENT
);
143 }else if(strcmp("(",buffer
)==0){
144 styler
.ColourTo(pos1
,SCE_FORTH_COMMENT
);
146 if(cur_pos
<lengthDoc
) cur_pos
++;
147 styler
.ColourTo(cur_pos
,SCE_FORTH_COMMENT
);
148 }else if(strcmp("[",buffer
)==0){
149 styler
.ColourTo(pos1
,SCE_FORTH_STRING
);
151 if(cur_pos
<lengthDoc
) cur_pos
++;
152 styler
.ColourTo(cur_pos
,SCE_FORTH_STRING
);
153 }else if(strcmp("{",buffer
)==0){
154 styler
.ColourTo(pos1
,SCE_FORTH_LOCALE
);
156 if(cur_pos
<lengthDoc
) cur_pos
++;
157 styler
.ColourTo(cur_pos
,SCE_FORTH_LOCALE
);
158 }else if(strings
.InList(buffer
)) {
159 styler
.ColourTo(pos1
,SCE_FORTH_STRING
);
161 if(cur_pos
<lengthDoc
) cur_pos
++;
162 styler
.ColourTo(cur_pos
,SCE_FORTH_STRING
);
163 }else if(control
.InList(buffer
)) {
164 styler
.ColourTo(pos1
,SCE_FORTH_CONTROL
);
165 styler
.ColourTo(pos2
,SCE_FORTH_CONTROL
);
166 }else if(keyword
.InList(buffer
)) {
167 styler
.ColourTo(pos1
,SCE_FORTH_KEYWORD
);
168 styler
.ColourTo(pos2
,SCE_FORTH_KEYWORD
);
169 }else if(defword
.InList(buffer
)) {
170 styler
.ColourTo(pos1
,SCE_FORTH_KEYWORD
);
171 styler
.ColourTo(pos2
,SCE_FORTH_KEYWORD
);
173 styler
.ColourTo(pos1
-1,SCE_FORTH_DEFAULT
);
174 styler
.ColourTo(pos1
,SCE_FORTH_DEFWORD
);
175 styler
.ColourTo(pos2
,SCE_FORTH_DEFWORD
);
176 }else if(preword1
.InList(buffer
)) {
177 styler
.ColourTo(pos1
,SCE_FORTH_PREWORD1
);
179 styler
.ColourTo(pos2
,SCE_FORTH_PREWORD1
);
180 }else if(preword2
.InList(buffer
)) {
181 styler
.ColourTo(pos1
,SCE_FORTH_PREWORD2
);
183 styler
.ColourTo(pos2
,SCE_FORTH_PREWORD2
);
185 styler
.ColourTo(pos1
,SCE_FORTH_STRING
);
186 styler
.ColourTo(pos2
,SCE_FORTH_STRING
);
187 }else if(is_number(buffer
)){
188 styler
.ColourTo(pos1
,SCE_FORTH_NUMBER
);
189 styler
.ColourTo(pos2
,SCE_FORTH_NUMBER
);
198 if(control.InList(buffer)) {
199 styler.ColourTo(i,SCE_FORTH_CONTROL);
200 } else if(keyword.InList(buffer)) {
201 styler.ColourTo(i-1,SCE_FORTH_KEYWORD );
202 } else if(defword.InList(buffer)) {
203 styler.ColourTo(i-1,SCE_FORTH_DEFWORD );
204 // prev_state=SCE_FORTH_DEFWORD
205 } else if(preword1.InList(buffer)) {
206 styler.ColourTo(i-1,SCE_FORTH_PREWORD1 );
207 // state=SCE_FORTH_PREWORD1;
208 } else if(preword2.InList(buffer)) {
209 styler.ColourTo(i-1,SCE_FORTH_PREWORD2 );
211 styler.ColourTo(i-1,SCE_FORTH_DEFAULT);
216 for (int i = startPos; i < lengthDoc; i++) {
218 chNext = styler.SafeGetCharAt(i + 1);
219 if(i!=startPos) chPrev=styler.SafeGetCharAt(i - 1);
221 if (styler.IsLeadByte(ch)) {
222 chNext = styler.SafeGetCharAt(i + 2);
227 fprintf(f_debug,"%c %d ",ch,state);
230 case SCE_FORTH_DEFAULT:
231 if(is_whitespace(ch)) {
232 // whitespace is simply ignored here...
233 styler.ColourTo(i,SCE_FORTH_DEFAULT);
235 } else if( ch == '\\' && is_blank(chNext)) {
236 // signals the start of an one line comment...
237 state = SCE_FORTH_COMMENT;
238 styler.ColourTo(i,SCE_FORTH_COMMENT);
239 } else if( is_whitespace(chPrev) && ch == '(' && is_whitespace(chNext)) {
240 // signals the start of a plain comment...
241 state = SCE_FORTH_COMMENT_ML;
242 styler.ColourTo(i,SCE_FORTH_COMMENT_ML);
243 } else if( isdigit(ch) ) {
244 // signals the start of a number
246 buffer[bufferCount++] = ch;
247 state = SCE_FORTH_NUMBER;
248 } else if( !is_whitespace(ch)) {
249 // signals the start of an identifier
251 buffer[bufferCount++] = ch;
252 state = SCE_FORTH_IDENTIFIER;
254 // style it the default style..
255 styler.ColourTo(i,SCE_FORTH_DEFAULT);
259 case SCE_FORTH_COMMENT:
260 // if we find a newline here,
261 // we simply go to default state
262 // else continue to work on it...
263 if( ch == '\n' || ch == '\r' ) {
264 state = SCE_FORTH_DEFAULT;
266 styler.ColourTo(i,SCE_FORTH_COMMENT);
270 case SCE_FORTH_COMMENT_ML:
272 state = SCE_FORTH_DEFAULT;
274 styler.ColourTo(i+1,SCE_FORTH_COMMENT_ML);
278 case SCE_FORTH_IDENTIFIER:
279 // stay in CONF_IDENTIFIER state until we find a non-alphanumeric
280 if( !is_whitespace(ch) ) {
281 buffer[bufferCount++] = ch;
283 state = SCE_FORTH_DEFAULT;
284 buffer[bufferCount] = '\0';
286 fprintf(f_debug,"\nid %s\n",buffer);
289 // check if the buffer contains a keyword,
290 // and highlight it if it is a keyword...
291 // switch(prev_state)
292 // case SCE_FORTH_DEFAULT:
293 if(control.InList(buffer)) {
294 styler.ColourTo(i,SCE_FORTH_CONTROL);
295 } else if(keyword.InList(buffer)) {
296 styler.ColourTo(i-1,SCE_FORTH_KEYWORD );
297 } else if(defword.InList(buffer)) {
298 styler.ColourTo(i-1,SCE_FORTH_DEFWORD );
299 // prev_state=SCE_FORTH_DEFWORD
300 } else if(preword1.InList(buffer)) {
301 styler.ColourTo(i-1,SCE_FORTH_PREWORD1 );
302 // state=SCE_FORTH_PREWORD1;
303 } else if(preword2.InList(buffer)) {
304 styler.ColourTo(i-1,SCE_FORTH_PREWORD2 );
306 styler.ColourTo(i-1,SCE_FORTH_DEFAULT);
311 // push back the faulty character
312 chNext = styler[i--];
316 case SCE_FORTH_NUMBER:
317 // stay in CONF_NUMBER state until we find a non-numeric
319 buffer[bufferCount++] = ch;
321 state = SCE_FORTH_DEFAULT;
322 buffer[bufferCount] = '\0';
323 // Colourize here... (normal number)
324 styler.ColourTo(i-1,SCE_FORTH_NUMBER);
325 // push back a character
326 chNext = styler[i--];
338 static void FoldForthDoc(unsigned int, int, int, WordList
*[],
342 static const char * const forthWordLists
[] = {
346 "prewords with one argument",
347 "prewords with two arguments",
348 "string definition keywords",
352 LexerModule
lmForth(SCLEX_FORTH
, ColouriseForthDoc
, "forth",FoldForthDoc
,forthWordLists
);