]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/LexPOV.cxx
1e44450bbc70419c749b531df63c5eed5e85c817
[wxWidgets.git] / src / stc / scintilla / src / LexPOV.cxx
1 // Scintilla source code edit control
2 /** @file LexPOV.cxx
3 ** Lexer for POV-Ray, based on lexer for C++.
4 **/
5 // Copyright 2003 by Steven te Brinke <steven.t.b@zonnet.nl>
6 // The License.txt file describes the conditions under which this software may be distributed.
7
8 #include <stdlib.h>
9 #include <string.h>
10 #include <ctype.h>
11 #include <stdio.h>
12 #include <stdarg.h>
13
14 #include "Platform.h"
15
16 #include "PropSet.h"
17 #include "Accessor.h"
18 #include "StyleContext.h"
19 #include "KeyWords.h"
20 #include "Scintilla.h"
21 #include "SciLexer.h"
22
23 #define KEYWORD_BOXHEADER 1
24 #define KEYWORD_FOLDCONTRACTED 2
25
26 static inline bool IsAWordChar(const int ch) {
27 return (ch < 0x80) && (isalnum(ch) || ch == '.' || ch == '_');
28 }
29
30 static inline bool IsAWordStart(const int ch) {
31 return (ch < 0x80) && (isalnum(ch) || ch == '_');
32 }
33
34 static inline bool IsStateComment(const int state) {
35 return ((state == SCE_POV_COMMENT) ||
36 (state == SCE_POV_COMMENTLINE) ||
37 (state == SCE_POV_COMMENTDOC));
38 }
39
40 static inline bool IsStateString(const int state) {
41 return ((state == SCE_POV_STRING));
42 }
43
44 static void ColourisePOVDoc(unsigned int startPos, int length, int initStyle, WordList *keywordlists[],
45 Accessor &styler) {
46
47 WordList &keywords = *keywordlists[0];
48 WordList &keywords2 = *keywordlists[1];
49
50 // Do not leak onto next line
51 /*if (initStyle == SCE_POV_STRINGEOL)
52 initStyle = SCE_POV_DEFAULT;*/
53
54 StyleContext sc(startPos, length, initStyle, styler);
55
56 bool caseSensitive = styler.GetPropertyInt("pov.case.sensitive", 1) != 0;
57
58 for (; sc.More(); sc.Forward()) {
59
60 /*if (sc.atLineStart && (sc.state == SCE_POV_STRING)) {
61 // Prevent SCE_POV_STRINGEOL from leaking back to previous line
62 sc.SetState(SCE_POV_STRING);
63 }*/
64
65 // Handle line continuation generically.
66 if (sc.ch == '\\') {
67 if (sc.chNext == '\n' || sc.chNext == '\r') {
68 sc.Forward();
69 if (sc.ch == '\r' && sc.chNext == '\n') {
70 sc.Forward();
71 }
72 continue;
73 }
74 }
75
76 // Determine if the current state should terminate.
77 if (sc.state == SCE_POV_OPERATOR || sc.state == SCE_POV_BRACE) {
78 sc.SetState(SCE_POV_DEFAULT);
79 } else if (sc.state == SCE_POV_NUMBER) {
80 if (!IsADigit(sc.ch) || sc.ch != '.') {
81 sc.SetState(SCE_POV_DEFAULT);
82 }
83 } else if (sc.state == SCE_POV_IDENTIFIER) {
84 if (!IsAWordChar(sc.ch) || (sc.ch == '.')) {
85 char s[100];
86 if (caseSensitive) {
87 sc.GetCurrent(s, sizeof(s));
88 } else {
89 sc.GetCurrentLowered(s, sizeof(s));
90 }
91 if (keywords.InList(s)) {
92 sc.ChangeState(SCE_POV_WORD);
93 } else if (keywords2.InList(s)) {
94 sc.ChangeState(SCE_POV_WORD2);
95 }
96 sc.SetState(SCE_POV_DEFAULT);
97 }
98 } else if (sc.state == SCE_POV_COMMENT) {
99 if (sc.Match('*', '/')) {
100 sc.Forward();
101 sc.ForwardSetState(SCE_POV_DEFAULT);
102 }
103 } else if (sc.state == SCE_POV_COMMENTDOC) {
104 if (sc.Match('*', '/')) {
105 sc.Forward();
106 sc.ForwardSetState(SCE_POV_DEFAULT);
107 }
108 } else if (sc.state == SCE_POV_COMMENTLINE) {
109 if (sc.atLineEnd) {
110 sc.SetState(SCE_POV_DEFAULT);
111 }
112 } else if (sc.state == SCE_POV_STRING) {
113 if (sc.ch == '\\') {
114 if (sc.chNext == '\"' || sc.chNext == '\\') {
115 sc.Forward();
116 }
117 } else if (sc.ch == '\"') {
118 sc.ForwardSetState(SCE_POV_DEFAULT);
119 }
120 }
121
122 // Determine if a new state should be entered.
123 if (sc.state == SCE_POV_DEFAULT) {
124 if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {
125 sc.SetState(SCE_POV_NUMBER);
126 } else if (IsAWordStart(sc.ch) || (sc.ch == '#')) {
127 sc.SetState(SCE_POV_IDENTIFIER);
128 } else if (sc.Match('/', '*')) {
129 sc.SetState(SCE_POV_COMMENT);
130 sc.Forward(); // Eat the * so it isn't used for the end of the comment
131 } else if (sc.Match('/', '/')) {
132 sc.SetState(SCE_POV_COMMENTLINE);
133 } else if (sc.ch == '\"') {
134 sc.SetState(SCE_POV_STRING);
135 //} else if (isoperator(static_cast<char>(sc.ch))) {
136 } else if (sc.ch == '+' || sc.ch == '-' || sc.ch == '*' || sc.ch == '/' || sc.ch == '=' || sc.ch == '<' || sc.ch == '>' || sc.ch == '&' || sc.ch == '|' || sc.ch == '!' || sc.ch == '?' || sc.ch == ':') {
137 sc.SetState(SCE_POV_OPERATOR);
138 } else if (sc.ch == '{' || sc.ch == '}') {
139 sc.SetState(SCE_POV_BRACE);
140 }
141 }
142
143 }
144 sc.Complete();
145 }
146
147 static bool IsStreamCommentStyle(int style) {
148 return style == SCE_POV_COMMENT ||
149 style == SCE_POV_COMMENTDOC;
150 }
151
152 static void FoldNoBoxPOVDoc(unsigned int startPos, int length, int initStyle,
153 Accessor &styler) {
154 bool foldComment = styler.GetPropertyInt("fold.comment", 1) != 0;
155 bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
156 unsigned int endPos = startPos + length;
157 int visibleChars = 0;
158 int lineCurrent = styler.GetLine(startPos);
159 int levelPrev = styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK;
160 int levelCurrent = levelPrev;
161 char chNext = styler[startPos];
162 int styleNext = styler.StyleAt(startPos);
163 int style = initStyle;
164 for (unsigned int i = startPos; i < endPos; i++) {
165 char ch = chNext;
166 chNext = styler.SafeGetCharAt(i + 1);
167 int stylePrev = style;
168 style = styleNext;
169 styleNext = styler.StyleAt(i + 1);
170 bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
171 if (foldComment && IsStreamCommentStyle(style)) {
172 if (!IsStreamCommentStyle(stylePrev)) {
173 levelCurrent++;
174 } else if (!IsStreamCommentStyle(styleNext) && !atEOL) {
175 // Comments don't end at end of line and the next character may be unstyled.
176 levelCurrent--;
177 }
178 }
179 if (style == SCE_POV_BRACE) {
180 if (ch == '{') {
181 levelCurrent++;
182 } else if (ch == '}') {
183 levelCurrent--;
184 }
185 }
186 if (atEOL) {
187 int lev = levelPrev;
188 if (visibleChars == 0 && foldCompact)
189 lev |= SC_FOLDLEVELWHITEFLAG;
190 if ((levelCurrent > levelPrev) && (visibleChars > 0))
191 lev |= SC_FOLDLEVELHEADERFLAG;
192 if (lev != styler.LevelAt(lineCurrent)) {
193 styler.SetLevel(lineCurrent, lev);
194 }
195 lineCurrent++;
196 levelPrev = levelCurrent;
197 visibleChars = 0;
198 }
199 if (!isspacechar(ch))
200 visibleChars++;
201 }
202 // Fill in the real level of the next line, keeping the current flags as they will be filled in later
203 int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK;
204 styler.SetLevel(lineCurrent, levelPrev | flagsNext);
205 }
206
207 static void FoldPOVDoc(unsigned int startPos, int length, int initStyle, WordList *[], Accessor &styler) {
208 FoldNoBoxPOVDoc(startPos, length, initStyle, styler);
209 }
210
211 static const char * const POVWordLists[] = {
212 "Primary keywords and identifiers",
213 "Secondary keywords and identifiers",
214 0,
215 };
216
217 static void ColourisePOVDocSensitive(unsigned int startPos, int length, int initStyle, WordList *keywordlists[],
218 Accessor &styler) {
219 ColourisePOVDoc(startPos, length, initStyle, keywordlists, styler);
220 }
221
222 LexerModule lmPOV(SCLEX_POV, ColourisePOVDocSensitive, "pov", FoldPOVDoc, POVWordLists);