]>
git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/LexOpal.cxx
d1d188998f056a593016a8ac7fbd658f067deea0
1 // Scintilla source code edit control
3 ** Lexer for OPAL (functional language similar to Haskell)
4 ** Written by Sebastian Pipping <webmaster@hartwork.org>
18 #include "Scintilla.h"
20 #include "StyleContext.h"
22 inline static void getRange( unsigned int start
, unsigned int end
, Accessor
& styler
, char * s
, unsigned int len
)
25 while( ( i
< end
- start
+ 1 ) && ( i
< len
- 1 ) )
27 s
[i
] = static_cast<char>( styler
[ start
+ i
] );
33 inline bool HandleString( unsigned int & cur
, unsigned int one_too_much
, Accessor
& styler
)
37 // Wait for string to close
38 bool even_backslash_count
= true; // Without gaps in between
39 cur
++; // Skip initial quote
42 if( cur
>= one_too_much
)
44 styler
.ColourTo( cur
- 1, SCE_OPAL_STRING
);
48 ch
= styler
.SafeGetCharAt( cur
);
49 if( ( ch
== '\015' ) || ( ch
== '\012' ) ) // Deny multi-line strings
51 styler
.ColourTo( cur
- 1, SCE_OPAL_STRING
);
52 styler
.StartSegment( cur
);
57 if( even_backslash_count
)
61 styler
.ColourTo( cur
, SCE_OPAL_STRING
);
63 if( cur
>= one_too_much
)
69 styler
.StartSegment( cur
);
75 even_backslash_count
= false;
80 even_backslash_count
= true;
88 inline bool HandleCommentBlock( unsigned int & cur
, unsigned int one_too_much
, Accessor
& styler
, bool could_fail
)
95 if( cur
>= one_too_much
)
97 styler
.ColourTo( cur
- 1, SCE_OPAL_DEFAULT
);
101 ch
= styler
.SafeGetCharAt( cur
);
104 styler
.ColourTo( cur
- 1, SCE_OPAL_DEFAULT
);
105 styler
.StartSegment( cur
);
110 // Wait for comment close
112 bool star_found
= false;
115 if( cur
>= one_too_much
)
117 styler
.ColourTo( cur
- 1, SCE_OPAL_COMMENT_BLOCK
);
118 return false; // STOP
121 ch
= styler
.SafeGetCharAt( cur
);
126 styler
.ColourTo( cur
, SCE_OPAL_COMMENT_BLOCK
);
128 if( cur
>= one_too_much
)
130 return false; // STOP
134 styler
.StartSegment( cur
);
151 inline bool HandleCommentLine( unsigned int & cur
, unsigned int one_too_much
, Accessor
& styler
, bool could_fail
)
158 if( cur
>= one_too_much
)
160 styler
.ColourTo( cur
- 1, SCE_OPAL_DEFAULT
);
161 return false; // STOP
164 ch
= styler
.SafeGetCharAt( cur
);
167 styler
.ColourTo( cur
- 1, SCE_OPAL_DEFAULT
);
168 styler
.StartSegment( cur
);
173 if( cur
>= one_too_much
)
175 styler
.ColourTo( cur
- 1, SCE_OPAL_DEFAULT
);
176 return false; // STOP
179 ch
= styler
.SafeGetCharAt( cur
);
180 if( ( ch
!= ' ' ) && ( ch
!= '\t' ) )
182 styler
.ColourTo( cur
- 1, SCE_OPAL_DEFAULT
);
183 styler
.StartSegment( cur
);
188 // Wait for end of line
189 bool fifteen_found
= false;
195 if( cur
>= one_too_much
)
197 styler
.ColourTo( cur
- 1, SCE_OPAL_COMMENT_LINE
);
198 return false; // STOP
201 ch
= styler
.SafeGetCharAt( cur
);
207 // One newline on Windows (015, 012)
211 // One newline on MAC (015) and another char
215 styler
.ColourTo( cur
- 1, SCE_OPAL_COMMENT_LINE
);
216 styler
.StartSegment( cur
);
223 fifteen_found
= true;
225 else if( ch
== '\012' )
227 // One newline on Linux (012)
228 styler
.ColourTo( cur
- 1, SCE_OPAL_COMMENT_LINE
);
229 styler
.StartSegment( cur
);
236 inline bool HandlePar( unsigned int & cur
, Accessor
& styler
)
238 styler
.ColourTo( cur
, SCE_OPAL_PAR
);
242 styler
.StartSegment( cur
);
246 inline bool HandleSpace( unsigned int & cur
, unsigned int one_too_much
, Accessor
& styler
)
253 if( cur
>= one_too_much
)
255 styler
.ColourTo( cur
- 1, SCE_OPAL_SPACE
);
259 ch
= styler
.SafeGetCharAt( cur
);
270 styler
.ColourTo( cur
- 1, SCE_OPAL_SPACE
);
271 styler
.StartSegment( cur
);
277 inline bool HandleInteger( unsigned int & cur
, unsigned int one_too_much
, Accessor
& styler
)
284 if( cur
>= one_too_much
)
286 styler
.ColourTo( cur
- 1, SCE_OPAL_INTEGER
);
287 return false; // STOP
290 ch
= styler
.SafeGetCharAt( cur
);
293 styler
.ColourTo( cur
- 1, SCE_OPAL_INTEGER
);
294 styler
.StartSegment( cur
);
300 inline bool HandleWord( unsigned int & cur
, unsigned int one_too_much
, Accessor
& styler
, WordList
* keywordlists
[] )
303 const unsigned int beg
= cur
;
308 ch
= styler
.SafeGetCharAt( cur
);
309 if( ( ch
!= '_' ) && ( ch
!= '-' ) &&
310 !islower( ch
) && !isupper( ch
) && !isdigit( ch
) ) break;
313 if( cur
>= one_too_much
)
319 const int ide_len
= cur
- beg
+ 1;
320 char * ide
= new char[ ide_len
];
321 getRange( beg
, cur
, styler
, ide
, ide_len
);
323 WordList
& keywords
= *keywordlists
[ 0 ];
324 WordList
& classwords
= *keywordlists
[ 1 ];
326 if( keywords
.InList( ide
) ) // Keyword
330 styler
.ColourTo( cur
- 1, SCE_OPAL_KEYWORD
);
331 if( cur
>= one_too_much
)
333 return false; // STOP
337 styler
.StartSegment( cur
);
341 else if( classwords
.InList( ide
) ) // Sort
345 styler
.ColourTo( cur
- 1, SCE_OPAL_SORT
);
346 if( cur
>= one_too_much
)
348 return false; // STOP
352 styler
.StartSegment( cur
);
356 else if( !strcmp( ide
, "true" ) || !strcmp( ide
, "false" ) ) // Bool const
360 styler
.ColourTo( cur
- 1, SCE_OPAL_BOOL_CONST
);
361 if( cur
>= one_too_much
)
363 return false; // STOP
367 styler
.StartSegment( cur
);
371 else // Unknown keyword
375 styler
.ColourTo( cur
- 1, SCE_OPAL_DEFAULT
);
376 if( cur
>= one_too_much
)
378 return false; // STOP
382 styler
.StartSegment( cur
);
389 inline bool HandleSkip( unsigned int & cur
, unsigned int one_too_much
, Accessor
& styler
)
392 styler
.ColourTo( cur
- 1, SCE_OPAL_DEFAULT
);
393 if( cur
>= one_too_much
)
395 return false; // STOP
399 styler
.StartSegment( cur
);
404 static void ColouriseOpalDoc( unsigned int startPos
, int length
, int initStyle
, WordList
*keywordlists
[], Accessor
& styler
)
406 styler
.StartAt( startPos
);
407 styler
.StartSegment( startPos
);
409 unsigned int & cur
= startPos
;
410 const unsigned int one_too_much
= startPos
+ length
;
412 int state
= initStyle
;
418 case SCE_OPAL_KEYWORD
:
420 if( !HandleWord( cur
, one_too_much
, styler
, keywordlists
) ) return;
421 state
= SCE_OPAL_DEFAULT
;
424 case SCE_OPAL_INTEGER
:
425 if( !HandleInteger( cur
, one_too_much
, styler
) ) return;
426 state
= SCE_OPAL_DEFAULT
;
429 case SCE_OPAL_COMMENT_BLOCK
:
430 if( !HandleCommentBlock( cur
, one_too_much
, styler
, false ) ) return;
431 state
= SCE_OPAL_DEFAULT
;
434 case SCE_OPAL_COMMENT_LINE
:
435 if( !HandleCommentLine( cur
, one_too_much
, styler
, false ) ) return;
436 state
= SCE_OPAL_DEFAULT
;
439 case SCE_OPAL_STRING
:
440 if( !HandleString( cur
, one_too_much
, styler
) ) return;
441 state
= SCE_OPAL_DEFAULT
;
444 default: // SCE_OPAL_DEFAULT:
446 char ch
= styler
.SafeGetCharAt( cur
);
452 if( !HandleString( cur
, one_too_much
, styler
) ) return;
457 if( !HandleCommentBlock( cur
, one_too_much
, styler
, true ) ) return;
462 if( !HandleCommentLine( cur
, one_too_much
, styler
, true ) ) return;
472 if( !HandlePar( cur
, styler
) ) return;
480 if( !HandleSpace( cur
, one_too_much
, styler
) ) return;
488 if( !HandleInteger( cur
, one_too_much
, styler
) ) return;
492 else if( islower( ch
) || isupper( ch
) )
494 if( !HandleWord( cur
, one_too_much
, styler
, keywordlists
) ) return;
501 if( !HandleSkip( cur
, one_too_much
, styler
) ) return;
512 static const char * const opalWordListDesc
[] = {
518 LexerModule
lmOpal(SCLEX_OPAL
, ColouriseOpalDoc
, "opal", NULL
, opalWordListDesc
);