]>
git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/LexEScript.cxx
295aaec531b117479bbaca91d52f0342f496fbc1
1 // Scintilla source code edit control
2 /** @file LexESCRIPT.cxx
5 // Copyright 2003 by Patrizio Bekerle (patrizio@bekerle.com)
17 #include "StyleContext.h"
19 #include "Scintilla.h"
23 using namespace Scintilla
;
27 static inline bool IsAWordChar(const int ch
) {
28 return (ch
< 0x80) && (isalnum(ch
) || ch
== '.' || ch
== '_');
31 static inline bool IsAWordStart(const int ch
) {
32 return (ch
< 0x80) && (isalnum(ch
) || ch
== '_');
37 static void ColouriseESCRIPTDoc(unsigned int startPos
, int length
, int initStyle
, WordList
*keywordlists
[],
40 WordList
&keywords
= *keywordlists
[0];
41 WordList
&keywords2
= *keywordlists
[1];
42 WordList
&keywords3
= *keywordlists
[2];
44 // Do not leak onto next line
45 /*if (initStyle == SCE_ESCRIPT_STRINGEOL)
46 initStyle = SCE_ESCRIPT_DEFAULT;*/
48 StyleContext
sc(startPos
, length
, initStyle
, styler
);
50 bool caseSensitive
= styler
.GetPropertyInt("escript.case.sensitive", 0) != 0;
52 for (; sc
.More(); sc
.Forward()) {
54 /*if (sc.atLineStart && (sc.state == SCE_ESCRIPT_STRING)) {
55 // Prevent SCE_ESCRIPT_STRINGEOL from leaking back to previous line
56 sc.SetState(SCE_ESCRIPT_STRING);
59 // Handle line continuation generically.
61 if (sc
.chNext
== '\n' || sc
.chNext
== '\r') {
63 if (sc
.ch
== '\r' && sc
.chNext
== '\n') {
70 // Determine if the current state should terminate.
71 if (sc
.state
== SCE_ESCRIPT_OPERATOR
|| sc
.state
== SCE_ESCRIPT_BRACE
) {
72 sc
.SetState(SCE_ESCRIPT_DEFAULT
);
73 } else if (sc
.state
== SCE_ESCRIPT_NUMBER
) {
74 if (!IsADigit(sc
.ch
) || sc
.ch
!= '.') {
75 sc
.SetState(SCE_ESCRIPT_DEFAULT
);
77 } else if (sc
.state
== SCE_ESCRIPT_IDENTIFIER
) {
78 if (!IsAWordChar(sc
.ch
) || (sc
.ch
== '.')) {
81 sc
.GetCurrent(s
, sizeof(s
));
83 sc
.GetCurrentLowered(s
, sizeof(s
));
86 // sc.GetCurrentLowered(s, sizeof(s));
88 if (keywords
.InList(s
)) {
89 sc
.ChangeState(SCE_ESCRIPT_WORD
);
90 } else if (keywords2
.InList(s
)) {
91 sc
.ChangeState(SCE_ESCRIPT_WORD2
);
92 } else if (keywords3
.InList(s
)) {
93 sc
.ChangeState(SCE_ESCRIPT_WORD3
);
94 // sc.state = SCE_ESCRIPT_IDENTIFIER;
96 sc
.SetState(SCE_ESCRIPT_DEFAULT
);
98 } else if (sc
.state
== SCE_ESCRIPT_COMMENT
) {
99 if (sc
.Match('*', '/')) {
101 sc
.ForwardSetState(SCE_ESCRIPT_DEFAULT
);
103 } else if (sc
.state
== SCE_ESCRIPT_COMMENTDOC
) {
104 if (sc
.Match('*', '/')) {
106 sc
.ForwardSetState(SCE_ESCRIPT_DEFAULT
);
108 } else if (sc
.state
== SCE_ESCRIPT_COMMENTLINE
) {
110 sc
.SetState(SCE_ESCRIPT_DEFAULT
);
112 } else if (sc
.state
== SCE_ESCRIPT_STRING
) {
114 if (sc
.chNext
== '\"' || sc
.chNext
== '\\') {
117 } else if (sc
.ch
== '\"') {
118 sc
.ForwardSetState(SCE_ESCRIPT_DEFAULT
);
122 // Determine if a new state should be entered.
123 if (sc
.state
== SCE_ESCRIPT_DEFAULT
) {
124 if (IsADigit(sc
.ch
) || (sc
.ch
== '.' && IsADigit(sc
.chNext
))) {
125 sc
.SetState(SCE_ESCRIPT_NUMBER
);
126 } else if (IsAWordStart(sc
.ch
) || (sc
.ch
== '#')) {
127 sc
.SetState(SCE_ESCRIPT_IDENTIFIER
);
128 } else if (sc
.Match('/', '*')) {
129 sc
.SetState(SCE_ESCRIPT_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_ESCRIPT_COMMENTLINE
);
133 } else if (sc
.ch
== '\"') {
134 sc
.SetState(SCE_ESCRIPT_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_ESCRIPT_OPERATOR
);
138 } else if (sc
.ch
== '{' || sc
.ch
== '}') {
139 sc
.SetState(SCE_ESCRIPT_BRACE
);
148 static int classifyFoldPointESCRIPT(const char* s
, const char* prevWord
) {
150 if (strcmp(prevWord
, "end") == 0) return lev
;
151 if ((strcmp(prevWord
, "else") == 0 && strcmp(s
, "if") == 0) || strcmp(s
, "elseif") == 0)
154 if (strcmp(s
, "for") == 0 || strcmp(s
, "foreach") == 0
155 || strcmp(s
, "program") == 0 || strcmp(s
, "function") == 0
156 || strcmp(s
, "while") == 0 || strcmp(s
, "case") == 0
157 || strcmp(s
, "if") == 0 ) {
159 } else if ( strcmp(s
, "endfor") == 0 || strcmp(s
, "endforeach") == 0
160 || strcmp(s
, "endprogram") == 0 || strcmp(s
, "endfunction") == 0
161 || strcmp(s
, "endwhile") == 0 || strcmp(s
, "endcase") == 0
162 || strcmp(s
, "endif") == 0 ) {
170 static bool IsStreamCommentStyle(int style
) {
171 return style
== SCE_ESCRIPT_COMMENT
||
172 style
== SCE_ESCRIPT_COMMENTDOC
||
173 style
== SCE_ESCRIPT_COMMENTLINE
;
176 static void FoldESCRIPTDoc(unsigned int startPos
, int length
, int initStyle
, WordList
*[], Accessor
&styler
) {
177 //~ bool foldComment = styler.GetPropertyInt("fold.comment") != 0;
178 // Do not know how to fold the comment at the moment.
179 bool foldCompact
= styler
.GetPropertyInt("fold.compact", 1) != 0;
180 bool foldComment
= true;
181 unsigned int endPos
= startPos
+ length
;
182 int visibleChars
= 0;
183 int lineCurrent
= styler
.GetLine(startPos
);
184 int levelPrev
= styler
.LevelAt(lineCurrent
) & SC_FOLDLEVELNUMBERMASK
;
185 int levelCurrent
= levelPrev
;
186 char chNext
= styler
[startPos
];
187 int styleNext
= styler
.StyleAt(startPos
);
188 int style
= initStyle
;
191 char prevWord
[32] = "";
193 for (unsigned int i
= startPos
; i
< endPos
; i
++) {
195 chNext
= styler
.SafeGetCharAt(i
+ 1);
196 int stylePrev
= style
;
198 styleNext
= styler
.StyleAt(i
+ 1);
199 bool atEOL
= (ch
== '\r' && chNext
!= '\n') || (ch
== '\n');
202 if (foldComment
&& IsStreamCommentStyle(style
)) {
203 if (!IsStreamCommentStyle(stylePrev
)) {
205 } else if (!IsStreamCommentStyle(styleNext
) && !atEOL
) {
206 // Comments don't end at end of line and the next character may be unstyled.
211 if (foldComment
&& (style
== SCE_ESCRIPT_COMMENTLINE
)) {
212 if ((ch
== '/') && (chNext
== '/')) {
213 char chNext2
= styler
.SafeGetCharAt(i
+ 2);
214 if (chNext2
== '{') {
216 } else if (chNext2
== '}') {
222 if (stylePrev
== SCE_ESCRIPT_DEFAULT
&& style
== SCE_ESCRIPT_WORD3
)
224 // Store last word start point.
228 if (style
== SCE_ESCRIPT_WORD3
) {
229 if(iswordchar(ch
) && !iswordchar(chNext
)) {
232 for(j
= 0; ( j
< 31 ) && ( j
< i
-lastStart
+1 ); j
++) {
233 s
[j
] = static_cast<char>(tolower(styler
[lastStart
+ j
]));
236 levelCurrent
+= classifyFoldPointESCRIPT(s
, prevWord
);
242 if (visibleChars
== 0 && foldCompact
)
243 lev
|= SC_FOLDLEVELWHITEFLAG
;
244 if ((levelCurrent
> levelPrev
) && (visibleChars
> 0))
245 lev
|= SC_FOLDLEVELHEADERFLAG
;
246 if (lev
!= styler
.LevelAt(lineCurrent
)) {
247 styler
.SetLevel(lineCurrent
, lev
);
250 levelPrev
= levelCurrent
;
252 strcpy(prevWord
, "");
255 if (!isspacechar(ch
))
259 // Fill in the real level of the next line, keeping the current flags as they will be filled in later
260 int flagsNext
= styler
.LevelAt(lineCurrent
) & ~SC_FOLDLEVELNUMBERMASK
;
261 styler
.SetLevel(lineCurrent
, levelPrev
| flagsNext
);
266 static const char * const ESCRIPTWordLists
[] = {
267 "Primary keywords and identifiers",
268 "Intrinsic functions",
269 "Extended and user defined functions",
273 LexerModule
lmESCRIPT(SCLEX_ESCRIPT
, ColouriseESCRIPTDoc
, "escript", FoldESCRIPTDoc
, ESCRIPTWordLists
);