~ubuntu-branches/ubuntu/quantal/mysql-workbench/quantal

« back to all changes in this revision

Viewing changes to ext/scintilla/lexers/LexPB.cxx

  • Committer: Package Import Robot
  • Author(s): Dmitry Smirnov
  • Date: 2012-03-01 21:57:30 UTC
  • Revision ID: package-import@ubuntu.com-20120301215730-o7y8av8y38n162ro
Tags: upstream-5.2.38+dfsg
ImportĀ upstreamĀ versionĀ 5.2.38+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Scintilla source code edit control
 
2
// @file LexPB.cxx
 
3
// Lexer for PowerBasic by Roland Walter, roland@rowalt.de (for PowerBasic see www.powerbasic.com)
 
4
//
 
5
// Changes:
 
6
// 17.10.2003: Toggling of subs/functions now until next sub/function - this gives better results
 
7
// 29.10.2003: 1. Bug: Toggling didn't work for subs/functions added in editor
 
8
//             2. Own colors for PB constants and Inline Assembler SCE_B_CONSTANT and SCE_B_ASM
 
9
//             3. Several smaller syntax coloring improvements and speed optimizations
 
10
// 12.07.2004: 1. Toggling for macros added
 
11
//             2. Further folding speed optimitations (for people dealing with very large listings)
 
12
//
 
13
// Necessary changes for the PB lexer in Scintilla project:
 
14
//  - In SciLexer.h and Scintilla.iface:
 
15
//
 
16
//    #define SCLEX_POWERBASIC 51       //ID for PowerBasic lexer
 
17
//    (...)
 
18
//    #define SCE_B_DEFAULT 0           //in both VB and PB lexer
 
19
//    #define SCE_B_COMMENT 1           //in both VB and PB lexer
 
20
//    #define SCE_B_NUMBER 2            //in both VB and PB lexer
 
21
//    #define SCE_B_KEYWORD 3           //in both VB and PB lexer
 
22
//    #define SCE_B_STRING 4            //in both VB and PB lexer
 
23
//    #define SCE_B_PREPROCESSOR 5      //VB lexer only, not in PB lexer
 
24
//    #define SCE_B_OPERATOR 6          //in both VB and PB lexer
 
25
//    #define SCE_B_IDENTIFIER 7        //in both VB and PB lexer
 
26
//    #define SCE_B_DATE 8              //VB lexer only, not in PB lexer
 
27
//    #define SCE_B_CONSTANT 13         //PB lexer only, not in VB lexer
 
28
//    #define SCE_B_ASM 14              //PB lexer only, not in VB lexer
 
29
 
 
30
//  - Statement added to KeyWords.cxx:      'LINK_LEXER(lmPB);'
 
31
//  - Statement added to scintilla_vc6.mak: '$(DIR_O)\LexPB.obj: ...\src\LexPB.cxx $(LEX_HEADERS)'
 
32
//
 
33
// Copyright for Scintilla: 1998-2001 by Neil Hodgson <neilh@scintilla.org>
 
34
// The License.txt file describes the conditions under which this software may be distributed.
 
35
 
 
36
#include <stdlib.h>
 
37
#include <string.h>
 
38
#include <stdio.h>
 
39
#include <stdarg.h>
 
40
#include <assert.h>
 
41
#include <ctype.h>
 
42
 
 
43
#include "ILexer.h"
 
44
#include "Scintilla.h"
 
45
#include "SciLexer.h"
 
46
 
 
47
#include "WordList.h"
 
48
#include "LexAccessor.h"
 
49
#include "Accessor.h"
 
50
#include "StyleContext.h"
 
51
#include "CharacterSet.h"
 
52
#include "LexerModule.h"
 
53
 
 
54
#ifdef SCI_NAMESPACE
 
55
using namespace Scintilla;
 
56
#endif
 
57
 
 
58
static inline bool IsTypeCharacter(const int ch)
 
59
{
 
60
    return ch == '%' || ch == '&' || ch == '@' || ch == '!' || ch == '#' || ch == '$' || ch == '?';
 
61
}
 
62
 
 
63
static inline bool IsAWordChar(const int ch)
 
64
{
 
65
    return (ch < 0x80) && (isalnum(ch) || ch == '.' || ch == '_');
 
66
}
 
67
 
 
68
static inline bool IsAWordStart(const int ch)
 
69
{
 
70
    return (ch < 0x80) && (isalnum(ch) || ch == '_');
 
71
}
 
72
 
 
73
bool MatchUpperCase(Accessor &styler, int pos, const char *s)   //Same as styler.Match() but uppercase comparison (a-z,A-Z and space only)
 
74
{
 
75
    char ch;
 
76
    for (int i=0; *s; i++)
 
77
    {
 
78
        ch=styler.SafeGetCharAt(pos+i);
 
79
        if (ch > 0x60) ch -= '\x20';
 
80
        if (*s != ch) return false;
 
81
        s++;
 
82
    }
 
83
    return true;
 
84
}
 
85
 
 
86
static void ColourisePBDoc(unsigned int startPos, int length, int initStyle,WordList *keywordlists[],Accessor &styler) {
 
87
 
 
88
    WordList &keywords = *keywordlists[0];
 
89
 
 
90
    styler.StartAt(startPos);
 
91
 
 
92
    StyleContext sc(startPos, length, initStyle, styler);
 
93
 
 
94
    for (; sc.More(); sc.Forward()) {
 
95
        switch (sc.state)
 
96
        {
 
97
            case SCE_B_OPERATOR:
 
98
            {
 
99
                sc.SetState(SCE_B_DEFAULT);
 
100
                break;
 
101
            }
 
102
            case SCE_B_KEYWORD:
 
103
            {
 
104
                if (!IsAWordChar(sc.ch))
 
105
                {
 
106
                    if (!IsTypeCharacter(sc.ch))
 
107
                    {
 
108
                        char s[100];
 
109
                        sc.GetCurrentLowered(s, sizeof(s));
 
110
                        if (keywords.InList(s))
 
111
                        {
 
112
                            if (strcmp(s, "rem") == 0)
 
113
                            {
 
114
                                sc.ChangeState(SCE_B_COMMENT);
 
115
                                if (sc.atLineEnd) {sc.SetState(SCE_B_DEFAULT);}
 
116
                            }
 
117
                            else if (strcmp(s, "asm") == 0)
 
118
                            {
 
119
                                sc.ChangeState(SCE_B_ASM);
 
120
                                if (sc.atLineEnd) {sc.SetState(SCE_B_DEFAULT);}
 
121
                            }
 
122
                            else
 
123
                            {
 
124
                                sc.SetState(SCE_B_DEFAULT);
 
125
                            }
 
126
                        }
 
127
                        else
 
128
                        {
 
129
                            sc.ChangeState(SCE_B_IDENTIFIER);
 
130
                            sc.SetState(SCE_B_DEFAULT);
 
131
                        }
 
132
                    }
 
133
                }
 
134
                break;
 
135
            }
 
136
            case SCE_B_NUMBER:
 
137
            {
 
138
                if (!IsAWordChar(sc.ch)) {sc.SetState(SCE_B_DEFAULT);}
 
139
                break;
 
140
            }
 
141
            case SCE_B_STRING:
 
142
            {
 
143
                if (sc.ch == '\"'){sc.ForwardSetState(SCE_B_DEFAULT);}
 
144
                break;
 
145
            }
 
146
            case SCE_B_CONSTANT:
 
147
            {
 
148
                if (!IsAWordChar(sc.ch)) {sc.SetState(SCE_B_DEFAULT);}
 
149
                break;
 
150
            }
 
151
            case SCE_B_COMMENT:
 
152
            {
 
153
                if (sc.atLineEnd) {sc.SetState(SCE_B_DEFAULT);}
 
154
                break;
 
155
            }
 
156
            case SCE_B_ASM:
 
157
            {
 
158
                if (sc.atLineEnd) {sc.SetState(SCE_B_DEFAULT);}
 
159
                break;
 
160
            }
 
161
        }  //switch (sc.state)
 
162
 
 
163
        // Determine if a new state should be entered:
 
164
        if (sc.state == SCE_B_DEFAULT)
 
165
        {
 
166
            if (sc.ch == '\'') {sc.SetState(SCE_B_COMMENT);}
 
167
            else if (sc.ch == '\"') {sc.SetState(SCE_B_STRING);}
 
168
            else if (sc.ch == '&' && tolower(sc.chNext) == 'h') {sc.SetState(SCE_B_NUMBER);}
 
169
            else if (sc.ch == '&' && tolower(sc.chNext) == 'b') {sc.SetState(SCE_B_NUMBER);}
 
170
            else if (sc.ch == '&' && tolower(sc.chNext) == 'o') {sc.SetState(SCE_B_NUMBER);}
 
171
            else if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {sc.SetState(SCE_B_NUMBER);}
 
172
            else if (IsAWordStart(sc.ch)) {sc.SetState(SCE_B_KEYWORD);}
 
173
            else if (sc.ch == '%') {sc.SetState(SCE_B_CONSTANT);}
 
174
            else if (sc.ch == '$') {sc.SetState(SCE_B_CONSTANT);}
 
175
            else if (sc.ch == '#') {sc.SetState(SCE_B_KEYWORD);}
 
176
            else if (sc.ch == '!') {sc.SetState(SCE_B_ASM);}
 
177
            else if (isoperator(static_cast<char>(sc.ch)) || (sc.ch == '\\')) {sc.SetState(SCE_B_OPERATOR);}
 
178
        }
 
179
    }      //for (; sc.More(); sc.Forward())
 
180
    sc.Complete();
 
181
}
 
182
 
 
183
//The folding routine for PowerBasic toggles SUBs and FUNCTIONs only. This was exactly what I wanted,
 
184
//nothing more. I had worked with this kind of toggling for several years when I used the great good old
 
185
//GFA Basic which is dead now. After testing the feature of toggling FOR-NEXT loops, WHILE-WEND loops
 
186
//and so on too I found this is more disturbing then helping (for me). So if You think in another way
 
187
//you can (or must) write Your own toggling routine ;-)
 
188
static void FoldPBDoc(unsigned int startPos, int length, int, WordList *[], Accessor &styler)
 
189
{
 
190
    // No folding enabled, no reason to continue...
 
191
    if( styler.GetPropertyInt("fold") == 0 )
 
192
        return;
 
193
 
 
194
    unsigned int endPos = startPos + length;
 
195
    int lineCurrent = styler.GetLine(startPos);
 
196
    int levelCurrent = SC_FOLDLEVELBASE;
 
197
    if (lineCurrent > 0)
 
198
        levelCurrent = styler.LevelAt(lineCurrent-1) >> 16;
 
199
    int levelNext = levelCurrent;
 
200
    char chNext = styler[startPos];
 
201
 
 
202
    bool fNewLine=true;
 
203
    bool fMightBeMultiLineMacro=false;
 
204
    bool fBeginOfCommentFound=false;
 
205
    for (unsigned int i = startPos; i < endPos; i++)
 
206
    {
 
207
        char ch = chNext;
 
208
        chNext = styler.SafeGetCharAt(i + 1);
 
209
 
 
210
        if (fNewLine)            //Begin of a new line (The Sub/Function/Macro keywords may occur at begin of line only)
 
211
        {
 
212
            fNewLine=false;
 
213
            fBeginOfCommentFound=false;
 
214
            switch (ch)
 
215
            {
 
216
            case ' ':      //Most lines start with space - so check this first, the code is the same as for 'default:'
 
217
            case '\t':     //Handle tab too
 
218
                {
 
219
                    int levelUse = levelCurrent;
 
220
                    int lev = levelUse | levelNext << 16;
 
221
                    styler.SetLevel(lineCurrent, lev);
 
222
                    break;
 
223
                }
 
224
            case 'F':
 
225
            case 'f':
 
226
                {
 
227
                                        switch (chNext)
 
228
                                        {
 
229
                    case 'U':
 
230
                    case 'u':
 
231
                                                {
 
232
                                                        if( MatchUpperCase(styler,i,"FUNCTION") )
 
233
                                                        {
 
234
                                                                styler.SetLevel(lineCurrent, (SC_FOLDLEVELBASE << 16) | SC_FOLDLEVELHEADERFLAG);
 
235
                                                                levelNext=SC_FOLDLEVELBASE+1;
 
236
                                                        }
 
237
                                                        break;
 
238
                                                }
 
239
                                        }
 
240
                break;
 
241
                }
 
242
            case 'S':
 
243
            case 's':
 
244
                {
 
245
                                        switch (chNext)
 
246
                                        {
 
247
                    case 'U':
 
248
                    case 'u':
 
249
                                                {
 
250
                                                        if( MatchUpperCase(styler,i,"SUB") )
 
251
                                                        {
 
252
                                                                styler.SetLevel(lineCurrent, (SC_FOLDLEVELBASE << 16) | SC_FOLDLEVELHEADERFLAG);
 
253
                                                                levelNext=SC_FOLDLEVELBASE+1;
 
254
                                                        }
 
255
                                                        break;
 
256
                                                }
 
257
                    case 'T':
 
258
                    case 't':
 
259
                                                {
 
260
                                                        if( MatchUpperCase(styler,i,"STATIC FUNCTION") )
 
261
                                                        {
 
262
                                                                styler.SetLevel(lineCurrent, (SC_FOLDLEVELBASE << 16) | SC_FOLDLEVELHEADERFLAG);
 
263
                                                                levelNext=SC_FOLDLEVELBASE+1;
 
264
                                                        }
 
265
                                                        else if( MatchUpperCase(styler,i,"STATIC SUB") )
 
266
                                                        {
 
267
                                                                styler.SetLevel(lineCurrent, (SC_FOLDLEVELBASE << 16) | SC_FOLDLEVELHEADERFLAG);
 
268
                                                                levelNext=SC_FOLDLEVELBASE+1;
 
269
                                                        }
 
270
                                                        break;
 
271
                                                }
 
272
                                        }
 
273
                break;
 
274
                }
 
275
            case 'C':
 
276
            case 'c':
 
277
                {
 
278
                                        switch (chNext)
 
279
                                        {
 
280
                    case 'A':
 
281
                    case 'a':
 
282
                                                {
 
283
                                                        if( MatchUpperCase(styler,i,"CALLBACK FUNCTION") )
 
284
                                                        {
 
285
                                                                styler.SetLevel(lineCurrent, (SC_FOLDLEVELBASE << 16) | SC_FOLDLEVELHEADERFLAG);
 
286
                                                                levelNext=SC_FOLDLEVELBASE+1;
 
287
                                                        }
 
288
                                                        break;
 
289
                                                }
 
290
                                        }
 
291
                break;
 
292
                }
 
293
            case 'M':
 
294
            case 'm':
 
295
                {
 
296
                                        switch (chNext)
 
297
                                        {
 
298
                    case 'A':
 
299
                    case 'a':
 
300
                                                {
 
301
                                                        if( MatchUpperCase(styler,i,"MACRO") )
 
302
                                                        {
 
303
                                                                fMightBeMultiLineMacro=true;  //Set folder level at end of line, we have to check for single line macro
 
304
                                                        }
 
305
                                                        break;
 
306
                                                }
 
307
                                        }
 
308
                break;
 
309
                }
 
310
            default:
 
311
                {
 
312
                    int levelUse = levelCurrent;
 
313
                    int lev = levelUse | levelNext << 16;
 
314
                    styler.SetLevel(lineCurrent, lev);
 
315
                    break;
 
316
                }
 
317
            }  //switch (ch)
 
318
        }  //if( fNewLine )
 
319
 
 
320
        switch (ch)
 
321
        {
 
322
            case '=':                              //To test single line macros
 
323
            {
 
324
                if (fBeginOfCommentFound==false)
 
325
                    fMightBeMultiLineMacro=false;  //The found macro is a single line macro only;
 
326
                break;
 
327
            }
 
328
            case '\'':                             //A comment starts
 
329
            {
 
330
                fBeginOfCommentFound=true;
 
331
                break;
 
332
            }
 
333
            case '\n':
 
334
            {
 
335
                if (fMightBeMultiLineMacro)        //The current line is the begin of a multi line macro
 
336
                {
 
337
                    fMightBeMultiLineMacro=false;
 
338
                    styler.SetLevel(lineCurrent, (SC_FOLDLEVELBASE << 16) | SC_FOLDLEVELHEADERFLAG);
 
339
                    levelNext=SC_FOLDLEVELBASE+1;
 
340
                }
 
341
                lineCurrent++;
 
342
                levelCurrent = levelNext;
 
343
                fNewLine=true;
 
344
                break;
 
345
            }
 
346
            case '\r':
 
347
            {
 
348
                if (chNext != '\n')
 
349
                {
 
350
                    lineCurrent++;
 
351
                    levelCurrent = levelNext;
 
352
                    fNewLine=true;
 
353
                }
 
354
                break;
 
355
            }
 
356
        }  //switch (ch)
 
357
    }  //for (unsigned int i = startPos; i < endPos; i++)
 
358
}
 
359
 
 
360
static const char * const pbWordListDesc[] = {
 
361
    "Keywords",
 
362
    0
 
363
};
 
364
 
 
365
LexerModule lmPB(SCLEX_POWERBASIC, ColourisePBDoc, "powerbasic", FoldPBDoc, pbWordListDesc);