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.
8 
9 #include <stdlib.h>
10 #include <string.h>
11 #include <stdio.h>
12 #include <stdarg.h>
13 #include <assert.h>
14 #include <ctype.h>
15 
16 #include "ILexer.h"
17 #include "Scintilla.h"
18 #include "SciLexer.h"
19 
20 #include "WordList.h"
21 #include "LexAccessor.h"
22 #include "Accessor.h"
23 #include "StyleContext.h"
24 #include "CharacterSet.h"
25 #include "LexerModule.h"
26 
27 #ifdef SCI_NAMESPACE
28 using namespace Scintilla;
29 #endif
30 
IsAWordChar(const int ch)31 static inline bool IsAWordChar(const int  ch) {
32 	return (ch < 0x80) && (isalnum(ch) || ch == '.' || ch == '_' || ch == '$' || ch == ':');
33 }
34 
IsAWordStart(const int ch)35 static inline bool IsAWordStart(const int ch) {
36 	return (ch < 0x80) && (isalnum(ch) || ch == '_');
37 }
38 
ColouriseBaanDoc(unsigned int startPos,int length,int initStyle,WordList * keywordlists[],Accessor & styler)39 static void ColouriseBaanDoc(unsigned int startPos, int length, int initStyle, WordList *keywordlists[],
40                             Accessor &styler) {
41 
42 	WordList &keywords = *keywordlists[0];
43 	WordList &keywords2 = *keywordlists[1];
44 	bool stylingWithinPreprocessor = styler.GetPropertyInt("styling.within.preprocessor") != 0;
45 
46 	if (initStyle == SCE_BAAN_STRINGEOL)	// Does not leak onto next line
47 		initStyle = SCE_BAAN_DEFAULT;
48 
49 	int visibleChars = 0;
50 
51 	StyleContext sc(startPos, length, initStyle, styler);
52 
53 	for (; sc.More(); sc.Forward()) {
54 
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);
60 			}
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);
69 				}
70 				sc.SetState(SCE_BAAN_DEFAULT);
71 			}
72 		} else if (sc.state == SCE_BAAN_PREPROCESSOR) {
73 			if (stylingWithinPreprocessor) {
74 				if (IsASpace(sc.ch)) {
75 					sc.SetState(SCE_BAAN_DEFAULT);
76 				}
77 			} else {
78 				if (sc.atLineEnd && (sc.chNext != '^')) {
79 					sc.SetState(SCE_BAAN_DEFAULT);
80 				}
81 			}
82 		} else if (sc.state == SCE_BAAN_COMMENT) {
83 			if (sc.atLineEnd) {
84 				sc.SetState(SCE_BAAN_DEFAULT);
85 			}
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();
90 				}
91 				sc.ForwardSetState(SCE_BAAN_DEFAULT);
92 			}
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;
100 			}
101 		}
102 
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);
126 			}
127 		}
128 		if (sc.atLineEnd) {
129 			// Reset states to begining of colourise so no surprises
130 			// if different sets of lines lexed.
131 			visibleChars = 0;
132 		}
133 		if (!IsASpace(sc.ch)) {
134 			visibleChars++;
135 		}
136 	}
137 	sc.Complete();
138 }
139 
FoldBaanDoc(unsigned int startPos,int length,int initStyle,WordList * [],Accessor & styler)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--;
166 			}
167 		}
168 		if (style == SCE_BAAN_OPERATOR) {
169 			if (ch == '{') {
170 				levelCurrent++;
171 			} else if (ch == '}') {
172 				levelCurrent--;
173 			}
174 		}
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);
183 			}
184 			lineCurrent++;
185 			levelPrev = levelCurrent;
186 			visibleChars = 0;
187 		}
188 		if (!isspacechar(ch))
189 			visibleChars++;
190 	}
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);
194 }
195 
196 LexerModule lmBaan(SCLEX_BAAN, ColouriseBaanDoc, "baan", FoldBaanDoc);
197