]>
git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/LexPOV.cxx
1 // Scintilla source code edit control
3 ** Lexer for POV-Ray SDL (Persistance of Vision Raytracer, Scene Description Language).
4 ** Written by Philippe Lhoste but this is mostly a derivative of LexCPP...
6 // Copyright 1998-2005 by Neil Hodgson <neilh@scintilla.org>
7 // The License.txt file describes the conditions under which this software may be distributed.
9 // Some points that distinguish from a simple C lexer:
10 // Identifiers start only by a character.
11 // No line continuation character.
12 // Strings are limited to 256 characters.
13 // Directives are similar to preprocessor commands,
14 // but we match directive keywords and colorize incorrect ones.
15 // Block comments can be nested (code stolen from my code in LexLua).
27 #include "StyleContext.h"
29 #include "Scintilla.h"
32 static inline bool IsAWordChar(int ch
) {
33 return ch
< 0x80 && (isalnum(ch
) || ch
== '_');
36 static inline bool IsAWordStart(int ch
) {
37 return ch
< 0x80 && isalpha(ch
);
40 static inline bool IsANumberChar(int ch
) {
41 // Not exactly following number definition (several dots are seen as OK, etc.)
42 // but probably enough in most cases.
44 (isdigit(ch
) || toupper(ch
) == 'E' ||
45 ch
== '.' || ch
== '-' || ch
== '+');
48 static void ColourisePovDoc(
49 unsigned int startPos
,
52 WordList
*keywordlists
[],
55 WordList
&keywords1
= *keywordlists
[0];
56 WordList
&keywords2
= *keywordlists
[1];
57 WordList
&keywords3
= *keywordlists
[2];
58 WordList
&keywords4
= *keywordlists
[3];
59 WordList
&keywords5
= *keywordlists
[4];
60 WordList
&keywords6
= *keywordlists
[5];
61 WordList
&keywords7
= *keywordlists
[6];
62 WordList
&keywords8
= *keywordlists
[7];
64 int currentLine
= styler
.GetLine(startPos
);
65 // Initialize the block comment /* */ nesting level, if we are inside such a comment.
66 int blockCommentLevel
= 0;
67 if (initStyle
== SCE_POV_COMMENT
) {
68 blockCommentLevel
= styler
.GetLineState(currentLine
- 1);
71 // Do not leak onto next line
72 if (initStyle
== SCE_POV_STRINGEOL
|| initStyle
== SCE_POV_COMMENTLINE
) {
73 initStyle
= SCE_POV_DEFAULT
;
78 StyleContext
sc(startPos
, length
, initStyle
, styler
);
80 for (; sc
.More(); sc
.Forward()) {
82 // Update the line state, so it can be seen by next line
83 currentLine
= styler
.GetLine(sc
.currentPos
);
84 if (sc
.state
== SCE_POV_COMMENT
) {
85 // Inside a block comment, we set the line state
86 styler
.SetLineState(currentLine
, blockCommentLevel
);
88 // Reset the line state
89 styler
.SetLineState(currentLine
, 0);
93 if (sc
.atLineStart
&& (sc
.state
== SCE_POV_STRING
)) {
94 // Prevent SCE_POV_STRINGEOL from leaking back to previous line
95 sc
.SetState(SCE_POV_STRING
);
98 // Determine if the current state should terminate.
99 if (sc
.state
== SCE_POV_OPERATOR
) {
100 sc
.SetState(SCE_POV_DEFAULT
);
101 } else if (sc
.state
== SCE_POV_NUMBER
) {
102 // We stop the number definition on non-numerical non-dot non-eE non-sign char
103 if (!IsANumberChar(sc
.ch
)) {
104 sc
.SetState(SCE_POV_DEFAULT
);
106 } else if (sc
.state
== SCE_POV_IDENTIFIER
) {
107 if (!IsAWordChar(sc
.ch
)) {
109 sc
.GetCurrent(s
, sizeof(s
));
110 if (keywords2
.InList(s
)) {
111 sc
.ChangeState(SCE_POV_WORD2
);
112 } else if (keywords3
.InList(s
)) {
113 sc
.ChangeState(SCE_POV_WORD3
);
114 } else if (keywords4
.InList(s
)) {
115 sc
.ChangeState(SCE_POV_WORD4
);
116 } else if (keywords5
.InList(s
)) {
117 sc
.ChangeState(SCE_POV_WORD5
);
118 } else if (keywords6
.InList(s
)) {
119 sc
.ChangeState(SCE_POV_WORD6
);
120 } else if (keywords7
.InList(s
)) {
121 sc
.ChangeState(SCE_POV_WORD7
);
122 } else if (keywords8
.InList(s
)) {
123 sc
.ChangeState(SCE_POV_WORD8
);
125 sc
.SetState(SCE_POV_DEFAULT
);
127 } else if (sc
.state
== SCE_POV_DIRECTIVE
) {
128 if (!IsAWordChar(sc
.ch
)) {
131 sc
.GetCurrent(s
, sizeof(s
));
133 // Skip # and whitespace between # and directive word
136 } while ((*p
== ' ' || *p
== '\t') && *p
!= '\0');
137 if (!keywords1
.InList(p
)) {
138 sc
.ChangeState(SCE_POV_BADDIRECTIVE
);
140 sc
.SetState(SCE_POV_DEFAULT
);
142 } else if (sc
.state
== SCE_POV_COMMENT
) {
143 if (sc
.Match('/', '*')) {
146 } else if (sc
.Match('*', '/') && blockCommentLevel
> 0) {
149 if (blockCommentLevel
== 0) {
150 sc
.ForwardSetState(SCE_POV_DEFAULT
);
153 } else if (sc
.state
== SCE_POV_COMMENTLINE
) {
155 sc
.ForwardSetState(SCE_POV_DEFAULT
);
157 } else if (sc
.state
== SCE_POV_STRING
) {
160 if (strchr("abfnrtuv0'\"", sc
.chNext
)) {
161 // Compound characters are counted as one.
162 // Note: for Unicode chars \u, we shouldn't count the next 4 digits...
165 } else if (sc
.ch
== '\"') {
166 sc
.ForwardSetState(SCE_POV_DEFAULT
);
167 } else if (sc
.atLineEnd
) {
168 sc
.ChangeState(SCE_POV_STRINGEOL
);
169 sc
.ForwardSetState(SCE_POV_DEFAULT
);
173 if (stringLen
> 256) {
174 // Strings are limited to 256 chars
175 sc
.SetState(SCE_POV_STRINGEOL
);
177 } else if (sc
.state
== SCE_POV_STRINGEOL
) {
179 if (sc
.chNext
== '\"' || sc
.chNext
== '\\') {
182 } else if (sc
.ch
== '\"') {
183 sc
.ForwardSetState(SCE_C_DEFAULT
);
184 } else if (sc
.atLineEnd
) {
185 sc
.ForwardSetState(SCE_POV_DEFAULT
);
189 // Determine if a new state should be entered.
190 if (sc
.state
== SCE_POV_DEFAULT
) {
191 if (IsADigit(sc
.ch
) || (sc
.ch
== '.' && IsADigit(sc
.chNext
))) {
192 sc
.SetState(SCE_POV_NUMBER
);
193 } else if (IsAWordStart(sc
.ch
)) {
194 sc
.SetState(SCE_POV_IDENTIFIER
);
195 } else if (sc
.Match('/', '*')) {
196 blockCommentLevel
= 1;
197 sc
.SetState(SCE_POV_COMMENT
);
198 sc
.Forward(); // Eat the * so it isn't used for the end of the comment
199 } else if (sc
.Match('/', '/')) {
200 sc
.SetState(SCE_POV_COMMENTLINE
);
201 } else if (sc
.ch
== '\"') {
202 sc
.SetState(SCE_POV_STRING
);
204 } else if (sc
.ch
== '#') {
205 sc
.SetState(SCE_POV_DIRECTIVE
);
206 // Skip whitespace between # and directive word
209 } while ((sc
.ch
== ' ' || sc
.ch
== '\t') && sc
.More());
211 sc
.SetState(SCE_POV_DEFAULT
);
213 } else if (isoperator(static_cast<char>(sc
.ch
))) {
214 sc
.SetState(SCE_POV_OPERATOR
);
221 static void FoldPovDoc(
222 unsigned int startPos
,
228 bool foldComment
= styler
.GetPropertyInt("fold.comment") != 0;
229 bool foldDirective
= styler
.GetPropertyInt("fold.directive") != 0;
230 bool foldCompact
= styler
.GetPropertyInt("fold.compact", 1) != 0;
231 unsigned int endPos
= startPos
+ length
;
232 int visibleChars
= 0;
233 int lineCurrent
= styler
.GetLine(startPos
);
234 int levelPrev
= styler
.LevelAt(lineCurrent
) & SC_FOLDLEVELNUMBERMASK
;
235 int levelCurrent
= levelPrev
;
236 char chNext
= styler
[startPos
];
237 int styleNext
= styler
.StyleAt(startPos
);
238 int style
= initStyle
;
239 for (unsigned int i
= startPos
; i
< endPos
; i
++) {
241 chNext
= styler
.SafeGetCharAt(i
+ 1);
242 int stylePrev
= style
;
244 styleNext
= styler
.StyleAt(i
+ 1);
245 bool atEOL
= (ch
== '\r' && chNext
!= '\n') || (ch
== '\n');
246 if (foldComment
&& (style
== SCE_POV_COMMENT
)) {
247 if (stylePrev
!= SCE_POV_COMMENT
) {
249 } else if ((styleNext
!= SCE_POV_COMMENT
) && !atEOL
) {
250 // Comments don't end at end of line and the next character may be unstyled.
254 if (foldComment
&& (style
== SCE_POV_COMMENTLINE
)) {
255 if ((ch
== '/') && (chNext
== '/')) {
256 char chNext2
= styler
.SafeGetCharAt(i
+ 2);
257 if (chNext2
== '{') {
259 } else if (chNext2
== '}') {
264 if (foldDirective
&& (style
== SCE_POV_DIRECTIVE
)) {
267 while ((j
<endPos
) && IsASpaceOrTab(styler
.SafeGetCharAt(j
))) {
272 if (style
== SCE_POV_OPERATOR
) {
275 } else if (ch
== '}') {
281 if (visibleChars
== 0 && foldCompact
)
282 lev
|= SC_FOLDLEVELWHITEFLAG
;
283 if ((levelCurrent
> levelPrev
) && (visibleChars
> 0))
284 lev
|= SC_FOLDLEVELHEADERFLAG
;
285 if (lev
!= styler
.LevelAt(lineCurrent
)) {
286 styler
.SetLevel(lineCurrent
, lev
);
289 levelPrev
= levelCurrent
;
292 if (!isspacechar(ch
))
295 // Fill in the real level of the next line, keeping the current flags as they will be filled in later
296 int flagsNext
= styler
.LevelAt(lineCurrent
) & ~SC_FOLDLEVELNUMBERMASK
;
297 styler
.SetLevel(lineCurrent
, levelPrev
| flagsNext
);
300 static const char * const povWordLists
[] = {
301 "Language directives",
302 "Objects & CSG & Appearance",
303 "Types & Modifiers & Items",
304 "Predefined Identifiers",
305 "Predefined Functions",
312 LexerModule
lmPOV(SCLEX_POV
, ColourisePovDoc
, "pov", FoldPovDoc
, povWordLists
);