1 // Scintilla source code edit control
2 /** @file LexLout.cxx
3  ** Lexer for the Basser Lout (>= version 3) typesetting language
4  **/
5 // Copyright 2003 by Kein-Hong Man <mkh@pl.jaring.my>
6 // The License.txt file describes the conditions under which this software may be distributed.
7 
8 #include <stdlib.h>
9 #include <string.h>
10 #include <stdio.h>
11 #include <stdarg.h>
12 #include <assert.h>
13 #include <ctype.h>
14 
15 #include "ILexer.h"
16 #include "Scintilla.h"
17 #include "SciLexer.h"
18 
19 #include "WordList.h"
20 #include "LexAccessor.h"
21 #include "Accessor.h"
22 #include "StyleContext.h"
23 #include "CharacterSet.h"
24 #include "LexerModule.h"
25 
26 #ifdef SCI_NAMESPACE
27 using namespace Scintilla;
28 #endif
29 
IsAWordChar(const int ch)30 static inline bool IsAWordChar(const int ch) {
31 	return (ch < 0x80) && (isalpha(ch) || ch == '@' || ch == '_');
32 }
33 
IsAnOther(const int ch)34 static inline bool IsAnOther(const int ch) {
35 	return (ch < 0x80) && (ch == '{' || ch == '}' ||
36 	ch == '!' || ch == '$' || ch == '%' || ch == '&' || ch == '\'' ||
37 	ch == '(' || ch == ')' || ch == '*' || ch == '+' || ch == ',' ||
38 	ch == '-' || ch == '.' || ch == '/' || ch == ':' || ch == ';' ||
39 	ch == '<' || ch == '=' || ch == '>' || ch == '?' || ch == '[' ||
40 	ch == ']' || ch == '^' || ch == '`' || ch == '|' || ch == '~');
41 }
42 
ColouriseLoutDoc(unsigned int startPos,int length,int initStyle,WordList * keywordlists[],Accessor & styler)43 static void ColouriseLoutDoc(unsigned int startPos, int length, int initStyle,
44 			     WordList *keywordlists[], Accessor &styler) {
45 
46 	WordList &keywords = *keywordlists[0];
47 	WordList &keywords2 = *keywordlists[1];
48 	WordList &keywords3 = *keywordlists[2];
49 
50 	int visibleChars = 0;
51 	int firstWordInLine = 0;
52 	int leadingAtSign = 0;
53 
54 	StyleContext sc(startPos, length, initStyle, styler);
55 
56 	for (; sc.More(); sc.Forward()) {
57 
58 		if (sc.atLineStart && (sc.state == SCE_LOUT_STRING)) {
59 			// Prevent SCE_LOUT_STRINGEOL from leaking back to previous line
60 			sc.SetState(SCE_LOUT_STRING);
61 		}
62 
63 		// Determine if the current state should terminate.
64 		if (sc.state == SCE_LOUT_COMMENT) {
65 			if (sc.atLineEnd) {
66 				sc.SetState(SCE_LOUT_DEFAULT);
67 				visibleChars = 0;
68 			}
69 		} else if (sc.state == SCE_LOUT_NUMBER) {
70 			if (!IsADigit(sc.ch) && sc.ch != '.') {
71 				sc.SetState(SCE_LOUT_DEFAULT);
72 			}
73 		} else if (sc.state == SCE_LOUT_STRING) {
74 			if (sc.ch == '\\') {
75 				if (sc.chNext == '\"' || sc.chNext == '\\') {
76 					sc.Forward();
77 				}
78 			} else if (sc.ch == '\"') {
79 				sc.ForwardSetState(SCE_LOUT_DEFAULT);
80 			} else if (sc.atLineEnd) {
81 				sc.ChangeState(SCE_LOUT_STRINGEOL);
82 				sc.ForwardSetState(SCE_LOUT_DEFAULT);
83 				visibleChars = 0;
84 			}
85 		} else if (sc.state == SCE_LOUT_IDENTIFIER) {
86 			if (!IsAWordChar(sc.ch)) {
87 				char s[100];
88 				sc.GetCurrent(s, sizeof(s));
89 
90 				if (leadingAtSign) {
91 					if (keywords.InList(s)) {
92 						sc.ChangeState(SCE_LOUT_WORD);
93 					} else {
94 						sc.ChangeState(SCE_LOUT_WORD4);
95 					}
96 				} else if (firstWordInLine && keywords3.InList(s)) {
97 					sc.ChangeState(SCE_LOUT_WORD3);
98 				}
99 				sc.SetState(SCE_LOUT_DEFAULT);
100 			}
101 		} else if (sc.state == SCE_LOUT_OPERATOR) {
102 			if (!IsAnOther(sc.ch)) {
103 				char s[100];
104 				sc.GetCurrent(s, sizeof(s));
105 
106 				if (keywords2.InList(s)) {
107 					sc.ChangeState(SCE_LOUT_WORD2);
108 				}
109 				sc.SetState(SCE_LOUT_DEFAULT);
110 			}
111 		}
112 
113 		// Determine if a new state should be entered.
114 		if (sc.state == SCE_LOUT_DEFAULT) {
115 			if (sc.ch == '#') {
116 				sc.SetState(SCE_LOUT_COMMENT);
117 			} else if (sc.ch == '\"') {
118 				sc.SetState(SCE_LOUT_STRING);
119 			} else if (IsADigit(sc.ch) ||
120 				  (sc.ch == '.' && IsADigit(sc.chNext))) {
121 				sc.SetState(SCE_LOUT_NUMBER);
122 			} else if (IsAWordChar(sc.ch)) {
123 				firstWordInLine = (visibleChars == 0);
124 				leadingAtSign = (sc.ch == '@');
125 				sc.SetState(SCE_LOUT_IDENTIFIER);
126 			} else if (IsAnOther(sc.ch)) {
127 				sc.SetState(SCE_LOUT_OPERATOR);
128 			}
129 		}
130 
131 		if (sc.atLineEnd) {
132 			// Reset states to begining of colourise so no surprises
133 			// if different sets of lines lexed.
134 			visibleChars = 0;
135 		}
136 		if (!IsASpace(sc.ch)) {
137 			visibleChars++;
138 		}
139 	}
140 	sc.Complete();
141 }
142 
FoldLoutDoc(unsigned int startPos,int length,int,WordList * [],Accessor & styler)143 static void FoldLoutDoc(unsigned int startPos, int length, int, WordList *[],
144                         Accessor &styler) {
145 
146 	unsigned int endPos = startPos + length;
147 	int visibleChars = 0;
148 	int lineCurrent = styler.GetLine(startPos);
149 	int levelPrev = styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK;
150 	int levelCurrent = levelPrev;
151 	char chNext = styler[startPos];
152 	bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
153 	int styleNext = styler.StyleAt(startPos);
154 	char s[10] = "";
155 
156 	for (unsigned int i = startPos; i < endPos; i++) {
157 		char ch = chNext;
158 		chNext = styler.SafeGetCharAt(i + 1);
159 		int style = styleNext;
160 		styleNext = styler.StyleAt(i + 1);
161 		bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
162 
163 		if (style == SCE_LOUT_WORD) {
164 			if (ch == '@') {
165 				for (unsigned int j = 0; j < 8; j++) {
166 					if (!IsAWordChar(styler[i + j])) {
167 						break;
168 					}
169 					s[j] = styler[i + j];
170 					s[j + 1] = '\0';
171 				}
172 				if (strcmp(s, "@Begin") == 0) {
173 					levelCurrent++;
174 				} else if (strcmp(s, "@End") == 0) {
175 					levelCurrent--;
176 				}
177 			}
178 		} else if (style == SCE_LOUT_OPERATOR) {
179 			if (ch == '{') {
180 				levelCurrent++;
181 			} else if (ch == '}') {
182 				levelCurrent--;
183 			}
184 		}
185 		if (atEOL) {
186 			int lev = levelPrev;
187 			if (visibleChars == 0 && foldCompact) {
188 				lev |= SC_FOLDLEVELWHITEFLAG;
189 			}
190 			if ((levelCurrent > levelPrev) && (visibleChars > 0)) {
191 				lev |= SC_FOLDLEVELHEADERFLAG;
192 			}
193 			if (lev != styler.LevelAt(lineCurrent)) {
194 				styler.SetLevel(lineCurrent, lev);
195 			}
196 			lineCurrent++;
197 			levelPrev = levelCurrent;
198 			visibleChars = 0;
199 		}
200 		if (!isspacechar(ch))
201 			visibleChars++;
202 	}
203 	// Fill in the real level of the next line, keeping the current flags as they will be filled in later
204 	int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK;
205 	styler.SetLevel(lineCurrent, levelPrev | flagsNext);
206 }
207 
208 static const char * const loutWordLists[] = {
209             "Predefined identifiers",
210             "Predefined delimiters",
211             "Predefined keywords",
212             0,
213         };
214 
215 LexerModule lmLout(SCLEX_LOUT, ColouriseLoutDoc, "lout", FoldLoutDoc, loutWordLists);
216