Debian Codegraph @

scintilla/lexilla/lexers/LexTxt2tags.cxx

raw
1 /******************************************************************
2 * LexTxt2tags.cxx
3 *
4 * A simple Txt2tags lexer for scintilla.
5 *
6 *
7 * Adapted by Eric Forgeot
8 * Based on the LexMarkdown.cxx by Jon Strait - [email protected]
9 *
10 * What could be improved:
11 * - Verbatim lines could be like for raw lines : when there is no space between the ``` and the following text, the first letter should be colored so the user would understand there must be a space for a valid tag.
12 * - marks such as bold, italic, strikeout, underline should begin to be highlighted only when they are closed and valid.
13 * - verbatim and raw area should be highlighted too.
14 *
15 * The License.txt file describes the conditions under which this
16 * software may be distributed.
17 *
18 *****************************************************************/
19
20 #include <stdlib.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <stdarg.h>
24 #include <assert.h>
25
26 #include <string>
27 #include <string_view>
28
29 #include "ILexer.h"
30 #include "Scintilla.h"
31 #include "SciLexer.h"
32
33 #include "WordList.h"
34 #include "LexAccessor.h"
35 #include "Accessor.h"
36 #include "StyleContext.h"
37 #include "CharacterSet.h"
38 #include "LexerModule.h"
39
40 using namespace Lexilla;
41
42
43
44 static inline bool IsNewline(const int ch) {
45 return (ch == '\n' || ch == '\r');
46 }
47
48 // True if can follow ch down to the end with possibly trailing whitespace
49 static bool FollowToLineEnd(const int ch, const int state, const Sci_PositionU endPos, StyleContext &sc) {
50 Sci_PositionU i = 0;
51 while (sc.GetRelative(++i) == ch)
52 ;
53 // Skip over whitespace
54 while (IsASpaceOrTab(sc.GetRelative(i)) && sc.currentPos + i < endPos)
55 ++i;
56 if (IsNewline(sc.GetRelative(i)) || sc.currentPos + i == endPos) {
57 sc.Forward(i);
58 sc.ChangeState(state);
59 sc.SetState(SCE_TXT2TAGS_LINE_BEGIN);
60 return true;
61 }
62 else return false;
63 }
64
65 // Does the previous line have more than spaces and tabs?
66 static bool HasPrevLineContent(StyleContext &sc) {
67 Sci_Position i = 0;
68 // Go back to the previous newline
69 while ((--i + sc.currentPos) && !IsNewline(sc.GetRelative(i)))
70 ;
71 while (--i + sc.currentPos) {
72 if (IsNewline(sc.GetRelative(i)))
73 break;
74 if (!IsASpaceOrTab(sc.GetRelative(i)))
75 return true;
76 }
77 return false;
78 }
79
80 // Separator line
81 static bool IsValidHrule(const Sci_PositionU endPos, StyleContext &sc) {
82 int count = 1;
83 Sci_PositionU i = 0;
84 for (;;) {
85 ++i;
86 int c = sc.GetRelative(i);
87 if (c == sc.ch)
88 ++count;
89 // hit a terminating character
90 else if (!IsASpaceOrTab(c) || sc.currentPos + i == endPos) {
91 // Are we a valid HRULE
92 if ((IsNewline(c) || sc.currentPos + i == endPos) &&
93 count >= 20 && !HasPrevLineContent(sc)) {
94 sc.SetState(SCE_TXT2TAGS_HRULE);
95 sc.Forward(i);
96 sc.SetState(SCE_TXT2TAGS_LINE_BEGIN);
97 return true;
98 }
99 else {
100 sc.SetState(SCE_TXT2TAGS_DEFAULT);
101 return false;
102 }
103 }
104 }
105 }
106
107 static void ColorizeTxt2tagsDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,
108 WordList **, Accessor &styler) {
109 Sci_PositionU endPos = startPos + length;
110 int precharCount = 0;
111 // Don't advance on a new loop iteration and retry at the same position.
112 // Useful in the corner case of having to start at the beginning file position
113 // in the default state.
114 bool freezeCursor = false;
115
116 StyleContext sc(startPos, length, initStyle, styler);
117
118 while (sc.More()) {
119 // Skip past escaped characters
120 if (sc.ch == '\\') {
121 sc.Forward();
122 continue;
123 }
124
125 // A blockquotes resets the line semantics
126 if (sc.state == SCE_TXT2TAGS_BLOCKQUOTE){
127 sc.Forward(2);
128 sc.SetState(SCE_TXT2TAGS_LINE_BEGIN);
129 }
130 // An option colors the whole line
131 if (sc.state == SCE_TXT2TAGS_OPTION){
132 FollowToLineEnd('%', SCE_TXT2TAGS_OPTION, endPos, sc);
133 }
134 if (sc.state == SCE_TXT2TAGS_POSTPROC){
135 FollowToLineEnd('%', SCE_TXT2TAGS_POSTPROC, endPos, sc);
136 }
137 if (sc.state == SCE_TXT2TAGS_PREPROC){
138 FollowToLineEnd('%', SCE_TXT2TAGS_PREPROC, endPos, sc);
139 }
140 // A comment colors the whole line
141 if (sc.state == SCE_TXT2TAGS_COMMENT){
142 FollowToLineEnd('%', SCE_TXT2TAGS_COMMENT, endPos, sc);
143 }
144 // Conditional state-based actions
145 if (sc.state == SCE_TXT2TAGS_CODE2) {
146 if (IsNewline(sc.ch))
147 sc.SetState(SCE_TXT2TAGS_LINE_BEGIN);
148 if (sc.Match("``") && sc.GetRelative(-2) != ' ') {
149 sc.Forward(2);
150 sc.SetState(SCE_TXT2TAGS_DEFAULT);
151 }
152 }
153 // Table
154 else if (sc.state == SCE_TXT2TAGS_CODE) {
155 if (IsNewline(sc.ch))
156 sc.SetState(SCE_TXT2TAGS_LINE_BEGIN);
157 if (sc.ch == '|' && sc.chPrev != ' ')
158 sc.ForwardSetState(SCE_TXT2TAGS_DEFAULT);
159 }
160 // Strong
161 else if (sc.state == SCE_TXT2TAGS_STRONG1) {
162 if (IsNewline(sc.ch))
163 sc.SetState(SCE_TXT2TAGS_LINE_BEGIN);
164 if (sc.Match("**") && sc.chPrev != ' ') {
165 sc.Forward(2);
166 sc.SetState(SCE_TXT2TAGS_DEFAULT);
167 }
168 }
169 // Emphasis
170 else if (sc.state == SCE_TXT2TAGS_EM1) {
171 if (IsNewline(sc.ch))
172 sc.SetState(SCE_TXT2TAGS_LINE_BEGIN);
173 if (sc.Match("//") && sc.chPrev != ' ') {
174 sc.Forward(2);
175 sc.ForwardSetState(SCE_TXT2TAGS_DEFAULT);
176 }
177 }
178 // Underline
179 else if (sc.state == SCE_TXT2TAGS_EM2) {
180 if (IsNewline(sc.ch))
181 sc.SetState(SCE_TXT2TAGS_LINE_BEGIN);
182 if (sc.Match("__") && sc.chPrev != ' ') {
183 sc.Forward(2);
184 sc.ForwardSetState(SCE_TXT2TAGS_DEFAULT);
185 }
186 }
187 // codeblock
188 else if (sc.state == SCE_TXT2TAGS_CODEBK) {
189 if (IsNewline(sc.ch))
190 sc.SetState(SCE_TXT2TAGS_LINE_BEGIN);
191 if (sc.atLineStart && sc.Match("```")) {
192 Sci_Position i = 1;
193 while (!IsNewline(sc.GetRelative(i)) && sc.currentPos + i < endPos)
194 i++;
195 sc.Forward(i);
196 sc.SetState(SCE_TXT2TAGS_DEFAULT);
197 }
198 }
199 // strikeout
200 else if (sc.state == SCE_TXT2TAGS_STRIKEOUT) {
201 if (IsNewline(sc.ch))
202 sc.SetState(SCE_TXT2TAGS_LINE_BEGIN);
203 if (sc.Match("--") && sc.chPrev != ' ') {
204 sc.Forward(2);
205 sc.SetState(SCE_TXT2TAGS_DEFAULT);
206 }
207 }
208 // Headers
209 else if (sc.state == SCE_TXT2TAGS_LINE_BEGIN) {
210 if (sc.Match("======"))
211 {
212 sc.SetState(SCE_TXT2TAGS_HEADER6);
213 sc.Forward();
214 }
215 else if (sc.Match("====="))
216 {
217 sc.SetState(SCE_TXT2TAGS_HEADER5);
218 sc.Forward();
219 }
220 else if (sc.Match("===="))
221 {
222 sc.SetState(SCE_TXT2TAGS_HEADER4);
223 sc.Forward();
224 }
225 else if (sc.Match("==="))
226 {
227 sc.SetState(SCE_TXT2TAGS_HEADER3);
228 sc.Forward();
229 }
230 //SetStateAndZoom(SCE_TXT2TAGS_HEADER3, 3, '=', sc);
231 else if (sc.Match("==")) {
232 sc.SetState(SCE_TXT2TAGS_HEADER2);
233 sc.Forward();
234 }
235 //SetStateAndZoom(SCE_TXT2TAGS_HEADER2, 2, '=', sc);
236 else if (sc.Match("=")) {
237 // Catch the special case of an unordered list
238 if (sc.chNext == '.' && IsASpaceOrTab(sc.GetRelative(2))) {
239 precharCount = 0;
240 sc.SetState(SCE_TXT2TAGS_PRECHAR);
241 }
242 else
243 {
244 sc.SetState(SCE_TXT2TAGS_HEADER1);
245 sc.Forward();
246 }
247 //SetStateAndZoom(SCE_TXT2TAGS_HEADER1, 1, '=', sc);
248 }
249
250 // Numbered title
251 else if (sc.Match("++++++"))
252 {
253 sc.SetState(SCE_TXT2TAGS_HEADER6);
254 sc.Forward();
255 }
256 else if (sc.Match("+++++"))
257 {
258 sc.SetState(SCE_TXT2TAGS_HEADER5);
259 sc.Forward();
260 }
261 else if (sc.Match("++++"))
262 {
263 sc.SetState(SCE_TXT2TAGS_HEADER4);
264 sc.Forward();
265 }
266 else if (sc.Match("+++"))
267 {
268 sc.SetState(SCE_TXT2TAGS_HEADER3);
269 sc.Forward();
270 }
271 //SetStateAndZoom(SCE_TXT2TAGS_HEADER3, 3, '+', sc);
272 else if (sc.Match("++")) {
273 sc.SetState(SCE_TXT2TAGS_HEADER2);
274 sc.Forward();
275 }
276 //SetStateAndZoom(SCE_TXT2TAGS_HEADER2, 2, '+', sc);
277 else if (sc.Match("+")) {
278 // Catch the special case of an unordered list
279 if (sc.chNext == ' ' && IsASpaceOrTab(sc.GetRelative(1))) {
280 // if (IsNewline(sc.ch)) {
281 //precharCount = 0;
282 // sc.SetState(SCE_TXT2TAGS_LINE_BEGIN);
283 //sc.SetState(SCE_TXT2TAGS_PRECHAR);
284 // }
285 // else {
286 // precharCount = 0;
287 sc.SetState(SCE_TXT2TAGS_OLIST_ITEM);
288 sc.Forward(2);
289 sc.SetState(SCE_TXT2TAGS_DEFAULT);
290 // sc.SetState(SCE_TXT2TAGS_PRECHAR);
291 // }
292 }
293 else
294 {
295 sc.SetState(SCE_TXT2TAGS_HEADER1);
296 sc.Forward();
297 }
298 }
299
300
301 // Codeblock
302 else if (sc.Match("```")) {
303 if (!HasPrevLineContent(sc))
304 // if (!FollowToLineEnd(sc))
305 sc.SetState(SCE_TXT2TAGS_CODEBK);
306 else
307 sc.SetState(SCE_TXT2TAGS_DEFAULT);
308 }
309
310 // Preproc
311 else if (sc.Match("%!preproc")) {
312 sc.SetState(SCE_TXT2TAGS_PREPROC);
313 }
314 // Postproc
315 else if (sc.Match("%!postproc")) {
316 sc.SetState(SCE_TXT2TAGS_POSTPROC);
317 }
318 // Option
319 else if (sc.Match("%!")) {
320 sc.SetState(SCE_TXT2TAGS_OPTION);
321 }
322
323 // Comment
324 else if (sc.ch == '%') {
325 sc.SetState(SCE_TXT2TAGS_COMMENT);
326 }
327 // list
328 else if (sc.ch == '-') {
329 precharCount = 0;
330 sc.SetState(SCE_TXT2TAGS_PRECHAR);
331 }
332 // def list
333 else if (sc.ch == ':') {
334 precharCount = 0;
335 sc.SetState(SCE_TXT2TAGS_OLIST_ITEM);
336 sc.Forward(1);
337 sc.SetState(SCE_TXT2TAGS_PRECHAR);
338 }
339 else if (IsNewline(sc.ch))
340 sc.SetState(SCE_TXT2TAGS_LINE_BEGIN);
341 else {
342 precharCount = 0;
343 sc.SetState(SCE_TXT2TAGS_PRECHAR);
344 }
345 }
346
347 // The header lasts until the newline
348 else if (sc.state == SCE_TXT2TAGS_HEADER1 || sc.state == SCE_TXT2TAGS_HEADER2 ||
349 sc.state == SCE_TXT2TAGS_HEADER3 || sc.state == SCE_TXT2TAGS_HEADER4 ||
350 sc.state == SCE_TXT2TAGS_HEADER5 || sc.state == SCE_TXT2TAGS_HEADER6) {
351 if (IsNewline(sc.ch))
352 sc.SetState(SCE_TXT2TAGS_LINE_BEGIN);
353 }
354
355 // New state only within the initial whitespace
356 if (sc.state == SCE_TXT2TAGS_PRECHAR) {
357 // Blockquote
358 if (sc.Match("\"\"\"") && precharCount < 5){
359
360 sc.SetState(SCE_TXT2TAGS_BLOCKQUOTE);
361 sc.Forward(1);
362 }
363 /*
364 // Begin of code block
365 else if (!HasPrevLineContent(sc) && (sc.chPrev == '\t' || precharCount >= 4))
366 sc.SetState(SCE_TXT2TAGS_CODEBK);
367 */
368 // HRule - Total of 20 or more hyphens, asterisks, or underscores
369 // on a line by themselves
370 else if ((sc.ch == '-' ) && IsValidHrule(endPos, sc))
371 ;
372 // Unordered list
373 else if ((sc.ch == '-') && IsASpaceOrTab(sc.chNext)) {
374 sc.SetState(SCE_TXT2TAGS_ULIST_ITEM);
375 sc.ForwardSetState(SCE_TXT2TAGS_DEFAULT);
376 }
377 // Ordered list
378 else if (IsADigit(sc.ch)) {
379 Sci_Position digitCount = 0;
380 while (IsADigit(sc.GetRelative(++digitCount)))
381 ;
382 if (sc.GetRelative(digitCount) == '.' &&
383 IsASpaceOrTab(sc.GetRelative(digitCount + 1))) {
384 sc.SetState(SCE_TXT2TAGS_OLIST_ITEM);
385 sc.Forward(digitCount + 1);
386 sc.SetState(SCE_TXT2TAGS_DEFAULT);
387 }
388 }
389 // Alternate Ordered list
390 else if (sc.ch == '+' && sc.chNext == ' ' && IsASpaceOrTab(sc.GetRelative(2))) {
391 // sc.SetState(SCE_TXT2TAGS_OLIST_ITEM);
392 // sc.Forward(2);
393 // sc.SetState(SCE_TXT2TAGS_DEFAULT);
394 }
395 else if (sc.ch != ' ' || precharCount > 2)
396 sc.SetState(SCE_TXT2TAGS_DEFAULT);
397 else
398 ++precharCount;
399 }
400
401 // New state anywhere in doc
402 if (sc.state == SCE_TXT2TAGS_DEFAULT) {
403 // if (sc.atLineStart && sc.ch == '#') {
404 // sc.SetState(SCE_TXT2TAGS_LINE_BEGIN);
405 // freezeCursor = true;
406 // }
407 // Links and Images
408 if (sc.Match("![") || sc.ch == '[') {
409 Sci_Position i = 0, j = 0, k = 0;
410 Sci_Position len = endPos - sc.currentPos;
411 while (i < len && (sc.GetRelative(++i) != ']' || sc.GetRelative(i - 1) == '\\'))
412 ;
413 if (sc.GetRelative(i) == ']') {
414 j = i;
415 if (sc.GetRelative(++i) == '(') {
416 while (i < len && (sc.GetRelative(++i) != '(' || sc.GetRelative(i - 1) == '\\'))
417 ;
418 if (sc.GetRelative(i) == '(')
419 k = i;
420 }
421
422 else if (sc.GetRelative(i) == '[' || sc.GetRelative(++i) == '[') {
423 while (i < len && (sc.GetRelative(++i) != ']' || sc.GetRelative(i - 1) == '\\'))
424 ;
425 if (sc.GetRelative(i) == ']')
426 k = i;
427 }
428 }
429 // At least a link text
430 if (j) {
431 sc.SetState(SCE_TXT2TAGS_LINK);
432 sc.Forward(j);
433 // Also has a URL or reference portion
434 if (k)
435 sc.Forward(k - j);
436 sc.ForwardSetState(SCE_TXT2TAGS_DEFAULT);
437 }
438 }
439 // Code - also a special case for alternate inside spacing
440 if (sc.Match("``") && sc.GetRelative(3) != ' ') {
441 sc.SetState(SCE_TXT2TAGS_CODE2);
442 sc.Forward();
443 }
444 else if (sc.ch == '|' && sc.GetRelative(3) != ' ') {
445 sc.SetState(SCE_TXT2TAGS_CODE);
446 }
447 // Strong
448 else if (sc.Match("**") && sc.GetRelative(2) != ' ') {
449 sc.SetState(SCE_TXT2TAGS_STRONG1);
450 sc.Forward();
451 }
452 // Emphasis
453 else if (sc.Match("//") && sc.GetRelative(2) != ' ') {
454 sc.SetState(SCE_TXT2TAGS_EM1);
455 sc.Forward();
456 }
457 else if (sc.Match("__") && sc.GetRelative(2) != ' ') {
458 sc.SetState(SCE_TXT2TAGS_EM2);
459 sc.Forward();
460 }
461 // Strikeout
462 else if (sc.Match("--") && sc.GetRelative(2) != ' ') {
463 sc.SetState(SCE_TXT2TAGS_STRIKEOUT);
464 sc.Forward();
465 }
466
467 // Beginning of line
468 else if (IsNewline(sc.ch))
469 sc.SetState(SCE_TXT2TAGS_LINE_BEGIN);
470 }
471 // Advance if not holding back the cursor for this iteration.
472 if (!freezeCursor)
473 sc.Forward();
474 freezeCursor = false;
475 }
476 sc.Complete();
477 }
478
479 extern const LexerModule lmTxt2tags(SCLEX_TXT2TAGS, ColorizeTxt2tagsDoc, "txt2tags");
480
481