summaryrefslogtreecommitdiffstats
path: root/src/LexFlagship.cpp
blob: db0314eb96b174238263ace66926b7ed9b59423b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Scintilla source code edit control
/** @file LexFlagShip.cxx
 ** Lexer for FlagShip 
 ** (Syntactically compatible to other XBase dialects, like dBase, Clipper, Fox etc.)
 **/
// Copyright 2005 by Randy Butler
// Copyright 1998-2003 by Neil Hodgson <neilh@scintilla.org>
// The License.txt file describes the conditions under which this software may be distributed.

#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <stdarg.h>

#include "Platform.h"

#include "PropSet.h"
#include "Accessor.h"
#include "StyleContext.h"
#include "KeyWords.h"
#include "Scintilla.h"
#include "SciLexer.h"

static bool IsFlagShipComment(Accessor &styler, int pos, int len) {
	return len>0 && styler[pos]=='\'';
}

static inline bool IsTypeCharacter(int ch) {
	return ch == '%' || ch == '&' || ch == '@' || ch == '!' || ch == '#' || ch == '$';
}

// Extended to accept accented characters
static inline bool IsAWordChar(int ch) {
	return ch >= 0x80 ||
	       (isalnum(ch) || ch == '.' || ch == '_');
}

static inline bool IsAWordStart(int ch) {
	return ch >= 0x80 ||
	       (isalnum(ch) || ch == '_');
}

static inline bool IsADateCharacter(const int ch) {
	return (ch < 0x80) &&
		(isalnum(ch) || ch == '|' || ch == '-' || ch == '/' || ch == ':' || ch == ' ' || ch == '\t');
}


static void ColouriseFlagShipDoc(unsigned int startPos, int length, int initStyle,
                           WordList *keywordlists[], Accessor &styler) {

	//bool FSScriptSyntax = true;
	WordList &keywords = *keywordlists[0];
	WordList &keywords2 = *keywordlists[1];
	WordList &keywords3 = *keywordlists[2];
	WordList &keywords4 = *keywordlists[3];

	styler.StartAt(startPos);

	int visibleChars = 0;

	StyleContext sc(startPos, length, initStyle, styler);

	for (; sc.More(); sc.Forward()) {

		if (sc.state == SCE_FS_OPERATOR) {
			sc.SetState(SCE_FS_DEFAULT);
		} else if (sc.state == SCE_FS_IDENTIFIER) {
			if (!IsAWordChar(sc.ch)) {
				char s[100];
				sc.GetCurrentLowered(s, sizeof(s));
				if (keywords.InList(s)) {
					sc.ChangeState(SCE_FS_KEYWORD);
				} else if (keywords2.InList(s)) {
					sc.ChangeState(SCE_FS_KEYWORD2);
				} else if (keywords3.InList(s)) {
					sc.ChangeState(SCE_FS_KEYWORD3);
				} else if (keywords4.InList(s)) {
					sc.ChangeState(SCE_FS_KEYWORD4);
				}// Else, it is really an identifier...
				sc.SetState(SCE_FS_DEFAULT);
			}
		} else if (sc.state == SCE_FS_NUMBER) {
			if (!IsAWordChar(sc.ch)) {
				sc.SetState(SCE_FS_DEFAULT);
			}
		} else if (sc.state == SCE_FS_STRING) {
			// VB doubles quotes to preserve them, so just end this string
			// state now as a following quote will start again
			if (sc.ch == '\"') {
				if (tolower(sc.chNext) == 'c') {
					sc.Forward();
				}
				sc.ForwardSetState(SCE_FS_DEFAULT);
			} else if (sc.atLineEnd) {
				sc.ChangeState(SCE_FS_STRINGEOL);
				sc.ForwardSetState(SCE_FS_DEFAULT);
			}
		} else if (sc.state == SCE_FS_COMMENT) {
			if (sc.Match('*', '/')) {   // new code
				sc.Forward();
				sc.ForwardSetState(SCE_FS_DEFAULT);
			//if (sc.atLineEnd) {       // old code
			//	sc.SetState(SCE_FS_DEFAULT);
			}
		} else if (sc.state == SCE_FS_COMMENTLINE) {  //new code
			if (sc.ch == '\r' || sc.ch == '\n') {
				sc.SetState(SCE_FS_DEFAULT);
				visibleChars = 0;
			}
		} else if (sc.state == SCE_FS_PREPROCESSOR) {
			if (sc.atLineEnd) {
				sc.SetState(SCE_FS_DEFAULT);
			}
		} else if (sc.state == SCE_FS_DATE) {
			if (sc.ch == '#' || !IsADateCharacter(sc.chNext)) {
				sc.ForwardSetState(SCE_FS_DEFAULT);
			}
		}

		// Determine if a new state should be entered.
		if (sc.state == SCE_FS_DEFAULT) {
			if (sc.Match('/', '*')) {  // New code
				sc.SetState(SCE_FS_COMMENT);
				sc.Forward();	// Eat the * so it isn't used for the end of the comment
			//if (sc.ch == '\'') {  // Old code
			//	sc.SetState(SCE_FS_COMMENT); // old code
			} else if (sc.Match('/', '/')) { // New code
				sc.SetState(SCE_FS_COMMENTLINE);
			} else if (sc.ch == '\"') {
				sc.SetState(SCE_FS_STRING);
			} else if (sc.ch == '#' && visibleChars == 0) {
				// Preprocessor commands are alone on their line
				sc.SetState(SCE_FS_PREPROCESSOR);
			} else if (sc.ch == '#') {
				int n = 1;
				int chSeek = ' ';
				while ((n < 100) && (chSeek == ' ' || chSeek == '\t')) {
					chSeek = sc.GetRelative(n);
					n++;
				}
				if (IsADigit(chSeek)) {
					sc.SetState(SCE_FS_DATE);
				} else {
					sc.SetState(SCE_FS_OPERATOR);
				}
			} else if (sc.ch == '&' && tolower(sc.chNext) == 'h') {
				sc.SetState(SCE_FS_NUMBER);
			} else if (sc.ch == '&' && tolower(sc.chNext) == 'o') {
				sc.SetState(SCE_FS_NUMBER);
			} else if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {
				sc.SetState(SCE_FS_NUMBER);
			} else if (IsAWordStart(sc.ch) || (sc.ch == '[')) {
				sc.SetState(SCE_FS_IDENTIFIER);
			} else if (isoperator(static_cast<char>(sc.ch)) || (sc.ch == '\\')) {
				sc.SetState(SCE_FS_OPERATOR);
			}
		}

		if (sc.atLineEnd) {
			visibleChars = 0;
		}
		if (!IsASpace(sc.ch)) {
			visibleChars++;
		}
	}
	sc.Complete();
}

static void FoldFlagShipDoc(unsigned int startPos, int length, int,
						   WordList *[], Accessor &styler) {

	int endPos = startPos + length;

	// Backtrack to previous line in case need to fix its fold status
	int lineCurrent = styler.GetLine(startPos);
	if (startPos > 0) {
		if (lineCurrent > 0) {
			lineCurrent--;
			startPos = styler.LineStart(lineCurrent);
		}
	}
	int spaceFlags = 0;
	int indentCurrent = styler.IndentAmount(lineCurrent, &spaceFlags, IsFlagShipComment);
	char chNext = styler[startPos];
	for (int i = startPos; i < endPos; i++) {
		char ch = chNext;
		chNext = styler.SafeGetCharAt(i + 1);

		if ((ch == '\r' && chNext != '\n') || (ch == '\n') || (i == endPos)) {
			int lev = indentCurrent;
			int indentNext = styler.IndentAmount(lineCurrent + 1, &spaceFlags, IsFlagShipComment);
			if (!(indentCurrent & SC_FOLDLEVELWHITEFLAG)) {
				// Only non whitespace lines can be headers
				if ((indentCurrent & SC_FOLDLEVELNUMBERMASK) < (indentNext & SC_FOLDLEVELNUMBERMASK)) {
					lev |= SC_FOLDLEVELHEADERFLAG;
				} else if (indentNext & SC_FOLDLEVELWHITEFLAG) {
					// Line after is blank so check the next - maybe should continue further?
					int spaceFlags2 = 0;
					int indentNext2 = styler.IndentAmount(lineCurrent + 2, &spaceFlags2, IsFlagShipComment);
					if ((indentCurrent & SC_FOLDLEVELNUMBERMASK) < (indentNext2 & SC_FOLDLEVELNUMBERMASK)) {
						lev |= SC_FOLDLEVELHEADERFLAG;
					}
				}
			}
			indentCurrent = indentNext;
			styler.SetLevel(lineCurrent, lev);
			lineCurrent++;
		}
	}
}


static const char * const FSWordListDesc[] = {
	"Keywords",
	"functions",
	"user2",
	"user3",
	0
};

LexerModule lmFlagShip(SCLEX_FLAGSHIP, ColouriseFlagShipDoc, "flagship", FoldFlagShipDoc, FSWordListDesc);