1
// Scintilla source code edit control
2
/** @file LexESCRIPT.cxx
5
// Copyright 2003 by Patrizio Bekerle (patrizio@bekerle.com)
15
#include "Scintilla.h"
19
#include "LexAccessor.h"
21
#include "StyleContext.h"
22
#include "CharacterSet.h"
23
#include "LexerModule.h"
26
using namespace Scintilla;
30
static inline bool IsAWordChar(const int ch) {
31
return (ch < 0x80) && (isalnum(ch) || ch == '.' || ch == '_');
34
static inline bool IsAWordStart(const int ch) {
35
return (ch < 0x80) && (isalnum(ch) || ch == '_');
40
static void ColouriseESCRIPTDoc(unsigned int startPos, int length, int initStyle, WordList *keywordlists[],
43
WordList &keywords = *keywordlists[0];
44
WordList &keywords2 = *keywordlists[1];
45
WordList &keywords3 = *keywordlists[2];
47
// Do not leak onto next line
48
/*if (initStyle == SCE_ESCRIPT_STRINGEOL)
49
initStyle = SCE_ESCRIPT_DEFAULT;*/
51
StyleContext sc(startPos, length, initStyle, styler);
53
bool caseSensitive = styler.GetPropertyInt("escript.case.sensitive", 0) != 0;
55
for (; sc.More(); sc.Forward()) {
57
/*if (sc.atLineStart && (sc.state == SCE_ESCRIPT_STRING)) {
58
// Prevent SCE_ESCRIPT_STRINGEOL from leaking back to previous line
59
sc.SetState(SCE_ESCRIPT_STRING);
62
// Handle line continuation generically.
64
if (sc.chNext == '\n' || sc.chNext == '\r') {
66
if (sc.ch == '\r' && sc.chNext == '\n') {
73
// Determine if the current state should terminate.
74
if (sc.state == SCE_ESCRIPT_OPERATOR || sc.state == SCE_ESCRIPT_BRACE) {
75
sc.SetState(SCE_ESCRIPT_DEFAULT);
76
} else if (sc.state == SCE_ESCRIPT_NUMBER) {
77
if (!IsADigit(sc.ch) || sc.ch != '.') {
78
sc.SetState(SCE_ESCRIPT_DEFAULT);
80
} else if (sc.state == SCE_ESCRIPT_IDENTIFIER) {
81
if (!IsAWordChar(sc.ch) || (sc.ch == '.')) {
84
sc.GetCurrent(s, sizeof(s));
86
sc.GetCurrentLowered(s, sizeof(s));
89
// sc.GetCurrentLowered(s, sizeof(s));
91
if (keywords.InList(s)) {
92
sc.ChangeState(SCE_ESCRIPT_WORD);
93
} else if (keywords2.InList(s)) {
94
sc.ChangeState(SCE_ESCRIPT_WORD2);
95
} else if (keywords3.InList(s)) {
96
sc.ChangeState(SCE_ESCRIPT_WORD3);
97
// sc.state = SCE_ESCRIPT_IDENTIFIER;
99
sc.SetState(SCE_ESCRIPT_DEFAULT);
101
} else if (sc.state == SCE_ESCRIPT_COMMENT) {
102
if (sc.Match('*', '/')) {
104
sc.ForwardSetState(SCE_ESCRIPT_DEFAULT);
106
} else if (sc.state == SCE_ESCRIPT_COMMENTDOC) {
107
if (sc.Match('*', '/')) {
109
sc.ForwardSetState(SCE_ESCRIPT_DEFAULT);
111
} else if (sc.state == SCE_ESCRIPT_COMMENTLINE) {
113
sc.SetState(SCE_ESCRIPT_DEFAULT);
115
} else if (sc.state == SCE_ESCRIPT_STRING) {
117
if (sc.chNext == '\"' || sc.chNext == '\\') {
120
} else if (sc.ch == '\"') {
121
sc.ForwardSetState(SCE_ESCRIPT_DEFAULT);
125
// Determine if a new state should be entered.
126
if (sc.state == SCE_ESCRIPT_DEFAULT) {
127
if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {
128
sc.SetState(SCE_ESCRIPT_NUMBER);
129
} else if (IsAWordStart(sc.ch) || (sc.ch == '#')) {
130
sc.SetState(SCE_ESCRIPT_IDENTIFIER);
131
} else if (sc.Match('/', '*')) {
132
sc.SetState(SCE_ESCRIPT_COMMENT);
133
sc.Forward(); // Eat the * so it isn't used for the end of the comment
134
} else if (sc.Match('/', '/')) {
135
sc.SetState(SCE_ESCRIPT_COMMENTLINE);
136
} else if (sc.ch == '\"') {
137
sc.SetState(SCE_ESCRIPT_STRING);
138
//} else if (isoperator(static_cast<char>(sc.ch))) {
139
} else if (sc.ch == '+' || sc.ch == '-' || sc.ch == '*' || sc.ch == '/' || sc.ch == '=' || sc.ch == '<' || sc.ch == '>' || sc.ch == '&' || sc.ch == '|' || sc.ch == '!' || sc.ch == '?' || sc.ch == ':') {
140
sc.SetState(SCE_ESCRIPT_OPERATOR);
141
} else if (sc.ch == '{' || sc.ch == '}') {
142
sc.SetState(SCE_ESCRIPT_BRACE);
151
static int classifyFoldPointESCRIPT(const char* s, const char* prevWord) {
153
if (strcmp(prevWord, "end") == 0) return lev;
154
if ((strcmp(prevWord, "else") == 0 && strcmp(s, "if") == 0) || strcmp(s, "elseif") == 0)
157
if (strcmp(s, "for") == 0 || strcmp(s, "foreach") == 0
158
|| strcmp(s, "program") == 0 || strcmp(s, "function") == 0
159
|| strcmp(s, "while") == 0 || strcmp(s, "case") == 0
160
|| strcmp(s, "if") == 0 ) {
162
} else if ( strcmp(s, "endfor") == 0 || strcmp(s, "endforeach") == 0
163
|| strcmp(s, "endprogram") == 0 || strcmp(s, "endfunction") == 0
164
|| strcmp(s, "endwhile") == 0 || strcmp(s, "endcase") == 0
165
|| strcmp(s, "endif") == 0 ) {
173
static bool IsStreamCommentStyle(int style) {
174
return style == SCE_ESCRIPT_COMMENT ||
175
style == SCE_ESCRIPT_COMMENTDOC ||
176
style == SCE_ESCRIPT_COMMENTLINE;
179
static void FoldESCRIPTDoc(unsigned int startPos, int length, int initStyle, WordList *[], Accessor &styler) {
180
//~ bool foldComment = styler.GetPropertyInt("fold.comment") != 0;
181
// Do not know how to fold the comment at the moment.
182
bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
183
bool foldComment = true;
184
unsigned int endPos = startPos + length;
185
int visibleChars = 0;
186
int lineCurrent = styler.GetLine(startPos);
187
int levelPrev = styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK;
188
int levelCurrent = levelPrev;
189
char chNext = styler[startPos];
190
int styleNext = styler.StyleAt(startPos);
191
int style = initStyle;
194
char prevWord[32] = "";
196
for (unsigned int i = startPos; i < endPos; i++) {
198
chNext = styler.SafeGetCharAt(i + 1);
199
int stylePrev = style;
201
styleNext = styler.StyleAt(i + 1);
202
bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
205
if (foldComment && IsStreamCommentStyle(style)) {
206
if (!IsStreamCommentStyle(stylePrev)) {
208
} else if (!IsStreamCommentStyle(styleNext) && !atEOL) {
209
// Comments don't end at end of line and the next character may be unstyled.
214
if (foldComment && (style == SCE_ESCRIPT_COMMENTLINE)) {
215
if ((ch == '/') && (chNext == '/')) {
216
char chNext2 = styler.SafeGetCharAt(i + 2);
217
if (chNext2 == '{') {
219
} else if (chNext2 == '}') {
225
if (stylePrev == SCE_ESCRIPT_DEFAULT && style == SCE_ESCRIPT_WORD3)
227
// Store last word start point.
231
if (style == SCE_ESCRIPT_WORD3) {
232
if(iswordchar(ch) && !iswordchar(chNext)) {
235
for(j = 0; ( j < 31 ) && ( j < i-lastStart+1 ); j++) {
236
s[j] = static_cast<char>(tolower(styler[lastStart + j]));
239
levelCurrent += classifyFoldPointESCRIPT(s, prevWord);
245
if (visibleChars == 0 && foldCompact)
246
lev |= SC_FOLDLEVELWHITEFLAG;
247
if ((levelCurrent > levelPrev) && (visibleChars > 0))
248
lev |= SC_FOLDLEVELHEADERFLAG;
249
if (lev != styler.LevelAt(lineCurrent)) {
250
styler.SetLevel(lineCurrent, lev);
253
levelPrev = levelCurrent;
255
strcpy(prevWord, "");
258
if (!isspacechar(ch))
262
// Fill in the real level of the next line, keeping the current flags as they will be filled in later
263
int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK;
264
styler.SetLevel(lineCurrent, levelPrev | flagsNext);
269
static const char * const ESCRIPTWordLists[] = {
270
"Primary keywords and identifiers",
271
"Intrinsic functions",
272
"Extended and user defined functions",
276
LexerModule lmESCRIPT(SCLEX_ESCRIPT, ColouriseESCRIPTDoc, "escript", FoldESCRIPTDoc, ESCRIPTWordLists);