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