]>
git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/LexASY.cxx
5bf979fd37c601032d725e062339bbb81199f48f
1 // Scintilla source code edit control
2 //Author: instanton (email: soft_share<at>126<dot>com)
3 // The License.txt file describes the conditions under which this software may be distributed.
15 #include "StyleContext.h"
17 #include "Scintilla.h"
19 #include "CharacterSet.h"
22 using namespace Scintilla
;
25 static void ColouriseAsyDoc(unsigned int startPos
, int length
, int initStyle
,
26 WordList
*keywordlists
[], Accessor
&styler
) {
28 WordList
&keywords
= *keywordlists
[0];
29 WordList
&keywords2
= *keywordlists
[1];
31 CharacterSet
setWordStart(CharacterSet::setAlpha
, "_", 0x80, true);
32 CharacterSet
setWord(CharacterSet::setAlphaNum
, "._", 0x80, true);
36 StyleContext
sc(startPos
, length
, initStyle
, styler
);
38 for (; sc
.More(); sc
.Forward()) {
41 if (sc
.state
== SCE_ASY_STRING
) {
42 sc
.SetState(SCE_ASY_STRING
);
48 if (sc
.chNext
== '\n' || sc
.chNext
== '\r') {
50 if (sc
.ch
== '\r' && sc
.chNext
== '\n') {
53 // continuationLine = true;
58 // Determine if the current state should terminate.
60 case SCE_ASY_OPERATOR
:
61 sc
.SetState(SCE_ASY_DEFAULT
);
64 if (!setWord
.Contains(sc
.ch
)) {
65 sc
.SetState(SCE_ASY_DEFAULT
);
68 case SCE_ASY_IDENTIFIER
:
69 if (!setWord
.Contains(sc
.ch
) || (sc
.ch
== '.')) {
71 sc
.GetCurrentLowered(s
, sizeof(s
));
72 if (keywords
.InList(s
)) {
73 sc
.ChangeState(SCE_ASY_WORD
);
74 } else if (keywords2
.InList(s
)) {
75 sc
.ChangeState(SCE_ASY_WORD2
);
77 sc
.SetState(SCE_ASY_DEFAULT
);
81 if (sc
.Match('*', '/')) {
83 sc
.ForwardSetState(SCE_ASY_DEFAULT
);
86 case SCE_ASY_COMMENTLINE
:
88 sc
.SetState(SCE_ASY_DEFAULT
);
93 sc
.ChangeState(SCE_ASY_STRINGEOL
);
94 } else if (sc
.ch
== '\\') {
95 if (sc
.chNext
== '\"' || sc
.chNext
== '\'' || sc
.chNext
== '\\') {
98 } else if (sc
.ch
== '\"') {
99 sc
.ForwardSetState(SCE_ASY_DEFAULT
);
102 case SCE_ASY_CHARACTER
:
104 sc
.ChangeState(SCE_ASY_STRINGEOL
);
105 } else if (sc
.ch
== '\\') {
106 if (sc
.chNext
== '\"' || sc
.chNext
== '\'' || sc
.chNext
== '\\') {
109 } else if (sc
.ch
== '\'') {
110 sc
.ForwardSetState(SCE_ASY_DEFAULT
);
115 // Determine if a new state should be entered.
116 if (sc
.state
== SCE_ASY_DEFAULT
) {
117 if (setWordStart
.Contains(sc
.ch
) || (sc
.ch
== '@')) {
118 sc
.SetState(SCE_ASY_IDENTIFIER
);
119 } else if (sc
.Match('/', '*')) {
120 sc
.SetState(SCE_ASY_COMMENT
);
122 } else if (sc
.Match('/', '/')) {
123 sc
.SetState(SCE_ASY_COMMENTLINE
);
124 } else if (sc
.ch
== '\"') {
125 sc
.SetState(SCE_ASY_STRING
);
126 } else if (sc
.ch
== '\'') {
127 sc
.SetState(SCE_ASY_CHARACTER
);
128 } else if (sc
.ch
== '#' && visibleChars
== 0) {
131 } while ((sc
.ch
== ' ' || sc
.ch
== '\t') && sc
.More());
133 sc
.SetState(SCE_ASY_DEFAULT
);
135 } else if (isoperator(static_cast<char>(sc
.ch
))) {
136 sc
.SetState(SCE_ASY_OPERATOR
);
144 static bool IsAsyCommentStyle(int style
) {
145 return style
== SCE_ASY_COMMENT
;
149 static inline bool isASYidentifier(int ch
) {
151 ((ch
>= 'a') && (ch
<= 'z')) || ((ch
>= 'A') && (ch
<= 'Z')) ;
154 static int ParseASYWord(unsigned int pos
, Accessor
&styler
, char *word
)
157 char ch
=styler
.SafeGetCharAt(pos
);
160 while(isASYidentifier(ch
) && length
<100){
163 ch
=styler
.SafeGetCharAt(pos
+length
);
169 static bool IsASYDrawingLine(int line
, Accessor
&styler
) {
170 int pos
= styler
.LineStart(line
);
171 int eol_pos
= styler
.LineStart(line
+ 1) - 1;
176 while (startpos
<eol_pos
){
177 char ch
= styler
[startpos
];
178 ParseASYWord(startpos
,styler
,buffer
);
179 bool drawcommands
= strncmp(buffer
,"draw",4)==0||
180 strncmp(buffer
,"pair",4)==0||strncmp(buffer
,"label",5)==0;
181 if (!drawcommands
&& ch
!=' ') return false;
182 else if (drawcommands
) return true;
188 static void FoldAsyDoc(unsigned int startPos
, int length
, int initStyle
,
189 WordList
*[], Accessor
&styler
) {
190 bool foldComment
= styler
.GetPropertyInt("fold.comment") != 0;
191 bool foldCompact
= styler
.GetPropertyInt("fold.compact", 1) != 0;
192 bool foldAtElse
= styler
.GetPropertyInt("fold.at.else", 0) != 0;
193 unsigned int endPos
= startPos
+ length
;
194 int visibleChars
= 0;
195 int lineCurrent
= styler
.GetLine(startPos
);
196 int levelCurrent
= SC_FOLDLEVELBASE
;
198 levelCurrent
= styler
.LevelAt(lineCurrent
-1) >> 16;
199 int levelMinCurrent
= levelCurrent
;
200 int levelNext
= levelCurrent
;
201 char chNext
= styler
[startPos
];
202 int styleNext
= styler
.StyleAt(startPos
);
203 int style
= initStyle
;
204 for (unsigned int i
= startPos
; i
< endPos
; i
++) {
206 chNext
= styler
.SafeGetCharAt(i
+ 1);
207 int stylePrev
= style
;
209 styleNext
= styler
.StyleAt(i
+ 1);
210 bool atEOL
= (ch
== '\r' && chNext
!= '\n') || (ch
== '\n');
211 if (foldComment
&& IsAsyCommentStyle(style
)) {
212 if (!IsAsyCommentStyle(stylePrev
) && (stylePrev
!= SCE_ASY_COMMENTLINEDOC
)) {
214 } else if (!IsAsyCommentStyle(styleNext
) && (styleNext
!= SCE_ASY_COMMENTLINEDOC
) && !atEOL
) {
218 if (style
== SCE_ASY_OPERATOR
) {
220 if (levelMinCurrent
> levelNext
) {
221 levelMinCurrent
= levelNext
;
224 } else if (ch
== '}') {
229 if (atEOL
&& IsASYDrawingLine(lineCurrent
, styler
)){
230 if (lineCurrent
==0 && IsASYDrawingLine(lineCurrent
+ 1, styler
))
232 else if (lineCurrent
!=0 && !IsASYDrawingLine(lineCurrent
- 1, styler
)
233 && IsASYDrawingLine(lineCurrent
+ 1, styler
)
236 else if (lineCurrent
!=0 && IsASYDrawingLine(lineCurrent
- 1, styler
) &&
237 !IsASYDrawingLine(lineCurrent
+1, styler
))
242 int levelUse
= levelCurrent
;
244 levelUse
= levelMinCurrent
;
246 int lev
= levelUse
| levelNext
<< 16;
247 if (visibleChars
== 0 && foldCompact
)
248 lev
|= SC_FOLDLEVELWHITEFLAG
;
249 if (levelUse
< levelNext
)
250 lev
|= SC_FOLDLEVELHEADERFLAG
;
251 if (lev
!= styler
.LevelAt(lineCurrent
)) {
252 styler
.SetLevel(lineCurrent
, lev
);
255 levelCurrent
= levelNext
;
256 levelMinCurrent
= levelCurrent
;
264 static const char * const asyWordLists
[] = {
265 "Primary keywords and identifiers",
266 "Secondary keywords and identifiers",
270 LexerModule
lmASY(SCLEX_ASYMPTOTE
, ColouriseAsyDoc
, "asy", FoldAsyDoc
, asyWordLists
);