From 66eef8269b4e08ff420b4c1bb67b3772de89f58c Mon Sep 17 00:00:00 2001 From: Charlie Gordon Date: Tue, 17 Nov 2020 23:42:40 +0100 Subject: [PATCH] Split exotic languages from lang/extra-mode.c: - add falcon magpie openscad tiger wolfram modes as separate modules --- Makefile | 3 +- lang/extra-modes.c | 960 +-------------------------------------------- lang/falcon.c | 228 +++++++++++ lang/magpie.c | 210 ++++++++++ lang/scad.c | 201 ++++++++++ lang/tiger.c | 186 +++++++++ lang/wolfram.c | 243 ++++++++++++ 7 files changed, 1071 insertions(+), 960 deletions(-) create mode 100644 lang/falcon.c create mode 100644 lang/magpie.c create mode 100644 lang/scad.c create mode 100644 lang/tiger.c create mode 100644 lang/wolfram.c diff --git a/Makefile b/Makefile index 0e2c25c..c30bcbe 100644 --- a/Makefile +++ b/Makefile @@ -135,7 +135,8 @@ ifdef CONFIG_ALL_MODES lang/lisp.o lang/makemode.o modes/markdown.o modes/orgmode.o lang/perl.o lang/script.o \ lang/ebnf.o lang/cobol.o lang/rlang.o lang/txl.o lang/nim.o lang/rebol.o lang/elm.o lang/jai.o lang/ats.o \ lang/rust.o lang/swift.o lang/icon.o lang/groovy.o lang/virgil.o \ - $(EXTRA_MODES) lang/extra-modes.o modes/fractal.o + lang/scad.o lang/magpie.o lang/falcon.o lang/wolfram.o lang/tiger.o \ + lang/extra-modes.o $(EXTRA_MODES) modes/fractal.o ifndef CONFIG_WIN32 OBJS+= modes/shell.o modes/dired.o modes/latex-mode.o modes/archive.o endif diff --git a/lang/extra-modes.c b/lang/extra-modes.c index 6882992..d1009af 100644 --- a/lang/extra-modes.c +++ b/lang/extra-modes.c @@ -328,7 +328,7 @@ static int is_vim_keyword(unsigned int *str, int from, int to, const char *p; int c, i, len = to - from; - if (len >= MAX_KEYWORD_SIZE) + if (len >= (int)sizeof(keyword)) return 0; for (i = 0; i < len; i++) { @@ -4645,959 +4645,6 @@ static int smalltalk_init(void) return 0; } -/*---------------- OpenSCAD language ----------------*/ - -static const char scad_keywords[] = { - "true|false|undef|" - "module|function|for|if|else|len|" -// "assert|break|case|const|continue|default|defined|do|exit|" -// "forward|goto|native|new|operator|public|return|sizeof|sleep|" -// "state|static|stock|switch|tagof|while|", -}; - -static const char scad_preprocessor_keywords[] = { - "use|include|" -}; - -static const char scad_types[] = { - //"void|" - //"bool|string|int|uint|uchar|nt8|short|ushort|long|ulong|size_t|ssize_t|" - //"double|va_list|unichar|" -}; - -enum { - IN_SCAD_COMMENT = 0x01, -}; - -enum { - SCAD_STYLE_TEXT = QE_STYLE_DEFAULT, - SCAD_STYLE_KEYWORD = QE_STYLE_KEYWORD, - SCAD_STYLE_TYPE = QE_STYLE_TYPE, - SCAD_STYLE_PREPROCESS = QE_STYLE_PREPROCESS, - SCAD_STYLE_COMMENT = QE_STYLE_COMMENT, - SCAD_STYLE_STRING = QE_STYLE_STRING, - SCAD_STYLE_NUMBER = QE_STYLE_NUMBER, - SCAD_STYLE_FUNCTION = QE_STYLE_FUNCTION, - SCAD_STYLE_ARGNAME = QE_STYLE_FUNCTION, -}; - -static void scad_colorize_line(QEColorizeContext *cp, - unsigned int *str, int n, ModeDef *syn) -{ - char keyword[MAX_KEYWORD_SIZE]; - int i = 0, start = i, c, style = 0, k, len, laststyle = 0, isnum; - int colstate = cp->colorize_state; - int level = colstate >> 1; - - if (colstate & IN_SCAD_COMMENT) - goto in_comment; - - while (i < n) { - start = i; - c = str[i++]; - switch (c) { - case '/': - if (str[i] == '/') { /* single line comment */ - i = n; - style = SCAD_STYLE_COMMENT; - break; - } - if (str[i] == '*') { /* multi-line comment */ - colstate |= IN_SCAD_COMMENT; - i++; - in_comment: - for (; i < n; i++) { - if (str[i] == '*' && str[i + 1] == '/') { - i += 2; - colstate &= ~IN_SCAD_COMMENT; - break; - } - } - style = SCAD_STYLE_COMMENT; - break; - } - continue; - case '<': - if (laststyle == SCAD_STYLE_PREPROCESS) { - /* filename for include and use directives */ - while (i < n) { - if (str[i++] == '>') - break; - } - style = SCAD_STYLE_STRING; - break; - } - continue; - case '(': - case '[': - case '{': - level <<= 1; - continue; - case '}': - case ']': - case ')': - level >>= 1; - continue; - case '\'': - case '\"': - /* parse string or char const */ - while (i < n) { - /* XXX: escape sequences? */ - if (str[i] == '\\' && i + 1 < n) { - i++; - continue; - } - if (str[i++] == (unsigned int)c) - break; - } - style = SCAD_STYLE_STRING; - break; - default: - /* parse identifiers, keywords and numbers */ - if (qe_isalnum_(c) || c == '$') { - isnum = qe_isdigit(c); - len = 0; - keyword[len++] = c; - for (; qe_isalnum_(str[i]) || str[i] == '.'; i++) { - if (str[i] == '.') { - if (!isnum) - break; - } else { - if (!qe_isdigit(str[i])) - isnum = 0; - } - if (len < countof(keyword) - 1) - keyword[len++] = str[i]; - } - keyword[len] = '\0'; - if (isnum) { - style = SCAD_STYLE_NUMBER; - } - if (strfind(syn->keywords, keyword)) { - style = SCAD_STYLE_KEYWORD; - } else - if (strfind(scad_preprocessor_keywords, keyword)) { - style = SCAD_STYLE_PREPROCESS; - } else - if (strfind(syn->types, keyword)) { - style = SCAD_STYLE_TYPE; - } else { - k = i; - if (qe_isblank(str[k])) - k++; - if ((level & 2) && str[k] == '=') { - style = SCAD_STYLE_ARGNAME; - } else - if (str[k] == '(') { - style = SCAD_STYLE_FUNCTION; - level |= 1; - } - } - break; - } - continue; - } - if (style) { - laststyle = style; - SET_COLOR(str, start, i, style); - style = 0; - } - } - cp->colorize_state = (colstate & IN_SCAD_COMMENT) | (level << 1); -} - -static ModeDef scad_mode = { - .name = "OpenSCAD", - .extensions = "scad", - .colorize_func = scad_colorize_line, - .keywords = scad_keywords, - .types = scad_types, - //.indent_func = c_indent_line, - //.auto_indent = 1, -}; - -static int scad_init(void) -{ - qe_register_mode(&scad_mode, MODEF_SYNTAX); - - return 0; -} - -/*---------------- Magpie coloring ----------------*/ - -static char const magpie_keywords[] = { - "and|as|break|case|catch|defclass|def|do|else|end|fn|for|if|" - "import|in|is|let|match|or|return|then|throw|var|val|while|with|" - "not|native|namespace|class|struct|using|new|interface|" - "get|set|shared|done|" - "false|true|nothing|it|xor|" -}; - -enum { - IN_MAGPIE_COMMENT = 0x0F, /* nested comment level */ - IN_MAGPIE_STRING = 0x10, /* double quote */ -}; - -enum { - MAGPIE_STYLE_TEXT = QE_STYLE_DEFAULT, - MAGPIE_STYLE_SHBANG = QE_STYLE_PREPROCESS, - MAGPIE_STYLE_COMMENT = QE_STYLE_COMMENT, - MAGPIE_STYLE_STRING = QE_STYLE_STRING, - MAGPIE_STYLE_CHAR = QE_STYLE_STRING, - MAGPIE_STYLE_NUMBER = QE_STYLE_NUMBER, - MAGPIE_STYLE_KEYWORD = QE_STYLE_KEYWORD, - MAGPIE_STYLE_TYPE = QE_STYLE_TYPE, - MAGPIE_STYLE_FUNCTION = QE_STYLE_FUNCTION, -}; - -static void magpie_colorize_line(QEColorizeContext *cp, - unsigned int *str, int n, ModeDef *syn) -{ - int i = 0, start = i, c, style = 0, level; - int state = cp->colorize_state; - char kbuf[64]; - - if ((level = state & IN_MAGPIE_COMMENT) != 0) - goto parse_comment; - - if (state & IN_MAGPIE_STRING) - goto parse_string; - - while (i < n) { - start = i; - c = str[i++]; - switch (c) { - case '#': - if (i == 1 && str[i] == '!') { - i = n; - style = MAGPIE_STYLE_SHBANG; - break; - } - i = n; - style = MAGPIE_STYLE_COMMENT; /* probably incorrect */ - break; - - case '/': - if (str[i] == '*') { - /* nexted C block comments */ - i++; - level = 1; - parse_comment: - for (; i < n; i++) { - if (str[i] == '*' && str[i + 1] == '/') { - i += 2; - if (--level == 0) - break; - } else - if (str[i] == '/' && str[i + 1] == '*') { - i += 2; - level++; - } - } - state &= ~IN_MAGPIE_COMMENT; - state |= level & IN_MAGPIE_COMMENT; - style = MAGPIE_STYLE_COMMENT; - break; - } - if (str[i] == '/') { - i = n; - style = MAGPIE_STYLE_COMMENT; - break; - } - continue; - - case '\'': - /* a single quoted character */ - while (i < n) { - c = str[i++]; - if (c == '\\') { - if (i < n) { - i += 1; - } - } else - if (c == '\'') { - break; - } - } - style = MAGPIE_STYLE_CHAR; - break; - - case '\"': - /* parse double quoted string const */ - state = IN_MAGPIE_STRING; - parse_string: - c = '\0'; - while (i < n) { - c = str[i++]; - if (c == '\\') { - if (i < n) { - i += 1; - } - } else - if (c == '#' && str[i] == '{') { - /* should parse full syntax */ - while (i < n && str[i++] != '}') - continue; - } else - if (c == '\"') { - state = 0; - break; - } - } - style = MAGPIE_STYLE_STRING; - break; - - case '.': - if (qe_isdigit_(str[i])) - goto parse_decimal; - continue; - - default: - if (qe_isdigit(c)) { - /* decimal numbers */ - for (; qe_isdigit_(str[i]); i++) - continue; - if (str[i] == '.') { - i++; - parse_decimal: - for (; qe_isdigit_(str[i]); i++) - continue; - } - style = MAGPIE_STYLE_NUMBER; - break; - } - if (qe_isalpha_(c)) { - i += ustr_get_identifier(kbuf, countof(kbuf), c, str, i, n); - if (strfind(syn->keywords, kbuf)) { - style = MAGPIE_STYLE_KEYWORD; - break; - } - if (qe_isblank(str[i])) - i++; - if (str[i] == '(' || str[i] == '{') { - style = MAGPIE_STYLE_FUNCTION; - break; - } - if (qe_isupper(kbuf[0]) && (start == 0 || str[start-1] != '.')) { - /* Types are capitalized */ - style = MAGPIE_STYLE_TYPE; - break; - } - continue; - } - continue; - } - if (style) { - SET_COLOR(str, start, i, style); - style = 0; - } - } - cp->colorize_state = state; -} - -static ModeDef magpie_mode = { - .name = "Magpie", - .extensions = "mag", - .shell_handlers = "magpie", - .keywords = magpie_keywords, - .colorize_func = magpie_colorize_line, -}; - -static int magpie_init(void) -{ - qe_register_mode(&magpie_mode, MODEF_SYNTAX); - - return 0; -} - -/*---- Giancarlo Niccolai's Falcon scripting language -----*/ - -static const char falcon_keywords[] = { - "if|elif|else|end|switch|select|case|default|" - "while|loop|for|break|continue|dropping|" - "forfirst|formiddle|forlast|" - "try|catch|raise|state|callable|launch|" - "function|return|innerfunc|fself|" - "object|self|provides|from|init|" - "class|const|enum|global|static|" - "export|load|import|as|" - "directive|def|macro|" - "to|and|or|not|in|notin|true|false|nil|" -}; - -enum { - IN_FALCON_COMMENT = 0x01, /* nested comment level */ - IN_FALCON_STRING1 = 0x10, /* single quote */ - IN_FALCON_STRING2 = 0x20, /* double quote */ -}; - -enum { - FALCON_STYLE_TEXT = QE_STYLE_DEFAULT, - FALCON_STYLE_SHBANG = QE_STYLE_PREPROCESS, - FALCON_STYLE_COMMENT = QE_STYLE_COMMENT, - FALCON_STYLE_STRING1 = QE_STYLE_STRING, - FALCON_STYLE_STRING2 = QE_STYLE_STRING, - FALCON_STYLE_NUMBER = QE_STYLE_NUMBER, - FALCON_STYLE_KEYWORD = QE_STYLE_KEYWORD, - FALCON_STYLE_TYPE = QE_STYLE_TYPE, - FALCON_STYLE_FUNCTION = QE_STYLE_FUNCTION, -}; - -static void falcon_colorize_line(QEColorizeContext *cp, - unsigned int *str, int n, ModeDef *syn) -{ - int i = 0, start = i, c, style = 0; - int state = cp->colorize_state; - char kbuf[64]; - - if (state & IN_FALCON_COMMENT) - goto parse_comment; - - if (state & IN_FALCON_STRING1) - goto parse_string1; - - if (state & IN_FALCON_STRING2) - goto parse_string2; - - while (i < n) { - start = i; - c = str[i++]; - switch (c) { - case '#': - if (i == 1 && str[i] == '!') { - i = n; - style = FALCON_STYLE_SHBANG; - break; - } - continue; - - case '/': - if (str[i] == '*') { - /* C block comments */ - i++; - state |= IN_FALCON_COMMENT; - parse_comment: - for (; i < n; i++) { - if (str[i] == '*' && str[i + 1] == '/') { - i += 2; - state &= ~IN_FALCON_COMMENT; - break; - } - } - style = FALCON_STYLE_COMMENT; - break; - } - if (str[i] == '/') { - i = n; - style = FALCON_STYLE_COMMENT; - break; - } - continue; - - case '\'': - /* a single quoted string literal */ - state = IN_FALCON_STRING1; - parse_string1: - while (i < n) { - c = str[i++]; - if (c == '\\') { - if (i < n) { - i += 1; - } - } else - if (c == '\'') { - state &= ~IN_FALCON_STRING1; - break; - } - } - style = FALCON_STYLE_STRING1; - break; - - case '\"': - /* parse double quoted string literal */ - state = IN_FALCON_STRING2; - parse_string2: - c = '\0'; - while (i < n) { - c = str[i++]; - if (c == '\\') { - if (i < n) { - i += 1; - } - } else - if (c == '\"') { - state &= ~IN_FALCON_STRING2; - break; - } - } - style = FALCON_STYLE_STRING1; - break; - - case '.': - if (qe_isdigit(str[i])) - goto parse_decimal; - continue; - - default: - if (qe_isdigit(c)) { - if (c == '0' && qe_tolower(str[i]) == 'x') { - /* hexadecimal numbers */ - for (i += 1; qe_isxdigit(str[i]); i++) - continue; - } else - if (c == '0') { - /* octal numbers */ - for (i += 1; qe_isoctdigit(str[i]); i++) - continue; - } else { - /* decimal numbers */ - for (; qe_isdigit(str[i]); i++) - continue; - if (str[i] == '.') { - i++; - parse_decimal: - for (; 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 = FALCON_STYLE_NUMBER; - break; - } - if (qe_isalpha_(c) || c > 0xA0) { - i += ustr_get_word(kbuf, countof(kbuf), c, str, i, n); - if (strfind(syn->keywords, kbuf)) { - style = FALCON_STYLE_KEYWORD; - break; - } - if (check_fcall(str, i)) { - style = FALCON_STYLE_FUNCTION; - break; - } - if (qe_isupper(kbuf[0]) && (start == 0 || str[start-1] != '.')) { - /* Types are capitalized */ - style = FALCON_STYLE_TYPE; - break; - } - continue; - } - continue; - } - if (style) { - SET_COLOR(str, start, i, style); - style = 0; - } - } - cp->colorize_state = state; -} - -static ModeDef falcon_mode = { - .name = "Falcon", - .extensions = "fal", - .shell_handlers = "falcon", - .colorize_func = falcon_colorize_line, - .keywords = falcon_keywords, -}; - -static int falcon_init(void) -{ - qe_register_mode(&falcon_mode, MODEF_SYNTAX); - - return 0; -} - -/*---------------- Mathematica / Wolfram language ----------------*/ - -static char const wolfram_keywords[] = { - "|False|True|None" - "|" -}; - -static char const wolfram_types[] = { - "|" -}; - -enum { - IN_WOLFRAM_DEFAULT = 0, - IN_WOLFRAM_STRING1 = 1, - IN_WOLFRAM_STRING2 = 2, - IN_WOLFRAM_COMMENT_SHIFT = 2, -}; - -enum { - WOLFRAM_STYLE_TEXT = QE_STYLE_DEFAULT, - WOLFRAM_STYLE_KEYWORD = QE_STYLE_KEYWORD, - WOLFRAM_STYLE_TYPE = QE_STYLE_TYPE, - WOLFRAM_STYLE_COMMENT = QE_STYLE_COMMENT, - WOLFRAM_STYLE_STRING1 = QE_STYLE_STRING, - WOLFRAM_STYLE_STRING2 = QE_STYLE_STRING, - WOLFRAM_STYLE_IDENTIFIER = QE_STYLE_DEFAULT, - WOLFRAM_STYLE_VARIABLE = QE_STYLE_VARIABLE, - WOLFRAM_STYLE_NUMBER = QE_STYLE_NUMBER, - WOLFRAM_STYLE_FUNCTION = QE_STYLE_FUNCTION, -}; - -static int wolfram_get_identifier(char *buf, int buf_size, int c, - const unsigned int *str, int i, int n) -{ - int len = 0, j; - - if (len < buf_size) { - buf[len++] = c; - } - for (j = i; j < n; j++) { - c = str[j]; - if (!qe_isalnum_(c) && c != '$' && c != '`') - break; - if (len < buf_size - 1) - buf[len++] = c; - } - if (len < buf_size) { - buf[len] = '\0'; - } - return j - i; -} - -static void wolfram_colorize_line(QEColorizeContext *cp, - unsigned int *str, int n, ModeDef *syn) -{ - char kbuf[MAX_KEYWORD_SIZE]; - int i = 0, start = i, c, c1 = 0, k, style = 0, comment_level, base; - int colstate = cp->colorize_state; - - switch (colstate) { - case IN_WOLFRAM_DEFAULT: - break; - case IN_WOLFRAM_STRING1: - goto in_string1; - case IN_WOLFRAM_STRING2: - goto in_string2; - default: - comment_level = colstate >> IN_WOLFRAM_COMMENT_SHIFT; - goto in_comment; - } - - while (i < n) { - start = i; - c = str[i++]; - switch (c) { - case '(': - if (str[i] == '*') { /* parse comment */ - comment_level = 1; - i++; - in_comment: - while (i < n) { - c = str[i++]; - if (c == '(' && str[i] == '*') { - /* nested comment */ - i++; - comment_level++; - } else - if (c == '*' && str[i] == ')') { - i++; - if (--comment_level == 0) - break; - } - } - colstate = comment_level << IN_WOLFRAM_COMMENT_SHIFT; - style = WOLFRAM_STYLE_COMMENT; - break; - } - continue; - case '\'': - /* parse single quoted string */ - colstate = IN_WOLFRAM_STRING1; - in_string1: - /* XXX: should flag syntax error on - unescaped newline except at EOF */ - style = WOLFRAM_STYLE_STRING1; - while (i < n) { - c1 = str[i++]; - if (c1 == '\'') { - colstate = 0; - break; - } - if (c1 == '\\') { - if (i < n) - i++; - } - } - break; - case '\"': - /* parse double quoted string */ - colstate = IN_WOLFRAM_STRING2; - in_string2: - /* XXX: should flag syntax error on - unescaped newline except at EOF */ - style = WOLFRAM_STYLE_STRING2; - while (i < n) { - c1 = str[i++]; - if (c1 == '\"') { - colstate = 0; - break; - } - if (c1 == '\\') { - if (i < n) - i++; - } - } - break; - default: - /* parse numbers */ - base = 10; - if (c == '.' && qe_isdigit(str[i])) - goto in_number; - if (qe_isdigit(c)) { - int value = 0; - while (qe_isdigit(str[i])) { - value = value * 10 + str[i++] - '0'; - } - if (str[i] == '^' && str[i + 1] == '^' && value < 36) { - i += 2; - base = value; - while (qe_digit_value(str[i]) < base) - i++; - } - if (str[i] == '.') { - i++; - in_number: - while (qe_digit_value(str[i]) < base) - i++; - } - if (str[i] == '`') { - i++; - if (str[i] == '`') - i++; - while (qe_isdigit(str[i])) - i++; - } - if (str[i] == '*' && str[i + 1] == '^' && qe_isdigit(str[i + 2])) { - i += 3; - while (qe_isdigit(str[i])) - i++; - } - style = WOLFRAM_STYLE_NUMBER; - break; - } - /* parse identifiers and keywords */ - if (c == '$' || c == '#' || qe_isalpha_(c)) { - i += wolfram_get_identifier(kbuf, countof(kbuf), c, str, i, n); - if (strfind(syn->keywords, kbuf)) { - style = WOLFRAM_STYLE_KEYWORD; - } else - if (strfind(syn->types, kbuf)) { - style = WOLFRAM_STYLE_TYPE; - } else { - k = i; - if (qe_isblank(str[k])) - k++; - if (str[k] == '[') - style = WOLFRAM_STYLE_FUNCTION; - else - if (qe_islower(c) || c == '#' || c == '_') - style = WOLFRAM_STYLE_VARIABLE; - else - style = WOLFRAM_STYLE_IDENTIFIER; - } - break; - } - continue; - } - if (style) { - SET_COLOR(str, start, i, style); - style = 0; - } - } - cp->colorize_state = colstate; -} - -static ModeDef wolfram_mode = { - .name = "Wolfram", - .extensions = "nb", - .keywords = wolfram_keywords, - .types = wolfram_types, - .colorize_func = wolfram_colorize_line, -}; - -static int wolfram_init(void) -{ - qe_register_mode(&wolfram_mode, MODEF_SYNTAX); - - return 0; -} - -/*---------------- Tiger script coloring ----------------*/ - -static char const tiger_keywords[] = { - "|array|break|do|else|end|for|function|if|in|let|nil|of|then|to|type|var|while" - "|" -}; - -static char const tiger_types[] = { - "|int|string" - "|" -}; - -#if 0 -static char const tiger_tokens[] = { - "(|)|[|]|{|}|:|:=|.|,|;|*|/|+|-|=|<>|>|<|>=|<=|&||." -}; -#endif - -enum { - IN_TIGER_COMMENT = 0x01, - IN_TIGER_COMMENT_MASK = 0x0F, - IN_TIGER_STRING = 0x10, - IN_TIGER_STRING2 = 0x20, -}; - -enum { - TIGER_STYLE_TEXT = QE_STYLE_DEFAULT, - TIGER_STYLE_COMMENT = QE_STYLE_COMMENT, - TIGER_STYLE_STRING = QE_STYLE_STRING, - TIGER_STYLE_NUMBER = QE_STYLE_NUMBER, - TIGER_STYLE_KEYWORD = QE_STYLE_KEYWORD, - TIGER_STYLE_TYPE = QE_STYLE_TYPE, - TIGER_STYLE_FUNCTION = QE_STYLE_FUNCTION, -}; - -static void tiger_colorize_line(QEColorizeContext *cp, - unsigned int *str, int n, ModeDef *syn) -{ - int i = 0, j, start = i, c, sep = 0; - int state = cp->colorize_state; - char kbuf[64]; - - if (state & IN_TIGER_COMMENT_MASK) { - goto parse_comment; - } - if (state & IN_TIGER_STRING2) { - goto parse_string2; - } - if (state & IN_TIGER_STRING) { - goto parse_string; - } - - while (i < n) { - start = i; - c = str[i++]; - switch (c) { - case '/': - if (str[i] == '*') { - /* normal comment */ - i++; - state |= IN_TIGER_COMMENT; - parse_comment: - for (; i < n; i++) { - if (str[i] == '*' && str[i + 1] == '/') { - i += 2; - state -= IN_TIGER_COMMENT; - if (!(state & IN_TIGER_COMMENT_MASK)) - break; - } else - if (str[i] == '/' && str[i + 1] == '*') { - i += 2; - if ((state & IN_TIGER_COMMENT_MASK) != IN_TIGER_COMMENT_MASK) - state += IN_TIGER_COMMENT; - } - } - SET_COLOR(str, start, i, TIGER_STYLE_COMMENT); - continue; - } - break; - case '\"': - /* parse string const */ - sep = c; - state |= IN_TIGER_STRING; - parse_string: - while (i < n) { - c = str[i++]; - if (c == '\\') { - if (i == n) { - state |= IN_TIGER_STRING2; - break; - } - if (str[i] == ' ') { - state |= IN_TIGER_STRING2; - parse_string2: - while (i < n && str[i] == ' ') - i++; - if (i == n) - break; - if (str[i] == '\\') - i++; - state &= ~IN_TIGER_STRING2; - } else { - i += 1; - } - } else - if (c == sep) { - state &= ~IN_TIGER_STRING; - break; - } - } - SET_COLOR(str, start, i, TIGER_STYLE_STRING); - continue; - default: - if (qe_isdigit(c)) { - for (; i < n; i++) { - if (!qe_isdigit(str[i])) - break; - } - SET_COLOR(str, start, i, TIGER_STYLE_NUMBER); - continue; - } - if (qe_isalpha(c)) { - i += ustr_get_identifier(kbuf, countof(kbuf), c, str, i, n); - if (strfind(syn->keywords, kbuf)) { - SET_COLOR(str, start, i, TIGER_STYLE_KEYWORD); - continue; - } - if (strfind(syn->types, kbuf)) { - SET_COLOR(str, start, i, TIGER_STYLE_TYPE); - continue; - } - for (j = i; j < n && qe_isspace(str[j]); j++) - continue; - /* function calls use parenthesized argument list or - single string or table literal */ - if (str[j] == '(') { - SET_COLOR(str, start, i, TIGER_STYLE_FUNCTION); - continue; - } - continue; - } - break; - } - } - cp->colorize_state = state; -} - -static ModeDef tiger_mode = { - .name = "Tiger", - .extensions = "tiger|tig", - .shell_handlers = "tiger", - .keywords = tiger_keywords, - .types = tiger_types, - .colorize_func = tiger_colorize_line, -}; - -static int tiger_init(void) -{ - qe_register_mode(&tiger_mode, MODEF_SYNTAX); - - return 0; -} - /*----------------*/ static int extra_modes_init(void) @@ -5625,11 +4672,6 @@ static int extra_modes_init(void) emf_init(); agena_init(); smalltalk_init(); - scad_init(); - magpie_init(); - falcon_init(); - wolfram_init(); - tiger_init(); return 0; } diff --git a/lang/falcon.c b/lang/falcon.c new file mode 100644 index 0000000..a7b48bc --- /dev/null +++ b/lang/falcon.c @@ -0,0 +1,228 @@ +/* + * Falcon language mode for QEmacs. + * + * Copyright (c) 2000-2020 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" + +/*---- Giancarlo Niccolai's Falcon scripting language -----*/ + +static const char falcon_keywords[] = { + "if|elif|else|end|switch|select|case|default|" + "while|loop|for|break|continue|dropping|" + "forfirst|formiddle|forlast|" + "try|catch|raise|state|callable|launch|" + "function|return|innerfunc|fself|" + "object|self|provides|from|init|" + "class|const|enum|global|static|" + "export|load|import|as|" + "directive|def|macro|" + "to|and|or|not|in|notin|true|false|nil|" +}; + +enum { + IN_FALCON_COMMENT = 0x01, /* nested comment level */ + IN_FALCON_STRING1 = 0x10, /* single quote */ + IN_FALCON_STRING2 = 0x20, /* double quote */ +}; + +enum { + FALCON_STYLE_TEXT = QE_STYLE_DEFAULT, + FALCON_STYLE_SHBANG = QE_STYLE_PREPROCESS, + FALCON_STYLE_COMMENT = QE_STYLE_COMMENT, + FALCON_STYLE_STRING1 = QE_STYLE_STRING, + FALCON_STYLE_STRING2 = QE_STYLE_STRING, + FALCON_STYLE_NUMBER = QE_STYLE_NUMBER, + FALCON_STYLE_KEYWORD = QE_STYLE_KEYWORD, + FALCON_STYLE_TYPE = QE_STYLE_TYPE, + FALCON_STYLE_FUNCTION = QE_STYLE_FUNCTION, +}; + +static void falcon_colorize_line(QEColorizeContext *cp, + unsigned int *str, int n, ModeDef *syn) +{ + int i = 0, start = i, c, style = 0; + int state = cp->colorize_state; + char kbuf[64]; + + if (state & IN_FALCON_COMMENT) + goto parse_comment; + + if (state & IN_FALCON_STRING1) + goto parse_string1; + + if (state & IN_FALCON_STRING2) + goto parse_string2; + + while (i < n) { + start = i; + c = str[i++]; + switch (c) { + case '#': + if (i == 1 && str[i] == '!') { + i = n; + style = FALCON_STYLE_SHBANG; + break; + } + continue; + + case '/': + if (str[i] == '*') { + /* C block comments */ + i++; + state |= IN_FALCON_COMMENT; + parse_comment: + for (; i < n; i++) { + if (str[i] == '*' && str[i + 1] == '/') { + i += 2; + state &= ~IN_FALCON_COMMENT; + break; + } + } + style = FALCON_STYLE_COMMENT; + break; + } + if (str[i] == '/') { + i = n; + style = FALCON_STYLE_COMMENT; + break; + } + continue; + + case '\'': + /* a single quoted string literal */ + state = IN_FALCON_STRING1; + parse_string1: + while (i < n) { + c = str[i++]; + if (c == '\\') { + if (i < n) { + i += 1; + } + } else + if (c == '\'') { + state &= ~IN_FALCON_STRING1; + break; + } + } + style = FALCON_STYLE_STRING1; + break; + + case '\"': + /* parse double quoted string literal */ + state = IN_FALCON_STRING2; + parse_string2: + c = '\0'; + while (i < n) { + c = str[i++]; + if (c == '\\') { + if (i < n) { + i += 1; + } + } else + if (c == '\"') { + state &= ~IN_FALCON_STRING2; + break; + } + } + style = FALCON_STYLE_STRING1; + break; + + case '.': + if (qe_isdigit(str[i])) + goto parse_decimal; + continue; + + default: + if (qe_isdigit(c)) { + if (c == '0' && qe_tolower(str[i]) == 'x') { + /* hexadecimal numbers */ + for (i += 1; qe_isxdigit(str[i]); i++) + continue; + } else + if (c == '0') { + /* octal numbers */ + for (i += 1; qe_isoctdigit(str[i]); i++) + continue; + } else { + /* decimal numbers */ + for (; qe_isdigit(str[i]); i++) + continue; + if (str[i] == '.') { + i++; + parse_decimal: + for (; 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 = FALCON_STYLE_NUMBER; + break; + } + if (qe_isalpha_(c) || c > 0xA0) { + i += ustr_get_word(kbuf, countof(kbuf), c, str, i, n); + if (strfind(syn->keywords, kbuf)) { + style = FALCON_STYLE_KEYWORD; + break; + } + if (check_fcall(str, i)) { + style = FALCON_STYLE_FUNCTION; + break; + } + if (qe_isupper(kbuf[0]) && (start == 0 || str[start-1] != '.')) { + /* Types are capitalized */ + style = FALCON_STYLE_TYPE; + break; + } + continue; + } + continue; + } + if (style) { + SET_COLOR(str, start, i, style); + style = 0; + } + } + cp->colorize_state = state; +} + +static ModeDef falcon_mode = { + .name = "Falcon", + .extensions = "fal", + .shell_handlers = "falcon", + .colorize_func = falcon_colorize_line, + .keywords = falcon_keywords, +}; + +static int falcon_init(void) +{ + qe_register_mode(&falcon_mode, MODEF_SYNTAX); + + return 0; +} + +qe_module_init(falcon_init); diff --git a/lang/magpie.c b/lang/magpie.c new file mode 100644 index 0000000..20fd721 --- /dev/null +++ b/lang/magpie.c @@ -0,0 +1,210 @@ +/* + * Magpie language mode for QEmacs. + * + * Copyright (c) 2000-2020 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" + +/*---------------- Magpie coloring ----------------*/ + +static char const magpie_keywords[] = { + "and|as|break|case|catch|defclass|def|do|else|end|fn|for|if|" + "import|in|is|let|match|or|return|then|throw|var|val|while|with|" + "not|native|namespace|class|struct|using|new|interface|" + "get|set|shared|done|" + "false|true|nothing|it|xor|" +}; + +enum { + IN_MAGPIE_COMMENT = 0x0F, /* nested comment level */ + IN_MAGPIE_STRING = 0x10, /* double quote */ +}; + +enum { + MAGPIE_STYLE_TEXT = QE_STYLE_DEFAULT, + MAGPIE_STYLE_SHBANG = QE_STYLE_PREPROCESS, + MAGPIE_STYLE_COMMENT = QE_STYLE_COMMENT, + MAGPIE_STYLE_STRING = QE_STYLE_STRING, + MAGPIE_STYLE_CHAR = QE_STYLE_STRING, + MAGPIE_STYLE_NUMBER = QE_STYLE_NUMBER, + MAGPIE_STYLE_KEYWORD = QE_STYLE_KEYWORD, + MAGPIE_STYLE_TYPE = QE_STYLE_TYPE, + MAGPIE_STYLE_FUNCTION = QE_STYLE_FUNCTION, +}; + +static void magpie_colorize_line(QEColorizeContext *cp, + unsigned int *str, int n, ModeDef *syn) +{ + int i = 0, start = i, c, style = 0, level; + int state = cp->colorize_state; + char kbuf[64]; + + if ((level = state & IN_MAGPIE_COMMENT) != 0) + goto parse_comment; + + if (state & IN_MAGPIE_STRING) + goto parse_string; + + while (i < n) { + start = i; + c = str[i++]; + switch (c) { + case '#': + if (i == 1 && str[i] == '!') { + i = n; + style = MAGPIE_STYLE_SHBANG; + break; + } + i = n; + style = MAGPIE_STYLE_COMMENT; /* probably incorrect */ + break; + + case '/': + if (str[i] == '*') { + /* nexted C block comments */ + i++; + level = 1; + parse_comment: + for (; i < n; i++) { + if (str[i] == '*' && str[i + 1] == '/') { + i += 2; + if (--level == 0) + break; + } else + if (str[i] == '/' && str[i + 1] == '*') { + i += 2; + level++; + } + } + state &= ~IN_MAGPIE_COMMENT; + state |= level & IN_MAGPIE_COMMENT; + style = MAGPIE_STYLE_COMMENT; + break; + } + if (str[i] == '/') { + i = n; + style = MAGPIE_STYLE_COMMENT; + break; + } + continue; + + case '\'': + /* a single quoted character */ + while (i < n) { + c = str[i++]; + if (c == '\\') { + if (i < n) { + i += 1; + } + } else + if (c == '\'') { + break; + } + } + style = MAGPIE_STYLE_CHAR; + break; + + case '\"': + /* parse double quoted string const */ + state = IN_MAGPIE_STRING; + parse_string: + c = '\0'; + while (i < n) { + c = str[i++]; + if (c == '\\') { + if (i < n) { + i += 1; + } + } else + if (c == '#' && str[i] == '{') { + /* should parse full syntax */ + while (i < n && str[i++] != '}') + continue; + } else + if (c == '\"') { + state = 0; + break; + } + } + style = MAGPIE_STYLE_STRING; + break; + + case '.': + if (qe_isdigit_(str[i])) + goto parse_decimal; + continue; + + default: + if (qe_isdigit(c)) { + /* decimal numbers */ + for (; qe_isdigit_(str[i]); i++) + continue; + if (str[i] == '.') { + i++; + parse_decimal: + for (; qe_isdigit_(str[i]); i++) + continue; + } + style = MAGPIE_STYLE_NUMBER; + break; + } + if (qe_isalpha_(c)) { + i += ustr_get_identifier(kbuf, countof(kbuf), c, str, i, n); + if (strfind(syn->keywords, kbuf)) { + style = MAGPIE_STYLE_KEYWORD; + break; + } + if (qe_isblank(str[i])) + i++; + if (str[i] == '(' || str[i] == '{') { + style = MAGPIE_STYLE_FUNCTION; + break; + } + if (qe_isupper(kbuf[0]) && (start == 0 || str[start-1] != '.')) { + /* Types are capitalized */ + style = MAGPIE_STYLE_TYPE; + break; + } + continue; + } + continue; + } + if (style) { + SET_COLOR(str, start, i, style); + style = 0; + } + } + cp->colorize_state = state; +} + +static ModeDef magpie_mode = { + .name = "Magpie", + .extensions = "mag", + .shell_handlers = "magpie", + .keywords = magpie_keywords, + .colorize_func = magpie_colorize_line, +}; + +static int magpie_init(void) +{ + qe_register_mode(&magpie_mode, MODEF_SYNTAX); + + return 0; +} + +qe_module_init(magpie_init); diff --git a/lang/scad.c b/lang/scad.c new file mode 100644 index 0000000..d89cad1 --- /dev/null +++ b/lang/scad.c @@ -0,0 +1,201 @@ +/* + * OpenSCAD language mode for QEmacs. + * + * Copyright (c) 2000-2020 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" + +/*---------------- OpenSCAD language ----------------*/ + +static const char scad_keywords[] = { + "true|false|undef|" + "module|function|for|if|else|len|" +// "assert|break|case|const|continue|default|defined|do|exit|" +// "forward|goto|native|new|operator|public|return|sizeof|sleep|" +// "state|static|stock|switch|tagof|while|", +}; + +static const char scad_preprocessor_keywords[] = { + "use|include|" +}; + +static const char scad_types[] = { + //"void|" + //"bool|string|int|uint|uchar|nt8|short|ushort|long|ulong|size_t|ssize_t|" + //"double|va_list|unichar|" +}; + +enum { + IN_SCAD_COMMENT = 0x01, +}; + +enum { + SCAD_STYLE_TEXT = QE_STYLE_DEFAULT, + SCAD_STYLE_KEYWORD = QE_STYLE_KEYWORD, + SCAD_STYLE_TYPE = QE_STYLE_TYPE, + SCAD_STYLE_PREPROCESS = QE_STYLE_PREPROCESS, + SCAD_STYLE_COMMENT = QE_STYLE_COMMENT, + SCAD_STYLE_STRING = QE_STYLE_STRING, + SCAD_STYLE_NUMBER = QE_STYLE_NUMBER, + SCAD_STYLE_FUNCTION = QE_STYLE_FUNCTION, + SCAD_STYLE_ARGNAME = QE_STYLE_FUNCTION, +}; + +static void scad_colorize_line(QEColorizeContext *cp, + unsigned int *str, int n, ModeDef *syn) +{ + char keyword[16]; + int i = 0, start = i, c, style = 0, k, len, laststyle = 0, isnum; + int colstate = cp->colorize_state; + int level = colstate >> 1; + + if (colstate & IN_SCAD_COMMENT) + goto in_comment; + + while (i < n) { + start = i; + c = str[i++]; + switch (c) { + case '/': + if (str[i] == '/') { /* single line comment */ + i = n; + style = SCAD_STYLE_COMMENT; + break; + } + if (str[i] == '*') { /* multi-line comment */ + colstate |= IN_SCAD_COMMENT; + i++; + in_comment: + for (; i < n; i++) { + if (str[i] == '*' && str[i + 1] == '/') { + i += 2; + colstate &= ~IN_SCAD_COMMENT; + break; + } + } + style = SCAD_STYLE_COMMENT; + break; + } + continue; + case '<': + if (laststyle == SCAD_STYLE_PREPROCESS) { + /* filename for include and use directives */ + while (i < n) { + if (str[i++] == '>') + break; + } + style = SCAD_STYLE_STRING; + break; + } + continue; + case '(': + case '[': + case '{': + level <<= 1; + continue; + case '}': + case ']': + case ')': + level >>= 1; + continue; + case '\'': + case '\"': + /* parse string or char const */ + while (i < n) { + /* XXX: escape sequences? */ + if (str[i] == '\\' && i + 1 < n) { + i++; + continue; + } + if (str[i++] == (unsigned int)c) + break; + } + style = SCAD_STYLE_STRING; + break; + default: + /* parse identifiers, keywords and numbers */ + if (qe_isalnum_(c) || c == '$') { + isnum = qe_isdigit(c); + len = 0; + keyword[len++] = c; + for (; qe_isalnum_(str[i]) || str[i] == '.'; i++) { + if (str[i] == '.') { + if (!isnum) + break; + } else { + if (!qe_isdigit(str[i])) + isnum = 0; + } + if (len < countof(keyword) - 1) + keyword[len++] = str[i]; + } + keyword[len] = '\0'; + if (isnum) { + style = SCAD_STYLE_NUMBER; + } + if (strfind(syn->keywords, keyword)) { + style = SCAD_STYLE_KEYWORD; + } else + if (strfind(scad_preprocessor_keywords, keyword)) { + style = SCAD_STYLE_PREPROCESS; + } else + if (strfind(syn->types, keyword)) { + style = SCAD_STYLE_TYPE; + } else { + k = i; + if (qe_isblank(str[k])) + k++; + if ((level & 2) && str[k] == '=') { + style = SCAD_STYLE_ARGNAME; + } else + if (str[k] == '(') { + style = SCAD_STYLE_FUNCTION; + level |= 1; + } + } + break; + } + continue; + } + if (style) { + laststyle = style; + SET_COLOR(str, start, i, style); + style = 0; + } + } + cp->colorize_state = (colstate & IN_SCAD_COMMENT) | (level << 1); +} + +static ModeDef scad_mode = { + .name = "OpenSCAD", + .extensions = "scad", + .colorize_func = scad_colorize_line, + .keywords = scad_keywords, + .types = scad_types, + //.indent_func = c_indent_line, + //.auto_indent = 1, +}; + +static int scad_init(void) +{ + qe_register_mode(&scad_mode, MODEF_SYNTAX); + + return 0; +} + +qe_module_init(scad_init); diff --git a/lang/tiger.c b/lang/tiger.c new file mode 100644 index 0000000..5623a3b --- /dev/null +++ b/lang/tiger.c @@ -0,0 +1,186 @@ +/* + * Tiger language mode for QEmacs. + * + * Copyright (c) 2000-2020 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" + +/*---------------- Tiger script coloring ----------------*/ + +static char const tiger_keywords[] = { + "|array|break|do|else|end|for|function|if|in|let|nil|of|then|to|type|var|while" + "|" +}; + +static char const tiger_types[] = { + "|int|string" + "|" +}; + +#if 0 +static char const tiger_tokens[] = { + "(|)|[|]|{|}|:|:=|.|,|;|*|/|+|-|=|<>|>|<|>=|<=|&||." +}; +#endif + +enum { + IN_TIGER_COMMENT = 0x01, + IN_TIGER_COMMENT_MASK = 0x0F, + IN_TIGER_STRING = 0x10, + IN_TIGER_STRING2 = 0x20, +}; + +enum { + TIGER_STYLE_TEXT = QE_STYLE_DEFAULT, + TIGER_STYLE_COMMENT = QE_STYLE_COMMENT, + TIGER_STYLE_STRING = QE_STYLE_STRING, + TIGER_STYLE_NUMBER = QE_STYLE_NUMBER, + TIGER_STYLE_KEYWORD = QE_STYLE_KEYWORD, + TIGER_STYLE_TYPE = QE_STYLE_TYPE, + TIGER_STYLE_FUNCTION = QE_STYLE_FUNCTION, +}; + +static void tiger_colorize_line(QEColorizeContext *cp, + unsigned int *str, int n, ModeDef *syn) +{ + int i = 0, j, start = i, c, sep = 0; + int state = cp->colorize_state; + char kbuf[64]; + + if (state & IN_TIGER_COMMENT_MASK) { + goto parse_comment; + } + if (state & IN_TIGER_STRING2) { + goto parse_string2; + } + if (state & IN_TIGER_STRING) { + goto parse_string; + } + + while (i < n) { + start = i; + c = str[i++]; + switch (c) { + case '/': + if (str[i] == '*') { + /* normal comment */ + i++; + state |= IN_TIGER_COMMENT; + parse_comment: + for (; i < n; i++) { + if (str[i] == '*' && str[i + 1] == '/') { + i += 2; + state -= IN_TIGER_COMMENT; + if (!(state & IN_TIGER_COMMENT_MASK)) + break; + } else + if (str[i] == '/' && str[i + 1] == '*') { + i += 2; + if ((state & IN_TIGER_COMMENT_MASK) != IN_TIGER_COMMENT_MASK) + state += IN_TIGER_COMMENT; + } + } + SET_COLOR(str, start, i, TIGER_STYLE_COMMENT); + continue; + } + break; + case '\"': + /* parse string const */ + sep = c; + state |= IN_TIGER_STRING; + parse_string: + while (i < n) { + c = str[i++]; + if (c == '\\') { + if (i == n) { + state |= IN_TIGER_STRING2; + break; + } + if (str[i] == ' ') { + state |= IN_TIGER_STRING2; + parse_string2: + while (i < n && str[i] == ' ') + i++; + if (i == n) + break; + if (str[i] == '\\') + i++; + state &= ~IN_TIGER_STRING2; + } else { + i += 1; + } + } else + if (c == sep) { + state &= ~IN_TIGER_STRING; + break; + } + } + SET_COLOR(str, start, i, TIGER_STYLE_STRING); + continue; + default: + if (qe_isdigit(c)) { + for (; i < n; i++) { + if (!qe_isdigit(str[i])) + break; + } + SET_COLOR(str, start, i, TIGER_STYLE_NUMBER); + continue; + } + if (qe_isalpha(c)) { + i += ustr_get_identifier(kbuf, countof(kbuf), c, str, i, n); + if (strfind(syn->keywords, kbuf)) { + SET_COLOR(str, start, i, TIGER_STYLE_KEYWORD); + continue; + } + if (strfind(syn->types, kbuf)) { + SET_COLOR(str, start, i, TIGER_STYLE_TYPE); + continue; + } + for (j = i; j < n && qe_isspace(str[j]); j++) + continue; + /* function calls use parenthesized argument list or + single string or table literal */ + if (str[j] == '(') { + SET_COLOR(str, start, i, TIGER_STYLE_FUNCTION); + continue; + } + continue; + } + break; + } + } + cp->colorize_state = state; +} + +static ModeDef tiger_mode = { + .name = "Tiger", + .extensions = "tiger|tig", + .shell_handlers = "tiger", + .keywords = tiger_keywords, + .types = tiger_types, + .colorize_func = tiger_colorize_line, +}; + +static int tiger_init(void) +{ + qe_register_mode(&tiger_mode, MODEF_SYNTAX); + + return 0; +} + +qe_module_init(tiger_init); diff --git a/lang/wolfram.c b/lang/wolfram.c new file mode 100644 index 0000000..da5fbd5 --- /dev/null +++ b/lang/wolfram.c @@ -0,0 +1,243 @@ +/* + * Wolfram language mode for QEmacs. + * + * Copyright (c) 2000-2020 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" + +/*---------------- Mathematica / Wolfram language ----------------*/ + +static char const wolfram_keywords[] = { + "|False|True|None" + "|" +}; + +static char const wolfram_types[] = { + "|" +}; + +enum { + IN_WOLFRAM_DEFAULT = 0, + IN_WOLFRAM_STRING1 = 1, + IN_WOLFRAM_STRING2 = 2, + IN_WOLFRAM_COMMENT_SHIFT = 2, +}; + +enum { + WOLFRAM_STYLE_TEXT = QE_STYLE_DEFAULT, + WOLFRAM_STYLE_KEYWORD = QE_STYLE_KEYWORD, + WOLFRAM_STYLE_TYPE = QE_STYLE_TYPE, + WOLFRAM_STYLE_COMMENT = QE_STYLE_COMMENT, + WOLFRAM_STYLE_STRING1 = QE_STYLE_STRING, + WOLFRAM_STYLE_STRING2 = QE_STYLE_STRING, + WOLFRAM_STYLE_IDENTIFIER = QE_STYLE_DEFAULT, + WOLFRAM_STYLE_VARIABLE = QE_STYLE_VARIABLE, + WOLFRAM_STYLE_NUMBER = QE_STYLE_NUMBER, + WOLFRAM_STYLE_FUNCTION = QE_STYLE_FUNCTION, +}; + +static int wolfram_get_identifier(char *buf, int buf_size, int c, + const unsigned int *str, int i, int n) +{ + int len = 0, j; + + if (len < buf_size) { + buf[len++] = c; + } + for (j = i; j < n; j++) { + c = str[j]; + if (!qe_isalnum_(c) && c != '$' && c != '`') + break; + if (len < buf_size - 1) + buf[len++] = c; + } + if (len < buf_size) { + buf[len] = '\0'; + } + return j - i; +} + +static void wolfram_colorize_line(QEColorizeContext *cp, + unsigned int *str, int n, ModeDef *syn) +{ + char kbuf[16]; + int i = 0, start = i, c, c1 = 0, k, style = 0, comment_level, base; + int colstate = cp->colorize_state; + + switch (colstate) { + case IN_WOLFRAM_DEFAULT: + break; + case IN_WOLFRAM_STRING1: + goto in_string1; + case IN_WOLFRAM_STRING2: + goto in_string2; + default: + comment_level = colstate >> IN_WOLFRAM_COMMENT_SHIFT; + goto in_comment; + } + + while (i < n) { + start = i; + c = str[i++]; + switch (c) { + case '(': + if (str[i] == '*') { /* parse comment */ + comment_level = 1; + i++; + in_comment: + while (i < n) { + c = str[i++]; + if (c == '(' && str[i] == '*') { + /* nested comment */ + i++; + comment_level++; + } else + if (c == '*' && str[i] == ')') { + i++; + if (--comment_level == 0) + break; + } + } + colstate = comment_level << IN_WOLFRAM_COMMENT_SHIFT; + style = WOLFRAM_STYLE_COMMENT; + break; + } + continue; + case '\'': + /* parse single quoted string */ + colstate = IN_WOLFRAM_STRING1; + in_string1: + /* XXX: should flag syntax error on + unescaped newline except at EOF */ + style = WOLFRAM_STYLE_STRING1; + while (i < n) { + c1 = str[i++]; + if (c1 == '\'') { + colstate = 0; + break; + } + if (c1 == '\\') { + if (i < n) + i++; + } + } + break; + case '\"': + /* parse double quoted string */ + colstate = IN_WOLFRAM_STRING2; + in_string2: + /* XXX: should flag syntax error on + unescaped newline except at EOF */ + style = WOLFRAM_STYLE_STRING2; + while (i < n) { + c1 = str[i++]; + if (c1 == '\"') { + colstate = 0; + break; + } + if (c1 == '\\') { + if (i < n) + i++; + } + } + break; + default: + /* parse numbers */ + base = 10; + if (c == '.' && qe_isdigit(str[i])) + goto in_number; + if (qe_isdigit(c)) { + int value = 0; + while (qe_isdigit(str[i])) { + value = value * 10 + str[i++] - '0'; + } + if (str[i] == '^' && str[i + 1] == '^' && value < 36) { + i += 2; + base = value; + while (qe_digit_value(str[i]) < base) + i++; + } + if (str[i] == '.') { + i++; + in_number: + while (qe_digit_value(str[i]) < base) + i++; + } + if (str[i] == '`') { + i++; + if (str[i] == '`') + i++; + while (qe_isdigit(str[i])) + i++; + } + if (str[i] == '*' && str[i + 1] == '^' && qe_isdigit(str[i + 2])) { + i += 3; + while (qe_isdigit(str[i])) + i++; + } + style = WOLFRAM_STYLE_NUMBER; + break; + } + /* parse identifiers and keywords */ + if (c == '$' || c == '#' || qe_isalpha_(c)) { + i += wolfram_get_identifier(kbuf, countof(kbuf), c, str, i, n); + if (strfind(syn->keywords, kbuf)) { + style = WOLFRAM_STYLE_KEYWORD; + } else + if (strfind(syn->types, kbuf)) { + style = WOLFRAM_STYLE_TYPE; + } else { + k = i; + if (qe_isblank(str[k])) + k++; + if (str[k] == '[') + style = WOLFRAM_STYLE_FUNCTION; + else + if (qe_islower(c) || c == '#' || c == '_') + style = WOLFRAM_STYLE_VARIABLE; + else + style = WOLFRAM_STYLE_IDENTIFIER; + } + break; + } + continue; + } + if (style) { + SET_COLOR(str, start, i, style); + style = 0; + } + } + cp->colorize_state = colstate; +} + +static ModeDef wolfram_mode = { + .name = "Wolfram", + .extensions = "nb", + .keywords = wolfram_keywords, + .types = wolfram_types, + .colorize_func = wolfram_colorize_line, +}; + +static int wolfram_init(void) +{ + qe_register_mode(&wolfram_mode, MODEF_SYNTAX); + + return 0; +} + +qe_module_init(wolfram_init);