~ubuntu-branches/ubuntu/hardy/codeblocks/hardy-backports

« back to all changes in this revision

Viewing changes to src/sdk/wxscintilla/src/scintilla/src/LexVB.cxx

  • Committer: Bazaar Package Importer
  • Author(s): Michael Casadevall
  • Date: 2008-07-17 04:39:23 UTC
  • Revision ID: james.westby@ubuntu.com-20080717043923-gmsy5cwkdjswghkm
Tags: upstream-8.02
ImportĀ upstreamĀ versionĀ 8.02

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Scintilla source code edit control
 
2
/** @file LexVB.cxx
 
3
 ** Lexer for Visual Basic and VBScript.
 
4
 **/
 
5
// Copyright 1998-2005 by Neil Hodgson <neilh@scintilla.org>
 
6
// The License.txt file describes the conditions under which this software may be distributed.
 
7
 
 
8
#include <stdlib.h>
 
9
#include <string.h>
 
10
#include <ctype.h>
 
11
#include <stdio.h>
 
12
#include <stdarg.h>
 
13
 
 
14
#include "Platform.h"
 
15
 
 
16
#include "PropSet.h"
 
17
#include "Accessor.h"
 
18
#include "StyleContext.h"
 
19
#include "KeyWords.h"
 
20
#include "Scintilla.h"
 
21
#include "SciLexer.h"
 
22
 
 
23
// Internal state, highlighted as number
 
24
#define SCE_B_FILENUMBER SCE_B_DEFAULT+100
 
25
 
 
26
 
 
27
static bool IsVBComment(Accessor &styler, int pos, int len) {
 
28
        return len > 0 && styler[pos] == '\'';
 
29
}
 
30
 
 
31
static inline bool IsTypeCharacter(int ch) {
 
32
        return ch == '%' || ch == '&' || ch == '@' || ch == '!' || ch == '#' || ch == '$';
 
33
}
 
34
 
 
35
// Extended to accept accented characters
 
36
static inline bool IsAWordChar(int ch) {
 
37
        return ch >= 0x80 ||
 
38
               (isalnum(ch) || ch == '.' || ch == '_');
 
39
}
 
40
 
 
41
static inline bool IsAWordStart(int ch) {
 
42
        return ch >= 0x80 ||
 
43
               (isalpha(ch) || ch == '_');
 
44
}
 
45
 
 
46
static inline bool IsANumberChar(int ch) {
 
47
        // Not exactly following number definition (several dots are seen as OK, etc.)
 
48
        // but probably enough in most cases.
 
49
        return (ch < 0x80) &&
 
50
                (isdigit(ch) || toupper(ch) == 'E' ||
 
51
             ch == '.' || ch == '-' || ch == '+');
 
52
}
 
53
 
 
54
static void ColouriseVBDoc(unsigned int startPos, int length, int initStyle,
 
55
                           WordList *keywordlists[], Accessor &styler, bool vbScriptSyntax) {
 
56
 
 
57
        WordList &keywords = *keywordlists[0];
 
58
        WordList &keywords2 = *keywordlists[1];
 
59
        WordList &keywords3 = *keywordlists[2];
 
60
        WordList &keywords4 = *keywordlists[3];
 
61
 
 
62
        styler.StartAt(startPos);
 
63
 
 
64
        int visibleChars = 0;
 
65
        int fileNbDigits = 0;
 
66
 
 
67
        // Do not leak onto next line
 
68
        if (initStyle == SCE_B_STRINGEOL || initStyle == SCE_B_COMMENT || initStyle == SCE_B_PREPROCESSOR) {
 
69
                initStyle = SCE_B_DEFAULT;
 
70
        }
 
71
 
 
72
        StyleContext sc(startPos, length, initStyle, styler);
 
73
 
 
74
        for (; sc.More(); sc.Forward()) {
 
75
 
 
76
                if (sc.state == SCE_B_OPERATOR) {
 
77
                        sc.SetState(SCE_B_DEFAULT);
 
78
                } else if (sc.state == SCE_B_IDENTIFIER) {
 
79
                        if (!IsAWordChar(sc.ch)) {
 
80
                                // In Basic (except VBScript), a variable name or a function name
 
81
                                // can end with a special character indicating the type of the value
 
82
                                // held or returned.
 
83
                                bool skipType = false;
 
84
                                if (!vbScriptSyntax && IsTypeCharacter(sc.ch)) {
 
85
                                        sc.Forward();   // Skip it
 
86
                                        skipType = true;
 
87
                                }
 
88
                                if (sc.ch == ']') {
 
89
                                        sc.Forward();
 
90
                                }
 
91
                                char s[100];
 
92
                                sc.GetCurrentLowered(s, sizeof(s));
 
93
                                if (skipType) {
 
94
                                        s[strlen(s) - 1] = '\0';
 
95
                                }
 
96
                                if (strcmp(s, "rem") == 0) {
 
97
                                        sc.ChangeState(SCE_B_COMMENT);
 
98
                                } else {
 
99
                                        if (keywords.InList(s)) {
 
100
                                                sc.ChangeState(SCE_B_KEYWORD);
 
101
                                        } else if (keywords2.InList(s)) {
 
102
                                                sc.ChangeState(SCE_B_KEYWORD2);
 
103
                                        } else if (keywords3.InList(s)) {
 
104
                                                sc.ChangeState(SCE_B_KEYWORD3);
 
105
                                        } else if (keywords4.InList(s)) {
 
106
                                                sc.ChangeState(SCE_B_KEYWORD4);
 
107
                                        }       // Else, it is really an identifier...
 
108
                                        sc.SetState(SCE_B_DEFAULT);
 
109
                                }
 
110
                        }
 
111
                } else if (sc.state == SCE_B_NUMBER) {
 
112
                        // We stop the number definition on non-numerical non-dot non-eE non-sign char
 
113
                        // Also accepts A-F for hex. numbers
 
114
                        if (!IsANumberChar(sc.ch) && !(tolower(sc.ch) >= 'a' && tolower(sc.ch) <= 'f')) {
 
115
                                sc.SetState(SCE_B_DEFAULT);
 
116
                        }
 
117
                } else if (sc.state == SCE_B_STRING) {
 
118
                        // VB doubles quotes to preserve them, so just end this string
 
119
                        // state now as a following quote will start again
 
120
                        if (sc.ch == '\"') {
 
121
                                if (tolower(sc.chNext) == 'c') {
 
122
                                        sc.Forward();
 
123
                                }
 
124
                                sc.ForwardSetState(SCE_B_DEFAULT);
 
125
                        } else if (sc.atLineEnd) {
 
126
                                sc.ChangeState(SCE_B_STRINGEOL);
 
127
                                sc.ForwardSetState(SCE_B_DEFAULT);
 
128
                        }
 
129
                } else if (sc.state == SCE_B_COMMENT) {
 
130
                        if (sc.atLineEnd) {
 
131
                                sc.ForwardSetState(SCE_B_DEFAULT);
 
132
                        }
 
133
                } else if (sc.state == SCE_B_PREPROCESSOR) {
 
134
                        if (sc.atLineEnd) {
 
135
                                sc.ForwardSetState(SCE_B_DEFAULT);
 
136
                        }
 
137
                } else if (sc.state == SCE_B_FILENUMBER) {
 
138
                        if (IsADigit(sc.ch)) {
 
139
                                fileNbDigits++;
 
140
                                if (fileNbDigits > 3) {
 
141
                                        sc.ChangeState(SCE_B_DATE);
 
142
                                }
 
143
                        } else if (sc.ch == '\r' || sc.ch == '\n' || sc.ch == ',') {
 
144
                                // Regular uses: Close #1; Put #1, ...; Get #1, ... etc.
 
145
                                // Too bad if date is format #27, Oct, 2003# or something like that...
 
146
                                // Use regular number state
 
147
                                sc.ChangeState(SCE_B_NUMBER);
 
148
                                sc.SetState(SCE_B_DEFAULT);
 
149
                        } else if (sc.ch == '#') {
 
150
                                sc.ChangeState(SCE_B_DATE);
 
151
                                sc.ForwardSetState(SCE_B_DEFAULT);
 
152
                        } else {
 
153
                                sc.ChangeState(SCE_B_DATE);
 
154
                        }
 
155
                        if (sc.state != SCE_B_FILENUMBER) {
 
156
                                fileNbDigits = 0;
 
157
                        }
 
158
                } else if (sc.state == SCE_B_DATE) {
 
159
                        if (sc.atLineEnd) {
 
160
                                sc.ChangeState(SCE_B_STRINGEOL);
 
161
                                sc.ForwardSetState(SCE_B_DEFAULT);
 
162
                        } else if (sc.ch == '#') {
 
163
                                sc.ForwardSetState(SCE_B_DEFAULT);
 
164
                        }
 
165
                }
 
166
 
 
167
                if (sc.state == SCE_B_DEFAULT) {
 
168
                        if (sc.ch == '\'') {
 
169
                                sc.SetState(SCE_B_COMMENT);
 
170
                        } else if (sc.ch == '\"') {
 
171
                                sc.SetState(SCE_B_STRING);
 
172
                        } else if (sc.ch == '#' && visibleChars == 0) {
 
173
                                // Preprocessor commands are alone on their line
 
174
                                sc.SetState(SCE_B_PREPROCESSOR);
 
175
                        } else if (sc.ch == '#') {
 
176
                                // It can be a date literal, ending with #, or a file number, from 1 to 511
 
177
                                // The date literal depends on the locale, so anything can go between #'s.
 
178
                                // Can be #January 1, 1993# or #1 Jan 93# or #05/11/2003#, etc.
 
179
                                // So we set the FILENUMBER state, and switch to DATE if it isn't a file number
 
180
                                sc.SetState(SCE_B_FILENUMBER);
 
181
                        } else if (sc.ch == '&' && tolower(sc.chNext) == 'h') {
 
182
                                // Hexadecimal number
 
183
                                sc.SetState(SCE_B_NUMBER);
 
184
                                sc.Forward();
 
185
                        } else if (sc.ch == '&' && tolower(sc.chNext) == 'o') {
 
186
                                // Octal number
 
187
                                sc.SetState(SCE_B_NUMBER);
 
188
                                sc.Forward();
 
189
                        } else if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {
 
190
                                sc.SetState(SCE_B_NUMBER);
 
191
                        } else if (IsAWordStart(sc.ch) || (sc.ch == '[')) {
 
192
                                sc.SetState(SCE_B_IDENTIFIER);
 
193
                        } else if (isoperator(static_cast<char>(sc.ch)) || (sc.ch == '\\')) {   // Integer division
 
194
                                sc.SetState(SCE_B_OPERATOR);
 
195
                        }
 
196
                }
 
197
 
 
198
                if (sc.atLineEnd) {
 
199
                        visibleChars = 0;
 
200
                }
 
201
                if (!IsASpace(sc.ch)) {
 
202
                        visibleChars++;
 
203
                }
 
204
        }
 
205
        sc.Complete();
 
206
}
 
207
 
 
208
static void FoldVBDoc(unsigned int startPos, int length, int,
 
209
                                                   WordList *[], Accessor &styler) {
 
210
        int endPos = startPos + length;
 
211
 
 
212
        // Backtrack to previous line in case need to fix its fold status
 
213
        int lineCurrent = styler.GetLine(startPos);
 
214
        if (startPos > 0) {
 
215
                if (lineCurrent > 0) {
 
216
                        lineCurrent--;
 
217
                        startPos = styler.LineStart(lineCurrent);
 
218
                }
 
219
        }
 
220
        int spaceFlags = 0;
 
221
        int indentCurrent = styler.IndentAmount(lineCurrent, &spaceFlags, IsVBComment);
 
222
        char chNext = styler[startPos];
 
223
        for (int i = startPos; i < endPos; i++) {
 
224
                char ch = chNext;
 
225
                chNext = styler.SafeGetCharAt(i + 1);
 
226
 
 
227
                if ((ch == '\r' && chNext != '\n') || (ch == '\n') || (i == endPos)) {
 
228
                        int lev = indentCurrent;
 
229
                        int indentNext = styler.IndentAmount(lineCurrent + 1, &spaceFlags, IsVBComment);
 
230
                        if (!(indentCurrent & SC_FOLDLEVELWHITEFLAG)) {
 
231
                                // Only non whitespace lines can be headers
 
232
                                if ((indentCurrent & SC_FOLDLEVELNUMBERMASK) < (indentNext & SC_FOLDLEVELNUMBERMASK)) {
 
233
                                        lev |= SC_FOLDLEVELHEADERFLAG;
 
234
                                } else if (indentNext & SC_FOLDLEVELWHITEFLAG) {
 
235
                                        // Line after is blank so check the next - maybe should continue further?
 
236
                                        int spaceFlags2 = 0;
 
237
                                        int indentNext2 = styler.IndentAmount(lineCurrent + 2, &spaceFlags2, IsVBComment);
 
238
                                        if ((indentCurrent & SC_FOLDLEVELNUMBERMASK) < (indentNext2 & SC_FOLDLEVELNUMBERMASK)) {
 
239
                                                lev |= SC_FOLDLEVELHEADERFLAG;
 
240
                                        }
 
241
                                }
 
242
                        }
 
243
                        indentCurrent = indentNext;
 
244
                        styler.SetLevel(lineCurrent, lev);
 
245
                        lineCurrent++;
 
246
                }
 
247
        }
 
248
}
 
249
 
 
250
static void ColouriseVBNetDoc(unsigned int startPos, int length, int initStyle,
 
251
                           WordList *keywordlists[], Accessor &styler) {
 
252
        ColouriseVBDoc(startPos, length, initStyle, keywordlists, styler, false);
 
253
}
 
254
 
 
255
static void ColouriseVBScriptDoc(unsigned int startPos, int length, int initStyle,
 
256
                           WordList *keywordlists[], Accessor &styler) {
 
257
        ColouriseVBDoc(startPos, length, initStyle, keywordlists, styler, true);
 
258
}
 
259
 
 
260
static const char * const vbWordListDesc[] = {
 
261
        "Keywords",
 
262
        "user1",
 
263
        "user2",
 
264
        "user3",
 
265
        0
 
266
};
 
267
 
 
268
LexerModule lmVB(SCLEX_VB, ColouriseVBNetDoc, "vb", FoldVBDoc, vbWordListDesc);
 
269
LexerModule lmVBScript(SCLEX_VBSCRIPT, ColouriseVBScriptDoc, "vbscript", FoldVBDoc, vbWordListDesc);
 
270