[c54f87]: lexers / LexVisualProlog.cxx  Maximize  Restore  History

Download this file

505 lines (466 with data), 17.2 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
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
// Scintilla source code edit control
/** @file LexVisualProlog.cxx
** Lexer for Visual Prolog.
**/
// Author Thomas Linder Puls, Prolog Development Denter A/S, http://www.visual-prolog.com
// Based on Lexer for C++, C, Java, and JavaScript.
// Copyright 1998-2005 by Neil Hodgson <neilh@scintilla.org>
// The License.txt file describes the conditions under which this software may be distributed.
// The line state contains:
// In SCE_VISUALPROLOG_STRING_VERBATIM_EOL (i.e. multiline string literal): The closingQuote.
// else (for SCE_VISUALPROLOG_COMMENT_BLOCK): The comment nesting level
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <stdarg.h>
#include <assert.h>
#ifdef _MSC_VER
#pragma warning(disable: 4786)
#endif
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#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 "CharacterCategory.h"
#include "LexerModule.h"
#include "OptionSet.h"
#ifdef SCI_NAMESPACE
using namespace Scintilla;
#endif
// Options used for LexerVisualProlog
struct OptionsVisualProlog {
OptionsVisualProlog() {
}
};
static const char *const visualPrologWordLists[] = {
"Major keywords (class, predicates, ...)",
"Minor keywords (if, then, try, ...)",
"Directive keywords without the '#' (include, requires, ...)",
"Documentation keywords without the '@' (short, detail, ...)",
0,
};
struct OptionSetVisualProlog : public OptionSet<OptionsVisualProlog> {
OptionSetVisualProlog() {
DefineWordListSets(visualPrologWordLists);
}
};
class LexerVisualProlog : public ILexer {
WordList majorKeywords;
WordList minorKeywords;
WordList directiveKeywords;
WordList docKeywords;
OptionsVisualProlog options;
OptionSetVisualProlog osVisualProlog;
public:
LexerVisualProlog() {
}
virtual ~LexerVisualProlog() {
}
void SCI_METHOD Release() {
delete this;
}
int SCI_METHOD Version() const {
return lvOriginal;
}
const char * SCI_METHOD PropertyNames() {
return osVisualProlog.PropertyNames();
}
int SCI_METHOD PropertyType(const char *name) {
return osVisualProlog.PropertyType(name);
}
const char * SCI_METHOD DescribeProperty(const char *name) {
return osVisualProlog.DescribeProperty(name);
}
int SCI_METHOD PropertySet(const char *key, const char *val);
const char * SCI_METHOD DescribeWordListSets() {
return osVisualProlog.DescribeWordListSets();
}
int SCI_METHOD WordListSet(int n, const char *wl);
void SCI_METHOD Lex(unsigned int startPos, int length, int initStyle, IDocument *pAccess);
void SCI_METHOD Fold(unsigned int startPos, int length, int initStyle, IDocument *pAccess);
void * SCI_METHOD PrivateCall(int, void *) {
return 0;
}
static ILexer *LexerFactoryVisualProlog() {
return new LexerVisualProlog();
}
};
int SCI_METHOD LexerVisualProlog::PropertySet(const char *key, const char *val) {
if (osVisualProlog.PropertySet(&options, key, val)) {
return 0;
}
return -1;
}
int SCI_METHOD LexerVisualProlog::WordListSet(int n, const char *wl) {
WordList *wordListN = 0;
switch (n) {
case 0:
wordListN = &majorKeywords;
break;
case 1:
wordListN = &minorKeywords;
break;
case 2:
wordListN = &directiveKeywords;
break;
case 3:
wordListN = &docKeywords;
break;
}
int firstModification = -1;
if (wordListN) {
WordList wlNew;
wlNew.Set(wl);
if (*wordListN != wlNew) {
wordListN->Set(wl);
firstModification = 0;
}
}
return firstModification;
}
// Functor used to truncate history
struct After {
int line;
After(int line_) : line(line_) {}
};
static bool isLowerLetter(int ch){
return ccLl == CategoriseCharacter(ch);
}
static bool isUpperLetter(int ch){
return ccLu == CategoriseCharacter(ch);
}
static bool isAlphaNum(int ch){
CharacterCategory cc = CategoriseCharacter(ch);
return (ccLu == cc || ccLl == cc || ccLt == cc || ccLm == cc || ccLo == cc || ccNd == cc || ccNl == cc || ccNo == cc);
}
static bool isIdChar(int ch){
return ('_') == ch || isAlphaNum(ch);
}
static bool isOpenStringVerbatim(int next, int &closingQuote){
switch (next) {
case L'<':
closingQuote = L'>';
return true;
case L'>':
closingQuote = L'<';
return true;
case L'(':
closingQuote = L')';
return true;
case L')':
closingQuote = L'(';
return true;
case L'[':
closingQuote = L']';
return true;
case L']':
closingQuote = L'[';
return true;
case L'{':
closingQuote = L'}';
return true;
case L'}':
closingQuote = L'{';
return true;
case L'_':
case L'.':
case L',':
case L';':
return false;
default:
if (isAlphaNum(next)) {
return false;
} else {
closingQuote = next;
return true;
}
}
}
// Look ahead to see which colour "end" should have (takes colour after the following keyword)
static void endLookAhead(char s[], LexAccessor &styler, int start) {
char ch = styler.SafeGetCharAt(start, '\n');
while (' ' == ch) {
start++;
ch = styler.SafeGetCharAt(start, '\n');
}
int i = 0;
while (i < 100 && isLowerLetter(ch)){
s[i] = ch;
i++;
ch = styler.SafeGetCharAt(start + i, '\n');
}
s[i] = '\0';
}
static void forwardEscapeLiteral(StyleContext &sc, int EscapeState) {
sc.Forward();
if (sc.Match('"') || sc.Match('\'') || sc.Match('\\') || sc.Match('n') || sc.Match('l') || sc.Match('r') || sc.Match('t')) {
sc.ChangeState(EscapeState);
} else if (sc.Match('u')) {
if (IsADigit(sc.chNext, 16)) {
sc.Forward();
if (IsADigit(sc.chNext, 16)) {
sc.Forward();
if (IsADigit(sc.chNext, 16)) {
sc.Forward();
if (IsADigit(sc.chNext, 16)) {
sc.Forward();
sc.ChangeState(EscapeState);
}
}
}
}
}
}
void SCI_METHOD LexerVisualProlog::Lex(unsigned int startPos, int length, int initStyle, IDocument *pAccess) {
LexAccessor styler(pAccess);
CharacterSet setDoxygen(CharacterSet::setAlpha, "");
CharacterSet setNumber(CharacterSet::setNone, "+-.0123456789abcdefABCDEFxoXO");
StyleContext sc(startPos, length, initStyle, styler, 0x7f);
int styleBeforeDocKeyword = SCE_VISUALPROLOG_DEFAULT;
int currentLine = styler.GetLine(startPos);
int closingQuote = '"';
int nestLevel = 0;
if (currentLine >= 1)
{
nestLevel = styler.GetLineState(currentLine - 1);
closingQuote = nestLevel;
}
// Truncate ppDefineHistory before current line
for (; sc.More(); sc.Forward()) {
// Determine if the current state should terminate.
switch (sc.state) {
case SCE_VISUALPROLOG_OPERATOR:
sc.SetState(SCE_VISUALPROLOG_DEFAULT);
break;
case SCE_VISUALPROLOG_NUMBER:
// We accept almost anything because of hex. and number suffixes
if (!(setNumber.Contains(sc.ch))) {
sc.SetState(SCE_VISUALPROLOG_DEFAULT);
}
break;
case SCE_VISUALPROLOG_IDENTIFIER:
if (!isIdChar(sc.ch)) {
char s[1000];
sc.GetCurrent(s, sizeof(s));
if (0 == strcmp(s, "end")) {
endLookAhead(s, styler, sc.currentPos);
}
if (majorKeywords.InList(s)) {
sc.ChangeState(SCE_VISUALPROLOG_KEY_MAJOR);
} else if (minorKeywords.InList(s)) {
sc.ChangeState(SCE_VISUALPROLOG_KEY_MINOR);
}
sc.SetState(SCE_VISUALPROLOG_DEFAULT);
}
break;
case SCE_VISUALPROLOG_VARIABLE:
case SCE_VISUALPROLOG_ANONYMOUS:
if (!isIdChar(sc.ch)) {
sc.SetState(SCE_VISUALPROLOG_DEFAULT);
}
break;
case SCE_VISUALPROLOG_KEY_DIRECTIVE:
if (!isLowerLetter(sc.ch)) {
char s[1000];
sc.GetCurrent(s, sizeof(s));
if (!directiveKeywords.InList(s+1)) {
sc.ChangeState(SCE_VISUALPROLOG_IDENTIFIER);
}
sc.SetState(SCE_VISUALPROLOG_DEFAULT);
}
break;
case SCE_VISUALPROLOG_COMMENT_BLOCK:
if (sc.Match('*', '/')) {
sc.Forward();
nestLevel--;
int nextState = (nestLevel == 0) ? SCE_VISUALPROLOG_DEFAULT : SCE_VISUALPROLOG_COMMENT_BLOCK;
sc.ForwardSetState(nextState);
} else if (sc.Match('/', '*')) {
sc.Forward();
nestLevel++;
} else if (sc.Match('@')) {
styleBeforeDocKeyword = sc.state;
sc.SetState(SCE_VISUALPROLOG_COMMENT_KEY_ERROR);
}
break;
case SCE_VISUALPROLOG_COMMENT_LINE:
if (sc.atLineEnd) {
int nextState = (nestLevel == 0) ? SCE_VISUALPROLOG_DEFAULT : SCE_VISUALPROLOG_COMMENT_BLOCK;
sc.SetState(nextState);
} else if (sc.Match('@')) {
styleBeforeDocKeyword = sc.state;
sc.SetState(SCE_VISUALPROLOG_COMMENT_KEY_ERROR);
}
break;
case SCE_VISUALPROLOG_COMMENT_KEY_ERROR:
if (!setDoxygen.Contains(sc.ch) || sc.atLineEnd) {
char s[1000];
sc.GetCurrent(s, sizeof(s));
if (docKeywords.InList(s+1)) {
sc.ChangeState(SCE_VISUALPROLOG_COMMENT_KEY);
}
if (SCE_VISUALPROLOG_COMMENT_LINE == styleBeforeDocKeyword && sc.atLineEnd) {
// end line comment
int nextState = (nestLevel == 0) ? SCE_VISUALPROLOG_DEFAULT : SCE_VISUALPROLOG_COMMENT_BLOCK;
sc.SetState(nextState);
} else {
sc.SetState(styleBeforeDocKeyword);
if (SCE_VISUALPROLOG_COMMENT_BLOCK == styleBeforeDocKeyword && sc.Match('*', '/')) {
// we have consumed the '*' if it comes immediately after the docKeyword
sc.Forward();
sc.Forward();
nestLevel--;
if (0 == nestLevel) {
sc.SetState(SCE_VISUALPROLOG_DEFAULT);
}
}
}
}
break;
case SCE_VISUALPROLOG_STRING_ESCAPE:
case SCE_VISUALPROLOG_STRING_ESCAPE_ERROR:
// return to SCE_VISUALPROLOG_STRING and treat as such (fall-through)
sc.SetState(SCE_VISUALPROLOG_STRING);
case SCE_VISUALPROLOG_STRING:
if (sc.atLineEnd) {
sc.SetState(SCE_VISUALPROLOG_STRING_EOL_OPEN);
} else if (sc.Match(closingQuote)) {
sc.ForwardSetState(SCE_VISUALPROLOG_DEFAULT);
} else if (sc.Match('\\')) {
sc.SetState(SCE_VISUALPROLOG_STRING_ESCAPE_ERROR);
forwardEscapeLiteral(sc, SCE_VISUALPROLOG_STRING_ESCAPE);
}
break;
case SCE_VISUALPROLOG_STRING_EOL_OPEN:
if (sc.atLineStart) {
sc.SetState(SCE_VISUALPROLOG_DEFAULT);
}
break;
case SCE_VISUALPROLOG_STRING_VERBATIM_SPECIAL:
case SCE_VISUALPROLOG_STRING_VERBATIM_EOL:
// return to SCE_VISUALPROLOG_STRING_VERBATIM and treat as such (fall-through)
sc.SetState(SCE_VISUALPROLOG_STRING_VERBATIM);
case SCE_VISUALPROLOG_STRING_VERBATIM:
if (sc.atLineEnd) {
sc.SetState(SCE_VISUALPROLOG_STRING_VERBATIM_EOL);
} else if (sc.Match(closingQuote)) {
if (closingQuote == sc.chNext) {
sc.SetState(SCE_VISUALPROLOG_STRING_VERBATIM_SPECIAL);
sc.Forward();
} else {
sc.ForwardSetState(SCE_VISUALPROLOG_DEFAULT);
}
}
break;
}
if (sc.atLineEnd) {
// Update the line state, so it can be seen by next line
int lineState = 0;
if (SCE_VISUALPROLOG_STRING_VERBATIM_EOL == sc.state) {
lineState = closingQuote;
} else if (SCE_VISUALPROLOG_COMMENT_BLOCK == sc.state) {
lineState = nestLevel;
}
styler.SetLineState(currentLine, lineState);
currentLine++;
}
// Determine if a new state should be entered.
if (sc.state == SCE_VISUALPROLOG_DEFAULT) {
if (sc.Match('@') && isOpenStringVerbatim(sc.chNext, closingQuote)) {
sc.SetState(SCE_VISUALPROLOG_STRING_VERBATIM);
sc.Forward();
} else if (IsADigit(sc.ch) || (sc.Match('.') && IsADigit(sc.chNext))) {
sc.SetState(SCE_VISUALPROLOG_NUMBER);
} else if (isLowerLetter(sc.ch)) {
sc.SetState(SCE_VISUALPROLOG_IDENTIFIER);
} else if (isUpperLetter(sc.ch)) {
sc.SetState(SCE_VISUALPROLOG_VARIABLE);
} else if (sc.Match('_')) {
sc.SetState(SCE_VISUALPROLOG_ANONYMOUS);
} else if (sc.Match('/', '*')) {
sc.SetState(SCE_VISUALPROLOG_COMMENT_BLOCK);
nestLevel = 1;
sc.Forward(); // Eat the * so it isn't used for the end of the comment
} else if (sc.Match('%')) {
sc.SetState(SCE_VISUALPROLOG_COMMENT_LINE);
} else if (sc.Match('\'')) {
closingQuote = '\'';
sc.SetState(SCE_VISUALPROLOG_STRING);
} else if (sc.Match('"')) {
closingQuote = '"';
sc.SetState(SCE_VISUALPROLOG_STRING);
} else if (sc.Match('#')) {
sc.SetState(SCE_VISUALPROLOG_KEY_DIRECTIVE);
} else if (isoperator(static_cast<char>(sc.ch)) || sc.Match('\\')) {
sc.SetState(SCE_VISUALPROLOG_OPERATOR);
}
}
}
sc.Complete();
styler.Flush();
}
// Store both the current line's fold level and the next lines in the
// level store to make it easy to pick up with each increment
// and to make it possible to fiddle the current level for "} else {".
void SCI_METHOD LexerVisualProlog::Fold(unsigned int startPos, int length, int initStyle, IDocument *pAccess) {
LexAccessor styler(pAccess);
unsigned int endPos = startPos + length;
int visibleChars = 0;
int currentLine = styler.GetLine(startPos);
int levelCurrent = SC_FOLDLEVELBASE;
if (currentLine > 0)
levelCurrent = styler.LevelAt(currentLine-1) >> 16;
int levelMinCurrent = levelCurrent;
int levelNext = levelCurrent;
char chNext = styler[startPos];
int styleNext = styler.StyleAt(startPos);
int style = initStyle;
for (unsigned int i = startPos; i < endPos; i++) {
char ch = chNext;
chNext = styler.SafeGetCharAt(i + 1);
style = styleNext;
styleNext = styler.StyleAt(i + 1);
bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
if (style == SCE_VISUALPROLOG_OPERATOR) {
if (ch == '{') {
// Measure the minimum before a '{' to allow
// folding on "} else {"
if (levelMinCurrent > levelNext) {
levelMinCurrent = levelNext;
}
levelNext++;
} else if (ch == '}') {
levelNext--;
}
}
if (!IsASpace(ch))
visibleChars++;
if (atEOL || (i == endPos-1)) {
int levelUse = levelCurrent;
int lev = levelUse | levelNext << 16;
if (levelUse < levelNext)
lev |= SC_FOLDLEVELHEADERFLAG;
if (lev != styler.LevelAt(currentLine)) {
styler.SetLevel(currentLine, lev);
}
currentLine++;
levelCurrent = levelNext;
levelMinCurrent = levelCurrent;
if (atEOL && (i == static_cast<unsigned int>(styler.Length()-1))) {
// There is an empty line at end of file so give it same level and empty
styler.SetLevel(currentLine, (levelCurrent | levelCurrent << 16) | SC_FOLDLEVELWHITEFLAG);
}
visibleChars = 0;
}
}
}
LexerModule lmVisualProlog(SCLEX_VISUALPROLOG, LexerVisualProlog::LexerFactoryVisualProlog, "visualprolog", visualPrologWordLists);

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks