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