*** empty log message ***
[anjuta-git-plugin.git] / scintilla / LexLisp.cxx
blob549b573e2ed3275a31d97e9a5d1dfdbdadd7d01a
1 // Scintilla source code edit control
2 /** @file LexLisp.cxx
3 ** Lexer for Lisp.
4 ** Written by Alexey Yutkin.
5 **/
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.
9 #include <stdlib.h>
10 #include <string.h>
11 #include <ctype.h>
12 #include <stdio.h>
13 #include <stdarg.h>
15 #include "Platform.h"
17 #include "PropSet.h"
18 #include "Accessor.h"
19 #include "KeyWords.h"
20 #include "Scintilla.h"
21 #include "SciLexer.h"
24 static inline bool isLispoperator(char ch) {
25 if (isascii(ch) && isalnum(ch))
26 return false;
27 if (ch == '\'' || ch == '(' || ch == ')' )
28 return true;
29 return false;
32 static 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);
40 char s[100];
41 unsigned int i;
42 bool digit_flag = true;
43 for (i = 0; (i < end - start + 1) && (i < 99); i++) {
44 s[i] = styler[start + i];
45 s[i + 1] = '\0';
46 if (!isdigit(s[i]) && (s[i] != '.')) digit_flag = false;
48 char chAttr = SCE_LISP_IDENTIFIER;
50 if(digit_flag) chAttr = SCE_LISP_NUMBER;
51 else {
52 if (keywords.InList(s)) {
53 chAttr = SCE_LISP_KEYWORD;
56 styler.ColourTo(end, chAttr);
57 return;
61 static void ColouriseLispDoc(unsigned int startPos, int length, int initStyle, WordList *keywordlists[],
62 Accessor &styler) {
64 WordList &keywords = *keywordlists[0];
66 styler.StartAt(startPos);
68 int state = initStyle;
69 char chNext = styler[startPos];
70 unsigned int lengthDoc = startPos + length;
71 styler.StartSegment(startPos);
72 for (unsigned int i = startPos; i < lengthDoc; i++) {
73 char ch = chNext;
74 chNext = styler.SafeGetCharAt(i + 1);
76 bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
78 if (styler.IsLeadByte(ch)) {
79 chNext = styler.SafeGetCharAt(i + 2);
80 i += 1;
81 continue;
84 if (state == SCE_LISP_DEFAULT) {
85 if (isLispwordstart(ch)) {
86 styler.ColourTo(i - 1, state);
87 state = SCE_LISP_IDENTIFIER;
89 else if (ch == ';') {
90 styler.ColourTo(i - 1, state);
91 state = SCE_LISP_COMMENT;
93 else if (isLispoperator(ch) || ch=='\'') {
94 styler.ColourTo(i - 1, state);
95 styler.ColourTo(i, SCE_LISP_OPERATOR);
97 else if (ch == '\"') {
98 styler.ColourTo(i - 1, state);
99 state = SCE_LISP_STRING;
101 } else if (state == SCE_LISP_IDENTIFIER) {
102 if (!isLispwordstart(ch)) {
103 classifyWordLisp(styler.GetStartSegment(), i - 1, keywords, styler);
104 state = SCE_LISP_DEFAULT;
105 } /*else*/
106 if (isLispoperator(ch) || ch=='\'') {
107 styler.ColourTo(i - 1, state);
108 styler.ColourTo(i, SCE_LISP_OPERATOR);
111 } else {
112 if (state == SCE_LISP_COMMENT) {
113 if (atEOL) {
114 styler.ColourTo(i - 1, state);
115 state = SCE_LISP_DEFAULT;
117 } else if (state == SCE_LISP_STRING) {
118 if (ch == '\\') {
119 if (chNext == '\"' || chNext == '\'' || chNext == '\\') {
120 i++;
121 chNext = styler.SafeGetCharAt(i + 1);
123 } else if (ch == '\"') {
124 styler.ColourTo(i, state);
125 state = SCE_LISP_DEFAULT;
131 styler.ColourTo(lengthDoc - 1, state);
134 static void FoldLispDoc(unsigned int startPos, int length, int /* initStyle */, WordList *[],
135 Accessor &styler) {
136 unsigned int lengthDoc = startPos + length;
137 int visibleChars = 0;
138 int lineCurrent = styler.GetLine(startPos);
139 int levelPrev = styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK;
140 int levelCurrent = levelPrev;
141 char chNext = styler[startPos];
142 int styleNext = styler.StyleAt(startPos);
143 for (unsigned int i = startPos; i < lengthDoc; i++) {
144 char ch = chNext;
145 chNext = styler.SafeGetCharAt(i + 1);
146 int style = styleNext;
147 styleNext = styler.StyleAt(i + 1);
148 bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
149 if (style == SCE_LISP_OPERATOR) {
150 if (ch == '(') {
151 levelCurrent++;
152 } else if (ch == ')') {
153 levelCurrent--;
156 if (atEOL) {
157 int lev = levelPrev;
158 if (visibleChars == 0)
159 lev |= SC_FOLDLEVELWHITEFLAG;
160 if ((levelCurrent > levelPrev) && (visibleChars > 0))
161 lev |= SC_FOLDLEVELHEADERFLAG;
162 if (lev != styler.LevelAt(lineCurrent)) {
163 styler.SetLevel(lineCurrent, lev);
165 lineCurrent++;
166 levelPrev = levelCurrent;
167 visibleChars = 0;
169 if (!isspacechar(ch))
170 visibleChars++;
172 // Fill in the real level of the next line, keeping the current flags as they will be filled in later
173 int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK;
174 styler.SetLevel(lineCurrent, levelPrev | flagsNext);
177 static const char * const lispWordListDesc[] = {
178 "Keywords",
182 LexerModule lmLISP(SCLEX_LISP, ColouriseLispDoc, "lisp", FoldLispDoc, lispWordListDesc);