]> git.saurik.com Git - wxWidgets.git/blame - src/stc/scintilla/lexers/LexAVE.cxx
simplify code so it always returns the same object
[wxWidgets.git] / src / stc / scintilla / lexers / LexAVE.cxx
CommitLineData
65ec6247
RD
1// SciTE - Scintilla based Text Editor
2/** @file LexAVE.cxx
3 ** Lexer for Avenue.
9e730a78
RD
4 **
5 ** Written by Alexey Yutkin <yutkin@geol.msu.ru>.
65ec6247 6 **/
9e730a78 7// Copyright 1998-2002 by Neil Hodgson <neilh@scintilla.org>
65ec6247
RD
8// The License.txt file describes the conditions under which this software may be distributed.
9
10#include <stdlib.h>
11#include <string.h>
9e730a78 12#include <stdio.h>
1dcf666d
RD
13#include <stdarg.h>
14#include <assert.h>
15#include <ctype.h>
65ec6247 16
1dcf666d
RD
17#include "ILexer.h"
18#include "Scintilla.h"
19#include "SciLexer.h"
65ec6247 20
1dcf666d
RD
21#include "WordList.h"
22#include "LexAccessor.h"
65ec6247 23#include "Accessor.h"
9e730a78 24#include "StyleContext.h"
1dcf666d
RD
25#include "CharacterSet.h"
26#include "LexerModule.h"
65ec6247 27
7e0c58e9
RD
28#ifdef SCI_NAMESPACE
29using namespace Scintilla;
30#endif
9e730a78
RD
31
32
33static inline bool IsAWordChar(const int ch) {
34 return (ch < 0x80) && (isalnum(ch) || ch == '.' || ch == '_');
35}
36static inline bool IsEnumChar(const int ch) {
37 return (ch < 0x80) && (isalnum(ch)|| ch == '_');
38}
39static inline bool IsANumberChar(const int ch) {
40 return (ch < 0x80) && (isalnum(ch) || ch == '.' );
41}
42
43inline bool IsAWordStart(const int ch) {
44 return (ch < 0x80) && (isalnum(ch) || ch == '_');
45}
46
47inline bool isAveOperator(char ch) {
1dcf666d 48 if (isascii(ch) && isalnum(ch))
9e730a78
RD
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
61static void ColouriseAveDoc(
62 unsigned int startPos,
63 int length,
64 int initStyle,
65 WordList *keywordlists[],
65ec6247
RD
66 Accessor &styler) {
67
68 WordList &keywords = *keywordlists[0];
9e730a78
RD
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 }
65ec6247 79
9e730a78 80 StyleContext sc(startPos, length, initStyle, styler);
65ec6247 81
9e730a78
RD
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 }
65ec6247 92
65ec6247 93
9e730a78
RD
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);
65ec6247 100 }
9e730a78
RD
101 } else if (sc.state == SCE_AVE_ENUM) {
102 if (!IsEnumChar(sc.ch)) {
103 sc.SetState(SCE_AVE_DEFAULT);
65ec6247 104 }
9e730a78
RD
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);
65ec6247 124 }
9e730a78
RD
125 } else if (sc.state == SCE_AVE_COMMENT) {
126 if (sc.atLineEnd) {
127 sc.SetState(SCE_AVE_DEFAULT);
65ec6247 128 }
9e730a78
RD
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);
65ec6247
RD
135 }
136 }
9e730a78
RD
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();
65ec6247
RD
154 }
155 }
9e730a78
RD
156 }
157 sc.Complete();
158}
65ec6247 159
9e730a78
RD
160static 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];
65ec6247 171
9e730a78
RD
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;
65ec6247 183 }
9e730a78
RD
184 s[j] = static_cast<char>(tolower(styler[i + j]));
185 s[j + 1] = '\0';
186 }
65ec6247 187
9e730a78
RD
188 if ((strcmp(s, "then") == 0) || (strcmp(s, "for") == 0) || (strcmp(s, "while") == 0)) {
189 levelCurrent++;
65ec6247 190 }
1e9bafca
RD
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.
9e730a78 194 levelCurrent--;
65ec6247
RD
195 }
196 }
9e730a78
RD
197 } else if (style == SCE_AVE_OPERATOR) {
198 if (ch == '{' || ch == '(') {
199 levelCurrent++;
200 } else if (ch == '}' || ch == ')') {
201 levelCurrent--;
202 }
65ec6247
RD
203 }
204
9e730a78
RD
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 }
65ec6247 223 }
65ec6247 224 // Fill in the real level of the next line, keeping the current flags as they will be filled in later
65ec6247 225
9e730a78
RD
226 int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK;
227 styler.SetLevel(lineCurrent, levelPrev | flagsNext);
65ec6247
RD
228}
229
9e730a78
RD
230LexerModule lmAVE(SCLEX_AVE, ColouriseAveDoc, "ave", FoldAveDoc);
231