]>
git.saurik.com Git - wxWidgets.git/blob - contrib/src/stc/scintilla/src/LexPB.cxx
d35944ecf7caebef022ff8a0aeea2041025f9b1e
1 // Scintilla source code edit control
3 ** Lexer for PowerBasic by Roland Walter, roland@rowalt.de
4 ** Last update: 17.10.2003 (toggling of subs/functions now until next sub/functin - this gives better results)
7 // Necessary changes in Scintilla project:
8 // - In SciLexer.h and Scintilla.iface:
10 // #define SCLEX_PB 51 //ID for PowerBasic lexer
12 // #define SCE_B_DEFAULT 0 //in both VB and PB lexer
13 // #define SCE_B_COMMENT 1 //in both VB and PB lexer
14 // #define SCE_B_NUMBER 2 //in both VB and PB lexer
15 // #define SCE_B_KEYWORD 3 //in both VB and PB lexer
16 // #define SCE_B_STRING 4 //in both VB and PB lexer
17 // #define SCE_B_PREPROCESSOR 5 //VB lexer only, unsupported by PB lexer
18 // #define SCE_B_OPERATOR 6 //in both VB and PB lexer
19 // #define SCE_B_IDENTIFIER 7 //in both VB and PB lexer
20 // #define SCE_B_DATE 8 //VB lexer only, unsupported by PB lexer
22 // - Statement added to KeyWords.cxx: 'LINK_LEXER(lmPB);'
23 // - Statement added to scintilla_vc6.mak: '$(DIR_O)\LexPB.obj: ...\src\LexPB.cxx $(LEX_HEADERS)'
25 // Copyright for Scintilla: 1998-2001 by Neil Hodgson <neilh@scintilla.org>
26 // The License.txt file describes the conditions under which this software may be distributed.
38 #include "StyleContext.h"
40 #include "Scintilla.h"
43 static inline bool IsTypeCharacter(const int ch
) {
44 return ch
== '%' || ch
== '&' || ch
== '@' || ch
== '!' || ch
== '#' || ch
== '$';
47 static inline bool IsAWordChar(const int ch
) {
48 return (ch
< 0x80) && (isalnum(ch
) || ch
== '.' || ch
== '_');
51 static inline bool IsAWordStart(const int ch
) {
52 return (ch
< 0x80) && (isalnum(ch
) || ch
== '_');
55 bool MatchUpperCase(Accessor
&styler
, int pos
, const char *s
) //Same as styler.Match() but uppercase comparison (a-z,A-Z and space only)
58 for (int i
=0; *s
; i
++)
60 ch
=styler
.SafeGetCharAt(pos
+i
);
61 if (ch
> 0x60) ch
-= '\x20';
62 if (*s
!= ch
) return false;
68 static void ColourisePBDoc(unsigned int startPos
, int length
, int initStyle
,WordList
*keywordlists
[],
71 WordList
&keywords
= *keywordlists
[0];
73 styler
.StartAt(startPos
);
75 StyleContext
sc(startPos
, length
, initStyle
, styler
);
77 for (; sc
.More(); sc
.Forward()) {
79 if (sc
.state
== SCE_B_OPERATOR
)
81 sc
.SetState(SCE_B_DEFAULT
);
83 else if (sc
.state
== SCE_B_KEYWORD
)
85 if (!IsAWordChar(sc
.ch
))
87 if (!IsTypeCharacter(sc
.ch
))
89 if (sc
.ch
== ']') {sc
.Forward();}
91 sc
.GetCurrentLowered(s
, sizeof(s
));
92 if (keywords
.InList(s
))
94 if (strcmp(s
, "rem") == 0)
96 sc
.ChangeState(SCE_B_COMMENT
);
97 if (sc
.atLineEnd
) {sc
.SetState(SCE_B_DEFAULT
);}
101 sc
.SetState(SCE_B_DEFAULT
);
106 sc
.ChangeState(SCE_B_IDENTIFIER
);
107 sc
.SetState(SCE_B_DEFAULT
);
112 else if (sc
.state
== SCE_B_NUMBER
)
114 if (!IsAWordChar(sc
.ch
)) {sc
.SetState(SCE_B_DEFAULT
);}
116 else if (sc
.state
== SCE_B_STRING
)
118 // PB doubles quotes to preserve them, so just end this string
119 // state now as a following quote will start again
122 if (tolower(sc
.chNext
) == 'c') {sc
.Forward();}
123 sc
.ForwardSetState(SCE_B_DEFAULT
);
126 else if (sc
.state
== SCE_B_COMMENT
)
128 if (sc
.atLineEnd
) {sc
.SetState(SCE_B_DEFAULT
);}
131 if (sc
.state
== SCE_B_DEFAULT
)
133 if (sc
.ch
== '\'') {sc
.SetState(SCE_B_COMMENT
);}
134 else if (sc
.ch
== '\"') {sc
.SetState(SCE_B_STRING
);}
135 else if (sc
.ch
== '#')
138 while ((n
< 100) && (chSeek
== ' ' || chSeek
== '\t'))
140 chSeek
= sc
.GetRelative(n
);
143 sc
.SetState(SCE_B_OPERATOR
);
145 else if (sc
.ch
== '&' && tolower(sc
.chNext
) == 'h') {sc
.SetState(SCE_B_NUMBER
);}
146 else if (sc
.ch
== '&' && tolower(sc
.chNext
) == 'b') {sc
.SetState(SCE_B_NUMBER
);}
147 else if (sc
.ch
== '&' && tolower(sc
.chNext
) == 'o') {sc
.SetState(SCE_B_NUMBER
);}
148 else if (IsADigit(sc
.ch
) || (sc
.ch
== '.' && IsADigit(sc
.chNext
))) {sc
.SetState(SCE_B_NUMBER
);}
149 else if (IsAWordStart(sc
.ch
) || (sc
.ch
== '[')) {sc
.SetState(SCE_B_KEYWORD
);}
150 else if (isoperator(static_cast<char>(sc
.ch
)) || (sc
.ch
== '\\')) {sc
.SetState(SCE_B_OPERATOR
);}
157 static void FoldPBDoc(unsigned int startPos
, int length
, int, WordList
*[], Accessor
&styler
)
159 // No folding enabled, no reason to continue...
160 if( styler
.GetPropertyInt("fold") == 0 )
163 unsigned int endPos
= startPos
+ length
;
164 int lineCurrent
= styler
.GetLine(startPos
);
165 int levelCurrent
= SC_FOLDLEVELBASE
;
167 levelCurrent
= styler
.LevelAt(lineCurrent
-1) >> 16;
168 int levelNext
= levelCurrent
;
169 char chNext
= styler
[startPos
];
172 for (unsigned int i
= startPos
; i
< endPos
; i
++)
175 chNext
= styler
.SafeGetCharAt(i
+ 1);
177 if( atEOL
) //Begin of a new line (The Sub/Function/Macro keywords may occur at begin of line only)
179 if( MatchUpperCase(styler
,i
,"FUNCTION") ) //else if(
181 styler
.SetLevel(lineCurrent
, (SC_FOLDLEVELBASE
<< 16) | SC_FOLDLEVELHEADERFLAG
);
182 levelNext
=SC_FOLDLEVELBASE
+1;
184 else if( MatchUpperCase(styler
,i
,"CALLBACK FUNCTION") )
186 styler
.SetLevel(lineCurrent
, (SC_FOLDLEVELBASE
<< 16) | SC_FOLDLEVELHEADERFLAG
);
187 levelNext
=SC_FOLDLEVELBASE
+1;
189 else if( MatchUpperCase(styler
,i
,"STATIC FUNCTION") )
191 styler
.SetLevel(lineCurrent
, (SC_FOLDLEVELBASE
<< 16) | SC_FOLDLEVELHEADERFLAG
);
192 levelNext
=SC_FOLDLEVELBASE
+1;
194 else if( MatchUpperCase(styler
,i
,"SUB") )
196 styler
.SetLevel(lineCurrent
, (SC_FOLDLEVELBASE
<< 16) | SC_FOLDLEVELHEADERFLAG
);
197 levelNext
=SC_FOLDLEVELBASE
+1;
199 else if( MatchUpperCase(styler
,i
,"STATIC SUB") )
201 styler
.SetLevel(lineCurrent
, (SC_FOLDLEVELBASE
<< 16) | SC_FOLDLEVELHEADERFLAG
);
202 levelNext
=SC_FOLDLEVELBASE
+1;
204 //else if( MatchUpperCase(styler,i,"MACRO") ) //ToDo: What's with single-line macros?
207 atEOL
= (ch
== '\r' && chNext
!= '\n') || (ch
== '\n');
211 levelCurrent
= levelNext
;
215 if (levelNext
== SC_FOLDLEVELBASE
)
217 int levelUse
= levelCurrent
;
218 int lev
= levelUse
| levelNext
<< 16;
219 styler
.SetLevel(lineCurrent
, lev
);
223 static const char * const pbWordListDesc
[] = {
228 LexerModule
lmPB(SCLEX_POWERBASIC
, ColourisePBDoc
, "powerbasic", FoldPBDoc
, pbWordListDesc
);