]>
git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/LexPerl.cxx
3c9972e2d6877e31adc5a6787013964dc714b8f2
1 // SciTE - Scintilla based Text Editor
2 // LexPerl.cxx - lexer for subset of Perl
3 // Copyright 1998-2000 by Neil Hodgson <neilh@scintilla.org>
4 // The License.txt file describes the conditions under which this software may be distributed.
17 #include "Scintilla.h"
20 inline bool isPerlOperator(char ch
) {
23 // '.' left out as it is used to make up numbers
24 if (ch
== '%' || ch
== '^' || ch
== '&' || ch
== '*' || ch
== '\\' ||
25 ch
== '(' || ch
== ')' || ch
== '-' || ch
== '+' ||
26 ch
== '=' || ch
== '|' || ch
== '{' || ch
== '}' ||
27 ch
== '[' || ch
== ']' || ch
== ':' || ch
== ';' ||
28 ch
== '<' || ch
== '>' || ch
== ',' || ch
== '/' ||
29 ch
== '?' || ch
== '!' || ch
== '.' || ch
== '~')
34 static int classifyWordPerl(unsigned int start
, unsigned int end
, WordList
&keywords
, Accessor
&styler
) {
36 bool wordIsNumber
= isdigit(styler
[start
]) || (styler
[start
] == '.');
37 for (unsigned int i
= 0; i
< end
- start
+ 1 && i
< 30; i
++) {
38 s
[i
] = styler
[start
+ i
];
41 char chAttr
= SCE_PL_IDENTIFIER
;
43 chAttr
= SCE_PL_NUMBER
;
45 if (keywords
.InList(s
))
48 styler
.ColourTo(end
, chAttr
);
52 static bool isEndVar(char ch
) {
53 return !isalnum(ch
) && ch
!= '#' && ch
!= '$' &&
54 ch
!= '_' && ch
!= '\'';
57 static bool isMatch(Accessor
&styler
, int lengthDoc
, int pos
, const char *val
) {
58 if ((pos
+ static_cast<int>(strlen(val
))) >= lengthDoc
) {
62 if (*val
!= styler
[pos
++]) {
70 static char opposite(char ch
) {
82 static void ColourisePerlDoc(unsigned int startPos
, int length
, int initStyle
,
83 WordList
*keywordlists
[], Accessor
&styler
) {
85 // Lexer for perl often has to backtrack to start of current style to determine
86 // which characters are being used as quotes, how deeply nested is the
87 // start position and what the termination string is for here documents
89 WordList
&keywords
= *keywordlists
[0];
98 sooked
[sookedpos
] = '\0';
99 int state
= initStyle
;
100 int lengthDoc
= startPos
+ length
;
101 // If in a long distance lexical state, seek to the beginning to find quote characters
102 if (state
== SCE_PL_HERE
|| state
== SCE_PL_REGEX
||
103 state
== SCE_PL_REGSUBST
|| state
== SCE_PL_LONGQUOTE
) {
104 while ((startPos
> 1) && (styler
.StyleAt(startPos
- 1) == state
)) {
107 state
= SCE_PL_DEFAULT
;
109 styler
.StartAt(startPos
);
111 char chNext
= styler
[startPos
];
112 styler
.StartSegment(startPos
);
113 for (int i
= startPos
; i
< lengthDoc
; i
++) {
115 chNext
= styler
.SafeGetCharAt(i
+ 1);
116 char chNext2
= styler
.SafeGetCharAt(i
+ 2);
118 if (styler
.IsLeadByte(ch
)) {
119 chNext
= styler
.SafeGetCharAt(i
+ 2);
125 if (state
== SCE_PL_DEFAULT
) {
126 if (iswordstart(ch
)) {
127 styler
.ColourTo(i
- 1, state
);
128 if (ch
== 's' && !isalnum(chNext
)) {
129 state
= SCE_PL_REGSUBST
;
134 } else if (ch
== 'm' && !isalnum(chNext
)) {
135 state
= SCE_PL_REGEX
;
140 } else if (ch
== 't' && chNext
== 'r' && !isalnum(chNext2
)) {
141 state
= SCE_PL_REGSUBST
;
148 } else if (ch
== 'q' && (chNext
== 'q' || chNext
== 'r' || chNext
== 'w' || chNext
== 'x') && !isalnum(chNext2
)) {
149 state
= SCE_PL_LONGQUOTE
;
160 } else if (ch
== '#') {
161 styler
.ColourTo(i
- 1, state
);
162 state
= SCE_PL_COMMENTLINE
;
163 } else if (ch
== '\"') {
164 styler
.ColourTo(i
- 1, state
);
165 state
= SCE_PL_STRING
;
166 } else if (ch
== '\'') {
169 styler
.ColourTo(i
, state
);
171 styler
.ColourTo(i
- 1, state
);
172 state
= SCE_PL_CHARACTER
;
174 } else if (ch
== '`') {
175 styler
.ColourTo(i
- 1, state
);
176 state
= SCE_PL_BACKTICKS
;
177 } else if (ch
== '$') {
179 styler
.ColourTo(i
- 1, state
);
180 if (isalnum(chNext
) || chNext
== '#' || chNext
== '$' || chNext
== '_') {
181 state
= SCE_PL_SCALAR
;
182 } else if (chNext
!= '{' && chNext
!= '[') {
183 styler
.ColourTo(i
, SCE_PL_SCALAR
);
188 styler
.ColourTo(i
, SCE_PL_SCALAR
);
190 } else if (ch
== '@') {
192 styler
.ColourTo(i
- 1, state
);
193 if (isalpha(chNext
) || chNext
== '#' || chNext
== '$' || chNext
== '_') {
194 state
= SCE_PL_ARRAY
;
195 } else if (chNext
!= '{' && chNext
!= '[') {
196 styler
.ColourTo(i
, SCE_PL_ARRAY
);
200 styler
.ColourTo(i
, SCE_PL_ARRAY
);
202 } else if (ch
== '%') {
204 styler
.ColourTo(i
- 1, state
);
205 if (isalpha(chNext
) || chNext
== '#' || chNext
== '$' || chNext
== '_') {
207 } else if (chNext
!= '{' && chNext
!= '[') {
208 styler
.ColourTo(i
, SCE_PL_HASH
);
212 styler
.ColourTo(i
, SCE_PL_HASH
);
214 } else if (ch
== '*') {
215 styler
.ColourTo(i
- 1, state
);
216 state
= SCE_PL_SYMBOLTABLE
;
217 } else if (ch
== '/' && preferRE
) {
218 styler
.ColourTo(i
- 1, state
);
219 state
= SCE_PL_REGEX
;
224 } else if (ch
== '<' && chNext
== '<') {
225 styler
.ColourTo(i
- 1, state
);
232 sooked
[sookedpos
] = '\0';
233 } else if (ch
== '=' && isalpha(chNext
)) {
234 styler
.ColourTo(i
- 1, state
);
238 sooked
[sookedpos
] = '\0';
239 } else if (isPerlOperator(ch
)) {
240 if (ch
== ')' || ch
== ']')
244 styler
.ColourTo(i
- 1, state
);
245 styler
.ColourTo(i
, SCE_PL_OPERATOR
);
247 } else if (state
== SCE_PL_WORD
) {
248 if (!iswordchar(ch
) && ch
!= '\'') { // Archaic Perl has quotes inside names
249 if (isMatch(styler
, lengthDoc
, styler
.GetStartSegment(), "__DATA__")) {
250 styler
.ColourTo(i
, SCE_PL_DATASECTION
);
251 state
= SCE_PL_DATASECTION
;
252 } else if (isMatch(styler
, lengthDoc
, styler
.GetStartSegment(), "__END__")) {
253 styler
.ColourTo(i
, SCE_PL_DATASECTION
);
254 state
= SCE_PL_DATASECTION
;
256 if (classifyWordPerl(styler
.GetStartSegment(), i
- 1, keywords
, styler
) == SCE_PL_WORD
)
258 state
= SCE_PL_DEFAULT
;
260 state
= SCE_PL_COMMENTLINE
;
261 } else if (ch
== '\"') {
262 state
= SCE_PL_STRING
;
263 } else if (ch
== '\'') {
264 state
= SCE_PL_CHARACTER
;
265 } else if (ch
== '<' && chNext
== '<') {
269 sooked
[sookedpos
] = '\0';
270 } else if (isPerlOperator(ch
)) {
271 if (ch
== ')' || ch
== ']')
275 styler
.ColourTo(i
, SCE_PL_OPERATOR
);
276 state
= SCE_PL_DEFAULT
;
281 if (state
== SCE_PL_COMMENTLINE
) {
282 if (ch
== '\r' || ch
== '\n') {
283 styler
.ColourTo(i
- 1, state
);
284 state
= SCE_PL_DEFAULT
;
286 } else if (state
== SCE_PL_HERE
) {
287 if (isalnum(ch
) && quotes
< 2) {
288 sooked
[sookedpos
++] = ch
;
289 sooked
[sookedpos
] = '\0';
295 if (quotes
> 1 && isMatch(styler
, lengthDoc
, i
, sooked
) && (chPrev
== '\n' || chPrev
== '\r') ) {
296 styler
.ColourTo(i
+ sookedpos
- 1, SCE_PL_HERE
);
298 chNext
= styler
.SafeGetCharAt(i
);
299 if (chNext
== '\n' || chNext
== '\r') {
300 state
= SCE_PL_DEFAULT
;
304 } else if (state
== SCE_PL_STRING
) {
306 if (chNext
== '\"' || chNext
== '\'' || chNext
== '\\') {
309 chNext
= styler
.SafeGetCharAt(i
+ 1);
311 } else if (ch
== '\"') {
312 styler
.ColourTo(i
, state
);
313 state
= SCE_PL_DEFAULT
;
316 chNext
= styler
.SafeGetCharAt(i
+ 1);
318 } else if (state
== SCE_PL_CHARACTER
) {
320 if (chNext
== '\"' || chNext
== '\'' || chNext
== '\\') {
323 chNext
= styler
.SafeGetCharAt(i
+ 1);
325 } else if (ch
== '\'') {
326 styler
.ColourTo(i
, state
);
327 state
= SCE_PL_DEFAULT
;
330 chNext
= styler
.SafeGetCharAt(i
+ 1);
332 } else if (state
== SCE_PL_BACKTICKS
) {
334 styler
.ColourTo(i
, state
);
335 state
= SCE_PL_DEFAULT
;
338 chNext
= styler
.SafeGetCharAt(i
+ 1);
340 } else if (state
== SCE_PL_POD
) {
342 if (isMatch(styler
, lengthDoc
, i
, "=cut")) {
343 styler
.ColourTo(i
- 1 + 4, state
);
345 state
= SCE_PL_DEFAULT
;
350 } else if (state
== SCE_PL_SCALAR
) {
352 styler
.ColourTo(i
- 1, state
);
353 state
= SCE_PL_DEFAULT
;
355 } else if (state
== SCE_PL_ARRAY
) {
357 styler
.ColourTo(i
- 1, state
);
358 state
= SCE_PL_DEFAULT
;
360 } else if (state
== SCE_PL_HASH
) {
362 styler
.ColourTo(i
- 1, state
);
363 state
= SCE_PL_DEFAULT
;
365 } else if (state
== SCE_PL_SYMBOLTABLE
) {
367 styler
.ColourTo(i
- 1, state
);
368 state
= SCE_PL_DEFAULT
;
370 } else if (state
== SCE_PL_REF
) {
372 styler
.ColourTo(i
- 1, state
);
373 state
= SCE_PL_DEFAULT
;
375 } else if (state
== SCE_PL_REGEX
) {
376 if (!quoteUp
&& !isspace(ch
)) {
378 quoteDown
= opposite(ch
);
381 if (ch
== quoteDown
&& chPrev
!= '\\') {
385 if (quoteUp
== quoteDown
) {
389 if (!isalpha(chNext
)) {
391 styler
.ColourTo(i
, state
);
392 state
= SCE_PL_DEFAULT
;
396 } else if (ch
== quoteUp
&& chPrev
!= '\\') {
398 } else if (!isalpha(chNext
)) {
400 styler
.ColourTo(i
, state
);
401 state
= SCE_PL_DEFAULT
;
406 } else if (state
== SCE_PL_REGSUBST
) {
407 if (!quoteUp
&& !isspace(ch
)) {
409 quoteDown
= opposite(ch
);
412 if (ch
== quoteDown
&& chPrev
!= '\\') {
417 if (!isalpha(chNext
)) {
419 styler
.ColourTo(i
, state
);
420 state
= SCE_PL_DEFAULT
;
424 if (quoteUp
== quoteDown
) {
427 } else if (ch
== quoteUp
&& chPrev
!= '\\') {
429 } else if (!isalpha(chNext
)) {
431 styler
.ColourTo(i
, state
);
432 state
= SCE_PL_DEFAULT
;
437 } else if (state
== SCE_PL_LONGQUOTE
) {
438 if (!quoteDown
&& !isspace(ch
)) {
440 quoteDown
= opposite(quoteUp
);
442 } else if (ch
== quoteDown
) {
447 styler
.ColourTo(i
, state
);
448 state
= SCE_PL_DEFAULT
;
451 if (quoteUp
== quoteDown
) {
455 } else if (ch
== quoteUp
) {
460 if (state
== SCE_PL_DEFAULT
) { // One of the above succeeded
462 state
= SCE_PL_COMMENTLINE
;
463 } else if (ch
== '\"') {
464 state
= SCE_PL_STRING
;
465 } else if (ch
== '\'') {
466 state
= SCE_PL_CHARACTER
;
467 } else if (iswordstart(ch
)) {
470 } else if (isoperator(ch
)) {
471 styler
.ColourTo(i
, SCE_PL_OPERATOR
);
477 styler
.ColourTo(lengthDoc
, state
);
480 LexerModule
lmPerl(SCLEX_PERL
, ColourisePerlDoc
);