]>
Commit | Line | Data |
---|---|---|
1 | // SciTE - Scintilla based Text Editor | |
2 | /** @file LexAVE.cxx | |
3 | ** Lexer for Avenue. | |
4 | ** | |
5 | ** Written by Alexey Yutkin <yutkin@geol.msu.ru>. | |
6 | **/ | |
7 | // Copyright 1998-2002 by Neil Hodgson <neilh@scintilla.org> | |
8 | // The License.txt file describes the conditions under which this software may be distributed. | |
9 | ||
10 | #include <stdlib.h> | |
11 | #include <string.h> | |
12 | #include <stdio.h> | |
13 | #include <stdarg.h> | |
14 | #include <assert.h> | |
15 | #include <ctype.h> | |
16 | ||
17 | #include "ILexer.h" | |
18 | #include "Scintilla.h" | |
19 | #include "SciLexer.h" | |
20 | ||
21 | #include "WordList.h" | |
22 | #include "LexAccessor.h" | |
23 | #include "Accessor.h" | |
24 | #include "StyleContext.h" | |
25 | #include "CharacterSet.h" | |
26 | #include "LexerModule.h" | |
27 | ||
28 | #ifdef SCI_NAMESPACE | |
29 | using namespace Scintilla; | |
30 | #endif | |
31 | ||
32 | ||
33 | static inline bool IsAWordChar(const int ch) { | |
34 | return (ch < 0x80) && (isalnum(ch) || ch == '.' || ch == '_'); | |
35 | } | |
36 | static inline bool IsEnumChar(const int ch) { | |
37 | return (ch < 0x80) && (isalnum(ch)|| ch == '_'); | |
38 | } | |
39 | static inline bool IsANumberChar(const int ch) { | |
40 | return (ch < 0x80) && (isalnum(ch) || ch == '.' ); | |
41 | } | |
42 | ||
43 | inline bool IsAWordStart(const int ch) { | |
44 | return (ch < 0x80) && (isalnum(ch) || ch == '_'); | |
45 | } | |
46 | ||
47 | inline bool isAveOperator(char ch) { | |
48 | if (isascii(ch) && isalnum(ch)) | |
49 | return false; | |
50 | // '.' left out as it is used to make up numbers | |
51 | if (ch == '*' || ch == '/' || ch == '-' || ch == '+' || | |
52 | ch == '(' || ch == ')' || ch == '=' || | |
53 | ch == '{' || ch == '}' || | |
54 | ch == '[' || ch == ']' || ch == ';' || | |
55 | ch == '<' || ch == '>' || ch == ',' || | |
56 | ch == '.' ) | |
57 | return true; | |
58 | return false; | |
59 | } | |
60 | ||
61 | static void ColouriseAveDoc( | |
62 | unsigned int startPos, | |
63 | int length, | |
64 | int initStyle, | |
65 | WordList *keywordlists[], | |
66 | Accessor &styler) { | |
67 | ||
68 | WordList &keywords = *keywordlists[0]; | |
69 | WordList &keywords2 = *keywordlists[1]; | |
70 | WordList &keywords3 = *keywordlists[2]; | |
71 | WordList &keywords4 = *keywordlists[3]; | |
72 | WordList &keywords5 = *keywordlists[4]; | |
73 | WordList &keywords6 = *keywordlists[5]; | |
74 | ||
75 | // Do not leak onto next line | |
76 | if (initStyle == SCE_AVE_STRINGEOL) { | |
77 | initStyle = SCE_AVE_DEFAULT; | |
78 | } | |
79 | ||
80 | StyleContext sc(startPos, length, initStyle, styler); | |
81 | ||
82 | for (; sc.More(); sc.Forward()) { | |
83 | if (sc.atLineEnd) { | |
84 | // Update the line state, so it can be seen by next line | |
85 | int currentLine = styler.GetLine(sc.currentPos); | |
86 | styler.SetLineState(currentLine, 0); | |
87 | } | |
88 | if (sc.atLineStart && (sc.state == SCE_AVE_STRING)) { | |
89 | // Prevent SCE_AVE_STRINGEOL from leaking back to previous line | |
90 | sc.SetState(SCE_AVE_STRING); | |
91 | } | |
92 | ||
93 | ||
94 | // Determine if the current state should terminate. | |
95 | if (sc.state == SCE_AVE_OPERATOR) { | |
96 | sc.SetState(SCE_AVE_DEFAULT); | |
97 | } else if (sc.state == SCE_AVE_NUMBER) { | |
98 | if (!IsANumberChar(sc.ch)) { | |
99 | sc.SetState(SCE_AVE_DEFAULT); | |
100 | } | |
101 | } else if (sc.state == SCE_AVE_ENUM) { | |
102 | if (!IsEnumChar(sc.ch)) { | |
103 | sc.SetState(SCE_AVE_DEFAULT); | |
104 | } | |
105 | } else if (sc.state == SCE_AVE_IDENTIFIER) { | |
106 | if (!IsAWordChar(sc.ch) || (sc.ch == '.')) { | |
107 | char s[100]; | |
108 | //sc.GetCurrent(s, sizeof(s)); | |
109 | sc.GetCurrentLowered(s, sizeof(s)); | |
110 | if (keywords.InList(s)) { | |
111 | sc.ChangeState(SCE_AVE_WORD); | |
112 | } else if (keywords2.InList(s)) { | |
113 | sc.ChangeState(SCE_AVE_WORD2); | |
114 | } else if (keywords3.InList(s)) { | |
115 | sc.ChangeState(SCE_AVE_WORD3); | |
116 | } else if (keywords4.InList(s)) { | |
117 | sc.ChangeState(SCE_AVE_WORD4); | |
118 | } else if (keywords5.InList(s)) { | |
119 | sc.ChangeState(SCE_AVE_WORD5); | |
120 | } else if (keywords6.InList(s)) { | |
121 | sc.ChangeState(SCE_AVE_WORD6); | |
122 | } | |
123 | sc.SetState(SCE_AVE_DEFAULT); | |
124 | } | |
125 | } else if (sc.state == SCE_AVE_COMMENT) { | |
126 | if (sc.atLineEnd) { | |
127 | sc.SetState(SCE_AVE_DEFAULT); | |
128 | } | |
129 | } else if (sc.state == SCE_AVE_STRING) { | |
130 | if (sc.ch == '\"') { | |
131 | sc.ForwardSetState(SCE_AVE_DEFAULT); | |
132 | } else if (sc.atLineEnd) { | |
133 | sc.ChangeState(SCE_AVE_STRINGEOL); | |
134 | sc.ForwardSetState(SCE_AVE_DEFAULT); | |
135 | } | |
136 | } | |
137 | ||
138 | // Determine if a new state should be entered. | |
139 | if (sc.state == SCE_AVE_DEFAULT) { | |
140 | if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) { | |
141 | sc.SetState(SCE_AVE_NUMBER); | |
142 | } else if (IsAWordStart(sc.ch)) { | |
143 | sc.SetState(SCE_AVE_IDENTIFIER); | |
144 | } else if (sc.Match('\"')) { | |
145 | sc.SetState(SCE_AVE_STRING); | |
146 | } else if (sc.Match('\'')) { | |
147 | sc.SetState(SCE_AVE_COMMENT); | |
148 | sc.Forward(); | |
149 | } else if (isAveOperator(static_cast<char>(sc.ch))) { | |
150 | sc.SetState(SCE_AVE_OPERATOR); | |
151 | } else if (sc.Match('#')) { | |
152 | sc.SetState(SCE_AVE_ENUM); | |
153 | sc.Forward(); | |
154 | } | |
155 | } | |
156 | } | |
157 | sc.Complete(); | |
158 | } | |
159 | ||
160 | static void FoldAveDoc(unsigned int startPos, int length, int /* initStyle */, WordList *[], | |
161 | Accessor &styler) { | |
162 | unsigned int lengthDoc = startPos + length; | |
163 | int visibleChars = 0; | |
164 | int lineCurrent = styler.GetLine(startPos); | |
165 | int levelPrev = styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK; | |
166 | int levelCurrent = levelPrev; | |
167 | char chNext = static_cast<char>(tolower(styler[startPos])); | |
168 | bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0; | |
169 | int styleNext = styler.StyleAt(startPos); | |
170 | char s[10]; | |
171 | ||
172 | for (unsigned int i = startPos; i < lengthDoc; i++) { | |
173 | char ch = static_cast<char>(tolower(chNext)); | |
174 | chNext = static_cast<char>(tolower(styler.SafeGetCharAt(i + 1))); | |
175 | int style = styleNext; | |
176 | styleNext = styler.StyleAt(i + 1); | |
177 | bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n'); | |
178 | if (style == SCE_AVE_WORD) { | |
179 | if (ch == 't' || ch == 'f' || ch == 'w' || ch == 'e') { | |
180 | for (unsigned int j = 0; j < 6; j++) { | |
181 | if (!iswordchar(styler[i + j])) { | |
182 | break; | |
183 | } | |
184 | s[j] = static_cast<char>(tolower(styler[i + j])); | |
185 | s[j + 1] = '\0'; | |
186 | } | |
187 | ||
188 | if ((strcmp(s, "then") == 0) || (strcmp(s, "for") == 0) || (strcmp(s, "while") == 0)) { | |
189 | levelCurrent++; | |
190 | } | |
191 | if ((strcmp(s, "end") == 0) || (strcmp(s, "elseif") == 0)) { | |
192 | // Normally "elseif" and "then" will be on the same line and will cancel | |
193 | // each other out. // As implemented, this does not support fold.at.else. | |
194 | levelCurrent--; | |
195 | } | |
196 | } | |
197 | } else if (style == SCE_AVE_OPERATOR) { | |
198 | if (ch == '{' || ch == '(') { | |
199 | levelCurrent++; | |
200 | } else if (ch == '}' || ch == ')') { | |
201 | levelCurrent--; | |
202 | } | |
203 | } | |
204 | ||
205 | if (atEOL) { | |
206 | int lev = levelPrev; | |
207 | if (visibleChars == 0 && foldCompact) { | |
208 | lev |= SC_FOLDLEVELWHITEFLAG; | |
209 | } | |
210 | if ((levelCurrent > levelPrev) && (visibleChars > 0)) { | |
211 | lev |= SC_FOLDLEVELHEADERFLAG; | |
212 | } | |
213 | if (lev != styler.LevelAt(lineCurrent)) { | |
214 | styler.SetLevel(lineCurrent, lev); | |
215 | } | |
216 | lineCurrent++; | |
217 | levelPrev = levelCurrent; | |
218 | visibleChars = 0; | |
219 | } | |
220 | if (!isspacechar(ch)) { | |
221 | visibleChars++; | |
222 | } | |
223 | } | |
224 | // Fill in the real level of the next line, keeping the current flags as they will be filled in later | |
225 | ||
226 | int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK; | |
227 | styler.SetLevel(lineCurrent, levelPrev | flagsNext); | |
228 | } | |
229 | ||
230 | LexerModule lmAVE(SCLEX_AVE, ColouriseAveDoc, "ave", FoldAveDoc); | |
231 |