updated Scintilla to 2.29
[TortoiseGit.git] / ext / scintilla / lexers / LexBaan.cxx
blob1542150c4a014d2896daa57f7ead55a596b35830
1 // Scintilla source code edit control
2 /** @file LexBaan.cxx
3 ** Lexer for Baan.
4 ** Based heavily on LexCPP.cxx
5 **/
6 // Copyright 2001- by Vamsi Potluru <vamsi@who.net> & Praveen Ambekar <ambekarpraveen@yahoo.com>
7 // The License.txt file describes the conditions under which this software may be distributed.
9 #include <stdlib.h>
10 #include <string.h>
11 #include <stdio.h>
12 #include <stdarg.h>
13 #include <assert.h>
14 #include <ctype.h>
16 #include "ILexer.h"
17 #include "Scintilla.h"
18 #include "SciLexer.h"
20 #include "WordList.h"
21 #include "LexAccessor.h"
22 #include "Accessor.h"
23 #include "StyleContext.h"
24 #include "CharacterSet.h"
25 #include "LexerModule.h"
27 #ifdef SCI_NAMESPACE
28 using namespace Scintilla;
29 #endif
31 static inline bool IsAWordChar(const int ch) {
32 return (ch < 0x80) && (isalnum(ch) || ch == '.' || ch == '_' || ch == '$' || ch == ':');
35 static inline bool IsAWordStart(const int ch) {
36 return (ch < 0x80) && (isalnum(ch) || ch == '_');
39 static void ColouriseBaanDoc(unsigned int startPos, int length, int initStyle, WordList *keywordlists[],
40 Accessor &styler) {
42 WordList &keywords = *keywordlists[0];
43 WordList &keywords2 = *keywordlists[1];
44 bool stylingWithinPreprocessor = styler.GetPropertyInt("styling.within.preprocessor") != 0;
46 if (initStyle == SCE_BAAN_STRINGEOL) // Does not leak onto next line
47 initStyle = SCE_BAAN_DEFAULT;
49 int visibleChars = 0;
51 StyleContext sc(startPos, length, initStyle, styler);
53 for (; sc.More(); sc.Forward()) {
55 if (sc.state == SCE_BAAN_OPERATOR) {
56 sc.SetState(SCE_BAAN_DEFAULT);
57 } else if (sc.state == SCE_BAAN_NUMBER) {
58 if (!IsAWordChar(sc.ch)) {
59 sc.SetState(SCE_BAAN_DEFAULT);
61 } else if (sc.state == SCE_BAAN_IDENTIFIER) {
62 if (!IsAWordChar(sc.ch)) {
63 char s[100];
64 sc.GetCurrentLowered(s, sizeof(s));
65 if (keywords.InList(s)) {
66 sc.ChangeState(SCE_BAAN_WORD);
67 } else if (keywords2.InList(s)) {
68 sc.ChangeState(SCE_BAAN_WORD2);
70 sc.SetState(SCE_BAAN_DEFAULT);
72 } else if (sc.state == SCE_BAAN_PREPROCESSOR) {
73 if (stylingWithinPreprocessor) {
74 if (IsASpace(sc.ch)) {
75 sc.SetState(SCE_BAAN_DEFAULT);
77 } else {
78 if (sc.atLineEnd && (sc.chNext != '^')) {
79 sc.SetState(SCE_BAAN_DEFAULT);
82 } else if (sc.state == SCE_BAAN_COMMENT) {
83 if (sc.atLineEnd) {
84 sc.SetState(SCE_BAAN_DEFAULT);
86 } else if (sc.state == SCE_BAAN_COMMENTDOC) {
87 if (sc.MatchIgnoreCase("enddllusage")) {
88 for (unsigned int i = 0; i < 10; i++){
89 sc.Forward();
91 sc.ForwardSetState(SCE_BAAN_DEFAULT);
93 } else if (sc.state == SCE_BAAN_STRING) {
94 if (sc.ch == '\"') {
95 sc.ForwardSetState(SCE_BAAN_DEFAULT);
96 } else if ((sc.atLineEnd) && (sc.chNext != '^')) {
97 sc.ChangeState(SCE_BAAN_STRINGEOL);
98 sc.ForwardSetState(SCE_C_DEFAULT);
99 visibleChars = 0;
103 if (sc.state == SCE_BAAN_DEFAULT) {
104 if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {
105 sc.SetState(SCE_BAAN_NUMBER);
106 } else if (sc.MatchIgnoreCase("dllusage")){
107 sc.SetState(SCE_BAAN_COMMENTDOC);
108 do {
109 sc.Forward();
110 } while ((!sc.atLineEnd) && sc.More());
111 } else if (IsAWordStart(sc.ch)) {
112 sc.SetState(SCE_BAAN_IDENTIFIER);
113 } else if (sc.Match('|')){
114 sc.SetState(SCE_BAAN_COMMENT);
115 } else if (sc.ch == '\"') {
116 sc.SetState(SCE_BAAN_STRING);
117 } else if (sc.ch == '#' && visibleChars == 0) {
118 // Preprocessor commands are alone on their line
119 sc.SetState(SCE_BAAN_PREPROCESSOR);
120 // Skip whitespace between # and preprocessor word
121 do {
122 sc.Forward();
123 } while (IsASpace(sc.ch) && sc.More());
124 } else if (isoperator(static_cast<char>(sc.ch))) {
125 sc.SetState(SCE_BAAN_OPERATOR);
128 if (sc.atLineEnd) {
129 // Reset states to begining of colourise so no surprises
130 // if different sets of lines lexed.
131 visibleChars = 0;
133 if (!IsASpace(sc.ch)) {
134 visibleChars++;
137 sc.Complete();
140 static void FoldBaanDoc(unsigned int startPos, int length, int initStyle, WordList *[],
141 Accessor &styler) {
142 bool foldComment = styler.GetPropertyInt("fold.comment") != 0;
143 bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
144 unsigned int endPos = startPos + length;
145 int visibleChars = 0;
146 int lineCurrent = styler.GetLine(startPos);
147 int levelPrev = styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK;
148 int levelCurrent = levelPrev;
149 char chNext = styler[startPos];
150 int styleNext = styler.StyleAt(startPos);
151 int style = initStyle;
152 for (unsigned int i = startPos; i < endPos; i++) {
153 char ch = chNext;
154 chNext = styler.SafeGetCharAt(i + 1);
155 int stylePrev = style;
156 style = styleNext;
157 styleNext = styler.StyleAt(i + 1);
158 bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
159 if (foldComment &&
160 (style == SCE_BAAN_COMMENT || style == SCE_BAAN_COMMENTDOC)) {
161 if (style != stylePrev) {
162 levelCurrent++;
163 } else if ((style != styleNext) && !atEOL) {
164 // Comments don't end at end of line and the next character may be unstyled.
165 levelCurrent--;
168 if (style == SCE_BAAN_OPERATOR) {
169 if (ch == '{') {
170 levelCurrent++;
171 } else if (ch == '}') {
172 levelCurrent--;
175 if (atEOL) {
176 int lev = levelPrev;
177 if (visibleChars == 0 && foldCompact)
178 lev |= SC_FOLDLEVELWHITEFLAG;
179 if ((levelCurrent > levelPrev) && (visibleChars > 0))
180 lev |= SC_FOLDLEVELHEADERFLAG;
181 if (lev != styler.LevelAt(lineCurrent)) {
182 styler.SetLevel(lineCurrent, lev);
184 lineCurrent++;
185 levelPrev = levelCurrent;
186 visibleChars = 0;
188 if (!isspacechar(ch))
189 visibleChars++;
191 // Fill in the real level of the next line, keeping the current flags as they will be filled in later
192 int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK;
193 styler.SetLevel(lineCurrent, levelPrev | flagsNext);
196 LexerModule lmBaan(SCLEX_BAAN, ColouriseBaanDoc, "baan", FoldBaanDoc);