Add an UI to enable/disable specific overlay handlers.
[TortoiseGit.git] / ext / scintilla / src / LexProgress.cxx
blobb7626e34d5c61902416b082979e0ab9877fb64cd
1 // Scintilla source code edit control
2 /** @file LexProgress.cxx
3 ** Lexer for Progress 4GL.
4 ** Based on LexCPP.cxx of Neil Hodgson <neilh@scintilla.org>
5 **/
6 // Copyright 2006-2007 by Yuval Papish <Yuval@YuvCom.com>
7 // The License.txt file describes the conditions under which this software may be distributed.
9 /** TODO:
10 WebSpeed support in html lexer
11 Support "end triggers" expression of the triggers phrase
12 change lmPS to lmProgress
13 Support more than 6 comments levels
14 **/
15 #include <stdlib.h>
16 #include <string.h>
17 #include <ctype.h>
18 #include <stdio.h>
19 #include <stdarg.h>
21 #include "Platform.h"
23 #include "PropSet.h"
24 #include "Accessor.h"
25 #include "StyleContext.h"
26 #include "KeyWords.h"
27 #include "Scintilla.h"
28 #include "SciLexer.h"
30 #ifdef SCI_NAMESPACE
31 using namespace Scintilla;
32 #endif
34 static inline bool IsAWordChar(int ch) {
35 return (ch < 0x80) && (isalnum(ch) || ch == '_');
38 static inline bool IsAWordStart(int ch) {
39 return (ch < 0x80) && (isalpha(ch) || ch == '_');
42 enum SentenceStart { SetSentenceStart = 0xf, ResetSentenceStart = 0x10}; // true -> bit = 0
44 static void Colourise4glDoc(unsigned int startPos, int length, int initStyle, WordList *keywordlists[],
45 Accessor &styler) {
47 WordList &keywords1 = *keywordlists[0];
48 WordList &keywords2 = *keywordlists[1];
49 WordList &keywords3 = *keywordlists[2];
50 //WordList &keywords4 = *keywordlists[3];
51 //WordList &keywords5 = *keywordlists[4];
53 int visibleChars = 0;
54 int mask;
56 StyleContext sc(startPos, length, initStyle, styler);
58 for (; sc.More(); sc.Forward()) {
60 if (sc.atLineStart) {
61 // Reset states to begining of colourise so no surprises
62 // if different sets of lines lexed.
63 visibleChars = 0;
66 // Handle line continuation generically.
67 if (sc.ch == '~') {
68 if (sc.chNext > ' ') {
69 // skip special char after ~
70 sc.Forward();
71 continue;
73 else {
74 // Skip whitespace between ~ and EOL
75 while (sc.More() && (sc.chNext == ' ' || sc.chNext == '\t') ) {
76 sc.Forward();
78 if (sc.chNext == '\n' || sc.chNext == '\r') {
79 sc.Forward();
80 if (sc.ch == '\r' && sc.chNext == '\n') {
81 sc.Forward();
83 sc.Forward();
84 continue;
88 // Determine if a new state should be terminated.
89 mask = sc.state & 0x10;
90 switch (sc.state & 0xf) {
91 case SCE_4GL_OPERATOR:
92 sc.SetState(SCE_4GL_DEFAULT | mask);
93 break;
94 case SCE_4GL_NUMBER:
95 if (!(IsADigit(sc.ch))) {
96 sc.SetState(SCE_4GL_DEFAULT | mask);
98 break;
99 case SCE_4GL_IDENTIFIER:
100 if (!IsAWordChar(sc.ch) && sc.ch != '-') {
101 char s[1000];
102 sc.GetCurrentLowered(s, sizeof(s));
103 if (((sc.state & 0x10) == 0) && keywords2.InList(s) || keywords3.InList(s)) {
104 sc.ChangeState(SCE_4GL_BLOCK | ResetSentenceStart);
106 else if (keywords1.InList(s)) {
107 if ((s[0] == 'e' && s[1] =='n' && s[2] == 'd' && !isalnum(s[3]) && s[3] != '-') ||
108 (s[0] == 'f' && s[1] =='o' && s[2] == 'r' && s[3] == 'w' && s[4] =='a' && s[5] == 'r' && s[6] == 'd'&& !isalnum(s[7]))) {
109 sc.ChangeState(SCE_4GL_END | ResetSentenceStart);
111 else if ((s[0] == 'e' && s[1] =='l' && s[2] == 's' && s[3] == 'e') ||
112 (s[0] == 't' && s[1] =='h' && s[2] == 'e' && s[3] == 'n')) {
113 sc.ChangeState(SCE_4GL_WORD & SetSentenceStart);
115 else {
116 sc.ChangeState(SCE_4GL_WORD | ResetSentenceStart);
119 sc.SetState(SCE_4GL_DEFAULT | (sc.state & 0x10));
121 break;
122 case SCE_4GL_PREPROCESSOR:
123 if (sc.atLineStart) {
124 sc.SetState(SCE_4GL_DEFAULT & SetSentenceStart);
125 } else if (sc.ch == '*' && sc.chNext == '/') {
126 sc.ForwardSetState(SCE_4GL_DEFAULT | mask);
128 break;
129 case SCE_4GL_STRING:
130 if (sc.ch == '\"') {
131 sc.ForwardSetState(SCE_4GL_DEFAULT | mask);
133 break;
134 case SCE_4GL_CHARACTER:
135 if (sc.ch == '\'') {
136 sc.ForwardSetState(SCE_4GL_DEFAULT | mask);
138 break;
139 default:
140 if ((sc.state & 0xf) >= SCE_4GL_COMMENT1) {
141 if (sc.ch == '*' && sc.chNext == '/') {
142 sc.Forward();
143 if ((sc.state & 0xf) == SCE_4GL_COMMENT1) {
144 sc.ForwardSetState(SCE_4GL_DEFAULT | mask);
146 else
147 sc.SetState((sc.state & 0x1f) - 1);
148 } else if (sc.ch == '/' && sc.chNext == '*') {
149 sc.Forward();
150 sc.SetState((sc.state & 0x1f) + 1);
155 // Determine if a new state should be entered.
156 mask = sc.state & 0x10;
157 if ((sc.state & 0xf) == SCE_4GL_DEFAULT) {
158 if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {
159 sc.SetState(SCE_4GL_NUMBER | ResetSentenceStart);
160 } else if (IsAWordStart(sc.ch) || (sc.ch == '@')) {
161 sc.SetState(SCE_4GL_IDENTIFIER | mask);
162 } else if (sc.ch == '/' && sc.chNext == '*') {
163 sc.SetState(SCE_4GL_COMMENT1 | mask);
164 sc.Forward();
165 } else if (sc.ch == '\"') {
166 sc.SetState(SCE_4GL_STRING | ResetSentenceStart);
167 } else if (sc.ch == '\'') {
168 sc.SetState(SCE_4GL_CHARACTER | ResetSentenceStart);
169 } else if (sc.ch == '&' && visibleChars == 0 && ((sc.state & 0x10) == 0)) {
170 sc.SetState(SCE_4GL_PREPROCESSOR | ResetSentenceStart);
171 // Skip whitespace between & and preprocessor word
172 do {
173 sc.Forward();
174 } while ((sc.ch == ' ' || sc.ch == '\t') && sc.More());
175 // Handle syntactical line termination
176 } else if ((sc.ch == '.' || sc.ch == ':' || sc.ch == '}') && (sc.chNext == ' ' || sc.chNext == '\t' || sc.chNext == '\n' || sc.chNext == '\r')) {
177 sc.SetState(sc.state & SetSentenceStart);
178 } else if (isoperator(static_cast<char>(sc.ch))) {
179 if (sc.ch == ':')
180 sc.SetState(SCE_4GL_OPERATOR & SetSentenceStart);
181 else
182 sc.SetState(SCE_4GL_OPERATOR | ResetSentenceStart);
186 if (!IsASpace(sc.ch)) {
187 visibleChars++;
190 sc.Complete();
193 static bool IsStreamCommentStyle(int style) {
194 return (style & 0xf) >= SCE_4GL_COMMENT1 ;
197 // Store both the current line's fold level and the next lines in the
198 // level store to make it easy to pick up with each increment
199 // and to make it possible to fiddle the current level for "} else {".
200 static void FoldNoBox4glDoc(unsigned int startPos, int length, int initStyle,
201 Accessor &styler) {
202 bool foldComment = styler.GetPropertyInt("fold.comment") != 0;
203 bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
204 bool foldAtElse = styler.GetPropertyInt("fold.at.else", 0) != 0;
205 unsigned int endPos = startPos + length;
206 int visibleChars = 0;
207 int lineCurrent = styler.GetLine(startPos);
208 int levelCurrent = SC_FOLDLEVELBASE;
209 if (lineCurrent > 0)
210 levelCurrent = styler.LevelAt(lineCurrent-1) >> 16;
211 int levelMinCurrent = levelCurrent;
212 int levelNext = levelCurrent;
213 char chNext = static_cast<char>(tolower(styler[startPos]));
214 int styleNext = styler.StyleAt(startPos);
215 int style = initStyle;
216 for (unsigned int i = startPos; i < endPos; i++) {
217 char ch = chNext;
218 chNext = static_cast<char>(tolower(styler.SafeGetCharAt(i + 1)));
219 int stylePrev = style;
220 style = styleNext;
221 styleNext = styler.StyleAt(i + 1);
222 bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
223 if (foldComment && IsStreamCommentStyle(style)) {
224 if (!IsStreamCommentStyle(stylePrev)) {
225 levelNext++;
226 } else if (!IsStreamCommentStyle(styleNext)) { // && !atEOL) {
227 // Comments don't end at end of line and the next character may be unstyled.
228 levelNext--;
231 else if ((style & 0xf) == SCE_4GL_BLOCK && !isalnum(chNext)) {
232 levelNext++;
234 else if ((style & 0xf) == SCE_4GL_END && (ch == 'e' || ch == 'f')) {
235 levelNext--;
237 if (atEOL) {
238 int levelUse = levelCurrent;
239 if (foldAtElse) {
240 levelUse = levelMinCurrent;
242 int lev = levelUse | levelNext << 16;
243 if (visibleChars == 0 && foldCompact)
244 lev |= SC_FOLDLEVELWHITEFLAG;
245 if (levelUse < levelNext)
246 lev |= SC_FOLDLEVELHEADERFLAG;
247 if (lev != styler.LevelAt(lineCurrent)) {
248 styler.SetLevel(lineCurrent, lev);
250 lineCurrent++;
251 levelCurrent = levelNext;
252 levelMinCurrent = levelCurrent;
253 visibleChars = 0;
255 if (!isspacechar(ch))
256 visibleChars++;
260 static void Fold4glDoc(unsigned int startPos, int length, int initStyle, WordList *[],
261 Accessor &styler) {
262 FoldNoBox4glDoc(startPos, length, initStyle, styler);
265 static const char * const FglWordLists[] = {
266 "Primary keywords and identifiers",
267 "Secondary keywords and identifiers",
268 "Documentation comment keywords",
269 "Unused",
270 "Global classes and typedefs",
274 LexerModule lmProgress(SCLEX_PS, Colourise4glDoc, "progress", Fold4glDoc, FglWordLists);