]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/LexASY.cxx
5bf979fd37c601032d725e062339bbb81199f48f
[wxWidgets.git] / src / stc / scintilla / src / LexASY.cxx
1 // Scintilla source code edit control
2 //Author: instanton (email: soft_share<at>126<dot>com)
3 // The License.txt file describes the conditions under which this software may be distributed.
4
5 #include <stdlib.h>
6 #include <string.h>
7 #include <ctype.h>
8 #include <stdio.h>
9 #include <stdarg.h>
10
11 #include "Platform.h"
12
13 #include "PropSet.h"
14 #include "Accessor.h"
15 #include "StyleContext.h"
16 #include "KeyWords.h"
17 #include "Scintilla.h"
18 #include "SciLexer.h"
19 #include "CharacterSet.h"
20
21 #ifdef SCI_NAMESPACE
22 using namespace Scintilla;
23 #endif
24
25 static void ColouriseAsyDoc(unsigned int startPos, int length, int initStyle,
26 WordList *keywordlists[], Accessor &styler) {
27
28 WordList &keywords = *keywordlists[0];
29 WordList &keywords2 = *keywordlists[1];
30
31 CharacterSet setWordStart(CharacterSet::setAlpha, "_", 0x80, true);
32 CharacterSet setWord(CharacterSet::setAlphaNum, "._", 0x80, true);
33
34 int visibleChars = 0;
35
36 StyleContext sc(startPos, length, initStyle, styler);
37
38 for (; sc.More(); sc.Forward()) {
39
40 if (sc.atLineStart) {
41 if (sc.state == SCE_ASY_STRING) {
42 sc.SetState(SCE_ASY_STRING);
43 }
44 visibleChars = 0;
45 }
46
47 if (sc.ch == '\\') {
48 if (sc.chNext == '\n' || sc.chNext == '\r') {
49 sc.Forward();
50 if (sc.ch == '\r' && sc.chNext == '\n') {
51 sc.Forward();
52 }
53 // continuationLine = true;
54 continue;
55 }
56 }
57
58 // Determine if the current state should terminate.
59 switch (sc.state) {
60 case SCE_ASY_OPERATOR:
61 sc.SetState(SCE_ASY_DEFAULT);
62 break;
63 case SCE_ASY_NUMBER:
64 if (!setWord.Contains(sc.ch)) {
65 sc.SetState(SCE_ASY_DEFAULT);
66 }
67 break;
68 case SCE_ASY_IDENTIFIER:
69 if (!setWord.Contains(sc.ch) || (sc.ch == '.')) {
70 char s[1000];
71 sc.GetCurrentLowered(s, sizeof(s));
72 if (keywords.InList(s)) {
73 sc.ChangeState(SCE_ASY_WORD);
74 } else if (keywords2.InList(s)) {
75 sc.ChangeState(SCE_ASY_WORD2);
76 }
77 sc.SetState(SCE_ASY_DEFAULT);
78 }
79 break;
80 case SCE_ASY_COMMENT:
81 if (sc.Match('*', '/')) {
82 sc.Forward();
83 sc.ForwardSetState(SCE_ASY_DEFAULT);
84 }
85 break;
86 case SCE_ASY_COMMENTLINE:
87 if (sc.atLineStart) {
88 sc.SetState(SCE_ASY_DEFAULT);
89 }
90 break;
91 case SCE_ASY_STRING:
92 if (sc.atLineEnd) {
93 sc.ChangeState(SCE_ASY_STRINGEOL);
94 } else if (sc.ch == '\\') {
95 if (sc.chNext == '\"' || sc.chNext == '\'' || sc.chNext == '\\') {
96 sc.Forward();
97 }
98 } else if (sc.ch == '\"') {
99 sc.ForwardSetState(SCE_ASY_DEFAULT);
100 }
101 break;
102 case SCE_ASY_CHARACTER:
103 if (sc.atLineEnd) {
104 sc.ChangeState(SCE_ASY_STRINGEOL);
105 } else if (sc.ch == '\\') {
106 if (sc.chNext == '\"' || sc.chNext == '\'' || sc.chNext == '\\') {
107 sc.Forward();
108 }
109 } else if (sc.ch == '\'') {
110 sc.ForwardSetState(SCE_ASY_DEFAULT);
111 }
112 break;
113 }
114
115 // Determine if a new state should be entered.
116 if (sc.state == SCE_ASY_DEFAULT) {
117 if (setWordStart.Contains(sc.ch) || (sc.ch == '@')) {
118 sc.SetState(SCE_ASY_IDENTIFIER);
119 } else if (sc.Match('/', '*')) {
120 sc.SetState(SCE_ASY_COMMENT);
121 sc.Forward(); //
122 } else if (sc.Match('/', '/')) {
123 sc.SetState(SCE_ASY_COMMENTLINE);
124 } else if (sc.ch == '\"') {
125 sc.SetState(SCE_ASY_STRING);
126 } else if (sc.ch == '\'') {
127 sc.SetState(SCE_ASY_CHARACTER);
128 } else if (sc.ch == '#' && visibleChars == 0) {
129 do {
130 sc.Forward();
131 } while ((sc.ch == ' ' || sc.ch == '\t') && sc.More());
132 if (sc.atLineEnd) {
133 sc.SetState(SCE_ASY_DEFAULT);
134 }
135 } else if (isoperator(static_cast<char>(sc.ch))) {
136 sc.SetState(SCE_ASY_OPERATOR);
137 }
138 }
139
140 }
141 sc.Complete();
142 }
143
144 static bool IsAsyCommentStyle(int style) {
145 return style == SCE_ASY_COMMENT;
146 }
147
148
149 static inline bool isASYidentifier(int ch) {
150 return
151 ((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z')) ;
152 }
153
154 static int ParseASYWord(unsigned int pos, Accessor &styler, char *word)
155 {
156 int length=0;
157 char ch=styler.SafeGetCharAt(pos);
158 *word=0;
159
160 while(isASYidentifier(ch) && length<100){
161 word[length]=ch;
162 length++;
163 ch=styler.SafeGetCharAt(pos+length);
164 }
165 word[length]=0;
166 return length;
167 }
168
169 static bool IsASYDrawingLine(int line, Accessor &styler) {
170 int pos = styler.LineStart(line);
171 int eol_pos = styler.LineStart(line + 1) - 1;
172
173 int startpos = pos;
174 char buffer[100]="";
175
176 while (startpos<eol_pos){
177 char ch = styler[startpos];
178 ParseASYWord(startpos,styler,buffer);
179 bool drawcommands = strncmp(buffer,"draw",4)==0||
180 strncmp(buffer,"pair",4)==0||strncmp(buffer,"label",5)==0;
181 if (!drawcommands && ch!=' ') return false;
182 else if (drawcommands) return true;
183 startpos++;
184 }
185 return false;
186 }
187
188 static void FoldAsyDoc(unsigned int startPos, int length, int initStyle,
189 WordList *[], Accessor &styler) {
190 bool foldComment = styler.GetPropertyInt("fold.comment") != 0;
191 bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
192 bool foldAtElse = styler.GetPropertyInt("fold.at.else", 0) != 0;
193 unsigned int endPos = startPos + length;
194 int visibleChars = 0;
195 int lineCurrent = styler.GetLine(startPos);
196 int levelCurrent = SC_FOLDLEVELBASE;
197 if (lineCurrent > 0)
198 levelCurrent = styler.LevelAt(lineCurrent-1) >> 16;
199 int levelMinCurrent = levelCurrent;
200 int levelNext = levelCurrent;
201 char chNext = styler[startPos];
202 int styleNext = styler.StyleAt(startPos);
203 int style = initStyle;
204 for (unsigned int i = startPos; i < endPos; i++) {
205 char ch = chNext;
206 chNext = styler.SafeGetCharAt(i + 1);
207 int stylePrev = style;
208 style = styleNext;
209 styleNext = styler.StyleAt(i + 1);
210 bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
211 if (foldComment && IsAsyCommentStyle(style)) {
212 if (!IsAsyCommentStyle(stylePrev) && (stylePrev != SCE_ASY_COMMENTLINEDOC)) {
213 levelNext++;
214 } else if (!IsAsyCommentStyle(styleNext) && (styleNext != SCE_ASY_COMMENTLINEDOC) && !atEOL) {
215 levelNext--;
216 }
217 }
218 if (style == SCE_ASY_OPERATOR) {
219 if (ch == '{') {
220 if (levelMinCurrent > levelNext) {
221 levelMinCurrent = levelNext;
222 }
223 levelNext++;
224 } else if (ch == '}') {
225 levelNext--;
226 }
227 }
228
229 if (atEOL && IsASYDrawingLine(lineCurrent, styler)){
230 if (lineCurrent==0 && IsASYDrawingLine(lineCurrent + 1, styler))
231 levelNext++;
232 else if (lineCurrent!=0 && !IsASYDrawingLine(lineCurrent - 1, styler)
233 && IsASYDrawingLine(lineCurrent + 1, styler)
234 )
235 levelNext++;
236 else if (lineCurrent!=0 && IsASYDrawingLine(lineCurrent - 1, styler) &&
237 !IsASYDrawingLine(lineCurrent+1, styler))
238 levelNext--;
239 }
240
241 if (atEOL) {
242 int levelUse = levelCurrent;
243 if (foldAtElse) {
244 levelUse = levelMinCurrent;
245 }
246 int lev = levelUse | levelNext << 16;
247 if (visibleChars == 0 && foldCompact)
248 lev |= SC_FOLDLEVELWHITEFLAG;
249 if (levelUse < levelNext)
250 lev |= SC_FOLDLEVELHEADERFLAG;
251 if (lev != styler.LevelAt(lineCurrent)) {
252 styler.SetLevel(lineCurrent, lev);
253 }
254 lineCurrent++;
255 levelCurrent = levelNext;
256 levelMinCurrent = levelCurrent;
257 visibleChars = 0;
258 }
259 if (!IsASpace(ch))
260 visibleChars++;
261 }
262 }
263
264 static const char * const asyWordLists[] = {
265 "Primary keywords and identifiers",
266 "Secondary keywords and identifiers",
267 0,
268 };
269
270 LexerModule lmASY(SCLEX_ASYMPTOTE, ColouriseAsyDoc, "asy", FoldAsyDoc, asyWordLists);