1 // Scintilla source code edit control
2 /** @file LexESCRIPT.cxx
3  ** Lexer for ESCRIPT
4  **/
5 // Copyright 2003 by Patrizio Bekerle (patrizio@bekerle.com)
6 
7 #include <stdlib.h>
8 #include <string.h>
9 #include <stdio.h>
10 #include <stdarg.h>
11 #include <assert.h>
12 #include <ctype.h>
13 
14 #include "ILexer.h"
15 #include "Scintilla.h"
16 #include "SciLexer.h"
17 
18 #include "WordList.h"
19 #include "LexAccessor.h"
20 #include "Accessor.h"
21 #include "StyleContext.h"
22 #include "CharacterSet.h"
23 #include "LexerModule.h"
24 
25 #ifdef SCI_NAMESPACE
26 using namespace Scintilla;
27 #endif
28 
29 
IsAWordChar(const int ch)30 static inline bool IsAWordChar(const int ch) {
31 	return (ch < 0x80) && (isalnum(ch) || ch == '.' || ch == '_');
32 }
33 
IsAWordStart(const int ch)34 static inline bool IsAWordStart(const int ch) {
35 	return (ch < 0x80) && (isalnum(ch) || ch == '_');
36 }
37 
38 
39 
ColouriseESCRIPTDoc(Sci_PositionU startPos,Sci_Position length,int initStyle,WordList * keywordlists[],Accessor & styler)40 static void ColouriseESCRIPTDoc(Sci_PositionU startPos, Sci_Position length, int initStyle, WordList *keywordlists[],
41                             Accessor &styler) {
42 
43 	WordList &keywords = *keywordlists[0];
44 	WordList &keywords2 = *keywordlists[1];
45 	WordList &keywords3 = *keywordlists[2];
46 
47 	// Do not leak onto next line
48 	/*if (initStyle == SCE_ESCRIPT_STRINGEOL)
49 		initStyle = SCE_ESCRIPT_DEFAULT;*/
50 
51 	StyleContext sc(startPos, length, initStyle, styler);
52 
53 	bool caseSensitive = styler.GetPropertyInt("escript.case.sensitive", 0) != 0;
54 
55 	for (; sc.More(); sc.Forward()) {
56 
57 		/*if (sc.atLineStart && (sc.state == SCE_ESCRIPT_STRING)) {
58 			// Prevent SCE_ESCRIPT_STRINGEOL from leaking back to previous line
59 			sc.SetState(SCE_ESCRIPT_STRING);
60 		}*/
61 
62 		// Handle line continuation generically.
63 		if (sc.ch == '\\') {
64 			if (sc.chNext == '\n' || sc.chNext == '\r') {
65 				sc.Forward();
66 				if (sc.ch == '\r' && sc.chNext == '\n') {
67 					sc.Forward();
68 				}
69 				continue;
70 			}
71 		}
72 
73 		// Determine if the current state should terminate.
74 		if (sc.state == SCE_ESCRIPT_OPERATOR || sc.state == SCE_ESCRIPT_BRACE) {
75 			sc.SetState(SCE_ESCRIPT_DEFAULT);
76 		} else if (sc.state == SCE_ESCRIPT_NUMBER) {
77 			if (!IsADigit(sc.ch) || sc.ch != '.') {
78 				sc.SetState(SCE_ESCRIPT_DEFAULT);
79 			}
80 		} else if (sc.state == SCE_ESCRIPT_IDENTIFIER) {
81 			if (!IsAWordChar(sc.ch) || (sc.ch == '.')) {
82 				char s[100];
83 				if (caseSensitive) {
84 					sc.GetCurrent(s, sizeof(s));
85 				} else {
86 					sc.GetCurrentLowered(s, sizeof(s));
87 				}
88 
89 //				sc.GetCurrentLowered(s, sizeof(s));
90 
91                                 if (keywords.InList(s)) {
92 					sc.ChangeState(SCE_ESCRIPT_WORD);
93 				} else if (keywords2.InList(s)) {
94 					sc.ChangeState(SCE_ESCRIPT_WORD2);
95 				} else if (keywords3.InList(s)) {
96 					sc.ChangeState(SCE_ESCRIPT_WORD3);
97                                         // sc.state = SCE_ESCRIPT_IDENTIFIER;
98 				}
99 				sc.SetState(SCE_ESCRIPT_DEFAULT);
100 			}
101 		} else if (sc.state == SCE_ESCRIPT_COMMENT) {
102 			if (sc.Match('*', '/')) {
103 				sc.Forward();
104 				sc.ForwardSetState(SCE_ESCRIPT_DEFAULT);
105 			}
106 		} else if (sc.state == SCE_ESCRIPT_COMMENTDOC) {
107 			if (sc.Match('*', '/')) {
108 				sc.Forward();
109 				sc.ForwardSetState(SCE_ESCRIPT_DEFAULT);
110 			}
111 		} else if (sc.state == SCE_ESCRIPT_COMMENTLINE) {
112 			if (sc.atLineEnd) {
113 				sc.SetState(SCE_ESCRIPT_DEFAULT);
114 			}
115 		} else if (sc.state == SCE_ESCRIPT_STRING) {
116 			if (sc.ch == '\\') {
117 				if (sc.chNext == '\"' || sc.chNext == '\\') {
118 					sc.Forward();
119 				}
120 			} else if (sc.ch == '\"') {
121 				sc.ForwardSetState(SCE_ESCRIPT_DEFAULT);
122 			}
123 		}
124 
125 		// Determine if a new state should be entered.
126 		if (sc.state == SCE_ESCRIPT_DEFAULT) {
127 			if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {
128 				sc.SetState(SCE_ESCRIPT_NUMBER);
129 			} else if (IsAWordStart(sc.ch) || (sc.ch == '#')) {
130 				sc.SetState(SCE_ESCRIPT_IDENTIFIER);
131 			} else if (sc.Match('/', '*')) {
132 				sc.SetState(SCE_ESCRIPT_COMMENT);
133 				sc.Forward();	// Eat the * so it isn't used for the end of the comment
134 			} else if (sc.Match('/', '/')) {
135 				sc.SetState(SCE_ESCRIPT_COMMENTLINE);
136 			} else if (sc.ch == '\"') {
137 				sc.SetState(SCE_ESCRIPT_STRING);
138 				//} else if (isoperator(static_cast<char>(sc.ch))) {
139 			} else if (sc.ch == '+' || sc.ch == '-' || sc.ch == '*' || sc.ch == '/' || sc.ch == '=' || sc.ch == '<' || sc.ch == '>' || sc.ch == '&' || sc.ch == '|' || sc.ch == '!' || sc.ch == '?' || sc.ch == ':') {
140 				sc.SetState(SCE_ESCRIPT_OPERATOR);
141 			} else if (sc.ch == '{' || sc.ch == '}') {
142 				sc.SetState(SCE_ESCRIPT_BRACE);
143 			}
144 		}
145 
146 	}
147 	sc.Complete();
148 }
149 
150 
classifyFoldPointESCRIPT(const char * s,const char * prevWord)151 static int classifyFoldPointESCRIPT(const char* s, const char* prevWord) {
152 	int lev = 0;
153 	if (strcmp(prevWord, "end") == 0) return lev;
154 	if ((strcmp(prevWord, "else") == 0 && strcmp(s, "if") == 0) || strcmp(s, "elseif") == 0)
155 		return -1;
156 
157         if (strcmp(s, "for") == 0 || strcmp(s, "foreach") == 0
158 	    || strcmp(s, "program") == 0 || strcmp(s, "function") == 0
159 	    || strcmp(s, "while") == 0 || strcmp(s, "case") == 0
160 	    || strcmp(s, "if") == 0 ) {
161 		lev = 1;
162 	} else if ( strcmp(s, "endfor") == 0 || strcmp(s, "endforeach") == 0
163 	    || strcmp(s, "endprogram") == 0 || strcmp(s, "endfunction") == 0
164 	    || strcmp(s, "endwhile") == 0 || strcmp(s, "endcase") == 0
165 	    || strcmp(s, "endif") == 0 ) {
166 		lev = -1;
167 	}
168 
169 	return lev;
170 }
171 
172 
IsStreamCommentStyle(int style)173 static bool IsStreamCommentStyle(int style) {
174 	return style == SCE_ESCRIPT_COMMENT ||
175 	       style == SCE_ESCRIPT_COMMENTDOC ||
176 	       style == SCE_ESCRIPT_COMMENTLINE;
177 }
178 
FoldESCRIPTDoc(Sci_PositionU startPos,Sci_Position length,int initStyle,WordList * [],Accessor & styler)179 static void FoldESCRIPTDoc(Sci_PositionU startPos, Sci_Position length, int initStyle, WordList *[], Accessor &styler) {
180 	//~ bool foldComment = styler.GetPropertyInt("fold.comment") != 0;
181 	// Do not know how to fold the comment at the moment.
182 	bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
183         bool foldComment = true;
184 	Sci_PositionU endPos = startPos + length;
185 	int visibleChars = 0;
186 	Sci_Position lineCurrent = styler.GetLine(startPos);
187 	int levelPrev = styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK;
188 	int levelCurrent = levelPrev;
189 	char chNext = styler[startPos];
190 	int styleNext = styler.StyleAt(startPos);
191 	int style = initStyle;
192 
193 	Sci_Position lastStart = 0;
194 	char prevWord[32] = "";
195 
196 	for (Sci_PositionU i = startPos; i < endPos; i++) {
197 		char ch = chNext;
198 		chNext = styler.SafeGetCharAt(i + 1);
199 		int stylePrev = style;
200 		style = styleNext;
201 		styleNext = styler.StyleAt(i + 1);
202 		bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
203 
204 
205 		if (foldComment && IsStreamCommentStyle(style)) {
206 			if (!IsStreamCommentStyle(stylePrev)) {
207 				levelCurrent++;
208 			} else if (!IsStreamCommentStyle(styleNext) && !atEOL) {
209 				// Comments don't end at end of line and the next character may be unstyled.
210 				levelCurrent--;
211 			}
212 		}
213 
214 		if (foldComment && (style == SCE_ESCRIPT_COMMENTLINE)) {
215 			if ((ch == '/') && (chNext == '/')) {
216 				char chNext2 = styler.SafeGetCharAt(i + 2);
217 				if (chNext2 == '{') {
218 					levelCurrent++;
219 				} else if (chNext2 == '}') {
220 					levelCurrent--;
221 				}
222 			}
223 		}
224 
225 		if (stylePrev == SCE_ESCRIPT_DEFAULT && style == SCE_ESCRIPT_WORD3)
226 		{
227 			// Store last word start point.
228 			lastStart = i;
229 		}
230 
231 		if (style == SCE_ESCRIPT_WORD3) {
232 			if(iswordchar(ch) && !iswordchar(chNext)) {
233 				char s[32];
234 				Sci_PositionU j;
235 				for(j = 0; ( j < 31 ) && ( j < i-lastStart+1 ); j++) {
236 					s[j] = static_cast<char>(tolower(styler[lastStart + j]));
237 				}
238 				s[j] = '\0';
239 				levelCurrent += classifyFoldPointESCRIPT(s, prevWord);
240 				strcpy(prevWord, s);
241 			}
242 		}
243 		if (atEOL) {
244 			int lev = levelPrev;
245 			if (visibleChars == 0 && foldCompact)
246 				lev |= SC_FOLDLEVELWHITEFLAG;
247 			if ((levelCurrent > levelPrev) && (visibleChars > 0))
248 				lev |= SC_FOLDLEVELHEADERFLAG;
249 			if (lev != styler.LevelAt(lineCurrent)) {
250 				styler.SetLevel(lineCurrent, lev);
251 			}
252 			lineCurrent++;
253 			levelPrev = levelCurrent;
254 			visibleChars = 0;
255 			strcpy(prevWord, "");
256 		}
257 
258 		if (!isspacechar(ch))
259 			visibleChars++;
260 	}
261 
262 	// Fill in the real level of the next line, keeping the current flags as they will be filled in later
263 	int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK;
264 	styler.SetLevel(lineCurrent, levelPrev | flagsNext);
265 }
266 
267 
268 
269 static const char * const ESCRIPTWordLists[] = {
270 	"Primary keywords and identifiers",
271 	"Intrinsic functions",
272 	"Extended and user defined functions",
273 	0,
274 };
275 
276 LexerModule lmESCRIPT(SCLEX_ESCRIPT, ColouriseESCRIPTDoc, "escript", FoldESCRIPTDoc, ESCRIPTWordLists);
277