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