Menu

[r1098]: / trunk / ext / scintilla / lexers / LexSML.cxx  Maximize  Restore  History

Download this file

225 lines (199 with data), 5.8 kB

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