Debian Codegraph @

scintilla/lexilla/lexers/LexVisualProlog.cxx

raw
1 // Scintilla source code edit control
2 /** @file LexVisualProlog.cxx
3 ** Lexer for Visual Prolog.
4 **/
5 // Author Thomas Linder Puls, PDC A/S, http://www.visual-prolog.com
6 // Based on Lexer for C++, C, Java, and JavaScript.
7 // Copyright 1998-2005 by Neil Hodgson <[email protected]>
8 // The License.txt file describes the conditions under which this software may be distributed.
9
10 // The line state contains:
11 // In SCE_VISUALPROLOG_STRING: The closing quote and information about verbatim string.
12 // and a stack of nesting kinds: comment, embedded (syntax) and (syntax) place holder
13
14 #include <stdlib.h>
15 #include <string.h>
16 #include <stdio.h>
17 #include <stdarg.h>
18 #include <assert.h>
19 #include <ctype.h>
20
21 #ifdef _MSC_VER
22 #pragma warning(disable: 4786)
23 #endif
24
25 #include <string>
26 #include <string_view>
27 #include <vector>
28 #include <map>
29 #include <algorithm>
30 #include <functional>
31
32 #include "ILexer.h"
33 #include "Scintilla.h"
34 #include "SciLexer.h"
35
36 #include "WordList.h"
37 #include "LexAccessor.h"
38 #include "Accessor.h"
39 #include "StyleContext.h"
40 #include "CharacterSet.h"
41 #include "CharacterCategory.h"
42 #include "LexerModule.h"
43 #include "OptionSet.h"
44 #include "DefaultLexer.h"
45
46 using namespace Scintilla;
47 using namespace Lexilla;
48
49 namespace {
50 // Options used for LexerVisualProlog
51 struct OptionsVisualProlog {
52 bool verbatimStrings;
53 bool backQuotedStrings;
54 OptionsVisualProlog() {
55 verbatimStrings = true;
56 backQuotedStrings = false;
57 }
58 };
59
60 static const char* const visualPrologWordLists[] = {
61 "Major keywords (class, predicates, ...)",
62 "Minor keywords (if, then, try, ...)",
63 "Directive keywords without the '#' (include, requires, ...)",
64 "Documentation keywords without the '@' (short, detail, ...)",
65 0,
66 };
67
68 struct OptionSetVisualProlog : public OptionSet<OptionsVisualProlog> {
69 OptionSetVisualProlog() {
70 DefineProperty("lexer.visualprolog.verbatim.strings", &OptionsVisualProlog::verbatimStrings,
71 "Set to 0 to disable highlighting verbatim strings using '@'.");
72 DefineProperty("lexer.visualprolog.backquoted.strings", &OptionsVisualProlog::backQuotedStrings,
73 "Set to 1 to enable using back quotes (``) to delimit strings.");
74 DefineWordListSets(visualPrologWordLists);
75 }
76 };
77
78 LexicalClass lexicalClasses[] = {
79 SCE_VISUALPROLOG_DEFAULT, "SCE_VISUALPROLOG_DEFAULT", "default", "Default style",
80 SCE_VISUALPROLOG_KEY_MAJOR, "SCE_VISUALPROLOG_KEY_MAJOR", "keyword major", "Major keyword",
81 SCE_VISUALPROLOG_KEY_MINOR, "SCE_VISUALPROLOG_KEY_MINOR", "keyword minor", "Minor keyword",
82 SCE_VISUALPROLOG_KEY_DIRECTIVE, "SCE_VISUALPROLOG_KEY_DIRECTIVE", "keyword preprocessor", "Directove keyword",
83 SCE_VISUALPROLOG_COMMENT_BLOCK, "SCE_VISUALPROLOG_COMMENT_BLOCK", "comment", "Multiline comment /* */",
84 SCE_VISUALPROLOG_COMMENT_LINE, "SCE_VISUALPROLOG_COMMENT_LINE", "comment line", "Line comment % ...",
85 SCE_VISUALPROLOG_COMMENT_KEY, "SCE_VISUALPROLOG_COMMENT_KEY", "comment documentation keyword", "Doc keyword in comment % @short ...",
86 SCE_VISUALPROLOG_COMMENT_KEY_ERROR, "SCE_VISUALPROLOG_COMMENT_KEY_ERROR", "comment", "A non recognized doc keyword % @qqq ...",
87 SCE_VISUALPROLOG_IDENTIFIER, "SCE_VISUALPROLOG_IDENTIFIER", "identifier", "Identifier (black)",
88 SCE_VISUALPROLOG_VARIABLE, "SCE_VISUALPROLOG_VARIABLE", "variable identifier", "Variable (green)",
89 SCE_VISUALPROLOG_ANONYMOUS, "SCE_VISUALPROLOG_ANONYMOUS", "variable anonymous identifier", "Anonymous Variable _XXX (dimmed green)",
90 SCE_VISUALPROLOG_NUMBER, "SCE_VISUALPROLOG_NUMBER", "numeric", "Number",
91 SCE_VISUALPROLOG_OPERATOR, "SCE_VISUALPROLOG_OPERATOR", "operator", "Operator",
92 SCE_VISUALPROLOG_STRING, "SCE_VISUALPROLOG_STRING", "literal string", "String literal",
93 SCE_VISUALPROLOG_STRING_QUOTE, "SCE_VISUALPROLOG_STRING_QUOTE", "literal string quote", "Quotes surrounding string literals",
94 SCE_VISUALPROLOG_STRING_ESCAPE, "SCE_VISUALPROLOG_STRING_ESCAPE", "literal string escapesequence", "Escape sequence in string literal",
95 SCE_VISUALPROLOG_STRING_ESCAPE_ERROR, "SCE_VISUALPROLOG_STRING_ESCAPE_ERROR", "error literal string escapesequence", "Error in escape sequence in string literal",
96 SCE_VISUALPROLOG_STRING_EOL, "SCE_VISUALPROLOG_STRING_EOL", "literal string multiline raw escapesequence", "Verbatim/multiline string literal EOL",
97 SCE_VISUALPROLOG_EMBEDDED, "SCE_VISUALPROLOG_EMBEDDED", "literal string embedded", "Embedded syntax [| ... |]",
98 SCE_VISUALPROLOG_PLACEHOLDER, "SCE_VISUALPROLOG_PLACEHOLDER", "operator embedded", "Syntax place holder {| ... |}:ident in embedded syntax"
99 };
100
101 LexicalClass getLexicalClass(int style) {
102 for (auto lc : lexicalClasses) {
103 if (style == lc.value) {
104 return lc;
105 }
106 }
107 return {style, "", "unused", ""};
108 }
109
110
111 class LexerVisualProlog : public DefaultLexer {
112 WordList majorKeywords;
113 WordList minorKeywords;
114 WordList directiveKeywords;
115 WordList docKeywords;
116 OptionsVisualProlog options;
117 OptionSetVisualProlog osVisualProlog;
118 public:
119 LexerVisualProlog() : DefaultLexer("visualprolog", SCLEX_VISUALPROLOG) {
120 }
121 virtual ~LexerVisualProlog() {
122 }
123 void SCI_METHOD Release() override {
124 delete this;
125 }
126 int SCI_METHOD Version() const override {
127 return lvRelease5;
128 }
129 const char* SCI_METHOD PropertyNames() override {
130 return osVisualProlog.PropertyNames();
131 }
132 int SCI_METHOD PropertyType(const char* name) override {
133 return osVisualProlog.PropertyType(name);
134 }
135 const char* SCI_METHOD DescribeProperty(const char* name) override {
136 return osVisualProlog.DescribeProperty(name);
137 }
138 Sci_Position SCI_METHOD PropertySet(const char* key, const char* val) override;
139 const char* SCI_METHOD PropertyGet(const char* key) override {
140 return osVisualProlog.PropertyGet(key);
141 }
142 const char* SCI_METHOD DescribeWordListSets() override {
143 return osVisualProlog.DescribeWordListSets();
144 }
145 Sci_Position SCI_METHOD WordListSet(int n, const char* wl) override;
146 void SCI_METHOD Lex(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument* pAccess) override;
147 void SCI_METHOD Fold(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument* pAccess) override;
148
149 void* SCI_METHOD PrivateCall(int, void*) override {
150 return 0;
151 }
152
153 int SCI_METHOD NamedStyles() override {
154 int namedStyles = 0;
155 for (auto lc : lexicalClasses) {
156 if (namedStyles < lc.value) {
157 namedStyles = lc.value;
158 }
159 }
160 return namedStyles;
161 }
162 const char* SCI_METHOD NameOfStyle(int style) override {
163 return getLexicalClass(style).name;
164 }
165 const char* SCI_METHOD TagsOfStyle(int style) override {
166 return getLexicalClass(style).tags;
167 }
168 const char* SCI_METHOD DescriptionOfStyle(int style) override {
169 return getLexicalClass(style).description;
170 }
171
172 static ILexer5* LexerFactoryVisualProlog() {
173 return new LexerVisualProlog();
174 }
175 };
176
177 Sci_Position SCI_METHOD LexerVisualProlog::PropertySet(const char* key, const char* val) {
178 if (osVisualProlog.PropertySet(&options, key, val)) {
179 return 0;
180 }
181 return -1;
182 }
183
184 Sci_Position SCI_METHOD LexerVisualProlog::WordListSet(int n, const char* wl) {
185 WordList* wordListN = 0;
186 switch (n) {
187 case 0:
188 wordListN = &majorKeywords;
189 break;
190 case 1:
191 wordListN = &minorKeywords;
192 break;
193 case 2:
194 wordListN = &directiveKeywords;
195 break;
196 case 3:
197 wordListN = &docKeywords;
198 break;
199 }
200 Sci_Position firstModification = -1;
201 if (wordListN) {
202 WordList wlNew;
203 wlNew.Set(wl);
204 if (*wordListN != wlNew) {
205 wordListN->Set(wl);
206 firstModification = 0;
207 }
208 }
209 return firstModification;
210 }
211
212 static bool isLowerLetter(int ch) {
213 return ccLl == CategoriseCharacter(ch);
214 }
215
216 static bool isUpperLetter(int ch) {
217 return ccLu == CategoriseCharacter(ch);
218 }
219
220 static bool isAlphaNum(int ch) {
221 CharacterCategory cc = CategoriseCharacter(ch);
222 return (ccLu == cc || ccLl == cc || ccLt == cc || ccLm == cc || ccLo == cc || ccNd == cc || ccNl == cc || ccNo == cc);
223 }
224
225 static bool isStringVerbatimOpenClose(int ch) {
226 CharacterCategory cc = CategoriseCharacter(ch);
227 return (ccPc <= cc && cc <= ccSo);
228 }
229
230 static bool isIdChar(int ch) {
231 return ('_') == ch || isAlphaNum(ch);
232 }
233
234 // Look ahead to see which colour "end" should have (takes colour after the following keyword)
235 static void endLookAhead(char s[], LexAccessor& styler, Sci_Position start) {
236 char ch = styler.SafeGetCharAt(start, '\n');
237 while (' ' == ch) {
238 start++;
239 ch = styler.SafeGetCharAt(start, '\n');
240 }
241 Sci_Position i = 0;
242 while (i < 100 && isLowerLetter(ch)) {
243 s[i] = ch;
244 i++;
245 ch = styler.SafeGetCharAt(start + i, '\n');
246 }
247 s[i] = '\0';
248 }
249
250
251 class lineState {
252 public:
253 bool verbatim = false;
254 int closingQuote = 0;
255 int kindStack = 0;
256
257 bool isOpenStringVerbatim(int next) {
258 if (next > 0x7FFF) {
259 return false;
260 }
261 switch (next) {
262 case L'<':
263 closingQuote = L'>';
264 return true;
265 case L'>':
266 closingQuote = L'<';
267 return true;
268 case L'(':
269 closingQuote = L')';
270 return true;
271 case L')':
272 closingQuote = L'(';
273 return true;
274 case L'[':
275 closingQuote = L']';
276 return true;
277 case L']':
278 closingQuote = L'[';
279 return true;
280 case L'{':
281 closingQuote = L'}';
282 return true;
283 case L'}':
284 closingQuote = L'{';
285 return true;
286 case L'_':
287 case L'.':
288 case L',':
289 case L';':
290 return false;
291 default:
292 if (isStringVerbatimOpenClose(next)) {
293 closingQuote = next;
294 return true;
295 } else {
296 return false;
297 }
298 }
299 }
300
301 enum kind {
302 none = 0,
303 comment = 1,
304 embedded = 2,
305 placeholder = 3
306 };
307
308 void setState(int state) {
309 verbatim = state >> 31;
310 closingQuote = state >> 16 & 0x7FFF;
311 kindStack = state & 0xFFFF;
312 }
313
314 int getState() {
315 return verbatim << 31 | closingQuote << 16 | (kindStack & 0xFFFF);
316 }
317
318 void enter(kind k) {
319 kindStack = kindStack << 2 | k;
320 }
321
322 void leave(kind k) {
323 if (k == currentKind()) {
324 kindStack = kindStack >> 2;
325 }
326 }
327 kind currentKind() {
328 return static_cast<kind>(kindStack & 0x3);
329 }
330 kind stateKind2(int ks) {
331 if (0 == ks) {
332 return none;
333 } else {
334 kind k1 = stateKind2(ks >> 2);
335 kind k2 = static_cast<kind>(ks & 0x3);
336 if (embedded == k1 && k2 == comment) {
337 return embedded;
338 } else {
339 return k2;
340 }
341 }
342 }
343 kind stateKind() {
344 return stateKind2(kindStack);
345 }
346 };
347
348 void SCI_METHOD LexerVisualProlog::Lex(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument* pAccess) {
349 LexAccessor styler(pAccess);
350 CharacterSet setDoxygen(CharacterSet::setAlpha, "");
351 CharacterSet setNumber(CharacterSet::setNone, "0123456789abcdefABCDEFxoXO_");
352
353 StyleContext sc(startPos, length, initStyle, styler, 0x7f);
354
355 int styleBeforeDocKeyword = SCE_VISUALPROLOG_DEFAULT;
356
357 lineState ls;
358 if (sc.currentLine >= 1) {
359 ls.setState(styler.GetLineState(sc.currentLine - 1));
360 }
361
362 bool newState = false;
363
364 for (; sc.More(); sc.Forward()) {
365
366 Sci_Position currentLineEntry = sc.currentLine;
367
368 if (newState) {
369 newState = false;
370 int state;
371 switch (ls.stateKind()) {
372 case lineState::comment:
373 state = SCE_VISUALPROLOG_COMMENT_BLOCK;
374 break;
375 case lineState::embedded:
376 state = SCE_VISUALPROLOG_EMBEDDED;
377 break;
378 case lineState::placeholder:
379 state = SCE_VISUALPROLOG_PLACEHOLDER;
380 break;
381 default:
382 state = SCE_VISUALPROLOG_DEFAULT;
383 break;
384 }
385 sc.SetState(state);
386 }
387
388 // Determine if the current state should terminate.
389 switch (sc.state) {
390 case SCE_VISUALPROLOG_OPERATOR:
391 sc.SetState(SCE_VISUALPROLOG_DEFAULT);
392 break;
393 case SCE_VISUALPROLOG_NUMBER:
394 // We accept almost anything because of hex, '.' and number suffixes
395 if (!(setNumber.Contains(sc.ch)) || (sc.Match('.') && IsADigit(sc.chNext))) {
396 sc.SetState(SCE_VISUALPROLOG_DEFAULT);
397 }
398 break;
399 case SCE_VISUALPROLOG_IDENTIFIER:
400 if (!isIdChar(sc.ch)) {
401 char s[1000];
402 sc.GetCurrent(s, sizeof(s));
403 if (0 == strcmp(s, "end")) {
404 endLookAhead(s, styler, sc.currentPos);
405 }
406 if (majorKeywords.InList(s)) {
407 sc.ChangeState(SCE_VISUALPROLOG_KEY_MAJOR);
408 } else if (minorKeywords.InList(s)) {
409 sc.ChangeState(SCE_VISUALPROLOG_KEY_MINOR);
410 }
411 sc.SetState(SCE_VISUALPROLOG_DEFAULT);
412 }
413 break;
414 case SCE_VISUALPROLOG_VARIABLE:
415 case SCE_VISUALPROLOG_ANONYMOUS:
416 if (!isIdChar(sc.ch)) {
417 sc.SetState(SCE_VISUALPROLOG_DEFAULT);
418 }
419 break;
420 case SCE_VISUALPROLOG_KEY_DIRECTIVE:
421 if (!isLowerLetter(sc.ch)) {
422 char s[1000];
423 sc.GetCurrent(s, sizeof(s));
424 if (!directiveKeywords.InList(s + 1)) {
425 sc.ChangeState(SCE_VISUALPROLOG_IDENTIFIER);
426 }
427 sc.SetState(SCE_VISUALPROLOG_DEFAULT);
428 }
429 break;
430 case SCE_VISUALPROLOG_COMMENT_LINE:
431 if (sc.MatchLineEnd()) {
432 int nextState = (lineState::comment == ls.currentKind()) ? SCE_VISUALPROLOG_COMMENT_BLOCK : SCE_VISUALPROLOG_DEFAULT;
433 sc.SetState(nextState);
434 } else if (sc.Match('@')) {
435 styleBeforeDocKeyword = SCE_VISUALPROLOG_COMMENT_LINE;
436 sc.SetState(SCE_VISUALPROLOG_COMMENT_KEY_ERROR);
437 }
438 break;
439 case SCE_VISUALPROLOG_COMMENT_BLOCK:
440 if (sc.Match('*', '/')) {
441 sc.Forward();
442 ls.leave(lineState::comment);
443 newState = true;
444 } else if (sc.Match('/', '*')) {
445 sc.Forward();
446 ls.enter(lineState::comment);
447 } else if (sc.Match('@')) {
448 styleBeforeDocKeyword = SCE_VISUALPROLOG_COMMENT_BLOCK;
449 sc.SetState(SCE_VISUALPROLOG_COMMENT_KEY_ERROR);
450 }
451 break;
452 case SCE_VISUALPROLOG_COMMENT_KEY_ERROR:
453 if (!setDoxygen.Contains(sc.ch) || sc.MatchLineEnd()) {
454 char s[1000];
455 sc.GetCurrent(s, sizeof(s));
456 if (docKeywords.InList(s + 1)) {
457 sc.ChangeState(SCE_VISUALPROLOG_COMMENT_KEY);
458 }
459 if (SCE_VISUALPROLOG_COMMENT_LINE == styleBeforeDocKeyword && sc.MatchLineEnd()) {
460 // end line comment
461 int nextState = (lineState::comment == ls.currentKind()) ? SCE_VISUALPROLOG_COMMENT_BLOCK : SCE_VISUALPROLOG_DEFAULT;
462 sc.SetState(nextState);
463 } else {
464 sc.SetState(styleBeforeDocKeyword);
465 if (SCE_VISUALPROLOG_COMMENT_BLOCK == styleBeforeDocKeyword && sc.Match('*', '/')) {
466 // we have consumed the '*' if it comes immediately after the docKeyword
467 sc.Forward();
468 ls.leave(lineState::comment);
469 newState = true;
470 }
471 }
472 }
473 break;
474 case SCE_VISUALPROLOG_STRING_ESCAPE_ERROR:
475 if (sc.atLineStart) {
476 sc.SetState(SCE_VISUALPROLOG_DEFAULT);
477 break;
478 }
479 [[fallthrough]];
480 case SCE_VISUALPROLOG_STRING_ESCAPE:
481 case SCE_VISUALPROLOG_STRING_QUOTE:
482 case SCE_VISUALPROLOG_STRING_EOL:
483 // return to SCE_VISUALPROLOG_STRING and treat as such (fallthrough)
484 sc.SetState(SCE_VISUALPROLOG_STRING);
485 [[fallthrough]];
486 case SCE_VISUALPROLOG_STRING:
487 if (sc.MatchLineEnd() | sc.atLineEnd) {
488 if (ls.verbatim) {
489 sc.SetState(SCE_VISUALPROLOG_STRING_EOL);
490 } else {
491 ls.closingQuote = 0;
492 sc.SetState(SCE_VISUALPROLOG_STRING_ESCAPE_ERROR);
493 }
494 } else if (sc.Match(ls.closingQuote)) {
495 if (ls.verbatim && ls.closingQuote == sc.chNext) {
496 sc.SetState(SCE_VISUALPROLOG_STRING_ESCAPE);
497 sc.Forward();
498 } else {
499 ls.closingQuote = 0;
500 sc.SetState(SCE_VISUALPROLOG_STRING_QUOTE);
501 sc.ForwardSetState(SCE_VISUALPROLOG_DEFAULT);
502 }
503 } else if (!ls.verbatim && sc.Match('\\')) {
504 sc.SetState(SCE_VISUALPROLOG_STRING_ESCAPE_ERROR);
505 sc.Forward();
506 if (sc.MatchLineEnd()) {
507 sc.ForwardSetState(SCE_VISUALPROLOG_DEFAULT);
508 } else {
509 if (sc.Match('"') || sc.Match('\'') || sc.Match('\\') || sc.Match('n') || sc.Match('l') || sc.Match('r') || sc.Match('t')) {
510 sc.ChangeState(SCE_VISUALPROLOG_STRING_ESCAPE);
511 } else if (sc.Match('u')) {
512 if (IsADigit(sc.chNext, 16)) {
513 sc.Forward();
514 if (IsADigit(sc.chNext, 16)) {
515 sc.Forward();
516 if (IsADigit(sc.chNext, 16)) {
517 sc.Forward();
518 if (IsADigit(sc.chNext, 16)) {
519 sc.Forward();
520 sc.ChangeState(SCE_VISUALPROLOG_STRING_ESCAPE);
521 }
522 }
523 }
524 }
525 }
526 }
527 }
528 break;
529 case SCE_VISUALPROLOG_EMBEDDED:
530 if (sc.Match('|', ']')) {
531 sc.Forward();
532 ls.leave(lineState::embedded);
533 newState = true;
534 } else if (sc.Match('[', '|')) {
535 sc.Forward();
536 ls.enter(lineState::embedded);
537 } else if (sc.Match('{', '|') && lineState::comment != ls.currentKind()) {
538 sc.SetState(SCE_VISUALPROLOG_DEFAULT);
539 } else if (sc.Match('/', '*')) {
540 sc.Forward();
541 ls.enter(lineState::comment);
542 } else if (sc.Match('*', '/')) {
543 sc.Forward();
544 ls.leave(lineState::comment);
545 newState = true;
546 }
547 break;
548 case SCE_VISUALPROLOG_PLACEHOLDER:
549 if (lineState::embedded == ls.currentKind()) {
550 sc.SetState(SCE_VISUALPROLOG_EMBEDDED);
551 } else {
552 sc.SetState(SCE_VISUALPROLOG_DEFAULT);
553 }
554 break;
555 }
556
557 if (currentLineEntry != sc.currentLine) {
558 styler.SetLineState(currentLineEntry, ls.getState());
559 }
560 if (sc.MatchLineEnd() | sc.atLineEnd) {
561 if (sc.More()) { // currentLine can be outside the document
562 styler.SetLineState(sc.currentLine, ls.getState());
563 }
564 }
565
566 // Determine if a new state should be entered.
567 if (sc.state == SCE_VISUALPROLOG_DEFAULT) {
568 if (options.verbatimStrings && sc.Match('@') && ls.isOpenStringVerbatim(sc.chNext)) {
569 ls.verbatim = true;
570 sc.SetState(SCE_VISUALPROLOG_STRING_QUOTE);
571 sc.Forward();
572 } else if (IsADigit(sc.ch) || (sc.Match('.') && IsADigit(sc.chNext))) {
573 sc.SetState(SCE_VISUALPROLOG_NUMBER);
574 } else if (isLowerLetter(sc.ch)) {
575 sc.SetState(SCE_VISUALPROLOG_IDENTIFIER);
576 } else if (isUpperLetter(sc.ch)) {
577 sc.SetState(SCE_VISUALPROLOG_VARIABLE);
578 } else if (sc.Match('_')) {
579 sc.SetState(SCE_VISUALPROLOG_ANONYMOUS);
580 } else if (sc.Match('/', '*')) {
581 sc.SetState(SCE_VISUALPROLOG_COMMENT_BLOCK);
582 ls.enter(lineState::comment);
583 sc.Forward();
584 } else if (sc.Match('%')) {
585 sc.SetState(SCE_VISUALPROLOG_COMMENT_LINE);
586 } else if (sc.Match('[', '|')) {
587 sc.SetState(SCE_VISUALPROLOG_EMBEDDED);
588 ls.enter(lineState::embedded);
589 sc.Forward();
590 } else if (sc.Match('{', '|')) {
591 sc.SetState(SCE_VISUALPROLOG_PLACEHOLDER);
592 ls.enter(lineState::placeholder);
593 sc.Forward();
594 } else if (sc.Match('|', '}')) {
595 sc.SetState(SCE_VISUALPROLOG_PLACEHOLDER);
596 sc.Forward();
597 if (':' == sc.chNext) {
598 sc.Forward();
599 for (; isIdChar(sc.chNext); sc.Forward()) {
600 }
601 }
602 ls.leave(lineState::placeholder);
603 newState = true;
604 } else if (sc.Match('\'')) {
605 ls.verbatim = false;
606 ls.closingQuote = '\'';
607 sc.SetState(SCE_VISUALPROLOG_STRING_QUOTE);
608 } else if (sc.Match('"')) {
609 ls.verbatim = false;
610 ls.closingQuote = '"';
611 sc.SetState(SCE_VISUALPROLOG_STRING_QUOTE);
612 } else if (options.backQuotedStrings && sc.Match('`')) {
613 ls.verbatim = false;
614 ls.closingQuote = '`';
615 sc.SetState(SCE_VISUALPROLOG_STRING_QUOTE);
616 } else if (sc.Match('#')) {
617 sc.SetState(SCE_VISUALPROLOG_KEY_DIRECTIVE);
618 } else if (isoperator(static_cast<char>(sc.ch)) || sc.Match('\\') ||
619 (!options.verbatimStrings && sc.Match('@'))) {
620 sc.SetState(SCE_VISUALPROLOG_OPERATOR);
621 }
622 }
623 }
624 sc.Complete();
625 styler.Flush();
626 }
627
628 // Store both the current line's fold level and the next lines in the
629 // level store to make it easy to pick up with each increment
630 // and to make it possible to fiddle the current level for "} else {".
631
632 #if defined(__clang__)
633 #if __has_warning("-Wunused-but-set-variable")
634 // Disable warning for visibleChars
635 #pragma clang diagnostic ignored "-Wunused-but-set-variable"
636 #endif
637 #endif
638
639 void SCI_METHOD LexerVisualProlog::Fold(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument* pAccess) {
640
641 LexAccessor styler(pAccess);
642
643 Sci_PositionU endPos = startPos + length;
644 int visibleChars = 0;
645 Sci_Position currentLine = styler.GetLine(startPos);
646 int levelCurrent = SC_FOLDLEVELBASE;
647 if (currentLine > 0)
648 levelCurrent = styler.LevelAt(currentLine - 1) >> 16;
649 int levelMinCurrent = levelCurrent;
650 int levelNext = levelCurrent;
651 char chNext = styler[startPos];
652 int styleNext = styler.StyleAt(startPos);
653 int style = initStyle;
654 for (Sci_PositionU i = startPos; i < endPos; i++) {
655 char ch = chNext;
656 chNext = styler.SafeGetCharAt(i + 1);
657 style = styleNext;
658 styleNext = styler.StyleAt(i + 1);
659 bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
660 if (style == SCE_VISUALPROLOG_OPERATOR) {
661 if (ch == '{') {
662 // Measure the minimum before a '{' to allow
663 // folding on "} else {"
664 if (levelMinCurrent > levelNext) {
665 levelMinCurrent = levelNext;
666 }
667 levelNext++;
668 } else if (ch == '}') {
669 levelNext--;
670 }
671 }
672 if (!IsASpace(ch))
673 visibleChars++;
674 if (atEOL || (i == endPos - 1)) {
675 int levelUse = levelCurrent;
676 int lev = levelUse | levelNext << 16;
677 if (levelUse < levelNext)
678 lev |= SC_FOLDLEVELHEADERFLAG;
679 if (lev != styler.LevelAt(currentLine)) {
680 styler.SetLevel(currentLine, lev);
681 }
682 currentLine++;
683 levelCurrent = levelNext;
684 levelMinCurrent = levelCurrent;
685 if (atEOL && (i == static_cast<Sci_PositionU>(styler.Length() - 1))) {
686 // There is an empty line at end of file so give it same level and empty
687 styler.SetLevel(currentLine, (levelCurrent | levelCurrent << 16) | SC_FOLDLEVELWHITEFLAG);
688 }
689 visibleChars = 0;
690 }
691 }
692 }
693 }
694
695 extern const LexerModule lmVisualProlog(SCLEX_VISUALPROLOG, LexerVisualProlog::LexerFactoryVisualProlog, "visualprolog", visualPrologWordLists);