File: LexDataflex.cxx

package info (click to toggle)
codequery 1.0.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,860 kB
  • sloc: cpp: 151,420; xml: 16,576; python: 5,602; ansic: 5,487; makefile: 559; perl: 496; ruby: 209; sql: 194; sh: 106; php: 53; vhdl: 51; erlang: 47; objc: 22; lisp: 18; cobol: 18; modula3: 17; asm: 14; fortran: 12; ml: 11; tcl: 6
file content (611 lines) | stat: -rw-r--r-- 19,851 bytes parent folder | download | duplicates (3)
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
// Scintilla source code edit control
/** @file LexDataflex.cxx
 ** Lexer for DataFlex.
 ** Based on LexPascal.cxx
 ** Written by Wil van Antwerpen, June 2019
 **/

/*
// The License.txt file describes the conditions under which this software may be distributed.

A few words about features of LexDataflex...

Generally speaking LexDataflex tries to support all available DataFlex features (up
to DataFlex 19.1 at this time).

~ FOLDING:

Folding is supported in the following cases:

- Folding of stream-like comments
- Folding of groups of consecutive line comments
- Folding of preprocessor blocks (the following preprocessor blocks are
supported: #IFDEF, #IFNDEF, #ENDIF and #HEADER / #ENDHEADER 
blocks), 
- Folding of code blocks on appropriate keywords (the following code blocks are
supported: "begin, struct, type, case / end" blocks, class & object
declarations and interface declarations)

Remarks:

- We pass 4 arrays to the lexer:
1. The DataFlex keyword list, these are normal DataFlex keywords
2. The Scope Open list, for example, begin / procedure / while
3. The Scope Close list, for example, end / end_procedure / loop
4. Operator list, for ex. + / - / * / Lt / iand
These lists are all mutually exclusive, scope open words should not be in the keyword list and vice versa

- Folding of code blocks tries to handle all special cases in which folding
should not occur. 

~ KEYWORDS:

The list of keywords that can be used in dataflex.properties file (up to DataFlex
19.1):

- Keywords: .. snipped .. see dataflex.properties file.

*/

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <assert.h>
#include <ctype.h>

#include <string>
#include <string_view>

#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"

using namespace Lexilla;


static void GetRangeLowered(Sci_PositionU start,
		Sci_PositionU end,
		Accessor &styler,
		char *s,
		Sci_PositionU len) {
	Sci_PositionU i = 0;
	while ((i < end - start + 1) && (i < len-1)) {
		s[i] = static_cast<char>(tolower(styler[start + i]));
		i++;
	}
	s[i] = '\0';
}

static void GetForwardRangeLowered(Sci_PositionU start,
		CharacterSet &charSet,
		Accessor &styler,
		char *s,
		Sci_PositionU len) {
	Sci_PositionU i = 0;
	while ((i < len-1) && charSet.Contains(styler.SafeGetCharAt(start + i))) {
		s[i] = static_cast<char>(tolower(styler.SafeGetCharAt(start + i)));
		i++;
	}
	s[i] = '\0';

}

enum {
	stateInICode = 0x1000,
	stateSingleQuoteOpen = 0x2000,
	stateDoubleQuoteOpen = 0x4000,
	stateFoldInPreprocessor = 0x0100,
	stateFoldInCaseStatement = 0x0200,
	stateFoldInPreprocessorLevelMask = 0x00FF,
	stateFoldMaskAll = 0x0FFF
};


static bool IsFirstDataFlexWord(Sci_Position pos, Accessor &styler) {
	Sci_Position line = styler.GetLine(pos);
	Sci_Position start_pos = styler.LineStart(line);
	for (Sci_Position i = start_pos; i < pos; i++) {
		char ch = styler.SafeGetCharAt(i);
		if (!(ch == ' ' || ch == '\t'))
			return false;
	}
	return true;
}


inline bool IsADataFlexField(int ch) {
	return (ch == '.');
}


static void ClassifyDataFlexWord(WordList *keywordlists[], StyleContext &sc, Accessor &styler) {
	WordList& keywords = *keywordlists[0];
	WordList& scopeOpen   = *keywordlists[1];
	WordList& scopeClosed = *keywordlists[2];
	WordList& operators   = *keywordlists[3];

	char s[100];
	int oldState;
	int newState;
	size_t tokenlen;

    oldState = sc.state;
	newState = oldState;
	sc.GetCurrentLowered(s, sizeof(s));
	tokenlen = strnlen(s,sizeof(s));
	if (keywords.InList(s)) {
		// keywords in DataFlex can be used as table column names (file.field) and as such they
		// should not be characterized as a keyword. So test for that.
		// for ex. somebody using date as field name.
        if (!IsADataFlexField(sc.GetRelative(-static_cast<int>(tokenlen+1)))) {
	      newState = SCE_DF_WORD;
	    }
	}
	if (oldState == newState) {
		if ((scopeOpen.InList(s) || scopeClosed.InList(s)) && (strcmp(s, "for") != 0) && (strcmp(s, "repeat") != 0)) {
			// scope words in DataFlex can be used as table column names (file.field) and as such they
			// should not be characterized as a scope word. So test for that.
			// for ex. somebody using procedure for field name.
			if (!IsADataFlexField(sc.GetRelative(-static_cast<int>(tokenlen+1)))) {
				newState = SCE_DF_SCOPEWORD;
			}
		} 
		// no code folding on the next words, but just want to paint them like keywords (as they are) (??? doesn't the code to the opposite?)
		if (strcmp(s, "if") == 0 ||  
			strcmp(s, "ifnot") == 0 ||
			strcmp(s, "case") == 0 ||
			strcmp(s, "else") == 0 ) {
				newState = SCE_DF_SCOPEWORD;
		} 
	}
	if (oldState != newState && newState == SCE_DF_WORD) {
		// a for loop must have for at the start of the line, for is also used in "define abc for 123"
		if ( (strcmp(s, "for") == 0) && (IsFirstDataFlexWord(sc.currentPos-3, styler)) ) {   
				newState = SCE_DF_SCOPEWORD;
		} 
	}
	if (oldState != newState && newState == SCE_DF_WORD) {
		// a repeat loop must have repeat at the start of the line, repeat is also used in 'move (repeat("d",5)) to sFoo'
		if ( (strcmp(s, "repeat") == 0) && (IsFirstDataFlexWord(sc.currentPos-6, styler)) ) {   
				newState = SCE_DF_SCOPEWORD;
		} 
	}
	if (oldState == newState)  {
	  if (operators.InList(s)) {
		  newState = SCE_DF_OPERATOR;
		} 
	}

	if (oldState != newState) {
		sc.ChangeState(newState);
	}
	sc.SetState(SCE_DF_DEFAULT);
}

static void ColouriseDataFlexDoc(Sci_PositionU startPos, Sci_Position length, int initStyle, WordList *keywordlists[],
		Accessor &styler) {
//	bool bSmartHighlighting = styler.GetPropertyInt("lexer.dataflex.smart.highlighting", 1) != 0;

			CharacterSet setWordStart(CharacterSet::setAlpha, "_$#@", 0x80, true);
			CharacterSet setWord(CharacterSet::setAlphaNum, "_$#@", 0x80, true);
	CharacterSet setNumber(CharacterSet::setDigits, ".-+eE");
	CharacterSet setHexNumber(CharacterSet::setDigits, "abcdefABCDEF");
	CharacterSet setOperator(CharacterSet::setNone, "*+-/<=>^");

	Sci_Position curLine = styler.GetLine(startPos);
	int curLineState = curLine > 0 ? styler.GetLineState(curLine - 1) : 0;

	StyleContext sc(startPos, length, initStyle, styler);

	for (; sc.More(); sc.Forward()) {
		if (sc.atLineEnd) {
			// Update the line state, so it can be seen by next line
			curLine = styler.GetLine(sc.currentPos);
			styler.SetLineState(curLine, curLineState);
		}

		// Determine if the current state should terminate.
		switch (sc.state) {
			case SCE_DF_NUMBER:
				if (!setNumber.Contains(sc.ch) || (sc.ch == '.' && sc.chNext == '.')) {
					sc.SetState(SCE_DF_DEFAULT);
				} else if (sc.ch == '-' || sc.ch == '+') {
					if (sc.chPrev != 'E' && sc.chPrev != 'e') {
						sc.SetState(SCE_DF_DEFAULT);
					}
				}
				break;
			case SCE_DF_IDENTIFIER:
				if (!setWord.Contains(sc.ch)) {
					ClassifyDataFlexWord(keywordlists, sc, styler);
				}
				break;
			case SCE_DF_HEXNUMBER:
				if (!(setHexNumber.Contains(sc.ch) || sc.ch == 'I') ) { // in |CI$22a we also want to color the "I"
					sc.SetState(SCE_DF_DEFAULT);
				}
				break;
			case SCE_DF_METATAG:
				if (sc.atLineStart || sc.chPrev == '}') {
					sc.SetState(SCE_DF_DEFAULT);
				}
				break;
			case SCE_DF_PREPROCESSOR:
				if (sc.atLineStart || IsASpaceOrTab(sc.ch)) {
					sc.SetState(SCE_DF_DEFAULT);
				}
				break;
			case SCE_DF_IMAGE:
				if (sc.atLineStart && sc.Match("/*")) {
					sc.Forward();  // these characters are still part of the DF Image
					sc.ForwardSetState(SCE_DF_DEFAULT);
				}
				break;
			case SCE_DF_PREPROCESSOR2:
				// we don't have inline comments or preprocessor2 commands
				//if (sc.Match('*', ')')) {
				//	sc.Forward();
				//	sc.ForwardSetState(SCE_DF_DEFAULT);
				//}
				break;
			case SCE_DF_COMMENTLINE:
				if (sc.atLineStart) {
					sc.SetState(SCE_DF_DEFAULT);
				}
				break;
			case SCE_DF_STRING:
				if (sc.atLineEnd) {
					sc.ChangeState(SCE_DF_STRINGEOL);
				} else if (sc.ch == '\'' && sc.chNext == '\'') {
					sc.Forward();
				} else if (sc.ch == '\"' && sc.chNext == '\"') {
					sc.Forward();
				} else if (sc.ch == '\'' || sc.ch == '\"') {
					if (sc.ch == '\'' && (curLineState & stateSingleQuoteOpen) ) {
 				    curLineState &= ~(stateSingleQuoteOpen);
					sc.ForwardSetState(SCE_DF_DEFAULT);
					}
					else if (sc.ch == '\"' && (curLineState & stateDoubleQuoteOpen) ) {
 				    curLineState &= ~(stateDoubleQuoteOpen);
					sc.ForwardSetState(SCE_DF_DEFAULT);
					}
				}
				break;
			case SCE_DF_STRINGEOL:
				if (sc.atLineStart) {
					sc.SetState(SCE_DF_DEFAULT);
				}
				break;
			case SCE_DF_SCOPEWORD:
				//if (!setHexNumber.Contains(sc.ch) && sc.ch != '$') {
				//	sc.SetState(SCE_DF_DEFAULT);
				//}
				break;
			case SCE_DF_OPERATOR:
//				if (bSmartHighlighting && sc.chPrev == ';') {
//					curLineState &= ~(stateInProperty | stateInExport);
//				}
				sc.SetState(SCE_DF_DEFAULT);
				break;
			case SCE_DF_ICODE:
				if (sc.atLineStart || IsASpace(sc.ch) || isoperator(sc.ch)) {
				sc.SetState(SCE_DF_DEFAULT);
				}
				break;
		}

		// Determine if a new state should be entered.
		if (sc.state == SCE_DF_DEFAULT) {
			if (IsADigit(sc.ch)) {
				sc.SetState(SCE_DF_NUMBER);
			} else if (sc.Match('/', '/') || sc.Match("#REM")) {
				sc.SetState(SCE_DF_COMMENTLINE);
			} else if ((sc.ch == '#' && !sc.Match("#REM")) && IsFirstDataFlexWord(sc.currentPos, styler)) {
				sc.SetState(SCE_DF_PREPROCESSOR);
			// || (sc.ch == '|' && sc.chNext == 'C' && sc.GetRelativeCharacter(2) == 'I' && sc.GetRelativeCharacter(3) == '$') ) {
			} else if ((sc.ch == '$' && ((!setWord.Contains(sc.chPrev)) || sc.chPrev == 'I' ) ) || (sc.Match("|CI$")) ) {
				sc.SetState(SCE_DF_HEXNUMBER); // start with $ and previous character not in a..zA..Z0..9 excluding "I" OR start with |CI$
			} else if (setWordStart.Contains(sc.ch)) {
				sc.SetState(SCE_DF_IDENTIFIER);
			} else if (sc.ch == '{') {
				sc.SetState(SCE_DF_METATAG);
			//} else if (sc.Match("(*$")) {
			//	sc.SetState(SCE_DF_PREPROCESSOR2);
			} else if (sc.ch == '/' && setWord.Contains(sc.chNext) &&  sc.atLineStart) {
				sc.SetState(SCE_DF_IMAGE);
			//	sc.Forward();	// Eat the * so it isn't used for the end of the comment
			} else if (sc.ch == '\'' || sc.ch == '\"') {
				if (sc.ch == '\'' && !(curLineState & stateDoubleQuoteOpen)) {
				  curLineState |= stateSingleQuoteOpen;
				} else if (sc.ch == '\"' && !(curLineState & stateSingleQuoteOpen)) {
				  curLineState |= stateDoubleQuoteOpen;
				}
			    sc.SetState(SCE_DF_STRING);
			} else if (setOperator.Contains(sc.ch)) {
				sc.SetState(SCE_DF_OPERATOR);
//			} else if (curLineState & stateInICode) {
				// ICode start ! in a string followed by close string mark is not icode
			} else if ((sc.ch == '!') && !(sc.ch == '!' && ((sc.chNext == '\"') || (sc.ch == '\'')) )) {
				sc.SetState(SCE_DF_ICODE);
			}
		}
	}

	if (sc.state == SCE_DF_IDENTIFIER && setWord.Contains(sc.chPrev)) {
		ClassifyDataFlexWord(keywordlists, sc, styler);
	}

	sc.Complete();
}

static bool IsStreamCommentStyle(int style) {
	return style == SCE_DF_IMAGE;
}

static bool IsCommentLine(Sci_Position line, Accessor &styler) {
	Sci_Position pos = styler.LineStart(line);
	Sci_Position eolPos = styler.LineStart(line + 1) - 1;
	for (Sci_Position i = pos; i < eolPos; i++) {
		char ch = styler[i];
		char chNext = styler.SafeGetCharAt(i + 1);
		int style = styler.StyleAt(i);
		if (ch == '/' && chNext == '/' && style == SCE_DF_COMMENTLINE) {
			return true;
		} else if (!IsASpaceOrTab(ch)) {
			return false;
		}
	}
	return false;
}



static unsigned int GetFoldInPreprocessorLevelFlag(int lineFoldStateCurrent) {
	return lineFoldStateCurrent & stateFoldInPreprocessorLevelMask;
}

static void SetFoldInPreprocessorLevelFlag(int &lineFoldStateCurrent, unsigned int nestLevel) {
	lineFoldStateCurrent &= ~stateFoldInPreprocessorLevelMask;
	lineFoldStateCurrent |= nestLevel & stateFoldInPreprocessorLevelMask;
}

static int ClassifyDataFlexPreprocessorFoldPoint(int &levelCurrent, int &lineFoldStateCurrent,
		Sci_PositionU startPos, Accessor &styler) {
	CharacterSet setWord(CharacterSet::setAlpha);

	char s[100];	// Size of the longest possible keyword + one additional character + null
	GetForwardRangeLowered(startPos, setWord, styler, s, sizeof(s));
	size_t iLen = strnlen(s,sizeof(s));
	size_t iWordSize = 0;

	unsigned int nestLevel = GetFoldInPreprocessorLevelFlag(lineFoldStateCurrent);

	if (strcmp(s, "command") == 0 ||
		// The #if/#ifdef etcetera commands are not currently foldable as it is easy to write code that
		// breaks the collaps logic, so we keep things simple and not include that for now.
		strcmp(s, "header") == 0) {
		nestLevel++;
		SetFoldInPreprocessorLevelFlag(lineFoldStateCurrent, nestLevel);
		lineFoldStateCurrent |= stateFoldInPreprocessor;
		levelCurrent++;
		iWordSize = iLen;
	} else if (strcmp(s, "endcommand") == 0 ||
		strcmp(s, "endheader") == 0) {
		nestLevel--;
		SetFoldInPreprocessorLevelFlag(lineFoldStateCurrent, nestLevel);
		if (nestLevel == 0) {
			lineFoldStateCurrent &= ~stateFoldInPreprocessor;
		}
		levelCurrent--;
		iWordSize = iLen;
		if (levelCurrent < SC_FOLDLEVELBASE) {
			levelCurrent = SC_FOLDLEVELBASE;
		}
	}
	return static_cast<int>(iWordSize);
}


static void ClassifyDataFlexWordFoldPoint(int &levelCurrent, int &lineFoldStateCurrent,
		Sci_PositionU lastStart, Sci_PositionU currentPos, WordList *[], Accessor &styler) {
	char s[100];

	// property fold.dataflex.compilerlist
	//	Set to 1 for enabling the code folding feature in *.prn files
	bool foldPRN = styler.GetPropertyInt("fold.dataflex.compilerlist",0) != 0;

	GetRangeLowered(lastStart, currentPos, styler, s, sizeof(s));

	if (strcmp(s, "case") == 0) {
		lineFoldStateCurrent |= stateFoldInCaseStatement;
	} else if (strcmp(s, "begin") == 0) {
		levelCurrent++;
	} else if (strcmp(s, "for") == 0 ||
		strcmp(s, "while") == 0 ||
		strcmp(s, "repeat") == 0 ||
		strcmp(s, "for_all") == 0 ||
		strcmp(s, "struct") == 0 ||
		strcmp(s, "type") == 0 ||
		strcmp(s, "begin_row") == 0 ||
		strcmp(s, "item_list") == 0 ||
		strcmp(s, "begin_constraints") == 0 ||
		strcmp(s, "begin_transaction") == 0 ||
		strcmp(s, "enum_list") == 0 ||
		strcmp(s, "class") == 0 || 
		strcmp(s, "object") == 0 ||
		strcmp(s, "cd_popup_object") == 0 ||
		strcmp(s, "procedure") == 0 ||
		strcmp(s, "procedure_section") == 0 ||
		strcmp(s, "function") == 0 ) {
			if ((IsFirstDataFlexWord(lastStart, styler )) || foldPRN) {
			levelCurrent++;
			}
	} else if (strcmp(s, "end") == 0) {  // end is not always the first keyword, for example "case end"
		levelCurrent--;
		if (levelCurrent < SC_FOLDLEVELBASE) {
			levelCurrent = SC_FOLDLEVELBASE;
		}
	} else if (strcmp(s, "loop") == 0 ||
				   strcmp(s, "until") == 0 ||
				   strcmp(s, "end_class") == 0 ||
				   strcmp(s, "end_object") == 0 ||
				   strcmp(s, "cd_end_object") == 0 ||
				   strcmp(s, "end_procedure") == 0 ||
				   strcmp(s, "end_function") == 0 ||
				   strcmp(s, "end_for_all") == 0 ||
				   strcmp(s, "end_struct") == 0 ||
				   strcmp(s, "end_type") == 0 ||
				   strcmp(s, "end_row") == 0 ||
				   strcmp(s, "end_item_list") == 0 ||
				   strcmp(s, "end_constraints") == 0 ||
				   strcmp(s, "end_transaction") == 0 ||
				   strcmp(s, "end_enum_list") == 0 ) {
	//		lineFoldStateCurrent &= ~stateFoldInRecord;
			if ((IsFirstDataFlexWord(lastStart, styler )) || foldPRN) {
				levelCurrent--;
				if (levelCurrent < SC_FOLDLEVELBASE) {
					levelCurrent = SC_FOLDLEVELBASE;
				}
			}
	}

}


static void ClassifyDataFlexMetaDataFoldPoint(int &levelCurrent, 
		Sci_PositionU lastStart, Sci_PositionU currentPos, WordList *[], Accessor &styler) {
	char s[100];

	GetRangeLowered(lastStart, currentPos, styler, s, sizeof(s));

    if (strcmp(s, "#beginsection") == 0) {
		levelCurrent++;
	} else if (strcmp(s, "#endsection") == 0) {
			levelCurrent--;
			if (levelCurrent < SC_FOLDLEVELBASE) {
				levelCurrent = SC_FOLDLEVELBASE;
			}
	}

}

static void FoldDataFlexDoc(Sci_PositionU startPos, Sci_Position length, int initStyle, WordList *keywordlists[],
		Accessor &styler) {
	bool foldComment = styler.GetPropertyInt("fold.comment") != 0;
	bool foldPreprocessor = styler.GetPropertyInt("fold.preprocessor") != 0;
	bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
	Sci_PositionU endPos = startPos + length;
	int visibleChars = 0;
	Sci_Position lineCurrent = styler.GetLine(startPos);
	int levelPrev = styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK;
	int levelCurrent = levelPrev;
	int lineFoldStateCurrent = lineCurrent > 0 ? styler.GetLineState(lineCurrent - 1) & stateFoldMaskAll : 0;
	char chNext = styler[startPos];
	int styleNext = styler.StyleAt(startPos);
	int style = initStyle;
	int iWordSize;

	Sci_Position lastStart = 0;
	CharacterSet setWord(CharacterSet::setAlphaNum, "_$#@", 0x80, true);

	for (Sci_PositionU i = startPos; i < endPos; i++) {
		char ch = chNext;
		chNext = styler.SafeGetCharAt(i + 1);
		int stylePrev = style;
		style = styleNext;
		styleNext = styler.StyleAt(i + 1);
		bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');

		if (foldComment && IsStreamCommentStyle(style)) {
			if (!IsStreamCommentStyle(stylePrev)) {
				levelCurrent++;
			} else if (!IsStreamCommentStyle(styleNext)) {
				levelCurrent--;
			}
		}
		if (foldComment && atEOL && IsCommentLine(lineCurrent, styler))
		{
			if (!IsCommentLine(lineCurrent - 1, styler)
			    && IsCommentLine(lineCurrent + 1, styler))
				levelCurrent++;
			else if (IsCommentLine(lineCurrent - 1, styler)
			         && !IsCommentLine(lineCurrent+1, styler))
				levelCurrent--;
		}
		if (foldPreprocessor) {
			if (style == SCE_DF_PREPROCESSOR) {
				iWordSize = ClassifyDataFlexPreprocessorFoldPoint(levelCurrent, lineFoldStateCurrent, i + 1, styler);
			//} else if (style == SCE_DF_PREPROCESSOR2 && ch == '(' && chNext == '*'
			//           && styler.SafeGetCharAt(i + 2) == '$') {
			//	ClassifyDataFlexPreprocessorFoldPoint(levelCurrent, lineFoldStateCurrent, i + 3, styler);
				i = i + iWordSize;
			}
		}

		if (stylePrev != SCE_DF_SCOPEWORD && style == SCE_DF_SCOPEWORD)
		{
			// Store last word start point.
			lastStart = i;
		}
		if (stylePrev == SCE_DF_SCOPEWORD) {
			if(setWord.Contains(ch) && !setWord.Contains(chNext)) {
				ClassifyDataFlexWordFoldPoint(levelCurrent, lineFoldStateCurrent, lastStart, i, keywordlists, styler);
			}
		}

		if (stylePrev == SCE_DF_METATAG && ch == '#')
		{
			// Store last word start point.
			lastStart = i;
		}
		if (stylePrev == SCE_DF_METATAG) {
			if(setWord.Contains(ch) && !setWord.Contains(chNext)) {
				ClassifyDataFlexMetaDataFoldPoint(levelCurrent, lastStart, i, keywordlists, styler);
			}
		}

		if (!IsASpace(ch))
			visibleChars++;

		if (atEOL) {
			int lev = levelPrev;
			if (visibleChars == 0 && foldCompact)
				lev |= SC_FOLDLEVELWHITEFLAG;
			if ((levelCurrent > levelPrev) && (visibleChars > 0))
				lev |= SC_FOLDLEVELHEADERFLAG;
			if (lev != styler.LevelAt(lineCurrent)) {
				styler.SetLevel(lineCurrent, lev);
			}
			int newLineState = (styler.GetLineState(lineCurrent) & ~stateFoldMaskAll) | lineFoldStateCurrent;
			styler.SetLineState(lineCurrent, newLineState);
			lineCurrent++;
			levelPrev = levelCurrent;
			visibleChars = 0;
		}
	}

	// If we didn't reach the EOL in previous loop, store line level and whitespace information.
	// The rest will be filled in later...
	int lev = levelPrev;
	if (visibleChars == 0 && foldCompact)
		lev |= SC_FOLDLEVELWHITEFLAG;
	styler.SetLevel(lineCurrent, lev);
}

static const char * const dataflexWordListDesc[] = {
	"Keywords",
	"Scope open",
	"Scope close",
	"Operators",
	0
};

LexerModule lmDataflex(SCLEX_DATAFLEX, ColouriseDataFlexDoc, "dataflex", FoldDataFlexDoc, dataflexWordListDesc);