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