~ubuntu-branches/ubuntu/raring/geany/raring-proposed

« back to all changes in this revision

Viewing changes to scintilla/LexCPP.cxx

  • Committer: Bazaar Package Importer
  • Author(s): Damián Viano
  • Date: 2008-05-02 11:37:45 UTC
  • mto: (3.1.1 lenny) (1.3.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: james.westby@ubuntu.com-20080502113745-7q62rqhl2ku02ptu
Import upstream version 0.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
#include "KeyWords.h"
20
20
#include "Scintilla.h"
21
21
#include "SciLexer.h"
22
 
 
23
 
#define SET_LOWER "abcdefghijklmnopqrstuvwxyz"
24
 
#define SET_UPPER "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
25
 
#define SET_DIGITS "0123456789"
26
 
 
27
 
class SetOfCharacters {
28
 
        int size;
29
 
        bool valueAfter;
30
 
        bool *bset;
31
 
public:
32
 
        SetOfCharacters(const char *setOfCharacters, int size_=0x80, bool valueAfter_=false) {
33
 
                size = size_;
34
 
                valueAfter = valueAfter_;
35
 
                bset = new bool[size];
36
 
                for (int i=0; i < size; i++) {
37
 
                        bset[i] = false;
38
 
                }
39
 
                for (const char *cp=setOfCharacters; *cp; cp++) {
40
 
                        int val = static_cast<unsigned char>(*cp);
41
 
                        PLATFORM_ASSERT(val >= 0);
42
 
                        PLATFORM_ASSERT(val < size);
43
 
                        bset[val] = true;
44
 
                }
45
 
        }
46
 
        ~SetOfCharacters() {
47
 
                delete []bset;
48
 
                bset = 0;
49
 
                size = 0;
50
 
        }
51
 
        void Add(int val) {
52
 
                PLATFORM_ASSERT(val >= 0);
53
 
                PLATFORM_ASSERT(val < size);
54
 
                bset[val] = true;
55
 
        }
56
 
        bool Contains(int val) {
57
 
                PLATFORM_ASSERT(val >= 0);
58
 
                return (val < size) ? bset[val] : valueAfter;
59
 
        }
60
 
};
 
22
#include "CharacterSet.h"
 
23
 
 
24
#ifdef SCI_NAMESPACE
 
25
using namespace Scintilla;
 
26
#endif
61
27
 
62
28
static bool IsSpaceEquiv(int state) {
63
29
        return (state <= SCE_C_COMMENTDOC) ||
66
32
                (state == SCE_C_COMMENTDOCKEYWORDERROR);
67
33
}
68
34
 
 
35
// Preconditions: sc.currentPos points to a character after '+' or '-'.
 
36
// The test for pos reaching 0 should be redundant,
 
37
// and is in only for safety measures.
 
38
// Limitation: this code will give the incorrect answer for code like
 
39
// a = b+++/ptn/...
 
40
// Putting a space between the '++' post-inc operator and the '+' binary op
 
41
// fixes this, and is highly recommended for readability anyway.
 
42
static bool FollowsPostfixOperator(StyleContext &sc, Accessor &styler) {
 
43
        int pos = (int) sc.currentPos;
 
44
        while (--pos > 0) {
 
45
                char ch = styler[pos];
 
46
                if (ch == '+' || ch == '-') {
 
47
                        return styler[pos - 1] == ch;
 
48
                }
 
49
        }
 
50
        return false;
 
51
}
 
52
 
69
53
static void ColouriseCppDoc(unsigned int startPos, int length, int initStyle, WordList *keywordlists[],
70
54
                            Accessor &styler, bool caseSensitive) {
71
55
 
76
60
 
77
61
        bool stylingWithinPreprocessor = styler.GetPropertyInt("styling.within.preprocessor") != 0;
78
62
 
79
 
        SetOfCharacters setOKBeforeRE("(=,");
80
 
 
81
 
        SetOfCharacters setDoxygen("$@\\&<>#{}[]" SET_LOWER);
82
 
 
83
 
        SetOfCharacters setWordStart("_" SET_LOWER SET_UPPER, 0x80, true);
84
 
        SetOfCharacters setWord("._" SET_LOWER SET_UPPER SET_DIGITS, 0x80, true);
 
63
        CharacterSet setOKBeforeRE(CharacterSet::setNone, "([{=,:;!%^&*|?~+-");
 
64
        CharacterSet setCouldBePostOp(CharacterSet::setNone, "+-");
 
65
 
 
66
        CharacterSet setDoxygen(CharacterSet::setLower, "$@\\&<>#{}[]");
 
67
 
 
68
        CharacterSet setWordStart(CharacterSet::setAlpha, "_", 0x80, true);
 
69
        CharacterSet setWord(CharacterSet::setAlphaNum, "._", 0x80, true);
85
70
        if (styler.GetPropertyInt("lexer.cpp.allow.dollars", 1) != 0) {
86
71
                setWordStart.Add('$');
87
72
                setWord.Add('$');
331
316
                                        sc.SetState(SCE_C_COMMENTLINEDOC);
332
317
                                else
333
318
                                        sc.SetState(SCE_C_COMMENTLINE);
334
 
                        } else if (sc.ch == '/' && setOKBeforeRE.Contains(chPrevNonWhite)) {
 
319
                        } else if (sc.ch == '/' && setOKBeforeRE.Contains(chPrevNonWhite) &&
 
320
                                (!setCouldBePostOp.Contains(chPrevNonWhite) || !FollowsPostfixOperator(sc, styler))) {
335
321
                                sc.SetState(SCE_C_REGEX);       // JavaScript's RegEx
336
322
                        } else if (sc.ch == '\"') {
337
323
                                sc.SetState(SCE_C_STRING);
371
357
// Store both the current line's fold level and the next lines in the
372
358
// level store to make it easy to pick up with each increment
373
359
// and to make it possible to fiddle the current level for "} else {".
374
 
static void FoldNoBoxCppDoc(unsigned int startPos, int length, int initStyle,
375
 
                            Accessor &styler) {
 
360
static void FoldCppDoc(unsigned int startPos, int length, int initStyle,
 
361
                                           WordList *[], Accessor &styler) {
376
362
        bool foldComment = styler.GetPropertyInt("fold.comment") != 0;
377
363
        bool foldPreprocessor = styler.GetPropertyInt("fold.preprocessor") != 0;
378
364
        bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
396
382
                styleNext = styler.StyleAt(i + 1);
397
383
                bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
398
384
                if (foldComment && IsStreamCommentStyle(style)) {
399
 
                        if (!IsStreamCommentStyle(stylePrev)) {
 
385
                        if (!IsStreamCommentStyle(stylePrev) && (stylePrev != SCE_C_COMMENTLINEDOC)) {
400
386
                                levelNext++;
401
 
                        } else if (!IsStreamCommentStyle(styleNext) && !atEOL) {
 
387
                        } else if (!IsStreamCommentStyle(styleNext) && (styleNext != SCE_C_COMMENTLINEDOC) && !atEOL) {
402
388
                                // Comments don't end at end of line and the next character may be unstyled.
403
389
                                levelNext--;
404
390
                        }
438
424
                                levelNext--;
439
425
                        }
440
426
                }
441
 
                if (atEOL) {
 
427
                if (!IsASpace(ch))
 
428
                        visibleChars++;
 
429
                if (atEOL || (i == endPos-1)) {
442
430
                        int levelUse = levelCurrent;
443
431
                        if (foldAtElse) {
444
432
                                levelUse = levelMinCurrent;
456
444
                        levelMinCurrent = levelCurrent;
457
445
                        visibleChars = 0;
458
446
                }
459
 
                if (!IsASpace(ch))
460
 
                        visibleChars++;
461
447
        }
462
448
}
463
449
 
464
 
static void FoldCppDoc(unsigned int startPos, int length, int initStyle, WordList *[],
465
 
                       Accessor &styler) {
466
 
        FoldNoBoxCppDoc(startPos, length, initStyle, styler);
467
 
}
468
 
 
469
450
static const char * const cppWordLists[] = {
470
451
            "Primary keywords and identifiers",
471
452
            "Secondary keywords and identifiers",