summaryrefslogtreecommitdiffstats
path: root/src/LexHaskell.cpp
blob: 0e4be85c6ba262a20ec380f8763f783c2640e2d5 (plain)
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/******************************************************************
 *    LexHaskell.cxx
 *
 *    A haskell lexer for the scintilla code control.
 *    Some stuff "lended" from LexPython.cxx and LexCPP.cxx.
 *    External lexer stuff inspired from the caml external lexer.
 *
 *    Written by Tobias Engvall - tumm at dtek dot chalmers dot se
 *
 *
 *    TODO:
 *    * Implement a folder :)
 *    * Nice Character-lexing (stuff inside '\''), LexPython has
 *      this.
 *
 *
 *****************************************************************/

#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <stdarg.h>

#include "Platform.h"

#include "PropSet.h"
#include "Accessor.h"
#include "StyleContext.h"
#include "KeyWords.h"
#include "Scintilla.h"
#include "SciLexer.h"

#ifdef BUILD_AS_EXTERNAL_LEXER

#include "ExternalLexer.h"
#include "WindowAccessor.h"

#define BUILD_EXTERNAL_LEXER 0

#endif

// Max level of nested comments
#define SCE_HA_COMMENTMAX SCE_HA_COMMENTBLOCK3


enum kwType { kwOther, kwClass, kwData, kwInstance, kwImport, kwModule, kwType};

static inline bool IsNewline(const int ch) {
   return (ch == '\n' || ch == '\r');
}

static inline bool IsWhitespace(const int ch) {
   return (  ch == ' '
          || ch == '\t'
          || IsNewline(ch) );
}

static inline bool IsAWordStart(const int ch) {
   return (ch < 0x80) && (isalnum(ch) || ch == '_');
}

static inline bool IsAWordChar(const int ch) {
   return (ch < 0x80) && (isalnum(ch) || ch == '.' || ch == '_' || ch == '\'');
}

static void ColorizeHaskellDoc(unsigned int startPos, int length, int initStyle,
                               WordList *keywordlists[], Accessor &styler) {

   WordList &keywords = *keywordlists[0];

   int kwLast = kwOther;

   StyleContext sc(startPos, length, initStyle, styler);

   for (; sc.More(); sc.Forward()) {

      // Check for state end
         // Operator
      if (sc.state == SCE_HA_OPERATOR) {
         kwLast = kwOther;
         sc.SetState(SCE_HA_DEFAULT);
      }
         // String
      else if (sc.state == SCE_HA_STRING) {
         if (sc.ch == '\"') {
            sc.ForwardSetState(SCE_HA_DEFAULT);
         }
      }
         // Char
      else if (sc.state == SCE_HA_CHARACTER) {
         if (sc.ch == '\'') {
            sc.ForwardSetState(SCE_HA_DEFAULT);
         }
      }
         // Number
      else if (sc.state == SCE_HA_NUMBER) {
         if (!IsADigit(sc.ch)) {
            sc.SetState(SCE_HA_DEFAULT);
         }
      }
         // Types, constructors, etc.
      else if (sc.state == SCE_HA_CAPITAL) {
         if (!IsAWordChar(sc.ch) || sc.ch == '.') {
            sc.SetState(SCE_HA_DEFAULT);
         }
      }
         // Identifier
      else if (sc.state == SCE_HA_IDENTIFIER) {
         if (!IsAWordChar(sc.ch)) {
            char s[100];
            sc.GetCurrent(s, sizeof(s));
            int style = SCE_HA_IDENTIFIER;
            if ((kwLast == kwImport) || (strcmp(s,"qualified") == 0) || (strcmp(s,"as") == 0)) {
               style = SCE_HA_IMPORT;
            } else if (keywords.InList(s)) {
               style = SCE_HA_KEYWORD;
            } else if (kwLast == kwData) {
               style = SCE_HA_DATA;
            } else if (kwLast == kwClass) {
               style = SCE_HA_CLASS;
            } else if (kwLast == kwModule) {
               style = SCE_HA_MODULE;
            } else if (isupper(s[0])) {
               style = SCE_HA_CAPITAL;
            }
            sc.ChangeState(style);
            sc.SetState(SCE_HA_DEFAULT);
            if (style == SCE_HA_KEYWORD) {
               if (0 == strcmp(s, "class"))
                  kwLast = kwClass;
               else if (0 == strcmp(s, "data"))
                  kwLast = kwData;
               else if (0 == strcmp(s, "instance"))
                  kwLast = kwInstance;
               else if (0 == strcmp(s, "import"))
                  kwLast = kwImport;
               else if (0 == strcmp(s, "module"))
                  kwLast = kwModule;
               else
                  kwLast = kwOther;
            } else if (style == SCE_HA_CLASS || style == SCE_HA_IMPORT ||
                       style == SCE_HA_MODULE || style == SCE_HA_CAPITAL ||
                       style == SCE_HA_DATA || style == SCE_HA_INSTANCE) {
               kwLast = kwOther;
            }
         }
      }
         // Comments
            // Oneliner
      else if (sc.state == SCE_HA_COMMENTLINE) {
         if (IsNewline(sc.ch))
            sc.SetState(SCE_HA_DEFAULT);
      }
            // Nested
      else if (sc.state >= SCE_HA_COMMENTBLOCK) {
         if (sc.Match("{-")) {
            if (sc.state < SCE_HA_COMMENTMAX)
               sc.SetState(sc.state + 1);
         }
         else if (sc.Match("-}")) {
            sc.Forward();
            if (sc.state == SCE_HA_COMMENTBLOCK)
               sc.ForwardSetState(SCE_HA_DEFAULT);
            else
               sc.ForwardSetState(sc.state - 1);
         }
      }
      // New state?
      if (sc.state == SCE_HA_DEFAULT) {
         // Digit
         if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {
            sc.SetState(SCE_HA_NUMBER);
         }
         // Comment line
         else if (sc.Match("--")) {
            sc.SetState(SCE_HA_COMMENTLINE);
         // Comment block
         }
         else if (sc.Match("{-")) {
            sc.SetState(SCE_HA_COMMENTBLOCK);
         }
         // String
         else if (sc.Match('\"')) {
            sc.SetState(SCE_HA_STRING);
         }
         // Character
         else if (sc.Match('\'') && IsWhitespace(sc.GetRelative(-1)) ) {
            sc.SetState(SCE_HA_CHARACTER);
         }
         // Stringstart
         else if (sc.Match('\"')) {
            sc.SetState(SCE_HA_STRING);
         }
         // Operator
         else if (isascii(sc.ch) && isoperator(static_cast<char>(sc.ch))) {
            sc.SetState(SCE_HA_OPERATOR);
         }
         // Keyword
         else if (IsAWordStart(sc.ch)) {
               sc.SetState(SCE_HA_IDENTIFIER);
         }

      }
   }
   sc.Complete();
}

// External stuff - used for dynamic-loading, not implemented in wxStyledTextCtrl yet.
// Inspired by the caml external lexer - Credits to Robert Roessler - http://www.rftp.com
#ifdef BUILD_EXTERNAL_LEXER
static const char* LexerName = "haskell";

void EXT_LEXER_DECL Lex(unsigned int lexer, unsigned int startPos, int length, int initStyle,
                        char *words[], WindowID window, char *props)
{
   PropSet ps;
   ps.SetMultiple(props);
   WindowAccessor wa(window, ps);

   int nWL = 0;
   for (; words[nWL]; nWL++) ;
   WordList** wl = new WordList* [nWL + 1];
   int i = 0;
   for (; i<nWL; i++)
   {
      wl[i] = new WordList();
      wl[i]->Set(words[i]);
   }
   wl[i] = 0;

   ColorizeHaskellDoc(startPos, length, initStyle, wl, wa);
   wa.Flush();
   for (i=nWL-1;i>=0;i--)
      delete wl[i];
   delete [] wl;
}

void EXT_LEXER_DECL Fold (unsigned int lexer, unsigned int startPos, int length, int initStyle,
                        char *words[], WindowID window, char *props)
{

}

int EXT_LEXER_DECL GetLexerCount()
{
   return 1;
}

void EXT_LEXER_DECL GetLexerName(unsigned int Index, char *name, int buflength)
{
   if (buflength > 0) {
      buflength--;
      int n = strlen(LexerName);
      if (n > buflength)
         n = buflength;
      memcpy(name, LexerName, n), name[n] = '\0';
   }
}
#endif

LexerModule lmHaskell(SCLEX_HASKELL, ColorizeHaskellDoc, "haskell");