evil-qemacs/lang/haskell.c
Charlie Gordon 7e70e611b6 Use char32_t for all code point variables and arrays
- char32_t is defined in cutils.h but may conflict with <uchar.h>
- all qe_isxxx functions take a char32_t
- add etf8x encoding for full range of char32_t type
- add utf8_decode_strict()
- add ustr_match_keyword()
- utf8_encode() / utf8_decode() handle up to 32 bits
- improve detect_charset():
  - detect utf8 and utf8x
  - avoid failure on partial utf8 sequence at end of buffer
  - detect overlong utf8 sequences
- fix display bugs for code points beyond MAX_UNICODE_DISPLAY
- simplify do_compare_windows(), avoid magic char values.
- rename functions:
    eb_encode_uchar -> eb_encode_char32
    eb_insert_uchar -> eb_insert_char32
    eb_insert_uchars -> eb_insert_char32_n
    eb_match_uchar -> eb_match_char32
    eb_delete_uchar -> eb_delete_char32
    eb_replace_uchar -> eb_replace_char32
    utf8_to_unicode -> utf8_to_char32
- use char32_t for all variables and arrays holding code-points
- fix some language syntax parsers.
- fix detect_eol_type_32bit(), used uint16_t instead of uint32_t
- bump VERSION to 5.4.0
2022-10-29 19:43:38 +02:00

257 lines
7.9 KiB
C

/*
* Haskell language mode for QEmacs.
*
* Copyright (c) 2000-2022 Charlie Gordon.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "qe.h"
/*---------------- Haskell coloring ----------------*/
static char const haskell_keywords[] = {
"|_|case|class|data|default|deriving|do|else|foreign"
"|if|import|in|infix|infixl|infixr|instance|let"
"|module|newtype|of|then|type|where|as|qualified"
"|return"
"|True|False"
};
static char const haskell_types[] = {
//String|Int|Char|Bool
"|"
};
enum {
HASKELL_STYLE_TEXT = QE_STYLE_DEFAULT,
HASKELL_STYLE_COMMENT = QE_STYLE_COMMENT,
HASKELL_STYLE_PP_COMMENT = QE_STYLE_PREPROCESS,
HASKELL_STYLE_STRING = QE_STYLE_STRING,
HASKELL_STYLE_NUMBER = QE_STYLE_NUMBER,
HASKELL_STYLE_KEYWORD = QE_STYLE_KEYWORD,
HASKELL_STYLE_FUNCTION = QE_STYLE_FUNCTION,
HASKELL_STYLE_TYPE = QE_STYLE_TYPE,
HASKELL_STYLE_SYMBOL = QE_STYLE_NUMBER,
};
enum {
IN_HASKELL_COMMENT = 0x0F,
IN_HASKELL_COMMENT_SHIFT = 0,
IN_HASKELL_PP_COMMENT = 0x10, /* compiler directives {-# ... #-} */
IN_HASKELL_STRING = 0x20,
};
static inline int haskell_is_symbol(char32_t c)
{
return qe_findchar("!#$%&+./<=>?@\\^|-~:", c);
}
static void haskell_colorize_line(QEColorizeContext *cp,
char32_t *str, int n, ModeDef *syn)
{
int i = 0, start = i, style = 0, level = 0, klen;
char32_t c, sep = 0;
int state = cp->colorize_state;
char kbuf[64];
if (state & IN_HASKELL_COMMENT)
goto parse_comment;
if (state & IN_HASKELL_STRING) {
sep = '\"';
state = 0;
while (qe_isblank(str[i]))
i++;
if (str[i] == '\\')
i++;
goto parse_string;
}
while (i < n) {
start = i;
c = str[i++];
switch (c) {
case '-':
if (str[i] == '-' && !haskell_is_symbol(str[i + 1])) {
i = n;
style = HASKELL_STYLE_COMMENT;
break;
}
goto parse_symbol;
case '{':
if (str[i] == '-') {
state |= 1 << IN_HASKELL_COMMENT_SHIFT;
i++;
if (str[i] == '#') {
state |= IN_HASKELL_PP_COMMENT;
i++;
}
parse_comment:
level = (state & IN_HASKELL_COMMENT) >> IN_HASKELL_COMMENT_SHIFT;
style = HASKELL_STYLE_COMMENT;
if (state & IN_HASKELL_PP_COMMENT)
style = HASKELL_STYLE_PP_COMMENT;
while (i < n) {
c = str[i++];
if (c == '{' && str[i] == '-') {
level++;
i++;
continue;
}
if (c == '-' && str[i] == '}') {
i++;
level--;
if (level == 0) {
state &= ~IN_HASKELL_PP_COMMENT;
i++;
break;
}
}
}
state &= ~IN_HASKELL_COMMENT;
state |= level << IN_HASKELL_COMMENT_SHIFT;
break;
}
/* FALL THRU */
case '}':
case '(':
case ')':
case '[':
case ']':
case ',':
case ';':
case '`':
/* special */
continue;
case '\'':
case '\"':
/* parse string const */
sep = c;
parse_string:
while (i < n) {
c = str[i++];
if (c == '\\') {
if (i == n) {
if (sep == '\"') {
/* XXX: should ignore whitespace */
state |= IN_HASKELL_STRING;
}
} else
if (str[i] == '^' && i + 1 < n && str[i + 1] != sep) {
i += 2;
} else {
i += 1;
}
} else
if (c == sep) {
state &= ~IN_HASKELL_STRING;
break;
}
}
style = HASKELL_STYLE_STRING;
break;
default:
if (qe_isdigit(c)) {
if (c == '0' && qe_tolower(str[i]) == 'o') {
/* octal numbers */
for (i += 1; qe_isoctdigit(str[i]); i++)
continue;
} else
if (c == '0' && qe_tolower(str[i]) == 'x') {
/* hexadecimal numbers */
for (i += 1; qe_isxdigit(str[i]); i++)
continue;
} else {
/* decimal numbers */
for (; qe_isdigit(str[i]); i++)
continue;
if (str[i] == '.' && qe_isdigit(str[i + 1])) {
/* decimal floats require a digit after the '.' */
for (i += 2; qe_isdigit(str[i]); i++)
continue;
if (qe_tolower(str[i]) == 'e') {
int k = i + 1;
if (str[k] == '+' || str[k] == '-')
k++;
if (qe_isdigit(str[k])) {
for (i = k + 1; qe_isdigit(str[i]); i++)
continue;
}
}
}
}
/* XXX: should detect malformed number constants */
style = HASKELL_STYLE_NUMBER;
break;
}
if (qe_isalpha_(c)) {
for (klen = 0, i--; qe_isalnum_(str[i]) || str[i] == '\''; i++) {
if (klen < countof(kbuf) - 1)
kbuf[klen++] = str[i];
}
kbuf[klen] = '\0';
if (strfind(syn->keywords, kbuf)) {
style = HASKELL_STYLE_KEYWORD;
break;
}
if (strfind(syn->types, kbuf)) {
style = HASKELL_STYLE_TYPE;
break;
}
if (check_fcall(str, i)) {
style = HASKELL_STYLE_FUNCTION;
break;
}
continue;
}
parse_symbol:
if (haskell_is_symbol(c)) {
for (; haskell_is_symbol(str[i]); i++)
continue;
style = HASKELL_STYLE_SYMBOL;
break;
}
continue;
}
if (style) {
SET_COLOR(str, start, i, style);
style = 0;
}
}
cp->colorize_state = state;
}
static ModeDef haskell_mode = {
.name = "Haskell",
.extensions = "hs|haskell",
.shell_handlers = "haskell",
.keywords = haskell_keywords,
.types = haskell_types,
.colorize_func = haskell_colorize_line,
};
static int haskell_init(void)
{
qe_register_mode(&haskell_mode, MODEF_SYNTAX);
return 0;
}
qe_module_init(haskell_init);