Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[2aad87]: lexers / LexSTTXT.cxx Maximize Restore History

Download this file

LexSTTXT.cxx    402 lines (359 with data), 10.7 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
// Scintilla source code edit control
/** @file LexSTTXT.cxx
** Lexer for Structured Text language.
** Written by Pavel Bulochkin
**/
// 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
static void ClassifySTTXTWord(WordList *keywordlists[], StyleContext &sc)
{
char s[256] = { 0 };
sc.GetCurrentLowered(s, sizeof(s));
if ((*keywordlists[0]).InList(s)) {
sc.ChangeState(SCE_STTXT_KEYWORD);
}
else if ((*keywordlists[1]).InList(s)) {
sc.ChangeState(SCE_STTXT_TYPE);
}
else if ((*keywordlists[2]).InList(s)) {
sc.ChangeState(SCE_STTXT_FUNCTION);
}
else if ((*keywordlists[3]).InList(s)) {
sc.ChangeState(SCE_STTXT_FB);
}
else if ((*keywordlists[4]).InList(s)) {
sc.ChangeState(SCE_STTXT_VARS);
}
else if ((*keywordlists[5]).InList(s)) {
sc.ChangeState(SCE_STTXT_PRAGMAS);
}
sc.SetState(SCE_STTXT_DEFAULT);
}
static void ColouriseSTTXTDoc (unsigned int startPos, int length, int initStyle,
WordList *keywordlists[], Accessor &styler)
{
StyleContext sc(startPos, length, initStyle, styler);
CharacterSet setWord(CharacterSet::setAlphaNum, "_", 0x80, true);
CharacterSet setWordStart(CharacterSet::setAlpha, "_", 0x80, true);
CharacterSet setNumber(CharacterSet::setDigits, "_.eE");
CharacterSet setHexNumber(CharacterSet::setDigits, "_abcdefABCDEF");
CharacterSet setOperator(CharacterSet::setNone,",.+-*/:;<=>[]()%&");
CharacterSet setDataTime(CharacterSet::setDigits,"_.-:dmshDMSH");
for ( ; sc.More() ; sc.Forward())
{
if(sc.atLineStart && sc.state != SCE_STTXT_COMMENT)
sc.SetState(SCE_STTXT_DEFAULT);
switch(sc.state)
{
case SCE_STTXT_NUMBER: {
if(!setNumber.Contains(sc.ch))
sc.SetState(SCE_STTXT_DEFAULT);
break;
}
case SCE_STTXT_HEXNUMBER: {
if(setHexNumber.Contains(sc.ch))
continue;
else if(setDataTime.Contains(sc.ch))
sc.SetState(SCE_STTXT_DATETIME);
else
sc.SetState(SCE_STTXT_DEFAULT);
break;
}
case SCE_STTXT_DATETIME: {
if(!setDataTime.Contains(sc.ch))
sc.SetState(SCE_STTXT_DEFAULT);
break;
}
case SCE_STTXT_OPERATOR: {
sc.SetState(SCE_STTXT_DEFAULT);
break;
}
case SCE_STTXT_PRAGMA: {
if (sc.ch == '}')
sc.ForwardSetState(SCE_STTXT_DEFAULT);
break;
}
case SCE_STTXT_COMMENTLINE: {
if (sc.atLineStart)
sc.SetState(SCE_STTXT_DEFAULT);
break;
}
case SCE_STTXT_COMMENT: {
if(sc.Match('*',')'))
{
sc.Forward();
sc.ForwardSetState(SCE_STTXT_DEFAULT);
}
break;
}
case SCE_STTXT_STRING1: {
if(sc.atLineEnd)
sc.SetState(SCE_STTXT_STRINGEOL);
else if(sc.ch == '\'' && sc.chPrev != '$')
sc.ForwardSetState(SCE_STTXT_DEFAULT);
break;
}
case SCE_STTXT_STRING2: {
if (sc.atLineEnd)
sc.SetState(SCE_STTXT_STRINGEOL);
else if(sc.ch == '\"' && sc.chPrev != '$')
sc.ForwardSetState(SCE_STTXT_DEFAULT);
break;
}
case SCE_STTXT_STRINGEOL: {
if(sc.atLineStart)
sc.SetState(SCE_STTXT_DEFAULT);
break;
}
case SCE_STTXT_CHARACTER: {
if(setHexNumber.Contains(sc.ch))
sc.SetState(SCE_STTXT_HEXNUMBER);
else if(setDataTime.Contains(sc.ch))
sc.SetState(SCE_STTXT_DATETIME);
else sc.SetState(SCE_STTXT_DEFAULT);
break;
}
case SCE_STTXT_IDENTIFIER: {
if(!setWord.Contains(sc.ch))
ClassifySTTXTWord(keywordlists, sc);
break;
}
}
if(sc.state == SCE_STTXT_DEFAULT)
{
if(IsADigit(sc.ch))
sc.SetState(SCE_STTXT_NUMBER);
else if (setWordStart.Contains(sc.ch))
sc.SetState(SCE_STTXT_IDENTIFIER);
else if (sc.Match('/', '/'))
sc.SetState(SCE_STTXT_COMMENTLINE);
else if(sc.Match('(', '*'))
sc.SetState(SCE_STTXT_COMMENT);
else if (sc.ch == '{')
sc.SetState(SCE_STTXT_PRAGMA);
else if (sc.ch == '\'')
sc.SetState(SCE_STTXT_STRING1);
else if (sc.ch == '\"')
sc.SetState(SCE_STTXT_STRING2);
else if(sc.ch == '#')
sc.SetState(SCE_STTXT_CHARACTER);
else if (setOperator.Contains(sc.ch))
sc.SetState(SCE_STTXT_OPERATOR);
}
}
if (sc.state == SCE_STTXT_IDENTIFIER && setWord.Contains(sc.chPrev))
ClassifySTTXTWord(keywordlists, sc);
sc.Complete();
}
static const char * const STTXTWordListDesc[] = {
"Keywords",
"Types",
"Functions",
"FB",
"Local_Var",
"Local_Pragma",
0
};
static bool IsCommentLine(int line, Accessor &styler, bool type)
{
int pos = styler.LineStart(line);
int eolPos = styler.LineStart(line + 1) - 1;
for (int i = pos; i < eolPos; i++)
{
char ch = styler[i];
char chNext = styler.SafeGetCharAt(i + 1);
int style = styler.StyleAt(i);
if(type) {
if (ch == '/' && chNext == '/' && style == SCE_STTXT_COMMENTLINE)
return true;
}
else if (ch == '(' && chNext == '*' && style == SCE_STTXT_COMMENT)
break;
if (!IsASpaceOrTab(ch))
return false;
}
for (int i = eolPos-2; i>pos; i--)
{
char ch = styler[i];
char chPrev = styler.SafeGetCharAt(i-1);
int style = styler.StyleAt(i);
if(ch == ')' && chPrev == '*' && style == SCE_STTXT_COMMENT)
return true;
if(!IsASpaceOrTab(ch))
return false;
}
return false;
}
static bool IsPragmaLine(int line, Accessor &styler)
{
int pos = styler.LineStart(line);
int eolPos = styler.LineStart(line+1) - 1;
for (int i = pos ; i < eolPos ; i++)
{
char ch = styler[i];
int style = styler.StyleAt(i);
if(ch == '{' && style == SCE_STTXT_PRAGMA)
return true;
else if (!IsASpaceOrTab(ch))
return false;
}
return false;
}
static void GetRangeUpper(unsigned int start,unsigned int end,Accessor &styler,char *s,unsigned int len)
{
unsigned int i = 0;
while ((i < end - start + 1) && (i < len-1)) {
s[i] = static_cast<char>(toupper(styler[start + i]));
i++;
}
s[i] = '\0';
}
static void ClassifySTTXTWordFoldPoint(int &levelCurrent,unsigned int lastStart,
unsigned int currentPos, Accessor &styler)
{
char s[256];
GetRangeUpper(lastStart, currentPos, styler, s, sizeof(s));
// See Table C.2 - Keywords
if (!strcmp(s, "ACTION") ||
!strcmp(s, "CASE") ||
!strcmp(s, "CONFIGURATION") ||
!strcmp(s, "FOR") ||
!strcmp(s, "FUNCTION") ||
!strcmp(s, "FUNCTION_BLOCK") ||
!strcmp(s, "IF") ||
!strcmp(s, "INITIAL_STEP") ||
!strcmp(s, "REPEAT") ||
!strcmp(s, "RESOURCE") ||
!strcmp(s, "STEP") ||
!strcmp(s, "STRUCT") ||
!strcmp(s, "TRANSITION") ||
!strcmp(s, "TYPE") ||
!strcmp(s, "VAR") ||
!strcmp(s, "VAR_INPUT") ||
!strcmp(s, "VAR_OUTPUT") ||
!strcmp(s, "VAR_IN_OUT") ||
!strcmp(s, "VAR_TEMP") ||
!strcmp(s, "VAR_EXTERNAL") ||
!strcmp(s, "VAR_ACCESS") ||
!strcmp(s, "VAR_CONFIG") ||
!strcmp(s, "VAR_GLOBAL") ||
!strcmp(s, "WHILE"))
{
levelCurrent++;
}
else if (!strcmp(s, "END_ACTION") ||
!strcmp(s, "END_CASE") ||
!strcmp(s, "END_CONFIGURATION") ||
!strcmp(s, "END_FOR") ||
!strcmp(s, "END_FUNCTION") ||
!strcmp(s, "END_FUNCTION_BLOCK") ||
!strcmp(s, "END_IF") ||
!strcmp(s, "END_REPEAT") ||
!strcmp(s, "END_RESOURCE") ||
!strcmp(s, "END_STEP") ||
!strcmp(s, "END_STRUCT") ||
!strcmp(s, "END_TRANSITION") ||
!strcmp(s, "END_TYPE") ||
!strcmp(s, "END_VAR") ||
!strcmp(s, "END_WHILE"))
{
levelCurrent--;
if (levelCurrent < SC_FOLDLEVELBASE) {
levelCurrent = SC_FOLDLEVELBASE;
}
}
}
static void FoldSTTXTDoc(unsigned int startPos, int length, int initStyle, WordList *[],Accessor &styler)
{
bool foldComment = styler.GetPropertyInt("fold.comment") != 0;
bool foldPreprocessor = styler.GetPropertyInt("fold.preprocessor") != 0;
bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
unsigned int endPos = startPos + length;
int visibleChars = 0;
int lineCurrent = styler.GetLine(startPos);
int levelPrev = styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK;
int levelCurrent = levelPrev;
char chNext = styler[startPos];
int styleNext = styler.StyleAt(startPos);
int style = initStyle;
int lastStart = 0;
CharacterSet setWord(CharacterSet::setAlphaNum, "_", 0x80, true);
for (unsigned int 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 && style == SCE_STTXT_COMMENT) {
if(stylePrev != SCE_STTXT_COMMENT)
levelCurrent++;
else if(styleNext != SCE_STTXT_COMMENT && !atEOL)
levelCurrent--;
}
if ( foldComment && atEOL && ( IsCommentLine(lineCurrent, styler,false)
|| IsCommentLine(lineCurrent,styler,true))) {
if(!IsCommentLine(lineCurrent-1, styler,true) && IsCommentLine(lineCurrent+1, styler,true))
levelCurrent++;
if (IsCommentLine(lineCurrent-1, styler,true) && !IsCommentLine(lineCurrent+1, styler,true))
levelCurrent--;
if (!IsCommentLine(lineCurrent-1, styler,false) && IsCommentLine(lineCurrent+1, styler,false))
levelCurrent++;
if (IsCommentLine(lineCurrent-1, styler,false) && !IsCommentLine(lineCurrent+1, styler,false))
levelCurrent--;
}
if(foldPreprocessor && atEOL && IsPragmaLine(lineCurrent, styler)) {
if(!IsPragmaLine(lineCurrent-1, styler) && IsPragmaLine(lineCurrent+1, styler ))
levelCurrent++;
else if(IsPragmaLine(lineCurrent-1, styler) && !IsPragmaLine(lineCurrent+1, styler))
levelCurrent--;
}
if (stylePrev != SCE_STTXT_KEYWORD && style == SCE_STTXT_KEYWORD) {
lastStart = i;
}
if(stylePrev == SCE_STTXT_KEYWORD) {
if(setWord.Contains(ch) && !setWord.Contains(chNext))
ClassifySTTXTWordFoldPoint(levelCurrent,lastStart, i, 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);
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);
}
}
LexerModule lmSTTXT(SCLEX_STTXT, ColouriseSTTXTDoc, "fcST", FoldSTTXTDoc, STTXTWordListDesc);