2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
|
|
|
eval_analyze.c
|
|
|
|
|
2009-08-17 12:41:51 +02:00
|
|
|
(c) 2000-2009 Benoît Minisini <gambas@users.sourceforge.net>
|
2007-12-30 17:41:49 +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
|
2009-08-17 12:41:51 +02:00
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
2007-12-30 17:41:49 +01:00
|
|
|
any later version.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#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*/
|
|
|
|
|
|
|
|
static EVAL_COLOR colors[EVAL_MAX_COLOR];
|
|
|
|
static int colors_len;
|
|
|
|
|
|
|
|
static int get_type(PATTERN *pattern)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int is_me_last(PATTERN pattern)
|
|
|
|
{
|
|
|
|
return PATTERN_is(pattern, RS_ME)
|
|
|
|
|| PATTERN_is(pattern, RS_SUPER)
|
|
|
|
|| PATTERN_is(pattern, RS_LAST)
|
|
|
|
|| PATTERN_is(pattern, RS_OPTIONAL)
|
|
|
|
|| PATTERN_is(pattern, RS_TRUE)
|
|
|
|
|| PATTERN_is(pattern, RS_FALSE)
|
2008-02-14 12:59:36 +01:00
|
|
|
|| PATTERN_is(pattern, RS_NULL)
|
|
|
|
|| PATTERN_is(pattern, RS_BYREF);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void get_symbol(PATTERN pattern, const char **symbol, int *len)
|
|
|
|
{
|
2008-03-27 17:27:24 +01:00
|
|
|
static char keyword[16];
|
|
|
|
int i;
|
2007-12-30 17:41:49 +01:00
|
|
|
SYMBOL *sym;
|
|
|
|
int type = PATTERN_type(pattern);
|
|
|
|
int index = PATTERN_index(pattern);
|
|
|
|
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case RT_RESERVED:
|
|
|
|
sym = TABLE_get_symbol(COMP_res_table, index);
|
|
|
|
break;
|
|
|
|
case RT_NUMBER:
|
|
|
|
case RT_IDENTIFIER:
|
|
|
|
case RT_CLASS:
|
|
|
|
sym = TABLE_get_symbol(EVAL->table, index);
|
|
|
|
break;
|
|
|
|
case RT_STRING:
|
|
|
|
case RT_TSTRING:
|
|
|
|
case RT_COMMENT:
|
|
|
|
case EVAL_TYPE_ERROR:
|
|
|
|
sym = TABLE_get_symbol(EVAL->string, index);
|
|
|
|
break;
|
|
|
|
case RT_SUBR:
|
|
|
|
*symbol = COMP_subr_info[index].name;
|
|
|
|
*len = strlen(*symbol);
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
*symbol = NULL;
|
|
|
|
*len = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
*symbol = sym->name;
|
|
|
|
*len = sym->len;
|
2009-04-05 01:52:47 +02:00
|
|
|
if (*len > EVAL_COLOR_MAX_LEN)
|
|
|
|
*len = EVAL_COLOR_MAX_LEN;
|
2008-03-27 17:27:24 +01:00
|
|
|
|
|
|
|
if (type == RT_RESERVED && !EVAL->rewrite)
|
|
|
|
{
|
|
|
|
memcpy(keyword, sym->name, sym->len);
|
|
|
|
for (i = 0; i < sym->len; i++)
|
|
|
|
keyword[i] = toupper(keyword[i]);
|
|
|
|
*symbol = keyword;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void add_data(int state, int len)
|
|
|
|
{
|
|
|
|
EVAL_COLOR *color;
|
|
|
|
|
2008-04-17 12:18:25 +02:00
|
|
|
if (colors_len >= EVAL_MAX_COLOR || len == 0)
|
2007-12-30 17:41:49 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
//printf("[%d] %d %d\n", colors_len, state, len);
|
|
|
|
|
|
|
|
color = &colors[colors_len];
|
|
|
|
color->state = state;
|
|
|
|
color->len = len;
|
2009-04-05 01:52:47 +02:00
|
|
|
color->alternate = FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
colors_len++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int is_proc(void)
|
|
|
|
{
|
|
|
|
PATTERN pattern;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!EVAL->pattern)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
for (i = 0;; i++)
|
|
|
|
{
|
|
|
|
pattern = EVAL->pattern[i];
|
|
|
|
if (PATTERN_is_end(pattern))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (PATTERN_is(pattern, RS_PRIVATE) || PATTERN_is(pattern, RS_PUBLIC) || PATTERN_is(pattern, RS_STATIC))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
return (PATTERN_is(pattern, RS_SUB) || PATTERN_is(pattern, RS_PROCEDURE) || PATTERN_is(pattern, RS_FUNCTION));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_indent(bool *empty)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int get_utf8_length(const char *s, int l)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0, len = 0; i < l; i++)
|
|
|
|
{
|
|
|
|
if ((s[i] & 0xC0) != 0x80)
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void analyze(EVAL_ANALYZE *result)
|
|
|
|
{
|
|
|
|
PATTERN *pattern;
|
|
|
|
const char *src;
|
|
|
|
int nspace;
|
|
|
|
bool empty;
|
|
|
|
int type, old_type, next_type;
|
|
|
|
const char *symbol;
|
2008-04-17 12:18:25 +02:00
|
|
|
bool space_before, space_after, in_quote;
|
2007-12-30 17:41:49 +01:00
|
|
|
//bool me = FALSE;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
pattern = EVAL->pattern;
|
|
|
|
src = EVAL->source;
|
|
|
|
colors_len = 0;
|
|
|
|
|
|
|
|
if (EVAL->len <= 0)
|
|
|
|
return;
|
|
|
|
|
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
|
|
|
|
|
|
|
if (empty)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!EVAL->pattern)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (nspace)
|
|
|
|
GB.AddString(&result->str, EVAL->source, nspace);
|
|
|
|
|
2009-07-14 04:24:54 +02:00
|
|
|
type = EVAL->comment ? EVAL_TYPE_COMMENT : EVAL_TYPE_END;
|
2007-12-30 17:41:49 +01:00
|
|
|
next_type = EVAL_TYPE_END;
|
|
|
|
space_after = FALSE;
|
2008-04-17 12:18:25 +02:00
|
|
|
in_quote = FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
old_type = next_type;
|
|
|
|
type = get_type(pattern);
|
|
|
|
next_type = type;
|
|
|
|
get_symbol(*pattern, &symbol, &len);
|
|
|
|
|
|
|
|
space_before = space_after;
|
|
|
|
space_after = FALSE;
|
|
|
|
|
|
|
|
if (type == EVAL_TYPE_END)
|
|
|
|
break;
|
|
|
|
|
2008-04-17 12:18:25 +02:00
|
|
|
if (in_quote && (type == EVAL_TYPE_RESERVED || type == EVAL_TYPE_DATATYPE || type == EVAL_TYPE_SUBR))
|
|
|
|
type = EVAL_TYPE_IDENTIFIER;
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case EVAL_TYPE_RESERVED:
|
|
|
|
//state = Keyword;
|
|
|
|
//if (old_type != EVAL_TYPE_OPERATOR)
|
|
|
|
//me = is_me_last(*pattern);
|
|
|
|
if (!is_me_last(*pattern))
|
|
|
|
space_before = TRUE;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
next_type = EVAL_TYPE_IDENTIFIER;
|
|
|
|
if (old_type != EVAL_TYPE_OPERATOR)
|
|
|
|
space_before = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*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-14 04:45:22 +01:00
|
|
|
if (len >= 2 && symbol[1] == '\'')
|
|
|
|
type = EVAL_TYPE_HELP;
|
2007-12-30 17:41:49 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EVAL_TYPE_OPERATOR:
|
|
|
|
|
2008-01-19 18:52:05 +01:00
|
|
|
if (index("([)]@", *symbol))
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
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;
|
2008-04-17 12:18:25 +02:00
|
|
|
in_quote = *symbol == '{';
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
else if (index("}", *symbol))
|
|
|
|
{
|
|
|
|
space_before = FALSE;
|
|
|
|
space_after = FALSE;
|
2008-04-17 12:18:25 +02:00
|
|
|
in_quote = FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
else if (index(".!", *symbol)) //symbol[0] == '.' && symbol[1] == 0)
|
|
|
|
{
|
|
|
|
//space_before = FALSE;
|
|
|
|
space_after = FALSE;
|
|
|
|
}
|
|
|
|
else if (PATTERN_is(*pattern, RS_NOT))
|
|
|
|
{
|
|
|
|
space_after = TRUE;
|
|
|
|
}
|
|
|
|
else if (*symbol == '-')
|
|
|
|
{
|
|
|
|
if (old_type == EVAL_TYPE_OPERATOR && (PATTERN_is(pattern[-1], RS_LBRA) || PATTERN_is(pattern[-1],RS_LSQR)))
|
|
|
|
space_before = FALSE;
|
|
|
|
else
|
|
|
|
space_before = TRUE;
|
|
|
|
|
|
|
|
space_after = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
space_before = TRUE;
|
|
|
|
space_after = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (old_type == EVAL_TYPE_RESERVED)
|
|
|
|
space_before = TRUE;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case EVAL_TYPE_ERROR:
|
|
|
|
space_before = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (space_before && old_type != EVAL_TYPE_END)
|
|
|
|
{
|
|
|
|
GB.AddString(&result->str, " ", 1);
|
|
|
|
add_data(EVAL_TYPE_END, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == EVAL_TYPE_STRING)
|
|
|
|
GB.AddString(&result->str, "\"", 1);
|
|
|
|
|
|
|
|
//len = strlen(symbol);
|
|
|
|
|
|
|
|
/*if (type == EVAL_TYPE_IDENTIFIER && len >= 2 &&
|
|
|
|
islower(symbol[0]) && islower(symbol[1]))
|
|
|
|
{
|
|
|
|
char c = toupper(symbol[0]);
|
|
|
|
GB.AddString(&result->str, &c, 1);
|
|
|
|
GB.AddString(&result->str, &symbol[1], len - 1);
|
|
|
|
}
|
|
|
|
else*/
|
|
|
|
if (len)
|
|
|
|
{
|
|
|
|
GB.AddString(&result->str, symbol, len);
|
|
|
|
//printf("add: %.*s\n", len, symbol);
|
|
|
|
len = get_utf8_length(symbol, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == EVAL_TYPE_STRING)
|
|
|
|
{
|
|
|
|
GB.AddString(&result->str, "\"", 1);
|
|
|
|
len += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
add_data(type, len);
|
|
|
|
//printf("add_data: %.d (%d)\n", type, len);
|
|
|
|
|
|
|
|
pattern++;
|
|
|
|
}
|
|
|
|
|
|
|
|
result->color = colors;
|
|
|
|
result->len = colors_len;
|
|
|
|
|
|
|
|
//fprintf(stderr, "analyze: %d %s\n", strlen(result->str), result->str);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define add_pattern(_type, _index) EVAL->pattern[EVAL->pattern_count++] = PATTERN_make((_type), (_index));
|
|
|
|
|
|
|
|
static void add_end_pattern(void)
|
|
|
|
{
|
|
|
|
int index;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
len = EVAL->len - (READ_source_ptr - EVAL->source);
|
|
|
|
if (len > 0)
|
|
|
|
{
|
|
|
|
TABLE_add_symbol(EVAL->string, READ_source_ptr, len, NULL, &index);
|
|
|
|
add_pattern(EVAL_TYPE_ERROR, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
add_pattern(EVAL_TYPE_END, 0);
|
2008-04-05 15:06:08 +02:00
|
|
|
//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;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("EVAL: %*.s\n", expr->len, expr->source);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
CLEAR(result);
|
|
|
|
|
|
|
|
while (len > 0 && src[len - 1] == ' ')
|
|
|
|
{
|
|
|
|
len--;
|
|
|
|
nspace++;
|
|
|
|
}
|
|
|
|
|
|
|
|
result->len = 0;
|
|
|
|
result->str = NULL;
|
|
|
|
|
|
|
|
if (len > 0)
|
|
|
|
{
|
|
|
|
EVAL = &EVAL_read_expr;
|
|
|
|
|
|
|
|
EVAL_clear(EVAL);
|
|
|
|
|
|
|
|
GB.NewString(&EVAL->source, src, len);
|
|
|
|
GB.AddString(&EVAL->source, "\0\0", 2);
|
|
|
|
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
|
|
|
|
|
|
|
EVAL_start(EVAL);
|
|
|
|
|
|
|
|
TRY
|
|
|
|
{
|
|
|
|
EVAL_read();
|
|
|
|
}
|
|
|
|
CATCH
|
|
|
|
{
|
|
|
|
add_end_pattern();
|
|
|
|
}
|
|
|
|
END_TRY
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
GB.FreeString(&EVAL->source);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result->proc = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (nspace > 0)
|
|
|
|
{
|
|
|
|
GB.AddString(&result->str, " ", nspace > 8 ? 8 : nspace);
|
|
|
|
nspace -= 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|