2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
CHighlight.c
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
(c) 2000-2012 Benoît Minisini <gambas@users.sourceforge.net>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
any later version.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
This program 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 General Public License for more details.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
MA 02110-1301, USA.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __CHIGHLIGHT_CPP
|
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "gb.eval.h"
|
|
|
|
#include "eval_analyze.h"
|
|
|
|
|
|
|
|
#include "CHighlight.h"
|
|
|
|
|
|
|
|
static void *_analyze_symbol = 0;
|
|
|
|
static void *_analyze_type = 0;
|
|
|
|
static void *_analyze_pos = 0;
|
2009-04-05 01:52:47 +02:00
|
|
|
static char *_analyze_text = 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
static char *_purged_line = NULL;
|
|
|
|
|
2008-03-11 14:29:47 +01:00
|
|
|
static int get_char_length(unsigned char c)
|
|
|
|
{
|
2011-03-23 14:04:44 +01:00
|
|
|
int n = 1;
|
|
|
|
|
|
|
|
if (c & 0x80)
|
|
|
|
{
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
c <<= 1;
|
|
|
|
if (!(c & 0x80))
|
|
|
|
break;
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
2008-03-11 14:29:47 +01:00
|
|
|
}
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
static char *purge(const char *s, int len_s, bool comment, bool string)
|
|
|
|
{
|
2011-03-23 14:04:44 +01:00
|
|
|
char c;
|
|
|
|
uint i;
|
|
|
|
int lc, ls;
|
|
|
|
bool in_comment = FALSE;
|
|
|
|
char wait = 0;
|
|
|
|
char *r = NULL;
|
|
|
|
|
|
|
|
for (i = 0; i < len_s; i += ls)
|
|
|
|
{
|
|
|
|
c = s[i];
|
|
|
|
ls = lc = get_char_length((unsigned char)c);
|
|
|
|
|
|
|
|
switch(wait)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
|
|
|
|
if (in_comment)
|
|
|
|
{
|
|
|
|
if (!comment)
|
|
|
|
c = ' ', lc = 1;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
2011-03-23 14:04:44 +01:00
|
|
|
else if (c == '"')
|
|
|
|
wait = '"';
|
|
|
|
else if (c == '\'')
|
|
|
|
in_comment = TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-03-23 14:04:44 +01:00
|
|
|
break;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-03-23 14:04:44 +01:00
|
|
|
case '"':
|
|
|
|
if (c == '"')
|
|
|
|
wait = 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
else if (c == '\\')
|
|
|
|
{
|
|
|
|
if (string)
|
|
|
|
{
|
|
|
|
if (i < len_s)
|
2011-09-07 21:43:11 +02:00
|
|
|
r = GB.AddChar(r, c);
|
2007-12-30 17:41:49 +01:00
|
|
|
//r += c;
|
|
|
|
i++;
|
|
|
|
c = s[i];
|
2008-03-11 14:29:47 +01:00
|
|
|
lc = get_char_length((unsigned char)c);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
i++;
|
|
|
|
if (i < len_s)
|
2011-09-07 21:43:11 +02:00
|
|
|
r = GB.AddChar(r, ' ');
|
2007-12-30 17:41:49 +01:00
|
|
|
//r += ' ';
|
2008-03-11 14:29:47 +01:00
|
|
|
c = ' ', lc = 1;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
}
|
2011-03-23 14:04:44 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!string)
|
|
|
|
c = ' ', lc = 1;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
2011-03-23 14:04:44 +01:00
|
|
|
break;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-03-11 14:29:47 +01:00
|
|
|
if (lc == 1)
|
2011-09-07 21:43:11 +02:00
|
|
|
r = GB.AddChar(r, c);
|
2008-03-11 14:29:47 +01:00
|
|
|
else
|
2011-09-07 21:43:11 +02:00
|
|
|
r = GB.AddString(r, &s[i], lc);
|
2011-03-23 14:04:44 +01:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
GB.FreeString(&_purged_line);
|
|
|
|
_purged_line = r;
|
|
|
|
|
2011-03-23 14:04:44 +01:00
|
|
|
return r;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int convState(int state)
|
|
|
|
{
|
2011-03-23 14:04:44 +01:00
|
|
|
switch(state)
|
|
|
|
{
|
|
|
|
case EVAL_TYPE_END: return HIGHLIGHT_NORMAL;
|
|
|
|
case EVAL_TYPE_RESERVED: return HIGHLIGHT_KEYWORD;
|
|
|
|
case EVAL_TYPE_IDENTIFIER: return HIGHLIGHT_SYMBOL;
|
|
|
|
case EVAL_TYPE_CLASS: return HIGHLIGHT_DATATYPE;
|
|
|
|
case EVAL_TYPE_NUMBER: return HIGHLIGHT_NUMBER;
|
|
|
|
case EVAL_TYPE_STRING: return HIGHLIGHT_STRING;
|
|
|
|
case EVAL_TYPE_SUBR: return HIGHLIGHT_SUBR;
|
|
|
|
case EVAL_TYPE_COMMENT: return HIGHLIGHT_COMMENT;
|
|
|
|
case EVAL_TYPE_OPERATOR: return HIGHLIGHT_OPERATOR;
|
|
|
|
case EVAL_TYPE_DATATYPE: return HIGHLIGHT_DATATYPE;
|
|
|
|
case EVAL_TYPE_ERROR: return HIGHLIGHT_ERROR;
|
2009-12-14 04:45:22 +01:00
|
|
|
case EVAL_TYPE_HELP: return HIGHLIGHT_HELP;
|
2010-12-18 05:27:59 +01:00
|
|
|
case EVAL_TYPE_PREPROCESSOR: return HIGHLIGHT_PREPROCESSOR;
|
2011-03-23 14:04:44 +01:00
|
|
|
default: return HIGHLIGHT_NORMAL;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2009-07-14 04:24:54 +02:00
|
|
|
static void analyze(const char *src, int len_src, bool rewrite, int state)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2011-03-23 14:04:44 +01:00
|
|
|
GB_ARRAY garray, tarray, parray;
|
|
|
|
int i, n, pos, len, p, upos, ulen, l;
|
|
|
|
char *str;
|
|
|
|
EVAL_ANALYZE result;
|
|
|
|
|
|
|
|
EVAL_analyze(src, len_src, state == HIGHLIGHT_COMMENT ? EVAL_TYPE_COMMENT : EVAL_TYPE_END, &result, rewrite);
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
for (i = 0; i < result.len; i++)
|
|
|
|
{
|
|
|
|
if (result.color[i].state != EVAL_TYPE_END)
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
GB.Array.New(&garray, GB_T_STRING, n);
|
|
|
|
GB.Array.New(&tarray, GB_T_INTEGER, n);
|
|
|
|
GB.Array.New(&parray, GB_T_INTEGER, n);
|
|
|
|
|
|
|
|
pos = 0;
|
|
|
|
upos = 0;
|
|
|
|
i = 0;
|
|
|
|
for (p = 0; p < result.len; p++)
|
|
|
|
{
|
|
|
|
len = result.color[p].len;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-04-05 01:52:47 +02:00
|
|
|
ulen = 0;
|
|
|
|
for (l = 0; l < len; l++)
|
|
|
|
ulen += get_char_length(result.str[upos + ulen]);
|
|
|
|
|
2011-03-23 14:04:44 +01:00
|
|
|
if (result.color[p].state != EVAL_TYPE_END)
|
|
|
|
{
|
|
|
|
str = GB.NewString(&result.str[upos], ulen);
|
|
|
|
*((char **)GB.Array.Get(garray, i)) = str;
|
|
|
|
*((int *)GB.Array.Get(tarray, i)) = convState(result.color[p].state);
|
|
|
|
*((int *)GB.Array.Get(parray, i)) = pos;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos += len;
|
|
|
|
upos += ulen;
|
|
|
|
}
|
|
|
|
|
|
|
|
GB.Unref(&_analyze_symbol);
|
|
|
|
_analyze_symbol = garray;
|
|
|
|
GB.Ref(garray);
|
|
|
|
|
|
|
|
GB.Unref(&_analyze_type);
|
|
|
|
_analyze_type = tarray;
|
|
|
|
GB.Ref(tarray);
|
|
|
|
|
|
|
|
GB.Unref(&_analyze_pos);
|
|
|
|
_analyze_pos = parray;
|
|
|
|
GB.Ref(parray);
|
|
|
|
|
2012-06-17 19:56:45 +02:00
|
|
|
GB.FreeString(&_analyze_text);
|
|
|
|
_analyze_text = result.str;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
BEGIN_METHOD_VOID(CHIGHLIGHT_exit)
|
|
|
|
|
2011-03-23 14:04:44 +01:00
|
|
|
GB.Unref(&_analyze_symbol);
|
|
|
|
GB.Unref(&_analyze_type);
|
|
|
|
GB.Unref(&_analyze_pos);
|
|
|
|
GB.FreeString(&_analyze_text);
|
|
|
|
GB.FreeString(&_purged_line);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_METHOD(CHIGHLIGHT_purge, GB_STRING text; GB_BOOLEAN comment; GB_BOOLEAN string)
|
|
|
|
|
2011-03-23 14:04:44 +01:00
|
|
|
bool comment = VARGOPT(comment, FALSE);
|
|
|
|
bool string = VARGOPT(string, FALSE);
|
|
|
|
|
|
|
|
if (comment && string)
|
|
|
|
GB.ReturnNewString(STRING(text), LENGTH(text));
|
2007-12-30 17:41:49 +01:00
|
|
|
else
|
2011-03-23 14:04:44 +01:00
|
|
|
GB.ReturnString(purge(STRING(text), LENGTH(text), comment, string));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2009-07-14 04:24:54 +02:00
|
|
|
BEGIN_METHOD(CHIGHLIGHT_analyze, GB_STRING text; GB_BOOLEAN rewrite; GB_INTEGER state)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-03-23 14:04:44 +01:00
|
|
|
analyze(STRING(text), LENGTH(text), VARGOPT(rewrite, FALSE), VARGOPT(state, HIGHLIGHT_NORMAL));
|
|
|
|
GB.ReturnObject(_analyze_symbol);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CHIGHLIGHT_analyze_symbols)
|
|
|
|
|
2011-03-23 14:04:44 +01:00
|
|
|
GB.ReturnObject(_analyze_symbol);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CHIGHLIGHT_analyze_types)
|
|
|
|
|
2011-03-23 14:04:44 +01:00
|
|
|
GB.ReturnObject(_analyze_type);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CHIGHLIGHT_analyze_positions)
|
|
|
|
|
2011-03-23 14:04:44 +01:00
|
|
|
GB.ReturnObject(_analyze_pos);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
2009-04-05 01:52:47 +02:00
|
|
|
BEGIN_PROPERTY(CHIGHLIGHT_analyze_text)
|
|
|
|
|
2011-03-23 14:04:44 +01:00
|
|
|
GB.ReturnString(_analyze_text);
|
2009-04-05 01:52:47 +02:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
2012-09-20 04:29:37 +02:00
|
|
|
BEGIN_PROPERTY(Highlight_Alternate)
|
|
|
|
|
|
|
|
GB.Deprecated("gb.eval", "Highlight.Alternate", NULL);
|
|
|
|
GB.ReturnInteger(-1);
|
|
|
|
|
|
|
|
END_PROPERTY
|
2008-03-27 17:27:24 +01:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
GB_DESC CHighlightDesc[] =
|
|
|
|
{
|
2011-03-23 14:04:44 +01:00
|
|
|
GB_DECLARE("Highlight", 0), GB_VIRTUAL_CLASS(),
|
|
|
|
|
|
|
|
GB_CONSTANT("Background", "i", HIGHLIGHT_BACKGROUND),
|
|
|
|
GB_CONSTANT("Normal", "i", HIGHLIGHT_NORMAL),
|
|
|
|
GB_CONSTANT("Keyword", "i", HIGHLIGHT_KEYWORD),
|
|
|
|
GB_CONSTANT("Function", "i", HIGHLIGHT_SUBR),
|
|
|
|
GB_CONSTANT("Operator", "i", HIGHLIGHT_OPERATOR),
|
|
|
|
GB_CONSTANT("Symbol", "i", HIGHLIGHT_SYMBOL),
|
|
|
|
GB_CONSTANT("Number", "i", HIGHLIGHT_NUMBER),
|
|
|
|
GB_CONSTANT("String", "i", HIGHLIGHT_STRING),
|
|
|
|
GB_CONSTANT("Comment", "i", HIGHLIGHT_COMMENT),
|
|
|
|
GB_CONSTANT("Breakpoint", "i", HIGHLIGHT_BREAKPOINT),
|
|
|
|
GB_CONSTANT("Current", "i", HIGHLIGHT_CURRENT),
|
|
|
|
GB_CONSTANT("DataType", "i", HIGHLIGHT_DATATYPE),
|
|
|
|
GB_CONSTANT("Selection", "i", HIGHLIGHT_SELECTION),
|
|
|
|
GB_CONSTANT("Highlight", "i", HIGHLIGHT_HIGHLIGHT),
|
|
|
|
GB_CONSTANT("CurrentLine", "i", HIGHLIGHT_LINE),
|
|
|
|
GB_CONSTANT("Error", "i", HIGHLIGHT_ERROR),
|
|
|
|
GB_CONSTANT("Help", "i", HIGHLIGHT_HELP),
|
|
|
|
GB_CONSTANT("Preprocessor", "i", HIGHLIGHT_PREPROCESSOR),
|
2012-09-20 04:29:37 +02:00
|
|
|
GB_STATIC_PROPERTY_READ("Alternate", "i", Highlight_Alternate),
|
2011-03-23 14:04:44 +01:00
|
|
|
|
|
|
|
GB_STATIC_METHOD("_exit", NULL, CHIGHLIGHT_exit, NULL),
|
|
|
|
GB_STATIC_METHOD("Analyze", "String[]", CHIGHLIGHT_analyze, "(Code)s[(Rewrite)b(State)i]"),
|
|
|
|
GB_STATIC_PROPERTY_READ("Symbols", "String[]", CHIGHLIGHT_analyze_symbols),
|
|
|
|
GB_STATIC_PROPERTY_READ("Types", "Integer[]", CHIGHLIGHT_analyze_types),
|
|
|
|
GB_STATIC_PROPERTY_READ("Positions", "Integer[]", CHIGHLIGHT_analyze_positions),
|
|
|
|
GB_STATIC_PROPERTY_READ("TextAfter", "s", CHIGHLIGHT_analyze_text),
|
|
|
|
GB_STATIC_METHOD("Purge", "s", CHIGHLIGHT_purge, "(Code)s[(Comment)b(String)b]"),
|
|
|
|
|
|
|
|
GB_END_DECLARE
|
2007-12-30 17:41:49 +01:00
|
|
|
};
|