]>
git.saurik.com Git - wxWidgets.git/blob - contrib/src/stc/scintilla/src/LexLisp.cxx
1 // Scintilla source code edit control
4 ** Written by Alexey Yutkin.
6 // Copyright 1998-2001 by Neil Hodgson <neilh@scintilla.org>
7 // The License.txt file describes the conditions under which this software may be distributed.
20 #include "Scintilla.h"
24 inline bool isLispoperator(char ch
) {
25 if (isascii(ch
) && isalnum(ch
))
27 if (ch
== '\'' || ch
== '(' || ch
== ')' )
32 inline bool isLispwordstart(char ch
) {
33 return isascii(ch
) && ch
!= ';' && !isspacechar(ch
) && !isLispoperator(ch
) &&
34 ch
!= '\n' && ch
!= '\r' && ch
!= '\"';
38 static void classifyWordLisp(unsigned int start
, unsigned int end
, WordList
&keywords
, Accessor
&styler
) {
39 PLATFORM_ASSERT(end
>= start
);
42 bool digit_flag
= true;
43 for (i
= 0; (i
< end
- start
+ 1) && (i
< 99); i
++) {
44 s
[i
] = styler
[start
+ i
];
46 if (!isdigit(s
[i
]) && (s
[i
] != '.')) digit_flag
= false;
48 char chAttr
= SCE_LISP_IDENTIFIER
;
50 if(digit_flag
) chAttr
= SCE_LISP_NUMBER
;
52 if (keywords
.InList(s
)) {
53 chAttr
= SCE_LISP_KEYWORD
;
56 styler
.ColourTo(end
, chAttr
);
61 static void ColouriseLispDoc(unsigned int startPos
, int length
, int initStyle
, WordList
*keywordlists
[],
64 WordList
&keywords
= *keywordlists
[0];
66 styler
.StartAt(startPos
);
68 int state
= initStyle
;
69 if (state
== SCE_LISP_STRINGEOL
) // Does not leak onto next line
70 state
= SCE_LISP_DEFAULT
;
72 char chNext
= styler
[startPos
];
73 unsigned int lengthDoc
= startPos
+ length
;
74 styler
.StartSegment(startPos
);
75 for (unsigned int i
= startPos
; i
< lengthDoc
; i
++) {
77 chNext
= styler
.SafeGetCharAt(i
+ 1);
79 bool atEOL
= (ch
== '\r' && chNext
!= '\n') || (ch
== '\n');
81 // Trigger on CR only (Mac style) or either on LF from CR+LF (Dos/Win) or on LF alone (Unix)
82 // Avoid triggering two times on Dos/Win
84 if (state
== SCE_LISP_STRINGEOL
) {
85 styler
.ColourTo(i
, state
);
86 state
= SCE_LISP_DEFAULT
;
90 if (styler
.IsLeadByte(ch
)) {
91 chNext
= styler
.SafeGetCharAt(i
+ 2);
97 if (state
== SCE_LISP_DEFAULT
) {
98 if (isLispwordstart(ch
)) {
99 styler
.ColourTo(i
- 1, state
);
100 state
= SCE_LISP_IDENTIFIER
;
102 else if (ch
== ';') {
103 styler
.ColourTo(i
- 1, state
);
104 state
= SCE_LISP_COMMENT
;
106 else if (isLispoperator(ch
) || ch
=='\'') {
107 styler
.ColourTo(i
- 1, state
);
108 styler
.ColourTo(i
, SCE_LISP_OPERATOR
);
111 else if (ch
== '\"') {
112 state
= SCE_LISP_STRING
;
114 } else if (state
== SCE_LISP_IDENTIFIER
) {
115 if (!isLispwordstart(ch
)) {
116 classifyWordLisp(styler
.GetStartSegment(), i
- 1, keywords
, styler
);
117 state
= SCE_LISP_DEFAULT
;
119 if (isLispoperator(ch
) || ch
=='\'') {
120 styler
.ColourTo(i
- 1, state
);
121 styler
.ColourTo(i
, SCE_LISP_OPERATOR
);
125 if (state
== SCE_LISP_COMMENT
) {
127 styler
.ColourTo(i
- 1, state
);
128 state
= SCE_LISP_DEFAULT
;
130 } else if (state
== SCE_LISP_STRING
) {
132 if (chNext
== '\"' || chNext
== '\'' || chNext
== '\\') {
135 chNext
= styler
.SafeGetCharAt(i
+ 1);
137 } else if (ch
== '\"') {
138 styler
.ColourTo(i
, state
);
139 state
= SCE_LISP_DEFAULT
;
140 } else if ((chNext
== '\r' || chNext
== '\n') && (chPrev
!= '\\')) {
141 styler
.ColourTo(i
- 1, SCE_LISP_STRINGEOL
);
142 state
= SCE_LISP_STRINGEOL
;
149 styler
.ColourTo(lengthDoc
- 1, state
);
152 static void FoldLispDoc(unsigned int startPos
, int length
, int /* initStyle */, WordList
*[],
154 unsigned int lengthDoc
= startPos
+ length
;
155 int visibleChars
= 0;
156 int lineCurrent
= styler
.GetLine(startPos
);
157 int levelPrev
= styler
.LevelAt(lineCurrent
) & SC_FOLDLEVELNUMBERMASK
;
158 int levelCurrent
= levelPrev
;
159 char chNext
= styler
[startPos
];
160 int styleNext
= styler
.StyleAt(startPos
);
161 for (unsigned int i
= startPos
; i
< lengthDoc
; i
++) {
163 chNext
= styler
.SafeGetCharAt(i
+ 1);
164 int style
= styleNext
;
165 styleNext
= styler
.StyleAt(i
+ 1);
166 bool atEOL
= (ch
== '\r' && chNext
!= '\n') || (ch
== '\n');
167 if (style
== SCE_LISP_OPERATOR
) {
170 } else if (ch
== ')') {
176 if (visibleChars
== 0)
177 lev
|= SC_FOLDLEVELWHITEFLAG
;
178 if ((levelCurrent
> levelPrev
) && (visibleChars
> 0))
179 lev
|= SC_FOLDLEVELHEADERFLAG
;
180 if (lev
!= styler
.LevelAt(lineCurrent
)) {
181 styler
.SetLevel(lineCurrent
, lev
);
184 levelPrev
= levelCurrent
;
187 if (!isspacechar(ch
))
190 // Fill in the real level of the next line, keeping the current flags as they will be filled in later
191 int flagsNext
= styler
.LevelAt(lineCurrent
) & ~SC_FOLDLEVELNUMBERMASK
;
192 styler
.SetLevel(lineCurrent
, levelPrev
| flagsNext
);
195 LexerModule
lmLISP(SCLEX_LISP
, ColouriseLispDoc
, "lisp", FoldLispDoc
);