2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
eval_analyze.c
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
(c) 2000-2012 Benoît Minisini <gambas@users.sourceforge.net>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-07-29 17:50:27 +02: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
|
|
|
|
2012-07-29 17:50:27 +02: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
|
|
|
|
2012-07-29 17:50:27 +02: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 __EVAL_ANALYZE_C
|
|
|
|
|
|
|
|
#include "gambas.h"
|
|
|
|
#include "gb_common.h"
|
|
|
|
#include "eval_analyze.h"
|
|
|
|
|
2008-03-27 17:27:24 +01:00
|
|
|
#include "CSystem.h"
|
2007-12-30 17:41:49 +01:00
|
|
|
/*#define DEBUG*/
|
|
|
|
|
2011-09-01 20:50:07 +02:00
|
|
|
static char _analyze_buffer[256];
|
|
|
|
static int _analyze_buffer_pos;
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
static EVAL_COLOR colors[EVAL_MAX_COLOR];
|
|
|
|
static int colors_len;
|
|
|
|
|
|
|
|
static int get_type(PATTERN *pattern)
|
|
|
|
{
|
2012-07-29 17:50:27 +02:00
|
|
|
int type = PATTERN_type(*pattern);
|
|
|
|
int index = PATTERN_index(*pattern);
|
|
|
|
|
|
|
|
if (type == EVAL_TYPE_RESERVED)
|
|
|
|
{
|
|
|
|
if (index >= RS_COLON)
|
|
|
|
{
|
|
|
|
if (!((index == RS_AND || index == RS_OR) && PATTERN_is(pattern[1], RS_IF)))
|
|
|
|
type = EVAL_TYPE_OPERATOR;
|
|
|
|
}
|
|
|
|
else if (RES_is_type(index))
|
|
|
|
type = EVAL_TYPE_DATATYPE;
|
2009-09-27 11:28:52 +02:00
|
|
|
else if (index == RS_WITH && pattern > EVAL->pattern)
|
|
|
|
{
|
|
|
|
index = PATTERN_index(pattern[-1]);
|
|
|
|
if (index == RS_BEGINS || index == RS_ENDS)
|
|
|
|
type = EVAL_TYPE_OPERATOR;
|
|
|
|
}
|
2012-07-29 17:50:27 +02:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
return type;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2012-09-04 09:29:39 +02:00
|
|
|
static int is_me_last_kind(PATTERN pattern)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-07-29 17:50:27 +02:00
|
|
|
return PATTERN_is(pattern, RS_ME)
|
|
|
|
|| PATTERN_is(pattern, RS_SUPER)
|
2012-09-05 01:11:11 +02:00
|
|
|
|| PATTERN_is(pattern, RS_LAST)
|
|
|
|
|| PATTERN_is(pattern, RS_TRUE)
|
|
|
|
|| PATTERN_is(pattern, RS_FALSE)
|
|
|
|
|| PATTERN_is(pattern, RS_PINF)
|
|
|
|
|| PATTERN_is(pattern, RS_MINF)
|
|
|
|
|| PATTERN_is(pattern, RS_NULL);
|
2012-09-04 09:29:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int is_optional_kind(PATTERN pattern)
|
|
|
|
{
|
|
|
|
return PATTERN_is(pattern, RS_OPTIONAL)
|
2012-07-29 17:50:27 +02:00
|
|
|
|| PATTERN_is(pattern, RS_BYREF);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void get_symbol(PATTERN pattern, const char **symbol, int *len)
|
|
|
|
{
|
2012-07-29 17:50:27 +02:00
|
|
|
static char keyword[16];
|
|
|
|
int i;
|
|
|
|
SYMBOL *sym;
|
|
|
|
int type = PATTERN_type(pattern);
|
|
|
|
int index = PATTERN_index(pattern);
|
|
|
|
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case RT_RESERVED:
|
2010-05-25 13:19:00 +02:00
|
|
|
*symbol = COMP_res_info[index].name;
|
|
|
|
*len = strlen(*symbol);
|
2010-05-27 15:51:09 +02:00
|
|
|
if (!EVAL->rewrite)
|
|
|
|
{
|
|
|
|
memcpy(keyword, *symbol, *len);
|
|
|
|
for (i = 0; i < *len; i++)
|
|
|
|
keyword[i] = toupper(keyword[i]);
|
|
|
|
*symbol = keyword;
|
|
|
|
}
|
2012-07-29 17:50:27 +02:00
|
|
|
return;
|
2010-06-05 01:48:53 +02:00
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
case RT_NUMBER:
|
|
|
|
case RT_IDENTIFIER:
|
|
|
|
sym = TABLE_get_symbol(EVAL->table, index);
|
|
|
|
break;
|
2010-06-05 01:48:53 +02:00
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
case RT_CLASS:
|
|
|
|
sym = TABLE_get_symbol(EVAL->table, index);
|
|
|
|
break;
|
2010-06-05 01:48:53 +02:00
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
case RT_STRING:
|
|
|
|
case RT_TSTRING:
|
|
|
|
case RT_COMMENT:
|
|
|
|
case EVAL_TYPE_ERROR:
|
|
|
|
sym = TABLE_get_symbol(EVAL->string, index);
|
|
|
|
break;
|
2010-06-05 01:48:53 +02:00
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
case RT_SUBR:
|
|
|
|
*symbol = COMP_subr_info[index].name;
|
|
|
|
*len = strlen(*symbol);
|
|
|
|
return;
|
2010-06-05 01:48:53 +02:00
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
default:
|
|
|
|
*symbol = NULL;
|
|
|
|
*len = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
*symbol = sym->name;
|
|
|
|
*len = sym->len;
|
|
|
|
if (*len > EVAL_COLOR_MAX_LEN)
|
|
|
|
*len = EVAL_COLOR_MAX_LEN;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void add_data(int state, int len)
|
|
|
|
{
|
2012-07-29 17:50:27 +02:00
|
|
|
EVAL_COLOR *color;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
if (colors_len >= EVAL_MAX_COLOR || len == 0)
|
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
//printf("[%d] %d %d\n", colors_len, state, len);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
color = &colors[colors_len];
|
|
|
|
color->state = state;
|
|
|
|
color->len = len;
|
2009-04-05 01:52:47 +02:00
|
|
|
color->alternate = FALSE;
|
2012-07-29 17:50:27 +02:00
|
|
|
colors_len++;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int is_proc(void)
|
|
|
|
{
|
2012-07-29 17:50:27 +02:00
|
|
|
PATTERN pattern;
|
|
|
|
int i;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
if (!EVAL->pattern)
|
|
|
|
return FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
for (i = 0;; i++)
|
|
|
|
{
|
|
|
|
pattern = EVAL->pattern[i];
|
|
|
|
if (PATTERN_is_end(pattern))
|
|
|
|
return FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
if (PATTERN_is(pattern, RS_PRIVATE) || PATTERN_is(pattern, RS_PUBLIC) || PATTERN_is(pattern, RS_STATIC) || PATTERN_is(pattern, RS_FAST))
|
|
|
|
continue;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
return (PATTERN_is(pattern, RS_SUB) || PATTERN_is(pattern, RS_PROCEDURE) || PATTERN_is(pattern, RS_FUNCTION));
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int get_indent(bool *empty)
|
|
|
|
{
|
2012-07-29 17:50:27 +02:00
|
|
|
int i;
|
|
|
|
unsigned char c;
|
|
|
|
|
|
|
|
*empty = TRUE;
|
|
|
|
|
|
|
|
for (i = 0; i < (int)EVAL->len; i++)
|
|
|
|
{
|
|
|
|
c = EVAL->source[i];
|
|
|
|
if (c > ' ')
|
|
|
|
{
|
|
|
|
*empty = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2009-12-15 01:06:54 +01:00
|
|
|
static int get_symbol_indent(const char *symbol, int len)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
if (!(symbol[i] > 0 && symbol[i] < 33))
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
static bool symbol_starts_with(const char *symbol, int len, int from, const char *comp)
|
|
|
|
{
|
|
|
|
int l = strlen(comp);
|
|
|
|
return (from < (len - l) && strncmp(&symbol[from], comp, l) == 0);
|
|
|
|
}
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
static int get_utf8_length(const char *s, int l)
|
|
|
|
{
|
2012-07-29 17:50:27 +02:00
|
|
|
int len;
|
|
|
|
int i;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
for (i = 0, len = 0; i < l; i++)
|
|
|
|
{
|
|
|
|
if ((s[i] & 0xC0) != 0x80)
|
|
|
|
len++;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
return len;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2011-09-01 20:50:07 +02:00
|
|
|
static void init_result()
|
|
|
|
{
|
|
|
|
_analyze_buffer_pos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void flush_result(EVAL_ANALYZE *result)
|
|
|
|
{
|
|
|
|
if (_analyze_buffer_pos > 0)
|
|
|
|
{
|
2011-09-07 21:43:11 +02:00
|
|
|
result->str = GB.AddString(result->str, _analyze_buffer, _analyze_buffer_pos);
|
2011-09-01 20:50:07 +02:00
|
|
|
_analyze_buffer_pos = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void add_result(EVAL_ANALYZE *result, const char *str, int len)
|
|
|
|
{
|
|
|
|
if ((_analyze_buffer_pos + len) > sizeof(_analyze_buffer))
|
|
|
|
flush_result(result);
|
|
|
|
|
|
|
|
if (len > sizeof(_analyze_buffer))
|
2011-09-07 21:43:11 +02:00
|
|
|
result->str = GB.AddString(result->str, str, len);
|
2011-09-01 20:50:07 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy(&_analyze_buffer[_analyze_buffer_pos], str, len);
|
|
|
|
_analyze_buffer_pos += len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void add_result_char(EVAL_ANALYZE *result, char c)
|
|
|
|
{
|
|
|
|
if ((_analyze_buffer_pos + 1) > sizeof(_analyze_buffer))
|
|
|
|
flush_result(result);
|
|
|
|
|
|
|
|
_analyze_buffer[_analyze_buffer_pos++] = c;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
static void analyze(EVAL_ANALYZE *result)
|
|
|
|
{
|
2012-07-29 17:50:27 +02:00
|
|
|
PATTERN *pattern;
|
|
|
|
int nspace;
|
|
|
|
bool empty = FALSE;
|
|
|
|
int type, old_type, next_type;
|
|
|
|
const char *symbol;
|
2013-01-06 17:13:31 +01:00
|
|
|
bool space_before, space_after;
|
2012-07-29 17:50:27 +02:00
|
|
|
int len, i;
|
2010-12-18 05:27:59 +01:00
|
|
|
bool preprocessor;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
pattern = EVAL->pattern;
|
|
|
|
colors_len = 0;
|
2010-12-05 21:17:24 +01:00
|
|
|
nspace = 0;
|
2010-12-18 05:27:59 +01:00
|
|
|
preprocessor = FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
if (EVAL->len <= 0)
|
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-07-14 04:24:54 +02:00
|
|
|
if (!EVAL->comment)
|
|
|
|
{
|
|
|
|
nspace = get_indent(&empty);
|
|
|
|
add_data(EVAL_TYPE_END, nspace);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
if (empty)
|
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
if (!pattern)
|
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-01 20:50:07 +02:00
|
|
|
init_result();
|
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
if (nspace)
|
|
|
|
add_result(result, EVAL->source, nspace);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
type = EVAL->comment ? EVAL_TYPE_COMMENT : EVAL_TYPE_END;
|
|
|
|
next_type = EVAL_TYPE_END;
|
|
|
|
space_after = FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
old_type = next_type;
|
|
|
|
type = get_type(pattern);
|
|
|
|
next_type = type;
|
|
|
|
get_symbol(*pattern, &symbol, &len);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
space_before = space_after;
|
|
|
|
space_after = FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
if (type == EVAL_TYPE_END)
|
|
|
|
break;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-01-06 17:13:31 +01:00
|
|
|
//if (in_quote && (type == EVAL_TYPE_RESERVED || type == EVAL_TYPE_DATATYPE || type == EVAL_TYPE_SUBR))
|
|
|
|
// type = EVAL_TYPE_IDENTIFIER;
|
2008-04-17 12:18:25 +02:00
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case EVAL_TYPE_RESERVED:
|
|
|
|
//state = Keyword;
|
|
|
|
//if (old_type != EVAL_TYPE_OPERATOR)
|
|
|
|
//me = is_me_last(*pattern);
|
2012-09-04 09:29:39 +02:00
|
|
|
|
|
|
|
if (is_me_last_kind(*pattern))
|
2012-07-29 17:50:27 +02:00
|
|
|
{
|
2012-09-04 09:29:39 +02:00
|
|
|
if (old_type != EVAL_TYPE_OPERATOR)
|
|
|
|
space_before = TRUE;
|
2012-07-29 17:50:27 +02:00
|
|
|
next_type = EVAL_TYPE_IDENTIFIER;
|
2012-09-04 09:29:39 +02:00
|
|
|
}
|
|
|
|
else if (is_optional_kind(*pattern))
|
|
|
|
{
|
2012-07-29 17:50:27 +02:00
|
|
|
if (old_type != EVAL_TYPE_OPERATOR)
|
|
|
|
space_before = TRUE;
|
|
|
|
}
|
2012-09-04 09:29:39 +02:00
|
|
|
else
|
|
|
|
space_before = TRUE;
|
|
|
|
|
|
|
|
/*if (!is_me_last_kind(*pattern))
|
|
|
|
space_before = TRUE;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (*pattern != RS_OPTIONAL)
|
|
|
|
if (old_type != EVAL_TYPE_OPERATOR)
|
|
|
|
space_before = TRUE;
|
|
|
|
}*/
|
2012-07-29 17:50:27 +02:00
|
|
|
|
|
|
|
if (preprocessor && PATTERN_is(pattern[-1], RS_SHARP))
|
2010-12-18 05:27:59 +01:00
|
|
|
space_before = FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
/*if (PATTERN_index(*pattern) >= RS_COLON)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
usym[i] = GB.toupper(symbol[i]);
|
|
|
|
usym[len] = 0;
|
|
|
|
|
|
|
|
symbol = usym;
|
|
|
|
}*/
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EVAL_TYPE_DATATYPE:
|
|
|
|
//state = Datatype;
|
|
|
|
if (old_type != EVAL_TYPE_OPERATOR)
|
|
|
|
space_before = TRUE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EVAL_TYPE_IDENTIFIER:
|
|
|
|
case EVAL_TYPE_CLASS:
|
|
|
|
//state = Symbol;
|
|
|
|
if (old_type != EVAL_TYPE_OPERATOR)
|
|
|
|
space_before = TRUE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EVAL_TYPE_NUMBER:
|
|
|
|
//state = Number;
|
|
|
|
if (old_type != EVAL_TYPE_OPERATOR)
|
|
|
|
space_before = TRUE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EVAL_TYPE_STRING:
|
|
|
|
//state = String;
|
|
|
|
if (old_type != EVAL_TYPE_OPERATOR)
|
|
|
|
space_before = TRUE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EVAL_TYPE_SUBR:
|
|
|
|
//state = Subr;
|
|
|
|
if (old_type != EVAL_TYPE_OPERATOR)
|
|
|
|
space_before = TRUE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EVAL_TYPE_COMMENT:
|
|
|
|
//state = Commentary;
|
|
|
|
space_before = *symbol != ' ';
|
2009-12-15 01:06:54 +01:00
|
|
|
i = get_symbol_indent(symbol, len);
|
|
|
|
if (i <= (len - 2) && symbol[i + 1] == '\'')
|
2009-12-14 04:45:22 +01:00
|
|
|
type = EVAL_TYPE_HELP;
|
2012-07-29 17:50:27 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
while (i < len && (uchar)symbol[i] == '\'')
|
|
|
|
i++;
|
|
|
|
|
|
|
|
while (i < len && (uchar)symbol[i] <= ' ')
|
|
|
|
i++;
|
|
|
|
|
|
|
|
if (i < len)
|
|
|
|
{
|
|
|
|
if (symbol_starts_with(symbol, len, i, "NOTE:")
|
|
|
|
|| symbol_starts_with(symbol, len, i, "TODO:")
|
|
|
|
|| symbol_starts_with(symbol, len, i, "FIXME:"))
|
|
|
|
type = EVAL_TYPE_HELP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EVAL_TYPE_OPERATOR:
|
|
|
|
|
|
|
|
if (index("([)]@", *symbol))
|
|
|
|
{
|
|
|
|
space_after = FALSE;
|
|
|
|
}
|
|
|
|
else if (index(":;,", *symbol))
|
|
|
|
{
|
|
|
|
space_before = FALSE;
|
|
|
|
space_after = TRUE;
|
|
|
|
}
|
|
|
|
else if (index("#{", *symbol))
|
|
|
|
{
|
|
|
|
if (old_type != EVAL_TYPE_OPERATOR)
|
|
|
|
space_before = TRUE;
|
|
|
|
space_after = FALSE;
|
|
|
|
//in_quote = *symbol == '{';
|
2010-12-18 05:27:59 +01:00
|
|
|
|
|
|
|
if (!preprocessor && *symbol == '#' && old_type == EVAL_TYPE_END)
|
|
|
|
preprocessor = TRUE;
|
2012-07-29 17:50:27 +02:00
|
|
|
}
|
|
|
|
else if (index("}", *symbol))
|
|
|
|
{
|
|
|
|
space_before = FALSE;
|
|
|
|
space_after = FALSE;
|
|
|
|
//in_quote = FALSE;
|
|
|
|
}
|
|
|
|
else if (index(".!", *symbol)) //symbol[0] == '.' && symbol[1] == 0)
|
|
|
|
{
|
|
|
|
//space_before = FALSE;
|
|
|
|
space_after = FALSE;
|
|
|
|
}
|
|
|
|
else if (PATTERN_is(*pattern, RS_NOT) || *symbol == '-')
|
|
|
|
{
|
2007-12-30 17:41:49 +01:00
|
|
|
if (old_type == EVAL_TYPE_OPERATOR && (PATTERN_is(pattern[-1], RS_LBRA) || PATTERN_is(pattern[-1],RS_LSQR)))
|
2012-07-29 17:50:27 +02:00
|
|
|
space_before = FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
else
|
2012-07-29 17:50:27 +02:00
|
|
|
space_before = TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
space_after = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
space_before = TRUE;
|
|
|
|
space_after = TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
if (old_type == EVAL_TYPE_RESERVED)
|
|
|
|
space_before = TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
break;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
case EVAL_TYPE_ERROR:
|
|
|
|
space_before = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
if (space_before && old_type != EVAL_TYPE_END)
|
|
|
|
{
|
2011-09-01 20:50:07 +02:00
|
|
|
add_result_char(result, ' ');
|
2012-07-29 17:50:27 +02:00
|
|
|
add_data(preprocessor ? EVAL_TYPE_PREPROCESSOR : EVAL_TYPE_END, 1);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
if (type == EVAL_TYPE_STRING)
|
2011-09-01 20:50:07 +02:00
|
|
|
add_result_char(result, '"');
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
if (len)
|
|
|
|
{
|
2010-06-05 01:48:53 +02:00
|
|
|
if (EVAL->rewrite && type == EVAL_TYPE_CLASS)
|
|
|
|
{
|
2011-09-01 20:50:07 +02:00
|
|
|
add_result_char(result, toupper(symbol[0]));
|
|
|
|
if (len > 1) add_result(result, &symbol[1], len - 1);
|
2010-06-05 01:48:53 +02:00
|
|
|
}
|
|
|
|
else
|
2011-09-01 20:50:07 +02:00
|
|
|
add_result(result, symbol, len);
|
2012-07-29 17:50:27 +02:00
|
|
|
//printf("add: %.*s\n", len, symbol);
|
|
|
|
len = get_utf8_length(symbol, len);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
if (type == EVAL_TYPE_STRING)
|
|
|
|
{
|
|
|
|
add_result_char(result, '"');
|
|
|
|
len += 2;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-18 15:41:43 +01:00
|
|
|
if (preprocessor && type != EVAL_TYPE_COMMENT && type != EVAL_TYPE_HELP)
|
2010-12-18 05:27:59 +01:00
|
|
|
add_data(EVAL_TYPE_PREPROCESSOR, len);
|
|
|
|
else
|
|
|
|
add_data(type, len);
|
2012-07-29 17:50:27 +02:00
|
|
|
//printf("add_data: %.d (%d)\n", type, len);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
pattern++;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-01 20:50:07 +02:00
|
|
|
flush_result(result);
|
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
result->color = colors;
|
|
|
|
result->len = colors_len;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
//fprintf(stderr, "analyze: %d %s\n", strlen(result->str), result->str);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define add_pattern(_type, _index) EVAL->pattern[EVAL->pattern_count++] = PATTERN_make((_type), (_index));
|
|
|
|
|
|
|
|
static void add_end_pattern(void)
|
|
|
|
{
|
2012-07-29 17:50:27 +02:00
|
|
|
int index;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
len = EVAL->len - (READ_source_ptr - EVAL->source);
|
|
|
|
if (len > 0)
|
|
|
|
{
|
|
|
|
TABLE_add_symbol(EVAL->string, READ_source_ptr, len, &index);
|
|
|
|
add_pattern(EVAL_TYPE_ERROR, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
add_pattern(EVAL_TYPE_END, 0);
|
|
|
|
//get_symbol(PATTERN_make(EVAL_TYPE_ERROR, index), &sym, &len);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-14 04:24:54 +02:00
|
|
|
PUBLIC void EVAL_analyze(const char *src, int len, int state, EVAL_ANALYZE *result, bool rewrite)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
int nspace = 0;
|
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
#ifdef DEBUG
|
|
|
|
printf("EVAL: %*.s\n", expr->len, expr->source);
|
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
CLEAR(result);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
while (len > 0 && src[len - 1] == ' ')
|
|
|
|
{
|
|
|
|
len--;
|
|
|
|
nspace++;
|
|
|
|
}
|
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
result->len = 0;
|
|
|
|
result->str = NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
if (len > 0)
|
|
|
|
{
|
2007-12-30 17:41:49 +01:00
|
|
|
EVAL = &EVAL_read_expr;
|
|
|
|
|
2010-08-29 22:51:10 +02:00
|
|
|
EVAL_clear(EVAL, FALSE);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
EVAL->source = GB.NewString(src, len);
|
2012-04-01 03:28:24 +02:00
|
|
|
EVAL->source = GB.AddString(EVAL->source, "\0\0", 2);
|
2007-12-30 17:41:49 +01:00
|
|
|
EVAL->len = len;
|
|
|
|
|
|
|
|
EVAL->analyze = TRUE;
|
2008-03-27 17:27:24 +01:00
|
|
|
EVAL->rewrite = rewrite;
|
2009-07-14 04:24:54 +02:00
|
|
|
EVAL->comment = state == EVAL_TYPE_COMMENT;
|
|
|
|
|
|
|
|
//fprintf(stderr, "EVAL_analyze: [%d] %.*s\n", EVAL->comment, len, src);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
EVAL_start(EVAL);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
TRY
|
|
|
|
{
|
|
|
|
EVAL_read();
|
|
|
|
}
|
|
|
|
CATCH
|
|
|
|
{
|
|
|
|
add_end_pattern();
|
|
|
|
}
|
|
|
|
END_TRY
|
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
analyze(result);
|
2009-07-14 04:24:54 +02:00
|
|
|
result->proc = is_proc();
|
|
|
|
result->state = EVAL->comment ? EVAL_TYPE_COMMENT : EVAL_TYPE_END;
|
|
|
|
|
|
|
|
//fprintf(stderr, "--> [%d]\n", EVAL->comment);
|
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
GB.FreeString(&EVAL->source);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result->proc = FALSE;
|
|
|
|
}
|
|
|
|
|
2012-07-29 17:50:27 +02:00
|
|
|
while (nspace > 0)
|
|
|
|
{
|
|
|
|
result->str = GB.AddString(result->str, " ", nspace > 8 ? 8 : nspace);
|
|
|
|
nspace -= 8;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|