]>
git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/LexPOV.cxx
1e44450bbc70419c749b531df63c5eed5e85c817
1 // Scintilla source code edit control
3 ** Lexer for POV-Ray, based on lexer for C++.
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.
18 #include "StyleContext.h"
20 #include "Scintilla.h"
23 #define KEYWORD_BOXHEADER 1
24 #define KEYWORD_FOLDCONTRACTED 2
26 static inline bool IsAWordChar(const int ch
) {
27 return (ch
< 0x80) && (isalnum(ch
) || ch
== '.' || ch
== '_');
30 static inline bool IsAWordStart(const int ch
) {
31 return (ch
< 0x80) && (isalnum(ch
) || ch
== '_');
34 static inline bool IsStateComment(const int state
) {
35 return ((state
== SCE_POV_COMMENT
) ||
36 (state
== SCE_POV_COMMENTLINE
) ||
37 (state
== SCE_POV_COMMENTDOC
));
40 static inline bool IsStateString(const int state
) {
41 return ((state
== SCE_POV_STRING
));
44 static void ColourisePOVDoc(unsigned int startPos
, int length
, int initStyle
, WordList
*keywordlists
[],
47 WordList
&keywords
= *keywordlists
[0];
48 WordList
&keywords2
= *keywordlists
[1];
50 // Do not leak onto next line
51 /*if (initStyle == SCE_POV_STRINGEOL)
52 initStyle = SCE_POV_DEFAULT;*/
54 StyleContext
sc(startPos
, length
, initStyle
, styler
);
56 bool caseSensitive
= styler
.GetPropertyInt("pov.case.sensitive", 1) != 0;
58 for (; sc
.More(); sc
.Forward()) {
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);
65 // Handle line continuation generically.
67 if (sc
.chNext
== '\n' || sc
.chNext
== '\r') {
69 if (sc
.ch
== '\r' && sc
.chNext
== '\n') {
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
);
83 } else if (sc
.state
== SCE_POV_IDENTIFIER
) {
84 if (!IsAWordChar(sc
.ch
) || (sc
.ch
== '.')) {
87 sc
.GetCurrent(s
, sizeof(s
));
89 sc
.GetCurrentLowered(s
, sizeof(s
));
91 if (keywords
.InList(s
)) {
92 sc
.ChangeState(SCE_POV_WORD
);
93 } else if (keywords2
.InList(s
)) {
94 sc
.ChangeState(SCE_POV_WORD2
);
96 sc
.SetState(SCE_POV_DEFAULT
);
98 } else if (sc
.state
== SCE_POV_COMMENT
) {
99 if (sc
.Match('*', '/')) {
101 sc
.ForwardSetState(SCE_POV_DEFAULT
);
103 } else if (sc
.state
== SCE_POV_COMMENTDOC
) {
104 if (sc
.Match('*', '/')) {
106 sc
.ForwardSetState(SCE_POV_DEFAULT
);
108 } else if (sc
.state
== SCE_POV_COMMENTLINE
) {
110 sc
.SetState(SCE_POV_DEFAULT
);
112 } else if (sc
.state
== SCE_POV_STRING
) {
114 if (sc
.chNext
== '\"' || sc
.chNext
== '\\') {
117 } else if (sc
.ch
== '\"') {
118 sc
.ForwardSetState(SCE_POV_DEFAULT
);
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
);
147 static bool IsStreamCommentStyle(int style
) {
148 return style
== SCE_POV_COMMENT
||
149 style
== SCE_POV_COMMENTDOC
;
152 static void FoldNoBoxPOVDoc(unsigned int startPos
, int length
, int initStyle
,
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
++) {
166 chNext
= styler
.SafeGetCharAt(i
+ 1);
167 int stylePrev
= style
;
169 styleNext
= styler
.StyleAt(i
+ 1);
170 bool atEOL
= (ch
== '\r' && chNext
!= '\n') || (ch
== '\n');
171 if (foldComment
&& IsStreamCommentStyle(style
)) {
172 if (!IsStreamCommentStyle(stylePrev
)) {
174 } else if (!IsStreamCommentStyle(styleNext
) && !atEOL
) {
175 // Comments don't end at end of line and the next character may be unstyled.
179 if (style
== SCE_POV_BRACE
) {
182 } else if (ch
== '}') {
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
);
196 levelPrev
= levelCurrent
;
199 if (!isspacechar(ch
))
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
);
207 static void FoldPOVDoc(unsigned int startPos
, int length
, int initStyle
, WordList
*[], Accessor
&styler
) {
208 FoldNoBoxPOVDoc(startPos
, length
, initStyle
, styler
);
211 static const char * const POVWordLists
[] = {
212 "Primary keywords and identifiers",
213 "Secondary keywords and identifiers",
217 static void ColourisePOVDocSensitive(unsigned int startPos
, int length
, int initStyle
, WordList
*keywordlists
[],
219 ColourisePOVDoc(startPos
, length
, initStyle
, keywordlists
, styler
);
222 LexerModule
lmPOV(SCLEX_POV
, ColourisePOVDocSensitive
, "pov", FoldPOVDoc
, POVWordLists
);