/********************************************************************
This file is part of the abs 0.907 distribution. abs is a spreadsheet
with graphical user interface.
Copyright (C) 1998-2001 André Bertin (Andre.Bertin@ping.be)
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 of the License, or
(at your option) any later version if in the same spirit as version 2.
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.
Concact: abs@pi.be
http://home.pi.be/bertin/abs.shtml
*********************************************************************/
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "abv.h"
static char line[512];
static int linenumber = 0;
static int curpos = 0;
static int begin_of_module;
static char **currentFile = NULL;
static char **currentFile_bak;
int
SetFileInput (code)
char **code;
{
currentFile = code;
begin_of_module = 1;
curpos = 0;
linenumber = 0;
return 0;
}
int
CheckLine (char *line, int ln)
{
int i, len;
int ret = 0;
int guillemet = 0;
int prime = 0;
int parent = 0;
int croch = 0;
if (line == NULL)
return 0;
len = strlen (line);
if (len < 2)
return 0;
while (line[0] == ' ' && len > 1)
{
line++;
len--;
}
if (line[0] == '\'')
return 0;
for (i = 0; i < len; i++)
{
switch (line[i])
{
case '"':
guillemet = !guillemet;
break;
case '\'':
if (!guillemet)
prime = !prime;
break;
case '(':
if (!guillemet)
parent++;
break;
case ')':
if (!guillemet)
parent--;
break;
case '[':
if (!guillemet)
croch++;
break;
case ']':
if (!guillemet)
croch--;
break;
}
}
if (currentFile == NULL)
{
if (guillemet)
{
ABVInform ("unballanced \"");
ret = 1;
}
if (prime)
{
ABVInform ("unballanced \'");
ret = 2;
}
if (parent)
{
ABVInform ("unballanced \(");
ret = 3;
}
if (croch)
{
ABVInform ("unballanced \[");
ret = 4;
}
}
else
{
if (guillemet)
{
compiler_message ("unballanced \" \n");
ret = 1;
}
if (prime)
{
compiler_message ("unballanced \' \n");
ret = 2;
}
if (parent)
{
compiler_message ("unballanced \( \n");
ret = 3;
}
if (croch)
{
compiler_message ("unballanced \[ \n");
ret = 4;
}
}
return ret;
}
int
SetStringInput (buf)
char *buf;
{
char *ret;
currentFile_bak = currentFile;
currentFile = NULL;
if (buf == NULL)
buf = " ";
ret = strchr (buf, '\n');
if (ret != NULL)
ret[0] = '\0';
if (CheckLine (buf, 0))
buf = "\"ERR!\"";
sprintf (line, "BEGIN_OF_FORMULA ");
strcat (line, buf);
strcat (line, " END_OF_FORMULA \n\n");
curpos = 0;
return 0;
}
int
parseerror ()
{
char errormsg[128];
if (currentFile != NULL)
{
sprintf (errormsg, "syntax error line %d col %d:%s", linenumber, curpos, line);
compiler_message (errormsg);
}
else
{
char *buf = line + 17;
int len = strlen (buf);
if (len > 17)
buf[len - 17] = '\0';
sprintf (errormsg, "syntax error in formula \"%s\"", buf);
}
return 0;
}
int
StringInput ()
{
int c;
if (currentFile)
{
if (curpos == 0)
{
linenumber++;
if (begin_of_module)
{
begin_of_module = 0;
sprintf (line, "BEGIN_OF_MODULE");
linenumber = 0;
}
else if (currentFile[linenumber - 1] == NULL)
{
sprintf (line, "END_OF_MODULE");
}
else
{
char *buf = currentFile[linenumber - 1];
while (buf[0] == ' ')
buf++;
strcpy (line, buf);
}
if (strchr (line, '\n') == NULL)
strcat (line, "\n\0");
if (strlen (line) < 2)
{
curpos = 0;
return (int) '\f';
};
if (CheckLine (line, linenumber))
{
sprintf (line, "\n");
};
}
c = (int) line[curpos];
if (c == '_' && line[curpos + 1] == '\n')
{
curpos = 0;
return (int) '\f';
}
if (c == '\'' && curpos == 0)
{
curpos = 0;
return (int) '\f';
}
if (c == '\n')
{
curpos = 0;
return c;
}
curpos++;
return c;
}
if (curpos < strlen (line))
{
c = (int) line[curpos];
curpos++;
return c;
}
return EOF;
}
static char tokenbuf[256];
int
FillTokenBuf (c)
char *c;
{
sprintf (tokenbuf, "%s", c);
return 0;
}
char *
GetLastToken ()
{
return tokenbuf;
}
syntax highlighted by Code2HTML, v. 0.9.1