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