1 | // Scintilla source code edit control |
2 | /** @file LexGAP.cxx |
3 | ** Lexer for the GAP language. (The GAP System for Computational Discrete Algebra) |
4 | ** http://www.gap-system.org |
5 | **/ |
6 | // Copyright 2007 by Istvan Szollosi ( szteven <at> gmail <dot> com ) |
7 | // The License.txt file describes the conditions under which this software may be distributed. |
8 | |
9 | #include <stdlib.h> |
10 | #include <string.h> |
11 | #include <stdio.h> |
12 | #include <stdarg.h> |
13 | #include <assert.h> |
14 | #include <ctype.h> |
15 | |
16 | #include <string> |
17 | #include <string_view> |
18 | |
19 | #include "ILexer.h" |
20 | #include "Scintilla.h" |
21 | #include "SciLexer.h" |
22 | |
23 | #include "WordList.h" |
24 | #include "LexAccessor.h" |
25 | #include "Accessor.h" |
26 | #include "StyleContext.h" |
27 | #include "CharacterSet.h" |
28 | #include "LexerModule.h" |
29 | |
30 | using namespace Lexilla; |
31 | |
32 | static inline bool IsGAPOperator(char ch) { |
33 | if (IsASCII(ch) && isalnum(ch)) return false; |
34 | if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || |
35 | ch == '^' || ch == ',' || ch == '!' || ch == '.' || |
36 | ch == '=' || ch == '<' || ch == '>' || ch == '(' || |
37 | ch == ')' || ch == ';' || ch == '[' || ch == ']' || |
38 | ch == '{' || ch == '}' || ch == ':' ) |
39 | return true; |
40 | return false; |
41 | } |
42 | |
43 | static void GetRange(Sci_PositionU start, Sci_PositionU end, Accessor &styler, char *s, Sci_PositionU len) { |
44 | Sci_PositionU i = 0; |
45 | while ((i < end - start + 1) && (i < len-1)) { |
46 | s[i] = static_cast<char>(styler[start + i]); |
47 | i++; |
48 | } |
49 | s[i] = '\0'; |
50 | } |
51 | |
52 | static void ColouriseGAPDoc(Sci_PositionU startPos, Sci_Position length, int initStyle, WordList *keywordlists[], Accessor &styler) { |
53 | |
54 | WordList &keywords1 = *keywordlists[0]; |
55 | WordList &keywords2 = *keywordlists[1]; |
56 | WordList &keywords3 = *keywordlists[2]; |
57 | WordList &keywords4 = *keywordlists[3]; |
58 | |
59 | // Do not leak onto next line |
60 | if (initStyle == SCE_GAP_STRINGEOL) initStyle = SCE_GAP_DEFAULT; |
61 | |
62 | StyleContext sc(startPos, length, initStyle, styler); |
63 | |
64 | for (; sc.More(); sc.Forward()) { |
65 | |
66 | // Prevent SCE_GAP_STRINGEOL from leaking back to previous line |
67 | if ( sc.atLineStart ) { |
68 | if (sc.state == SCE_GAP_STRING) sc.SetState(SCE_GAP_STRING); |
69 | if (sc.state == SCE_GAP_CHAR) sc.SetState(SCE_GAP_CHAR); |
70 | } |
71 | |
72 | // Handle line continuation generically |
73 | if (sc.ch == '\\' ) { |
74 | if (sc.chNext == '\n' || sc.chNext == '\r') { |
75 | sc.Forward(); |
76 | if (sc.ch == '\r' && sc.chNext == '\n') { |
77 | sc.Forward(); |
78 | } |
79 | continue; |
80 | } |
81 | } |
82 | |
83 | // Determine if the current state should terminate |
84 | switch (sc.state) { |
85 | case SCE_GAP_OPERATOR : |
86 | sc.SetState(SCE_GAP_DEFAULT); |
87 | break; |
88 | |
89 | case SCE_GAP_NUMBER : |
90 | if (!IsADigit(sc.ch)) { |
91 | if (sc.ch == '\\') { |
92 | if (!sc.atLineEnd) { |
93 | if (!IsADigit(sc.chNext)) { |
94 | sc.Forward(); |
95 | sc.ChangeState(SCE_GAP_IDENTIFIER); |
96 | } |
97 | } |
98 | } else if (isalpha(sc.ch) || sc.ch == '_') { |
99 | sc.ChangeState(SCE_GAP_IDENTIFIER); |
100 | } |
101 | else sc.SetState(SCE_GAP_DEFAULT); |
102 | } |
103 | break; |
104 | |
105 | case SCE_GAP_IDENTIFIER : |
106 | if (!(iswordstart(static_cast<char>(sc.ch)) || sc.ch == '$')) { |
107 | if (sc.ch == '\\') sc.Forward(); |
108 | else { |
109 | char s[1000]; |
110 | sc.GetCurrent(s, sizeof(s)); |
111 | if (keywords1.InList(s)) { |
112 | sc.ChangeState(SCE_GAP_KEYWORD); |
113 | } else if (keywords2.InList(s)) { |
114 | sc.ChangeState(SCE_GAP_KEYWORD2); |
115 | } else if (keywords3.InList(s)) { |
116 | sc.ChangeState(SCE_GAP_KEYWORD3); |
117 | } else if (keywords4.InList(s)) { |
118 | sc.ChangeState(SCE_GAP_KEYWORD4); |
119 | } |
120 | sc.SetState(SCE_GAP_DEFAULT); |
121 | } |
122 | } |
123 | break; |
124 | |
125 | case SCE_GAP_COMMENT : |
126 | if (sc.atLineEnd) { |
127 | sc.SetState(SCE_GAP_DEFAULT); |
128 | } |
129 | break; |
130 | |
131 | case SCE_GAP_STRING: |
132 | if (sc.atLineEnd) { |
133 | sc.ChangeState(SCE_GAP_STRINGEOL); |
134 | } else if (sc.ch == '\\') { |
135 | if (sc.chNext == '\"' || sc.chNext == '\'' || sc.chNext == '\\') { |
136 | sc.Forward(); |
137 | } |
138 | } else if (sc.ch == '\"') { |
139 | sc.ForwardSetState(SCE_GAP_DEFAULT); |
140 | } |
141 | break; |
142 | |
143 | case SCE_GAP_CHAR: |
144 | if (sc.atLineEnd) { |
145 | sc.ChangeState(SCE_GAP_STRINGEOL); |
146 | } else if (sc.ch == '\\') { |
147 | if (sc.chNext == '\"' || sc.chNext == '\'' || sc.chNext == '\\') { |
148 | sc.Forward(); |
149 | } |
150 | } else if (sc.ch == '\'') { |
151 | sc.ForwardSetState(SCE_GAP_DEFAULT); |
152 | } |
153 | break; |
154 | |
155 | case SCE_GAP_STRINGEOL: |
156 | if (sc.atLineStart) { |
157 | sc.SetState(SCE_GAP_DEFAULT); |
158 | } |
159 | break; |
160 | } |
161 | |
162 | // Determine if a new state should be entered |
163 | if (sc.state == SCE_GAP_DEFAULT) { |
164 | if (IsGAPOperator(static_cast<char>(sc.ch))) { |
165 | sc.SetState(SCE_GAP_OPERATOR); |
166 | } |
167 | else if (IsADigit(sc.ch)) { |
168 | sc.SetState(SCE_GAP_NUMBER); |
169 | } else if (isalpha(sc.ch) || sc.ch == '_' || sc.ch == '\\' || sc.ch == '$' || sc.ch == '~') { |
170 | sc.SetState(SCE_GAP_IDENTIFIER); |
171 | if (sc.ch == '\\') sc.Forward(); |
172 | } else if (sc.ch == '#') { |
173 | sc.SetState(SCE_GAP_COMMENT); |
174 | } else if (sc.ch == '\"') { |
175 | sc.SetState(SCE_GAP_STRING); |
176 | } else if (sc.ch == '\'') { |
177 | sc.SetState(SCE_GAP_CHAR); |
178 | } |
179 | } |
180 | |
181 | } |
182 | sc.Complete(); |
183 | } |
184 | |
185 | static int ClassifyFoldPointGAP(const char* s) { |
186 | int level = 0; |
187 | if (strcmp(s, "function" ) == 0 || |
188 | strcmp(s, "do" ) == 0 || |
189 | strcmp(s, "if" ) == 0 || |
190 | strcmp(s, "repeat" ) == 0 ) { |
191 | level = 1; |
192 | } else if (strcmp(s, "end" ) == 0 || |
193 | strcmp(s, "od" ) == 0 || |
194 | strcmp(s, "fi" ) == 0 || |
195 | strcmp(s, "until" ) == 0 ) { |
196 | level = -1; |
197 | } |
198 | return level; |
199 | } |
200 | |
201 | static void FoldGAPDoc( Sci_PositionU startPos, Sci_Position length, int initStyle, WordList** , Accessor &styler) { |
202 | Sci_PositionU endPos = startPos + length; |
203 | int visibleChars = 0; |
204 | Sci_Position lineCurrent = styler.GetLine(startPos); |
205 | int levelPrev = styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK; |
206 | int levelCurrent = levelPrev; |
207 | char chNext = styler[startPos]; |
208 | int styleNext = styler.StyleAt(startPos); |
209 | int style = initStyle; |
210 | |
211 | Sci_Position lastStart = 0; |
212 | |
213 | for (Sci_PositionU i = startPos; i < endPos; i++) { |
214 | char ch = chNext; |
215 | chNext = styler.SafeGetCharAt(i + 1); |
216 | int stylePrev = style; |
217 | style = styleNext; |
218 | styleNext = styler.StyleAt(i + 1); |
219 | bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n'); |
220 | |
221 | if (stylePrev != SCE_GAP_KEYWORD && style == SCE_GAP_KEYWORD) { |
222 | // Store last word start point. |
223 | lastStart = i; |
224 | } |
225 | |
226 | if (stylePrev == SCE_GAP_KEYWORD) { |
227 | if(iswordchar(ch) && !iswordchar(chNext)) { |
228 | char s[100]; |
229 | GetRange(lastStart, i, styler, s, sizeof(s)); |
230 | levelCurrent += ClassifyFoldPointGAP(s); |
231 | } |
232 | } |
233 | |
234 | if (atEOL) { |
235 | int lev = levelPrev; |
236 | if ((levelCurrent > levelPrev) && (visibleChars > 0)) |
237 | lev |= SC_FOLDLEVELHEADERFLAG; |
238 | if (lev != styler.LevelAt(lineCurrent)) { |
239 | styler.SetLevel(lineCurrent, lev); |
240 | } |
241 | lineCurrent++; |
242 | levelPrev = levelCurrent; |
243 | visibleChars = 0; |
244 | } |
245 | |
246 | if (!isspacechar(ch)) |
247 | visibleChars++; |
248 | } |
249 | |
250 | int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK; |
251 | styler.SetLevel(lineCurrent, levelPrev | flagsNext); |
252 | } |
253 | |
254 | static const char * const GAPWordListDesc[] = { |
255 | "Keywords 1" , |
256 | "Keywords 2" , |
257 | "Keywords 3 (unused)" , |
258 | "Keywords 4 (unused)" , |
259 | 0 |
260 | }; |
261 | |
262 | LexerModule lmGAP( |
263 | SCLEX_GAP, |
264 | ColouriseGAPDoc, |
265 | "gap" , |
266 | FoldGAPDoc, |
267 | GAPWordListDesc); |
268 | |