2009-04-24 23:35:41 +00:00
|
|
|
/******************************************************************
|
|
|
|
* 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
|
|
|
|
*
|
2010-09-05 22:56:27 +00:00
|
|
|
* Several bug fixes by Krasimir Angelov - kr.angelov at gmail.com
|
2009-04-24 23:35:41 +00:00
|
|
|
*
|
|
|
|
* TODO:
|
|
|
|
* * Implement a folder :)
|
|
|
|
* * Nice Character-lexing (stuff inside '\''), LexPython has
|
|
|
|
* this.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*****************************************************************/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
2010-08-21 23:59:56 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2010-08-21 23:59:56 +00:00
|
|
|
#include "ILexer.h"
|
|
|
|
#include "Scintilla.h"
|
|
|
|
#include "SciLexer.h"
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2009-08-23 02:24:48 +00:00
|
|
|
#include "PropSetSimple.h"
|
2010-08-21 23:59:56 +00:00
|
|
|
#include "WordList.h"
|
|
|
|
#include "LexAccessor.h"
|
2009-04-24 23:35:41 +00:00
|
|
|
#include "Accessor.h"
|
|
|
|
#include "StyleContext.h"
|
2010-08-21 23:59:56 +00:00
|
|
|
#include "CharacterSet.h"
|
|
|
|
#include "LexerModule.h"
|
2009-04-24 23:35:41 +00:00
|
|
|
|
|
|
|
#ifdef SCI_NAMESPACE
|
|
|
|
using namespace Scintilla;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef BUILD_AS_EXTERNAL_LEXER
|
|
|
|
|
|
|
|
#include "ExternalLexer.h"
|
|
|
|
#include "WindowAccessor.h"
|
|
|
|
|
|
|
|
#define BUILD_EXTERNAL_LEXER 0
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2010-09-05 22:56:27 +00:00
|
|
|
#define HA_MODE_DEFAULT 0
|
|
|
|
#define HA_MODE_IMPORT1 1
|
|
|
|
#define HA_MODE_IMPORT2 2
|
|
|
|
#define HA_MODE_IMPORT3 3
|
|
|
|
#define HA_MODE_MODULE 4
|
|
|
|
#define HA_MODE_FFI 5
|
|
|
|
#define HA_MODE_TYPE 6
|
2009-04-24 23:35:41 +00:00
|
|
|
|
|
|
|
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];
|
2010-09-05 22:56:27 +00:00
|
|
|
WordList &ffi = *keywordlists[1];
|
2009-04-24 23:35:41 +00:00
|
|
|
|
|
|
|
StyleContext sc(startPos, length, initStyle, styler);
|
|
|
|
|
2010-09-05 22:56:27 +00:00
|
|
|
int lineCurrent = styler.GetLine(startPos);
|
|
|
|
int state = lineCurrent ? styler.GetLineState(lineCurrent-1)
|
|
|
|
: HA_MODE_DEFAULT;
|
|
|
|
int mode = state & 0xF;
|
|
|
|
int xmode = state >> 4;
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2010-09-05 22:56:27 +00:00
|
|
|
while (sc.More()) {
|
2009-04-24 23:35:41 +00:00
|
|
|
// Check for state end
|
2010-09-05 22:56:27 +00:00
|
|
|
|
2009-04-24 23:35:41 +00:00
|
|
|
// Operator
|
|
|
|
if (sc.state == SCE_HA_OPERATOR) {
|
2010-09-05 22:56:27 +00:00
|
|
|
if (isascii(sc.ch) && isoperator(static_cast<char>(sc.ch))) {
|
|
|
|
sc.Forward();
|
|
|
|
} else {
|
|
|
|
styler.ColourTo(sc.currentPos - 1, sc.state);
|
|
|
|
sc.ChangeState(SCE_HA_DEFAULT);
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
// String
|
|
|
|
else if (sc.state == SCE_HA_STRING) {
|
|
|
|
if (sc.ch == '\"') {
|
2010-09-05 22:56:27 +00:00
|
|
|
sc.Forward();
|
|
|
|
styler.ColourTo(sc.currentPos-1, sc.state);
|
|
|
|
sc.ChangeState(SCE_HA_DEFAULT);
|
2009-04-24 23:35:41 +00:00
|
|
|
} else if (sc.ch == '\\') {
|
2010-09-05 22:56:27 +00:00
|
|
|
sc.Forward(2);
|
|
|
|
} else if (sc.atLineEnd) {
|
|
|
|
styler.ColourTo(sc.currentPos-1, sc.state);
|
|
|
|
sc.ChangeState(SCE_HA_DEFAULT);
|
|
|
|
} else {
|
|
|
|
sc.Forward();
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
// Char
|
|
|
|
else if (sc.state == SCE_HA_CHARACTER) {
|
|
|
|
if (sc.ch == '\'') {
|
2010-09-05 22:56:27 +00:00
|
|
|
sc.Forward();
|
|
|
|
styler.ColourTo(sc.currentPos-1, sc.state);
|
|
|
|
sc.ChangeState(SCE_HA_DEFAULT);
|
2009-04-24 23:35:41 +00:00
|
|
|
} else if (sc.ch == '\\') {
|
2010-09-05 22:56:27 +00:00
|
|
|
sc.Forward(2);
|
|
|
|
} else if (sc.atLineEnd) {
|
|
|
|
styler.ColourTo(sc.currentPos-1, sc.state);
|
|
|
|
sc.ChangeState(SCE_HA_DEFAULT);
|
|
|
|
} else {
|
|
|
|
sc.Forward();
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
// Number
|
|
|
|
else if (sc.state == SCE_HA_NUMBER) {
|
2010-09-05 22:56:27 +00:00
|
|
|
if (IsADigit(sc.ch, xmode)) {
|
|
|
|
sc.Forward();
|
|
|
|
} else if ((xmode == 10) &&
|
|
|
|
(sc.ch == 'e' || sc.ch == 'E') &&
|
|
|
|
(IsADigit(sc.chNext) || sc.chNext == '+' || sc.chNext == '-')) {
|
|
|
|
sc.Forward();
|
|
|
|
if (sc.ch == '+' || sc.ch == '-')
|
|
|
|
sc.Forward();
|
|
|
|
} else {
|
|
|
|
styler.ColourTo(sc.currentPos - 1, sc.state);
|
|
|
|
sc.ChangeState(SCE_HA_DEFAULT);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Identifier
|
|
|
|
else if (sc.state == SCE_HA_IDENTIFIER) {
|
2010-09-05 22:56:27 +00:00
|
|
|
if (IsAWordChar(sc.ch)) {
|
|
|
|
sc.Forward();
|
|
|
|
} else {
|
2009-04-24 23:35:41 +00:00
|
|
|
char s[100];
|
|
|
|
sc.GetCurrent(s, sizeof(s));
|
2010-09-05 22:56:27 +00:00
|
|
|
int style = sc.state;
|
|
|
|
int new_mode = 0;
|
|
|
|
if (keywords.InList(s)) {
|
2009-04-24 23:35:41 +00:00
|
|
|
style = SCE_HA_KEYWORD;
|
|
|
|
} else if (isupper(s[0])) {
|
2010-09-05 22:56:27 +00:00
|
|
|
if (mode >= HA_MODE_IMPORT1 && mode <= HA_MODE_IMPORT3) {
|
|
|
|
style = SCE_HA_MODULE;
|
|
|
|
new_mode = HA_MODE_IMPORT2;
|
|
|
|
} else if (mode == HA_MODE_MODULE)
|
|
|
|
style = SCE_HA_MODULE;
|
2009-04-24 23:35:41 +00:00
|
|
|
else
|
2010-09-05 22:56:27 +00:00
|
|
|
style = SCE_HA_CAPITAL;
|
|
|
|
} else if (mode == HA_MODE_IMPORT1 &&
|
|
|
|
strcmp(s,"qualified") == 0) {
|
|
|
|
style = SCE_HA_KEYWORD;
|
|
|
|
new_mode = HA_MODE_IMPORT1;
|
|
|
|
} else if (mode == HA_MODE_IMPORT2) {
|
|
|
|
if (strcmp(s,"as") == 0) {
|
|
|
|
style = SCE_HA_KEYWORD;
|
|
|
|
new_mode = HA_MODE_IMPORT3;
|
|
|
|
} else if (strcmp(s,"hiding") == 0) {
|
|
|
|
style = SCE_HA_KEYWORD;
|
|
|
|
}
|
|
|
|
} else if (mode == HA_MODE_FFI) {
|
|
|
|
if (ffi.InList(s)) {
|
|
|
|
style = SCE_HA_KEYWORD;
|
|
|
|
new_mode = HA_MODE_FFI;
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2010-09-05 22:56:27 +00:00
|
|
|
else if (mode == HA_MODE_TYPE) {
|
|
|
|
if (strcmp(s,"family") == 0)
|
|
|
|
style = SCE_HA_KEYWORD;
|
|
|
|
}
|
|
|
|
styler.ColourTo(sc.currentPos - 1, style);
|
|
|
|
if (strcmp(s,"import") == 0 && mode != HA_MODE_FFI)
|
|
|
|
new_mode = HA_MODE_IMPORT1;
|
|
|
|
else if (strcmp(s,"module") == 0)
|
|
|
|
new_mode = HA_MODE_MODULE;
|
|
|
|
else if (strcmp(s,"foreign") == 0)
|
|
|
|
new_mode = HA_MODE_FFI;
|
|
|
|
else if (strcmp(s,"type") == 0)
|
|
|
|
new_mode = HA_MODE_TYPE;
|
|
|
|
sc.ChangeState(SCE_HA_DEFAULT);
|
|
|
|
mode = new_mode;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
2010-09-05 22:56:27 +00:00
|
|
|
|
2009-04-24 23:35:41 +00:00
|
|
|
// Comments
|
|
|
|
// Oneliner
|
|
|
|
else if (sc.state == SCE_HA_COMMENTLINE) {
|
2010-09-05 22:56:27 +00:00
|
|
|
if (sc.atLineEnd) {
|
|
|
|
styler.ColourTo(sc.currentPos - 1, sc.state);
|
|
|
|
sc.ChangeState(SCE_HA_DEFAULT);
|
|
|
|
} else {
|
|
|
|
sc.Forward();
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
// Nested
|
2010-09-05 22:56:27 +00:00
|
|
|
else if (sc.state == SCE_HA_COMMENTBLOCK) {
|
2009-04-24 23:35:41 +00:00
|
|
|
if (sc.Match("{-")) {
|
2010-09-05 22:56:27 +00:00
|
|
|
sc.Forward(2);
|
|
|
|
xmode++;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
else if (sc.Match("-}")) {
|
2010-09-05 22:56:27 +00:00
|
|
|
sc.Forward(2);
|
|
|
|
xmode--;
|
|
|
|
if (xmode == 0) {
|
|
|
|
styler.ColourTo(sc.currentPos - 1, sc.state);
|
|
|
|
sc.ChangeState(SCE_HA_DEFAULT);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (sc.atLineEnd) {
|
|
|
|
// Remember the line state for future incremental lexing
|
|
|
|
styler.SetLineState(lineCurrent, (xmode << 4) | mode);
|
|
|
|
lineCurrent++;
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
sc.Forward();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// New state?
|
|
|
|
if (sc.state == SCE_HA_DEFAULT) {
|
|
|
|
// Digit
|
2010-09-05 22:56:27 +00:00
|
|
|
if (IsADigit(sc.ch) ||
|
|
|
|
(sc.ch == '.' && IsADigit(sc.chNext)) ||
|
|
|
|
(sc.ch == '-' && IsADigit(sc.chNext))) {
|
|
|
|
styler.ColourTo(sc.currentPos - 1, sc.state);
|
|
|
|
sc.ChangeState(SCE_HA_NUMBER);
|
|
|
|
if (sc.ch == '0' && (sc.chNext == 'X' || sc.chNext == 'x')) {
|
|
|
|
// Match anything starting with "0x" or "0X", too
|
|
|
|
sc.Forward(2);
|
|
|
|
xmode = 16;
|
|
|
|
} else if (sc.ch == '0' && (sc.chNext == 'O' || sc.chNext == 'o')) {
|
|
|
|
// Match anything starting with "0x" or "0X", too
|
|
|
|
sc.Forward(2);
|
|
|
|
xmode = 8;
|
|
|
|
} else {
|
|
|
|
sc.Forward();
|
|
|
|
xmode = 10;
|
|
|
|
}
|
|
|
|
mode = HA_MODE_DEFAULT;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
// Comment line
|
|
|
|
else if (sc.Match("--")) {
|
2010-09-05 22:56:27 +00:00
|
|
|
styler.ColourTo(sc.currentPos - 1, sc.state);
|
|
|
|
sc.Forward(2);
|
|
|
|
sc.ChangeState(SCE_HA_COMMENTLINE);
|
2009-04-24 23:35:41 +00:00
|
|
|
// Comment block
|
|
|
|
}
|
|
|
|
else if (sc.Match("{-")) {
|
2010-09-05 22:56:27 +00:00
|
|
|
styler.ColourTo(sc.currentPos - 1, sc.state);
|
|
|
|
sc.Forward(2);
|
|
|
|
sc.ChangeState(SCE_HA_COMMENTBLOCK);
|
|
|
|
xmode = 1;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
// String
|
|
|
|
else if (sc.Match('\"')) {
|
2010-09-05 22:56:27 +00:00
|
|
|
styler.ColourTo(sc.currentPos - 1, sc.state);
|
|
|
|
sc.Forward();
|
|
|
|
sc.ChangeState(SCE_HA_STRING);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
// Character
|
|
|
|
else if (sc.Match('\'')) {
|
2010-09-05 22:56:27 +00:00
|
|
|
styler.ColourTo(sc.currentPos - 1, sc.state);
|
|
|
|
sc.Forward();
|
|
|
|
sc.ChangeState(SCE_HA_CHARACTER);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2010-09-05 22:56:27 +00:00
|
|
|
else if (sc.ch == '(' || sc.ch == ')' ||
|
|
|
|
sc.ch == '{' || sc.ch == '}' ||
|
|
|
|
sc.ch == '[' || sc.ch == ']') {
|
|
|
|
styler.ColourTo(sc.currentPos - 1, sc.state);
|
|
|
|
sc.Forward();
|
|
|
|
styler.ColourTo(sc.currentPos - 1, SCE_HA_OPERATOR);
|
|
|
|
mode = HA_MODE_DEFAULT;
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
// Operator
|
|
|
|
else if (isascii(sc.ch) && isoperator(static_cast<char>(sc.ch))) {
|
2010-09-05 22:56:27 +00:00
|
|
|
styler.ColourTo(sc.currentPos - 1, sc.state);
|
|
|
|
sc.Forward();
|
|
|
|
sc.ChangeState(SCE_HA_OPERATOR);
|
|
|
|
mode = HA_MODE_DEFAULT;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
// Keyword
|
|
|
|
else if (IsAWordStart(sc.ch)) {
|
2010-09-05 22:56:27 +00:00
|
|
|
styler.ColourTo(sc.currentPos - 1, sc.state);
|
|
|
|
sc.Forward();
|
|
|
|
sc.ChangeState(SCE_HA_IDENTIFIER);
|
|
|
|
} else {
|
|
|
|
if (sc.atLineEnd) {
|
|
|
|
// Remember the line state for future incremental lexing
|
|
|
|
styler.SetLineState(lineCurrent, (xmode << 4) | mode);
|
|
|
|
lineCurrent++;
|
|
|
|
}
|
|
|
|
sc.Forward();
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
2009-08-23 02:24:48 +00:00
|
|
|
PropSetSimple ps;
|
2009-04-24 23:35:41 +00:00
|
|
|
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");
|