]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/LexSML.cxx
new settings for iphone
[wxWidgets.git] / src / stc / scintilla / src / LexSML.cxx
1 // Scintilla source code edit control
2 /** @file LexSML.cxx
3 ** Lexer for SML.
4 **/
5 // Copyright 2009 by James Moffatt and Yuzhou Xin
6 // Modified from LexCaml.cxx by Robert Roessler <robertr@rftp.com> Copyright 2005
7 // The License.txt file describes the conditions under which this software may be distributed.
8
9
10 #include <stdlib.h>
11 #include <string.h>
12 #include <ctype.h>
13 #include <stdio.h>
14 #include <stdarg.h>
15
16 #include "Platform.h"
17
18 #include "PropSet.h"
19 #include "Accessor.h"
20 #include "StyleContext.h"
21 #include "KeyWords.h"
22 #include "Scintilla.h"
23 #include "SciLexer.h"
24
25 inline int issml(int c) {return isalnum(c) || c == '_';}
26 inline int issmlf(int c) {return isalpha(c) || c == '_';}
27 inline int issmld(int c) {return isdigit(c) || c == '_';}
28
29
30 #ifdef SCI_NAMESPACE
31 using namespace Scintilla;
32 #endif
33
34 void ColouriseSMLDoc(
35 unsigned int startPos, int length,
36 int initStyle,
37 WordList *keywordlists[],
38 Accessor &styler)
39 {
40 StyleContext sc(startPos, length, initStyle, styler);
41 int nesting = 0;
42 if (sc.state < SCE_SML_STRING)
43 sc.state = SCE_SML_DEFAULT;
44 if (sc.state >= SCE_SML_COMMENT)
45 nesting = (sc.state & 0x0f) - SCE_SML_COMMENT;
46
47 int chBase = 0, chToken = 0, chLit = 0;
48 WordList& keywords = *keywordlists[0];
49 WordList& keywords2 = *keywordlists[1];
50 WordList& keywords3 = *keywordlists[2];
51 const int useMagic = styler.GetPropertyInt("lexer.caml.magic", 0);
52
53 while (sc.More()) {
54 int state2 = -1;
55 int chColor = sc.currentPos - 1;
56 bool advance = true;
57
58 switch (sc.state & 0x0f) {
59 case SCE_SML_DEFAULT:
60 chToken = sc.currentPos;
61 if (issmlf(sc.ch))
62 state2 = SCE_SML_IDENTIFIER;
63 else if (sc.Match('`') && issmlf(sc.chNext))
64 state2 = SCE_SML_TAGNAME;
65 else if (sc.Match('#')&&isdigit(sc.chNext))
66 state2 = SCE_SML_LINENUM;
67 else if (sc.Match('#','\"')){
68 state2 = SCE_SML_CHAR,chLit = 0;
69 sc.Forward();
70
71 }
72 else if (isdigit(sc.ch)) {
73 state2 = SCE_SML_NUMBER, chBase = 10;
74 if (sc.Match('0') && strchr("xX", sc.chNext))
75 chBase = 16, sc.Forward();}
76 else if (sc.Match('\"')&&sc.chPrev!='#')
77 state2 = SCE_SML_STRING;
78 else if (sc.Match('(', '*')){
79 state2 = SCE_SML_COMMENT,
80 sc.ch = ' ',
81 sc.Forward();}
82 else if (strchr("!~"
83 "=<>@^+-*/"
84 "()[];,:.#", sc.ch))
85 state2 = SCE_SML_OPERATOR;
86 break;
87
88 case SCE_SML_IDENTIFIER:
89 if (!(issml(sc.ch) || sc.Match('\''))) {
90 const int n = sc.currentPos - chToken;
91 if (n < 24) {
92 char t[24];
93 for (int i = -n; i < 0; i++)
94 t[n + i] = static_cast<char>(sc.GetRelative(i));
95 t[n] = '\0';
96 if ((n == 1 && sc.chPrev == '_') || keywords.InList(t))
97 sc.ChangeState(SCE_SML_KEYWORD);
98 else if (keywords2.InList(t))
99 sc.ChangeState(SCE_SML_KEYWORD2);
100 else if (keywords3.InList(t))
101 sc.ChangeState(SCE_SML_KEYWORD3);
102 }
103 state2 = SCE_SML_DEFAULT, advance = false;
104 }
105 break;
106
107 case SCE_SML_TAGNAME:
108 if (!(issml(sc.ch) || sc.Match('\'')))
109 state2 = SCE_SML_DEFAULT, advance = false;
110 break;
111
112 case SCE_SML_LINENUM:
113 if (!isdigit(sc.ch))
114 state2 = SCE_SML_DEFAULT, advance = false;
115 break;
116
117 case SCE_SML_OPERATOR: {
118 const char* o = 0;
119 if (issml(sc.ch) || isspace(sc.ch)
120 || (o = strchr(")]};,\'\"`#", sc.ch),o)
121 || !strchr("!$%&*+-./:<=>?@^|~", sc.ch)) {
122 if (o && strchr(")]};,", sc.ch)) {
123 if ((sc.Match(')') && sc.chPrev == '(')
124 || (sc.Match(']') && sc.chPrev == '['))
125 sc.ChangeState(SCE_SML_KEYWORD);
126 chColor++;
127 } else
128 advance = false;
129 state2 = SCE_SML_DEFAULT;
130 }
131 break;
132 }
133
134 case SCE_SML_NUMBER:
135 if (issmld(sc.ch) || IsADigit(sc.ch, chBase))
136 break;
137 if ((sc.Match('l') || sc.Match('L') || sc.Match('n'))
138 && (issmld(sc.chPrev) || IsADigit(sc.chPrev, chBase)))
139 break;
140 if (chBase == 10) {
141 if (sc.Match('.') && issmld(sc.chPrev))
142 break;
143 if ((sc.Match('e') || sc.Match('E'))
144 && (issmld(sc.chPrev) || sc.chPrev == '.'))
145 break;
146 if ((sc.Match('+') || sc.Match('-'))
147 && (sc.chPrev == 'e' || sc.chPrev == 'E'))
148 break;
149 }
150 state2 = SCE_SML_DEFAULT, advance = false;
151 break;
152
153 case SCE_SML_CHAR:
154 if (sc.Match('\\')) {
155 chLit = 1;
156 if (sc.chPrev == '\\')
157 sc.ch = ' ';
158 } else if ((sc.Match('\"') && sc.chPrev != '\\') || sc.atLineEnd) {
159 state2 = SCE_SML_DEFAULT;
160 chLit = 1;
161 if (sc.Match('\"'))
162 chColor++;
163 else
164 sc.ChangeState(SCE_SML_IDENTIFIER);
165 } else if (chLit < 1 && sc.currentPos - chToken >= 3)
166 sc.ChangeState(SCE_SML_IDENTIFIER), advance = false;
167 break;
168
169 case SCE_SML_STRING:
170 if (sc.Match('\\') && sc.chPrev == '\\')
171 sc.ch = ' ';
172 else if (sc.Match('\"') && sc.chPrev != '\\')
173 state2 = SCE_SML_DEFAULT, chColor++;
174 break;
175
176 case SCE_SML_COMMENT:
177 case SCE_SML_COMMENT1:
178 case SCE_SML_COMMENT2:
179 case SCE_SML_COMMENT3:
180 if (sc.Match('(', '*'))
181 state2 = sc.state + 1, chToken = sc.currentPos,
182 sc.ch = ' ',
183 sc.Forward(), nesting++;
184 else if (sc.Match(')') && sc.chPrev == '*') {
185 if (nesting)
186 state2 = (sc.state & 0x0f) - 1, chToken = 0, nesting--;
187 else
188 state2 = SCE_SML_DEFAULT;
189 chColor++;
190 } else if (useMagic && sc.currentPos - chToken == 4
191 && sc.Match('c') && sc.chPrev == 'r' && sc.GetRelative(-2) == '@')
192 sc.state |= 0x10;
193 break;
194 }
195
196 if (state2 >= 0)
197 styler.ColourTo(chColor, sc.state), sc.ChangeState(state2);
198 if (advance)
199 sc.Forward();
200 }
201
202 sc.Complete();
203 }
204
205 void FoldSMLDoc(
206 unsigned int startPos, int length,
207 int initStyle,
208 WordList *keywordlists[],
209 Accessor &styler)
210 {
211 //supress "not used" warnings
212 startPos || length || initStyle || keywordlists[0] || styler.Length();
213 }
214
215 static const char * const SMLWordListDesc[] = {
216 "Keywords",
217 "Keywords2",
218 "Keywords3",
219 0
220 };
221
222 LexerModule lmSML(SCLEX_SML, ColouriseSMLDoc, "SML", FoldSMLDoc, SMLWordListDesc);
223