Menu

[r1131]: / trunk / ext / scintilla / lexers / LexKVIrc.cxx  Maximize  Restore  History

Download this file

474 lines (388 with data), 16.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// Scintilla source code edit control
/** @file LexKVIrc.cxx
** Lexer for KVIrc script.
**/
// Copyright 2013 by OmegaPhil <OmegaPhil+scintilla@gmail.com>, based in
// part from LexPython Copyright 1998-2002 by Neil Hodgson <neilh@scintilla.org>
// and LexCmake Copyright 2007 by Cristian Adam <cristian [dot] adam [at] gmx [dot] net>
// The License.txt file describes the conditions under which this software may be distributed.
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <assert.h>
#include <ctype.h>
#include "ILexer.h"
#include "Scintilla.h"
#include "SciLexer.h"
#include "WordList.h"
#include "LexAccessor.h"
#include "Accessor.h"
#include "StyleContext.h"
#include "CharacterSet.h"
#include "LexerModule.h"
#ifdef SCI_NAMESPACE
using namespace Scintilla;
#endif
/* KVIrc Script syntactic rules: http://www.kvirc.net/doc/doc_syntactic_rules.html */
/* Utility functions */
static inline bool IsAWordChar(int ch) {
/* Keyword list includes modules, i.e. words including '.', and
* alias namespaces include ':' */
return (ch < 0x80) && (isalnum(ch) || ch == '_' || ch == '.'
|| ch == ':');
}
static inline bool IsAWordStart(int ch) {
/* Functions (start with '$') are treated separately to keywords */
return (ch < 0x80) && (isalnum(ch) || ch == '_' );
}
/* Interface function called by Scintilla to request some text to be
syntax highlighted */
static void ColouriseKVIrcDoc(Sci_PositionU startPos, Sci_Position length,
int initStyle, WordList *keywordlists[],
Accessor &styler)
{
/* Fetching style context */
StyleContext sc(startPos, length, initStyle, styler);
/* Accessing keywords and function-marking keywords */
WordList &keywords = *keywordlists[0];
WordList &functionKeywords = *keywordlists[1];
/* Looping for all characters - only automatically moving forward
* when asked for (transitions leaving strings and keywords do this
* already) */
bool next = true;
for( ; sc.More(); next ? sc.Forward() : (void)0 )
{
/* Resetting next */
next = true;
/* Dealing with different states */
switch (sc.state)
{
case SCE_KVIRC_DEFAULT:
/* Detecting single-line comments
* Unfortunately KVIrc script allows raw '#<channel
* name>' to be used, and appending # to an array returns
* its length...
* Going for a compromise where single line comments not
* starting on a newline are allowed in all cases except
* when they are preceeded with an opening bracket or comma
* (this will probably be the most common style a valid
* string-less channel name will be used with), with the
* array length case included
*/
if (
(sc.ch == '#' && sc.atLineStart) ||
(sc.ch == '#' && (
sc.chPrev != '(' && sc.chPrev != ',' &&
sc.chPrev != ']')
)
)
{
sc.SetState(SCE_KVIRC_COMMENT);
break;
}
/* Detecting multi-line comments */
if (sc.Match('/', '*'))
{
sc.SetState(SCE_KVIRC_COMMENTBLOCK);
break;
}
/* Detecting strings */
if (sc.ch == '"')
{
sc.SetState(SCE_KVIRC_STRING);
break;
}
/* Detecting functions */
if (sc.ch == '$')
{
sc.SetState(SCE_KVIRC_FUNCTION);
break;
}
/* Detecting variables */
if (sc.ch == '%')
{
sc.SetState(SCE_KVIRC_VARIABLE);
break;
}
/* Detecting numbers - isdigit is unsafe as it does not
* validate, use CharacterSet.h functions */
if (IsADigit(sc.ch))
{
sc.SetState(SCE_KVIRC_NUMBER);
break;
}
/* Detecting words */
if (IsAWordStart(sc.ch) && IsAWordChar(sc.chNext))
{
sc.SetState(SCE_KVIRC_WORD);
sc.Forward();
break;
}
/* Detecting operators */
if (isoperator(sc.ch))
{
sc.SetState(SCE_KVIRC_OPERATOR);
break;
}
break;
case SCE_KVIRC_COMMENT:
/* Breaking out of single line comment when a newline
* is introduced */
if (sc.ch == '\r' || sc.ch == '\n')
{
sc.SetState(SCE_KVIRC_DEFAULT);
break;
}
break;
case SCE_KVIRC_COMMENTBLOCK:
/* Detecting end of multi-line comment */
if (sc.Match('*', '/'))
{
// Moving the current position forward two characters
// so that '*/' is included in the comment
sc.Forward(2);
sc.SetState(SCE_KVIRC_DEFAULT);
/* Comment has been exited and the current position
* moved forward, yet the new current character
* has yet to be defined - loop without moving
* forward again */
next = false;
break;
}
break;
case SCE_KVIRC_STRING:
/* Detecting end of string - closing speechmarks */
if (sc.ch == '"')
{
/* Allowing escaped speechmarks to pass */
if (sc.chPrev == '\\')
break;
/* Moving the current position forward to capture the
* terminating speechmarks, and ending string */
sc.ForwardSetState(SCE_KVIRC_DEFAULT);
/* String has been exited and the current position
* moved forward, yet the new current character
* has yet to be defined - loop without moving
* forward again */
next = false;
break;
}
/* Functions and variables are now highlighted in strings
* Detecting functions */
if (sc.ch == '$')
{
/* Allowing escaped functions to pass */
if (sc.chPrev == '\\')
break;
sc.SetState(SCE_KVIRC_STRING_FUNCTION);
break;
}
/* Detecting variables */
if (sc.ch == '%')
{
/* Allowing escaped variables to pass */
if (sc.chPrev == '\\')
break;
sc.SetState(SCE_KVIRC_STRING_VARIABLE);
break;
}
/* Breaking out of a string when a newline is introduced */
if (sc.ch == '\r' || sc.ch == '\n')
{
/* Allowing escaped newlines */
if (sc.chPrev == '\\')
break;
sc.SetState(SCE_KVIRC_DEFAULT);
break;
}
break;
case SCE_KVIRC_FUNCTION:
case SCE_KVIRC_VARIABLE:
/* Detecting the end of a function/variable (word) */
if (!IsAWordChar(sc.ch))
{
sc.SetState(SCE_KVIRC_DEFAULT);
/* Word has been exited yet the current character
* has yet to be defined - loop without moving
* forward again */
next = false;
break;
}
break;
case SCE_KVIRC_STRING_FUNCTION:
case SCE_KVIRC_STRING_VARIABLE:
/* A function or variable in a string
* Detecting the end of a function/variable (word) */
if (!IsAWordChar(sc.ch))
{
sc.SetState(SCE_KVIRC_STRING);
/* Word has been exited yet the current character
* has yet to be defined - loop without moving
* forward again */
next = false;
break;
}
break;
case SCE_KVIRC_NUMBER:
/* Detecting the end of a number */
if (!IsADigit(sc.ch))
{
sc.SetState(SCE_KVIRC_DEFAULT);
/* Number has been exited yet the current character
* has yet to be defined - loop without moving
* forward */
next = false;
break;
}
break;
case SCE_KVIRC_OPERATOR:
/* Because '%' is an operator but is also the marker for
* a variable, I need to always treat operators as single
* character strings and therefore redo their detection
* after every character */
sc.SetState(SCE_KVIRC_DEFAULT);
/* Operator has been exited yet the current character
* has yet to be defined - loop without moving
* forward */
next = false;
break;
case SCE_KVIRC_WORD:
/* Detecting the end of a word */
if (!IsAWordChar(sc.ch))
{
/* Checking if the word was actually a keyword -
* fetching the current word, NULL-terminated like
* the keyword list */
char s[100];
Sci_Position wordLen = sc.currentPos - styler.GetStartSegment();
if (wordLen > 99)
wordLen = 99; /* Include '\0' in buffer */
Sci_Position i;
for( i = 0; i < wordLen; ++i )
{
s[i] = styler.SafeGetCharAt( styler.GetStartSegment() + i );
}
s[wordLen] = '\0';
/* Actually detecting keywords and fixing the state */
if (keywords.InList(s))
{
/* The SetState call actually commits the
* previous keyword state */
sc.ChangeState(SCE_KVIRC_KEYWORD);
}
else if (functionKeywords.InList(s))
{
// Detecting function keywords and fixing the state
sc.ChangeState(SCE_KVIRC_FUNCTION_KEYWORD);
}
/* Transitioning to default and committing the previous
* word state */
sc.SetState(SCE_KVIRC_DEFAULT);
/* Word has been exited yet the current character
* has yet to be defined - loop without moving
* forward again */
next = false;
break;
}
break;
}
}
/* Indicating processing is complete */
sc.Complete();
}
static void FoldKVIrcDoc(Sci_PositionU startPos, Sci_Position length, int /*initStyle - unused*/,
WordList *[], Accessor &styler)
{
/* Based on CMake's folder */
/* Exiting if folding isnt enabled */
if ( styler.GetPropertyInt("fold") == 0 )
return;
/* Obtaining current line number*/
Sci_Position currentLine = styler.GetLine(startPos);
/* Obtaining starting character - indentation is done on a line basis,
* not character */
Sci_PositionU safeStartPos = styler.LineStart( currentLine );
/* Initialising current level - this is defined as indentation level
* in the low 12 bits, with flag bits in the upper four bits.
* It looks like two indentation states are maintained in the returned
* 32bit value - 'nextLevel' in the most-significant bits, 'currentLevel'
* in the least-significant bits. Since the next level is the most
* up to date, this must refer to the current state of indentation.
* So the code bitshifts the old current level out of existence to
* get at the actual current state of indentation
* Based on the LexerCPP.cxx line 958 comment */
int currentLevel = SC_FOLDLEVELBASE;
if (currentLine > 0)
currentLevel = styler.LevelAt(currentLine - 1) >> 16;
int nextLevel = currentLevel;
// Looping for characters in range
for (Sci_PositionU i = safeStartPos; i < startPos + length; ++i)
{
/* Folding occurs after syntax highlighting, meaning Scintilla
* already knows where the comments are
* Fetching the current state */
int state = styler.StyleAt(i) & 31;
switch( styler.SafeGetCharAt(i) )
{
case '{':
/* Indenting only when the braces are not contained in
* a comment */
if (state != SCE_KVIRC_COMMENT &&
state != SCE_KVIRC_COMMENTBLOCK)
++nextLevel;
break;
case '}':
/* Outdenting only when the braces are not contained in
* a comment */
if (state != SCE_KVIRC_COMMENT &&
state != SCE_KVIRC_COMMENTBLOCK)
--nextLevel;
break;
case '\n':
case '\r':
/* Preparing indentation information to return - combining
* current and next level data */
int lev = currentLevel | nextLevel << 16;
/* If the next level increases the indent level, mark the
* current line as a fold point - current level data is
* in the least significant bits */
if (nextLevel > currentLevel )
lev |= SC_FOLDLEVELHEADERFLAG;
/* Updating indentation level if needed */
if (lev != styler.LevelAt(currentLine))
styler.SetLevel(currentLine, lev);
/* Updating variables */
++currentLine;
currentLevel = nextLevel;
/* Dealing with problematic Windows newlines -
* incrementing to avoid the extra newline breaking the
* fold point */
if (styler.SafeGetCharAt(i) == '\r' &&
styler.SafeGetCharAt(i + 1) == '\n')
++i;
break;
}
}
/* At this point the data has ended, so presumably the end of the line?
* Preparing indentation information to return - combining current
* and next level data */
int lev = currentLevel | nextLevel << 16;
/* If the next level increases the indent level, mark the current
* line as a fold point - current level data is in the least
* significant bits */
if (nextLevel > currentLevel )
lev |= SC_FOLDLEVELHEADERFLAG;
/* Updating indentation level if needed */
if (lev != styler.LevelAt(currentLine))
styler.SetLevel(currentLine, lev);
}
/* Registering wordlists */
static const char *const kvircWordListDesc[] = {
"primary",
"function_keywords",
0
};
/* Registering functions and wordlists */
LexerModule lmKVIrc(SCLEX_KVIRC, ColouriseKVIrcDoc, "kvirc", FoldKVIrcDoc,
kvircWordListDesc);
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.