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