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 <ctype.h>
12 #include <stdio.h>
13 #include <stdarg.h>
14 
15 #include "Platform.h"
16 
17 #include "PropSet.h"
18 #include "Accessor.h"
19 #include "StyleContext.h"
20 #include "KeyWords.h"
21 #include "Scintilla.h"
22 #include "SciLexer.h"
23 
IsAWordChar(const int ch)24 static inline bool IsAWordChar(const int  ch) {
25 	return (ch < 0x80) && (isalnum(ch) || ch == '.' || ch == '_' || ch == '$' || ch == ':');
26 }
27 
IsAWordStart(const int ch)28 static inline bool IsAWordStart(const int ch) {
29 	return (ch < 0x80) && (isalnum(ch) || ch == '_');
30 }
31 
ColouriseBaanDoc(unsigned int startPos,int length,int initStyle,WordList * keywordlists[],Accessor & styler)32 static void ColouriseBaanDoc(unsigned int startPos, int length, int initStyle, WordList *keywordlists[],
33                             Accessor &styler) {
34 
35 	WordList &keywords = *keywordlists[0];
36 	WordList &keywords2 = *keywordlists[1];
37 	bool stylingWithinPreprocessor = styler.GetPropertyInt("styling.within.preprocessor") != 0;
38 
39 	if (initStyle == SCE_BAAN_STRINGEOL)	// Does not leak onto next line
40 		initStyle = SCE_BAAN_DEFAULT;
41 
42 	int visibleChars = 0;
43 
44 	StyleContext sc(startPos, length, initStyle, styler);
45 
46 	for (; sc.More(); sc.Forward()) {
47 
48 		if (sc.state == SCE_BAAN_OPERATOR) {
49 			sc.SetState(SCE_BAAN_DEFAULT);
50 		} else if (sc.state == SCE_BAAN_NUMBER) {
51 			if (!IsAWordChar(sc.ch)) {
52 				sc.SetState(SCE_BAAN_DEFAULT);
53 			}
54 		} else if (sc.state == SCE_BAAN_IDENTIFIER) {
55 			if (!IsAWordChar(sc.ch)) {
56 				char s[100];
57 				sc.GetCurrentLowered(s, sizeof(s));
58 				if (keywords.InList(s)) {
59 					sc.ChangeState(SCE_BAAN_WORD);
60 				} else if (keywords2.InList(s)) {
61 					sc.ChangeState(SCE_BAAN_WORD2);
62 				}
63 				sc.SetState(SCE_BAAN_DEFAULT);
64 			}
65 		} else if (sc.state == SCE_BAAN_PREPROCESSOR) {
66 			if (stylingWithinPreprocessor) {
67 				if (IsASpace(sc.ch)) {
68 					sc.SetState(SCE_BAAN_DEFAULT);
69 				}
70 			} else {
71 				if (sc.atLineEnd && (sc.chNext != '^')) {
72 					sc.SetState(SCE_BAAN_DEFAULT);
73 				}
74 			}
75 		} else if (sc.state == SCE_BAAN_COMMENT) {
76 			if (sc.atLineEnd) {
77 				sc.SetState(SCE_BAAN_DEFAULT);
78 			}
79 		} else if (sc.state == SCE_BAAN_COMMENTDOC) {
80 			if (sc.MatchIgnoreCase("enddllusage")) {
81 				for (unsigned int i = 0; i < 10; i++){
82 					sc.Forward();
83 				}
84 				sc.ForwardSetState(SCE_BAAN_DEFAULT);
85 			}
86 		} else if (sc.state == SCE_BAAN_STRING) {
87 			if (sc.ch == '\"') {
88 				sc.ForwardSetState(SCE_BAAN_DEFAULT);
89 			} else if ((sc.atLineEnd) && (sc.chNext != '^')) {
90 				sc.ChangeState(SCE_BAAN_STRINGEOL);
91 				sc.ForwardSetState(SCE_C_DEFAULT);
92 				visibleChars = 0;
93 			}
94 		}
95 
96 		if (sc.state == SCE_BAAN_DEFAULT) {
97 			if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {
98 				sc.SetState(SCE_BAAN_NUMBER);
99 			} else if (sc.MatchIgnoreCase("dllusage")){
100 					sc.SetState(SCE_BAAN_COMMENTDOC);
101 					do {
102 						sc.Forward();
103 					} while ((!sc.atLineEnd) && sc.More());
104 			} else if (IsAWordStart(sc.ch)) {
105 					sc.SetState(SCE_BAAN_IDENTIFIER);
106 			} else if (sc.Match('|')){
107 					sc.SetState(SCE_BAAN_COMMENT);
108 			} else if (sc.ch == '\"') {
109 				sc.SetState(SCE_BAAN_STRING);
110 			} else if (sc.ch == '#' && visibleChars == 0) {
111 				// Preprocessor commands are alone on their line
112 				sc.SetState(SCE_BAAN_PREPROCESSOR);
113 				// Skip whitespace between # and preprocessor word
114 				do {
115 					sc.Forward();
116 				} while (IsASpace(sc.ch) && sc.More());
117 			} else if (isoperator(static_cast<char>(sc.ch))) {
118 				sc.SetState(SCE_BAAN_OPERATOR);
119 			}
120 		}
121 		if (sc.atLineEnd) {
122 			// Reset states to begining of colourise so no surprises
123 			// if different sets of lines lexed.
124 			visibleChars = 0;
125 		}
126 		if (!IsASpace(sc.ch)) {
127 			visibleChars++;
128 		}
129 	}
130 	sc.Complete();
131 }
132 
FoldBaanDoc(unsigned int startPos,int length,int initStyle,WordList * [],Accessor & styler)133 static void FoldBaanDoc(unsigned int startPos, int length, int initStyle, WordList *[],
134                             Accessor &styler) {
135 	bool foldComment = styler.GetPropertyInt("fold.comment") != 0;
136 	bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
137 	unsigned int endPos = startPos + length;
138 	int visibleChars = 0;
139 	int lineCurrent = styler.GetLine(startPos);
140 	int levelPrev = styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK;
141 	int levelCurrent = levelPrev;
142 	char chNext = styler[startPos];
143 	int styleNext = styler.StyleAt(startPos);
144 	int style = initStyle;
145 	for (unsigned int i = startPos; i < endPos; i++) {
146 		char ch = chNext;
147 		chNext = styler.SafeGetCharAt(i + 1);
148 		int stylePrev = style;
149 		style = styleNext;
150 		styleNext = styler.StyleAt(i + 1);
151 		bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
152 		if (foldComment &&
153 			(style == SCE_BAAN_COMMENT || style == SCE_BAAN_COMMENTDOC)) {
154 			if (style != stylePrev) {
155 				levelCurrent++;
156 			} else if ((style != styleNext) && !atEOL) {
157 				// Comments don't end at end of line and the next character may be unstyled.
158 				levelCurrent--;
159 			}
160 		}
161 		if (style == SCE_BAAN_OPERATOR) {
162 			if (ch == '{') {
163 				levelCurrent++;
164 			} else if (ch == '}') {
165 				levelCurrent--;
166 			}
167 		}
168 		if (atEOL) {
169 			int lev = levelPrev;
170 			if (visibleChars == 0 && foldCompact)
171 				lev |= SC_FOLDLEVELWHITEFLAG;
172 			if ((levelCurrent > levelPrev) && (visibleChars > 0))
173 				lev |= SC_FOLDLEVELHEADERFLAG;
174 			if (lev != styler.LevelAt(lineCurrent)) {
175 				styler.SetLevel(lineCurrent, lev);
176 			}
177 			lineCurrent++;
178 			levelPrev = levelCurrent;
179 			visibleChars = 0;
180 		}
181 		if (!isspacechar(ch))
182 			visibleChars++;
183 	}
184 	// Fill in the real level of the next line, keeping the current flags as they will be filled in later
185 	int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK;
186 	styler.SetLevel(lineCurrent, levelPrev | flagsNext);
187 }
188 
189 LexerModule lmBaan(SCLEX_BAAN, ColouriseBaanDoc, "baan", FoldBaanDoc);
190