2001-04-09 20:54:03 +01:00
|
|
|
|
/*************************************************************************
|
|
|
|
|
* *
|
|
|
|
|
* YAP Prolog *
|
|
|
|
|
* *
|
|
|
|
|
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
|
|
|
|
* *
|
2003-01-22 17:23:18 +00:00
|
|
|
|
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-2003 *
|
2001-04-09 20:54:03 +01:00
|
|
|
|
* *
|
|
|
|
|
**************************************************************************
|
|
|
|
|
* *
|
|
|
|
|
* File: %W% %G% *
|
2003-01-22 17:23:18 +00:00
|
|
|
|
* Last rev: 22-1-03 *
|
2001-04-09 20:54:03 +01:00
|
|
|
|
* mods: *
|
|
|
|
|
* comments: Prolog's scanner *
|
|
|
|
|
* *
|
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Description:
|
|
|
|
|
*
|
|
|
|
|
* This module produces a list of tokens for use by the parser. The calling
|
|
|
|
|
* program should supply a routine int nextch(charpos) int *charpos; which,
|
|
|
|
|
* when called should produce the next char or -1 if none availlable. The
|
|
|
|
|
* scanner will stop producing tokens when it either finds an end of file
|
|
|
|
|
* (-1) or a token consisting of just '.' followed by a blank or control
|
|
|
|
|
* char. Scanner errors will be signalled by the scanner exiting with a non-
|
|
|
|
|
* zero ErrorMsg and ErrorPos. Note that, even in this case, the scanner
|
|
|
|
|
* will try to find the end of the term. A function char
|
|
|
|
|
* *AllocScannerMemory(nbytes) should be supplied for allocating (temporary)
|
|
|
|
|
* space for strings and for the table of prolog variables occurring in the
|
|
|
|
|
* term.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "Yap.h"
|
|
|
|
|
#include "Yatom.h"
|
|
|
|
|
#include "Heap.h"
|
|
|
|
|
#include "yapio.h"
|
|
|
|
|
#include "alloc.h"
|
|
|
|
|
#include "eval.h"
|
2002-08-28 15:02:35 +01:00
|
|
|
|
#if _MSC_VER || defined(__MINGW32__)
|
|
|
|
|
#if HAVE_FINITE==1
|
|
|
|
|
#undef HAVE_FINITE
|
|
|
|
|
#endif
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
#endif
|
2002-06-17 16:28:01 +01:00
|
|
|
|
#include "iopreds.h"
|
2001-04-09 20:54:03 +01:00
|
|
|
|
#if HAVE_STRING_H
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* You just can't trust some machines */
|
|
|
|
|
#define my_isxdigit(C,SU,SL) (chtype[C] == NU || (C >= 'A' && \
|
|
|
|
|
C <= (SU)) || (C >= 'a' && C <= (SL)))
|
|
|
|
|
#define my_isupper(C) ( C >= 'A' && C <= 'Z' )
|
2003-01-08 16:45:35 +00:00
|
|
|
|
#define my_islower(C) ( C >= 'a' && C <= 'z' )
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
2003-01-08 16:45:35 +00:00
|
|
|
|
STATIC_PROTO(int my_getch, (int (*) (int)));
|
2001-04-09 20:54:03 +01:00
|
|
|
|
STATIC_PROTO(Term float_send, (char *));
|
2003-01-08 16:45:35 +00:00
|
|
|
|
STATIC_PROTO(Term get_num, (int *, int *, int, int (*) (int), int (*) (int)));
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
|
|
/* token table with some help from Richard O'Keefe's PD scanner */
|
|
|
|
|
static char chtype0[NUMBER_OF_CHARS+1] =
|
|
|
|
|
{
|
|
|
|
|
EF,
|
|
|
|
|
/* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
|
|
|
|
|
BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS,
|
|
|
|
|
|
|
|
|
|
/* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
|
|
|
|
|
BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS,
|
|
|
|
|
|
|
|
|
|
/* sp ! " # $ % & ' ( ) * + , - . / */
|
|
|
|
|
BS, SL, DC, SY, LC, CC, SY, QT, BK, BK, SY, SY, BK, SY, SY, SY,
|
|
|
|
|
|
|
|
|
|
/* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
|
|
|
|
|
NU, NU, NU, NU, NU, NU, NU, NU, NU, NU, SY, SL, SY, SY, SY, SY,
|
|
|
|
|
|
|
|
|
|
/* @ A B C D E F G H I J K L M N O */
|
|
|
|
|
SY, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC,
|
|
|
|
|
|
|
|
|
|
/* P Q R S T U V W X Y Z [ \ ] ^ _ */
|
|
|
|
|
UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, BK, SY, BK, SY, UL,
|
|
|
|
|
|
|
|
|
|
/* ` a b c d e f g h i j k l m n o */
|
|
|
|
|
SY, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC,
|
|
|
|
|
|
|
|
|
|
/* p q r s t u v w x y z { | } ~ del */
|
|
|
|
|
LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, BK, BK, BK, SY, BS,
|
|
|
|
|
|
|
|
|
|
/* 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 */
|
|
|
|
|
BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS,
|
|
|
|
|
|
|
|
|
|
/* 144 145 <20> 147 148 149 150 151 152 153 154 155 156 157 158 159 */
|
|
|
|
|
BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS,
|
|
|
|
|
|
|
|
|
|
/* <20> <20> <20> <20> <20> <20> <20> <20> <20> <20> <20> <20> <20> <20> <20> <20> */
|
|
|
|
|
BS, SY, SY, SY, SY, SY, SY, SY, SY, SY, LC, SY, SY, SY, SY, SY,
|
|
|
|
|
|
|
|
|
|
/* <20> <20> <20> <20> <20> <20> <20> <20> <20> <20> <20> <20> <20> <20> <20> <20> */
|
|
|
|
|
SY, SY, LC, LC, SY, SY, SY, SY, SY, LC, LC, SY, SY, SY, SY, SY,
|
|
|
|
|
|
|
|
|
|
/* <20> <20> <20> <20> <20> <20> <20> <20> <20> <20> <20> <20> <20> <20> <20> <20> */
|
|
|
|
|
UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC,
|
|
|
|
|
|
|
|
|
|
/* <20> <20> <20> <20> <20> <20> <20> <20> <20> <20> <20> <20> <20> <20> <20> <20> */
|
|
|
|
|
#ifdef vms
|
|
|
|
|
UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, LC,
|
|
|
|
|
#else
|
|
|
|
|
UC, UC, UC, UC, UC, UC, UC, SY, UC, UC, UC, UC, UC, UC, UC, LC,
|
|
|
|
|
#endif
|
|
|
|
|
/* <20> <20> <20> <20> <20> <20> <20> <20> <20> <20> <20> <20> <20> <20> <20> <20> */
|
|
|
|
|
LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC,
|
|
|
|
|
|
2002-01-23 05:01:06 +00:00
|
|
|
|
/* <20> <20> <20> <20> <20> <20> <20> <20> <20> <20> <20> <20> <20> cannot write the last three because of lcc */
|
2001-04-09 20:54:03 +01:00
|
|
|
|
#ifdef vms
|
|
|
|
|
LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC
|
|
|
|
|
#else
|
|
|
|
|
LC, LC, LC, LC, LC, LC, LC, SY, LC, LC, LC, LC, LC, LC, LC, LC
|
|
|
|
|
#endif
|
|
|
|
|
};
|
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
|
#define chtype (chtype0+1)
|
2002-11-18 18:18:05 +00:00
|
|
|
|
char *Yap_chtype = chtype0+1;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
|
static char *
|
2001-04-09 20:54:03 +01:00
|
|
|
|
AllocScannerMemory(unsigned int size)
|
|
|
|
|
{
|
2004-02-05 16:57:02 +00:00
|
|
|
|
#if USE_SYSTEM_MALLOC
|
|
|
|
|
return malloc(AdjustSize(size));
|
|
|
|
|
#else
|
2001-04-09 20:54:03 +01:00
|
|
|
|
char *AuxSpScan;
|
|
|
|
|
|
|
|
|
|
AuxSpScan = (char *)TR;
|
|
|
|
|
size = AdjustSize(size);
|
|
|
|
|
TR = (tr_fr_ptr)(AuxSpScan+size);
|
|
|
|
|
#if !OS_HANDLES_TR_OVERFLOW
|
2002-11-18 18:18:05 +00:00
|
|
|
|
if (Unsigned(Yap_TrailTop) == Unsigned(TR)) {
|
|
|
|
|
if(!Yap_growtrail (sizeof(CELL) * 16 * 1024L)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
|
return(NULL);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
return (AuxSpScan);
|
2004-02-05 16:57:02 +00:00
|
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
|
char *
|
2002-11-18 18:18:05 +00:00
|
|
|
|
Yap_AllocScannerMemory(unsigned int size)
|
2002-11-11 17:38:10 +00:00
|
|
|
|
{
|
|
|
|
|
return AllocScannerMemory(size);
|
|
|
|
|
}
|
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
|
inline static int
|
2003-01-08 16:45:35 +00:00
|
|
|
|
my_getch(int (*Nextch) (int))
|
2001-04-09 20:54:03 +01:00
|
|
|
|
{
|
2003-01-08 16:45:35 +00:00
|
|
|
|
int ch = (*Nextch) (Yap_c_input_stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
#ifdef DEBUG
|
2002-11-18 18:18:05 +00:00
|
|
|
|
if (Yap_Option[1])
|
|
|
|
|
fprintf(Yap_stderr, "[getch %c]", ch);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
#endif
|
|
|
|
|
return(ch);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern double atof(const char *);
|
|
|
|
|
|
|
|
|
|
static Term
|
|
|
|
|
float_send(char *s)
|
|
|
|
|
{
|
|
|
|
|
Float f = (Float)atof(s);
|
|
|
|
|
#if HAVE_FINITE
|
|
|
|
|
if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { /* iso */
|
|
|
|
|
if (!finite(f)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
|
Yap_ErrorMessage = "Float overflow while scanning";
|
2001-04-09 20:54:03 +01:00
|
|
|
|
return(MkEvalFl(0.0));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
return (MkEvalFl(f));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* we have an overflow at s */
|
|
|
|
|
static Term
|
2001-06-22 18:53:36 +01:00
|
|
|
|
read_int_overflow(const char *s, Int base, Int val)
|
2001-04-09 20:54:03 +01:00
|
|
|
|
{
|
|
|
|
|
#ifdef USE_GMP
|
|
|
|
|
/* try to scan it as a bignum */
|
2002-11-18 18:18:05 +00:00
|
|
|
|
MP_INT *new = Yap_PreAllocBigNum();
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
|
|
mpz_init_set_str (new, s, base);
|
2002-11-18 18:18:05 +00:00
|
|
|
|
return(Yap_MkBigIntTerm(new));
|
2001-04-09 20:54:03 +01:00
|
|
|
|
#else
|
|
|
|
|
/* try to scan it as a float */
|
2001-06-22 18:53:36 +01:00
|
|
|
|
return(MkIntegerTerm(val));
|
2001-04-09 20:54:03 +01:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2003-01-08 16:45:35 +00:00
|
|
|
|
static unsigned int
|
|
|
|
|
read_quoted_char(int *scan_nextp, int inp_stream, int (*QuotedNxtch)(int))
|
|
|
|
|
{
|
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
|
|
/* escape sequence */
|
|
|
|
|
restart:
|
|
|
|
|
ch = QuotedNxtch(inp_stream);
|
|
|
|
|
switch (ch) {
|
|
|
|
|
case 10:
|
2003-01-20 15:13:21 +00:00
|
|
|
|
ch = QuotedNxtch(inp_stream);
|
|
|
|
|
if (ch == '\\') goto restart;
|
|
|
|
|
return ch;
|
2003-01-08 16:45:35 +00:00
|
|
|
|
case 'a':
|
|
|
|
|
return '\a';
|
|
|
|
|
case 'b':
|
|
|
|
|
return '\b';
|
|
|
|
|
case 'c':
|
|
|
|
|
if (yap_flags[CHARACTER_ESCAPE_FLAG] == ISO_CHARACTER_ESCAPES) {
|
|
|
|
|
Yap_ErrorMessage = "invalid escape sequence \\c";
|
|
|
|
|
return 0;
|
|
|
|
|
} else {
|
|
|
|
|
/* sicstus */
|
|
|
|
|
ch = QuotedNxtch(inp_stream);
|
|
|
|
|
if (chtype[ch] == SL) {
|
|
|
|
|
goto restart;
|
|
|
|
|
} else {
|
|
|
|
|
return 'c';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
case 'd':
|
|
|
|
|
return 127;
|
|
|
|
|
case 'e':
|
2003-01-22 17:23:18 +00:00
|
|
|
|
return '\x1B'; /* <ESC>, a.k.a. \e */
|
2003-01-08 16:45:35 +00:00
|
|
|
|
case 'f':
|
|
|
|
|
return '\f';
|
|
|
|
|
case 'n':
|
|
|
|
|
return '\n';
|
|
|
|
|
case 'r':
|
|
|
|
|
return '\r';
|
|
|
|
|
case 't':
|
|
|
|
|
return '\t';
|
|
|
|
|
case 'v':
|
|
|
|
|
return '\v';
|
|
|
|
|
case '\\':
|
|
|
|
|
return '\\';
|
|
|
|
|
case '\'':
|
|
|
|
|
return '\'';
|
|
|
|
|
case '"':
|
|
|
|
|
return '"';
|
|
|
|
|
case '`':
|
|
|
|
|
return '`';
|
|
|
|
|
case '^':
|
|
|
|
|
if (yap_flags[CHARACTER_ESCAPE_FLAG] == ISO_CHARACTER_ESCAPES) {
|
|
|
|
|
Yap_ErrorMessage = "invalid escape sequence";
|
|
|
|
|
return 0;
|
|
|
|
|
} else {
|
|
|
|
|
ch = QuotedNxtch(inp_stream);
|
|
|
|
|
if (ch == '?') {/* delete character */
|
|
|
|
|
return 127;
|
|
|
|
|
} else if (ch >= 'a' && ch < 'z') {/* octal */
|
|
|
|
|
return ch - 'a';
|
|
|
|
|
} else if (ch >= 'A' && ch < 'Z') {/* octal */
|
|
|
|
|
return ch - 'A';
|
|
|
|
|
} else {
|
|
|
|
|
return '^';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
case '0':
|
|
|
|
|
case '1':
|
|
|
|
|
case '2':
|
|
|
|
|
case '3':
|
|
|
|
|
case '4':
|
|
|
|
|
case '5':
|
|
|
|
|
case '6':
|
|
|
|
|
case '7':
|
|
|
|
|
/* character in octal: maximum of 3 digits, terminates with \ */
|
|
|
|
|
if (yap_flags[CHARACTER_ESCAPE_FLAG] == ISO_CHARACTER_ESCAPES) {
|
|
|
|
|
unsigned char so_far = ch-'0';
|
|
|
|
|
ch = QuotedNxtch(inp_stream);
|
|
|
|
|
if (ch >= '0' && ch < '8') {/* octal */
|
|
|
|
|
so_far = so_far*8+(ch-'0');
|
|
|
|
|
ch = QuotedNxtch(inp_stream);
|
|
|
|
|
if (ch >= '0' && ch < '8') { /* octal */
|
|
|
|
|
so_far = so_far*8+(ch-'0');
|
|
|
|
|
ch = QuotedNxtch(inp_stream);
|
|
|
|
|
if (ch != '\\') {
|
|
|
|
|
Yap_ErrorMessage = "invalid octal escape sequence";
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
} else if (ch == '\\') {
|
|
|
|
|
return so_far;
|
|
|
|
|
} else {
|
|
|
|
|
Yap_ErrorMessage = "invalid octal escape sequence";
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
} else if (ch == '\\') {
|
|
|
|
|
return so_far;
|
|
|
|
|
} else {
|
|
|
|
|
Yap_ErrorMessage = "invalid octal escape sequence";
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
/* sicstus */
|
|
|
|
|
unsigned char so_far = ch-'0';
|
|
|
|
|
ch = QuotedNxtch(inp_stream);
|
|
|
|
|
if (ch >= '0' && ch < '8') {/* octal */
|
|
|
|
|
so_far = so_far*8+(ch-'0');
|
|
|
|
|
ch = QuotedNxtch(inp_stream);
|
|
|
|
|
if (ch >= '0' && ch < '8') { /* octal */
|
|
|
|
|
return so_far*8+(ch-'0');
|
|
|
|
|
} else {
|
|
|
|
|
*scan_nextp = FALSE;
|
|
|
|
|
return so_far;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
*scan_nextp = FALSE;
|
|
|
|
|
return so_far;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
case 'x':
|
|
|
|
|
/* hexadecimal character (YAP allows empty hexadecimal */
|
|
|
|
|
if (yap_flags[CHARACTER_ESCAPE_FLAG] == ISO_CHARACTER_ESCAPES) {
|
|
|
|
|
unsigned char so_far = 0;
|
|
|
|
|
ch = QuotedNxtch(inp_stream);
|
|
|
|
|
if (my_isxdigit(ch,'f','F')) {/* hexa */
|
|
|
|
|
so_far = so_far * 16 + (chtype[ch] == NU ? ch - '0' :
|
|
|
|
|
(my_isupper(ch) ? ch - 'A' : ch - 'a') + 10);
|
|
|
|
|
ch = QuotedNxtch(inp_stream);
|
|
|
|
|
if (my_isxdigit(ch,'f','F')) { /* hexa */
|
|
|
|
|
so_far = so_far * 16 + (chtype[ch] == NU ? ch - '0' :
|
|
|
|
|
(my_isupper(ch) ? ch - 'A' : ch - 'a') + 10);
|
|
|
|
|
ch = QuotedNxtch(inp_stream);
|
|
|
|
|
if (ch == '\\') {
|
|
|
|
|
return so_far;
|
|
|
|
|
} else {
|
|
|
|
|
Yap_ErrorMessage = "invalid hexadecimal escape sequence";
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
} else if (ch == '\\') {
|
|
|
|
|
return so_far;
|
|
|
|
|
} else {
|
|
|
|
|
Yap_ErrorMessage = "invalid hexadecimal escape sequence";
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
} else if (ch == '\\') {
|
|
|
|
|
return so_far;
|
|
|
|
|
} else {
|
|
|
|
|
Yap_ErrorMessage = "invalid hexadecimal escape sequence";
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
/* sicstus mode */
|
|
|
|
|
unsigned char so_far = 0;
|
|
|
|
|
ch = QuotedNxtch(inp_stream);
|
|
|
|
|
so_far = (chtype[ch] == NU ? ch - '0' :
|
|
|
|
|
my_isupper(ch) ? ch - 'A' + 10 :
|
|
|
|
|
my_islower(ch) ? ch - 'a' +10 : 0);
|
|
|
|
|
ch = QuotedNxtch(inp_stream);
|
2004-02-09 16:46:03 +00:00
|
|
|
|
return so_far*16 + (chtype[ch] == NU ? ch - '0' :
|
2003-01-08 16:45:35 +00:00
|
|
|
|
my_isupper(ch) ? ch - 'A' +10 :
|
|
|
|
|
my_islower(ch) ? ch - 'a' + 10 : 0);
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
/* accept sequence. Note that the ISO standard does not
|
|
|
|
|
consider this sequence legal, whereas SICStus would
|
|
|
|
|
eat up the escape sequence. */
|
|
|
|
|
if (yap_flags[CHARACTER_ESCAPE_FLAG] == ISO_CHARACTER_ESCAPES) {
|
|
|
|
|
Yap_ErrorMessage = "invalid escape sequence";
|
|
|
|
|
return 0;
|
|
|
|
|
} else {
|
|
|
|
|
/* sicstus */
|
|
|
|
|
if (chtype[ch] == SL) {
|
|
|
|
|
goto restart;
|
|
|
|
|
} else {
|
|
|
|
|
return ch;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
|
/* reads a number, either integer or float */
|
|
|
|
|
|
|
|
|
|
static Term
|
2003-01-08 16:45:35 +00:00
|
|
|
|
get_num(int *chp, int *chbuffp, int inp_stream, int (*Nxtch) (int), int (*QuotedNxtch) (int))
|
2001-04-09 20:54:03 +01:00
|
|
|
|
{
|
|
|
|
|
char *s = (char *)TR, *sp = s;
|
2003-01-08 16:45:35 +00:00
|
|
|
|
int ch = *chp;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
Int val = 0, base = ch - '0';
|
|
|
|
|
int might_be_float = TRUE, has_overflow = FALSE;
|
|
|
|
|
|
|
|
|
|
*sp++ = ch;
|
2003-01-08 16:45:35 +00:00
|
|
|
|
ch = Nxtch(inp_stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
/*
|
|
|
|
|
* because of things like 00'2, 03'2 and even better 12'2, I need to
|
|
|
|
|
* do this (have mercy)
|
|
|
|
|
*/
|
|
|
|
|
if (chtype[ch] == NU) {
|
|
|
|
|
*sp++ = ch;
|
|
|
|
|
base = 10 * base + ch - '0';
|
2003-01-08 16:45:35 +00:00
|
|
|
|
ch = Nxtch(inp_stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
}
|
|
|
|
|
if (ch == '\'') {
|
|
|
|
|
if (base > 36) {
|
2002-11-18 18:18:05 +00:00
|
|
|
|
Yap_ErrorMessage = "Admissible bases are 0..36";
|
2001-04-09 20:54:03 +01:00
|
|
|
|
return (TermNil);
|
|
|
|
|
}
|
|
|
|
|
might_be_float = FALSE;
|
|
|
|
|
*sp++ = ch;
|
2003-01-08 16:45:35 +00:00
|
|
|
|
ch = Nxtch(inp_stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
if (base == 0) {
|
|
|
|
|
Int ascii = ch;
|
2003-01-08 16:45:35 +00:00
|
|
|
|
int scan_extra = TRUE;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
2001-04-17 22:07:41 +01:00
|
|
|
|
if (ch == '\\' &&
|
|
|
|
|
yap_flags[CHARACTER_ESCAPE_FLAG] != CPROLOG_CHARACTER_ESCAPES) {
|
2003-01-08 16:45:35 +00:00
|
|
|
|
ascii = read_quoted_char(&scan_extra, inp_stream, QuotedNxtch);
|
2001-04-17 22:07:41 +01:00
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
|
/* a quick way to represent ASCII */
|
2003-01-08 16:45:35 +00:00
|
|
|
|
if (scan_extra)
|
|
|
|
|
*chp = Nxtch(inp_stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
return (MkIntTerm(ascii));
|
2003-01-08 16:45:35 +00:00
|
|
|
|
} else if (base >= 10 && base <= 36) {
|
2001-04-09 20:54:03 +01:00
|
|
|
|
int upper_case = 'A' - 11 + base;
|
|
|
|
|
int lower_case = 'a' - 11 + base;
|
|
|
|
|
|
|
|
|
|
while (my_isxdigit(ch, upper_case, lower_case)) {
|
|
|
|
|
Int oval = val;
|
|
|
|
|
*sp++ = ch;
|
|
|
|
|
val = val * base + (chtype[ch] == NU ? ch - '0' :
|
|
|
|
|
(my_isupper(ch) ? ch - 'A' : ch - 'a') + 10);
|
|
|
|
|
if (oval >= val && oval != 0) /* overflow */
|
|
|
|
|
has_overflow = (has_overflow || TRUE);
|
2003-01-08 16:45:35 +00:00
|
|
|
|
ch = Nxtch(inp_stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2003-01-08 16:45:35 +00:00
|
|
|
|
} else if ((ch == 'x' || ch == 'X') && base == 0) {
|
2001-04-09 20:54:03 +01:00
|
|
|
|
might_be_float = FALSE;
|
|
|
|
|
*sp++ = ch;
|
2003-01-08 16:45:35 +00:00
|
|
|
|
ch = Nxtch(inp_stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
while (my_isxdigit(ch, 'F', 'f')) {
|
|
|
|
|
Int oval = val;
|
|
|
|
|
*sp++ = ch;
|
|
|
|
|
val = val * 16 + (chtype[ch] == NU ? ch - '0' :
|
|
|
|
|
(my_isupper(ch) ? ch - 'A' : ch - 'a') + 10);
|
|
|
|
|
if (oval >= val && oval != 0) /* overflow */
|
|
|
|
|
has_overflow = (has_overflow || TRUE);
|
2003-01-08 16:45:35 +00:00
|
|
|
|
ch = Nxtch(inp_stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
}
|
2003-01-08 16:45:35 +00:00
|
|
|
|
*chp = ch;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
}
|
|
|
|
|
else if ((ch == 'o') && base == 0) {
|
|
|
|
|
might_be_float = FALSE;
|
|
|
|
|
base = 8;
|
|
|
|
|
*sp++ = ch;
|
2003-01-08 16:45:35 +00:00
|
|
|
|
*chp = Nxtch(inp_stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
val = base;
|
|
|
|
|
base = 10;
|
|
|
|
|
}
|
|
|
|
|
while (chtype[ch] == NU) {
|
|
|
|
|
Int oval = val;
|
2004-02-05 16:57:02 +00:00
|
|
|
|
if (ch != '0')
|
|
|
|
|
*sp++ = ch;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
if (ch - '0' >= base)
|
|
|
|
|
return (MkIntegerTerm(val));
|
|
|
|
|
val = val * base + ch - '0';
|
2003-02-24 11:01:01 +00:00
|
|
|
|
if (val/base != oval || val -oval*base != ch-'0') /* overflow */
|
2001-04-09 20:54:03 +01:00
|
|
|
|
has_overflow = (has_overflow || TRUE);
|
2003-01-08 16:45:35 +00:00
|
|
|
|
ch = Nxtch(inp_stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
}
|
|
|
|
|
if (might_be_float && (ch == '.' || ch == 'e' || ch == 'E')) {
|
|
|
|
|
if (ch == '.') {
|
|
|
|
|
*sp++ = '.';
|
2003-01-08 16:45:35 +00:00
|
|
|
|
if (chtype[ch = Nxtch(inp_stream)] != NU) {
|
|
|
|
|
*chbuffp = '.';
|
|
|
|
|
*chp = ch;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
*--sp = '\0';
|
|
|
|
|
if (has_overflow)
|
2001-06-22 18:53:36 +01:00
|
|
|
|
return(read_int_overflow(s,base,val));
|
2001-04-09 20:54:03 +01:00
|
|
|
|
return (MkIntegerTerm(val));
|
|
|
|
|
}
|
|
|
|
|
do
|
|
|
|
|
*sp++ = ch;
|
2003-01-08 16:45:35 +00:00
|
|
|
|
while (chtype[ch = Nxtch(inp_stream)] == NU);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
}
|
|
|
|
|
if (ch == 'e' || ch == 'E') {
|
2003-01-08 16:45:35 +00:00
|
|
|
|
char *sp0 = sp;
|
|
|
|
|
char cbuff = ch;
|
|
|
|
|
*sp++ = ch;
|
|
|
|
|
ch = Nxtch(inp_stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
if (ch == '-') {
|
2003-01-08 16:45:35 +00:00
|
|
|
|
cbuff = '-';
|
|
|
|
|
*sp++ = '-';
|
|
|
|
|
ch = Nxtch(inp_stream);
|
|
|
|
|
} else if (ch == '+') {
|
|
|
|
|
cbuff = '+';
|
|
|
|
|
ch = Nxtch(inp_stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
}
|
|
|
|
|
if (chtype[ch] != NU) {
|
2003-01-08 16:45:35 +00:00
|
|
|
|
/* error */
|
|
|
|
|
char *sp;
|
|
|
|
|
*chp = ch;
|
|
|
|
|
if (*sp0 == 'E') {
|
|
|
|
|
/* code the fact that we have E and not e */
|
|
|
|
|
if (cbuff == '+')
|
|
|
|
|
*chbuffp = '=';
|
|
|
|
|
else
|
|
|
|
|
*chbuffp = '_';
|
|
|
|
|
} else {
|
|
|
|
|
*chbuffp = cbuff;
|
|
|
|
|
}
|
|
|
|
|
*sp0 = '\0';
|
|
|
|
|
for (sp = s; sp < sp0; sp++) {
|
|
|
|
|
if (*sp == '.')
|
|
|
|
|
return (float_send(s));
|
|
|
|
|
}
|
|
|
|
|
return(MkIntegerTerm(val));
|
2001-04-09 20:54:03 +01:00
|
|
|
|
}
|
2003-01-08 16:45:35 +00:00
|
|
|
|
do {
|
2001-04-09 20:54:03 +01:00
|
|
|
|
*sp++ = ch;
|
2003-01-08 16:45:35 +00:00
|
|
|
|
} while (chtype[ch = Nxtch(inp_stream)] == NU);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
}
|
|
|
|
|
*sp = '\0';
|
2003-01-08 16:45:35 +00:00
|
|
|
|
*chp = ch;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
return (float_send(s));
|
|
|
|
|
} else if (has_overflow) {
|
|
|
|
|
*sp = '\0';
|
|
|
|
|
/* skip base */
|
2003-01-08 16:45:35 +00:00
|
|
|
|
*chp = ch;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
|
2001-06-22 18:53:36 +01:00
|
|
|
|
return(read_int_overflow(s+2,16,val));
|
2001-04-09 20:54:03 +01:00
|
|
|
|
if (s[1] == '\'')
|
2001-06-22 18:53:36 +01:00
|
|
|
|
return(read_int_overflow(s+2,base,val));
|
2001-04-09 20:54:03 +01:00
|
|
|
|
if (s[2] == '\'')
|
2001-06-22 18:53:36 +01:00
|
|
|
|
return(read_int_overflow(s+3,base,val));
|
|
|
|
|
return(read_int_overflow(s,base,val));
|
2003-01-08 16:45:35 +00:00
|
|
|
|
} else {
|
|
|
|
|
*chp = ch;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
return (MkIntegerTerm(val));
|
2003-01-08 16:45:35 +00:00
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* given a function Nxtch scan until we either find the number
|
|
|
|
|
or end of file */
|
|
|
|
|
Term
|
2002-11-18 18:18:05 +00:00
|
|
|
|
Yap_scan_num(int (*Nxtch) (int))
|
2001-04-09 20:54:03 +01:00
|
|
|
|
{
|
|
|
|
|
Term out;
|
|
|
|
|
int sign = 1;
|
2003-01-08 16:45:35 +00:00
|
|
|
|
int ch, cherr;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
2002-11-18 18:18:05 +00:00
|
|
|
|
Yap_ErrorMessage = NULL;
|
2003-01-08 16:45:35 +00:00
|
|
|
|
ch = Nxtch(-1);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
if (ch == '-') {
|
|
|
|
|
sign = -1;
|
2003-01-08 16:45:35 +00:00
|
|
|
|
ch = Nxtch(-1);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
} else if (ch == '+') {
|
2003-01-08 16:45:35 +00:00
|
|
|
|
ch = Nxtch(-1);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
}
|
|
|
|
|
if (chtype[ch] != NU) {
|
|
|
|
|
return(TermNil);
|
|
|
|
|
}
|
2003-01-08 16:45:35 +00:00
|
|
|
|
cherr = 0;
|
|
|
|
|
out = get_num(&ch, &cherr, -1, Nxtch, Nxtch);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
if (sign == -1) {
|
|
|
|
|
if (IsIntegerTerm(out))
|
|
|
|
|
out = MkIntegerTerm(-IntegerOfTerm(out));
|
|
|
|
|
else if (IsFloatTerm(out))
|
|
|
|
|
out = MkFloatTerm(-FloatOfTerm(out));
|
|
|
|
|
}
|
2003-01-08 16:45:35 +00:00
|
|
|
|
if (Yap_ErrorMessage != NULL || ch != -1 || cherr)
|
2001-04-09 20:54:03 +01:00
|
|
|
|
return(TermNil);
|
|
|
|
|
return(out);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TokEntry *
|
2003-01-08 16:45:35 +00:00
|
|
|
|
Yap_tokenizer(int inp_stream)
|
2001-04-09 20:54:03 +01:00
|
|
|
|
{
|
|
|
|
|
TokEntry *t, *l, *p;
|
|
|
|
|
enum TokenKinds kind;
|
|
|
|
|
int solo_flag = TRUE;
|
2003-01-08 16:45:35 +00:00
|
|
|
|
int ch;
|
|
|
|
|
int (*Nxtch) (int) = Stream[inp_stream].stream_getc_for_read;
|
|
|
|
|
int (*QuotedNxtch) (int) = Stream[inp_stream].stream_getc;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
2002-11-18 18:18:05 +00:00
|
|
|
|
Yap_ErrorMessage = NULL;
|
|
|
|
|
Yap_VarTable = NULL;
|
|
|
|
|
Yap_AnonVarTable = NULL;
|
|
|
|
|
Yap_eot_before_eof = FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
l = NIL;
|
|
|
|
|
p = NIL; /* Just to make lint happy */
|
2003-01-08 16:45:35 +00:00
|
|
|
|
ch = Nxtch(inp_stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
do {
|
2003-01-08 16:45:35 +00:00
|
|
|
|
int och, quote, isvar;
|
|
|
|
|
char *charp, *mp;
|
|
|
|
|
unsigned int len;
|
|
|
|
|
char *TokImage = NULL;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
t = (TokEntry *) AllocScannerMemory(sizeof(TokEntry));
|
2003-01-08 16:45:35 +00:00
|
|
|
|
t->TokNext = NULL;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
if (t == NULL) {
|
2002-11-18 18:18:05 +00:00
|
|
|
|
Yap_ErrorMessage = "not enough stack space to read in term";
|
2001-04-09 20:54:03 +01:00
|
|
|
|
if (p != NIL)
|
|
|
|
|
p->TokInfo = eot_tok;
|
|
|
|
|
/* serious error now */
|
|
|
|
|
return(l);
|
|
|
|
|
}
|
|
|
|
|
if (l == NIL)
|
|
|
|
|
l = t;
|
|
|
|
|
else
|
|
|
|
|
p->TokNext = t;
|
|
|
|
|
p = t;
|
2003-01-08 16:45:35 +00:00
|
|
|
|
restart:
|
|
|
|
|
while (chtype[ch] == BS) {
|
|
|
|
|
ch = Nxtch(inp_stream);
|
|
|
|
|
}
|
2003-02-14 10:56:56 +00:00
|
|
|
|
t->TokPos = GetCurInpPos(inp_stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
2003-01-08 16:45:35 +00:00
|
|
|
|
switch (chtype[ch]) {
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
2003-01-08 16:45:35 +00:00
|
|
|
|
case CC:
|
|
|
|
|
while ((ch = Nxtch(inp_stream)) != 10 && chtype[ch] != EF);
|
|
|
|
|
if (chtype[ch] != EF) {
|
|
|
|
|
/* blank space */
|
|
|
|
|
goto restart;
|
|
|
|
|
} else {
|
|
|
|
|
t->Tok = Ord(kind = eot_tok);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case UC:
|
|
|
|
|
case UL:
|
|
|
|
|
case LC:
|
|
|
|
|
och = ch;
|
|
|
|
|
ch = Nxtch(inp_stream);
|
|
|
|
|
scan_name:
|
|
|
|
|
TokImage = ((AtomEntry *) ( Yap_PreAllocCodeSpace()))->StrOfAE;
|
|
|
|
|
charp = TokImage;
|
|
|
|
|
isvar = (chtype[och] != LC);
|
|
|
|
|
*charp++ = och;
|
|
|
|
|
for (; chtype[ch] <= NU; ch = Nxtch(inp_stream))
|
2001-04-09 20:54:03 +01:00
|
|
|
|
*charp++ = ch;
|
2003-01-08 16:45:35 +00:00
|
|
|
|
*charp++ = '\0';
|
|
|
|
|
if (!isvar) {
|
|
|
|
|
/* don't do this in iso */
|
|
|
|
|
t->TokInfo = Unsigned(Yap_LookupAtom(TokImage));
|
|
|
|
|
Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage);
|
|
|
|
|
if (ch == '(')
|
|
|
|
|
solo_flag = FALSE;
|
|
|
|
|
t->Tok = Ord(kind = Name_tok);
|
|
|
|
|
} else {
|
|
|
|
|
t->TokInfo = Unsigned(Yap_LookupVar(TokImage));
|
|
|
|
|
Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage);
|
|
|
|
|
t->Tok = Ord(kind = Var_tok);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case NU:
|
|
|
|
|
{
|
|
|
|
|
int cherr, cha = ch;
|
|
|
|
|
cherr = 0;
|
|
|
|
|
t->TokInfo = get_num(&cha,&cherr,inp_stream,Nxtch,QuotedNxtch);
|
|
|
|
|
ch = cha;
|
|
|
|
|
if (cherr) {
|
|
|
|
|
TokEntry *e;
|
|
|
|
|
t->Tok = Number_tok;
|
2003-02-14 10:56:56 +00:00
|
|
|
|
t->TokPos = GetCurInpPos(inp_stream);
|
2003-01-08 16:45:35 +00:00
|
|
|
|
e = (TokEntry *) AllocScannerMemory(sizeof(TokEntry));
|
|
|
|
|
if (e == NULL) {
|
|
|
|
|
Yap_ErrorMessage = "not enough stack space to read in term";
|
|
|
|
|
p->TokInfo = eot_tok;
|
|
|
|
|
/* serious error now */
|
|
|
|
|
return(l);
|
2004-02-05 16:57:02 +00:00
|
|
|
|
} else {
|
|
|
|
|
e->TokNext = NULL;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
}
|
2003-01-08 16:45:35 +00:00
|
|
|
|
t->TokNext = e;
|
|
|
|
|
t = e;
|
|
|
|
|
p = e;
|
|
|
|
|
switch (cherr) {
|
|
|
|
|
case 'e':
|
|
|
|
|
case 'E':
|
|
|
|
|
och = cherr;
|
|
|
|
|
goto scan_name;
|
2002-11-19 17:10:45 +00:00
|
|
|
|
break;
|
2003-01-08 16:45:35 +00:00
|
|
|
|
case '=':
|
|
|
|
|
case '_':
|
|
|
|
|
/* handle error while parsing a float */
|
|
|
|
|
{
|
|
|
|
|
TokEntry *e2;
|
|
|
|
|
|
|
|
|
|
t->Tok = Ord(Var_tok);
|
|
|
|
|
t->TokInfo = Unsigned(Yap_LookupVar("E"));
|
2003-02-14 10:56:56 +00:00
|
|
|
|
t->TokPos = GetCurInpPos(inp_stream);
|
2003-01-08 16:45:35 +00:00
|
|
|
|
e2 = (TokEntry *) AllocScannerMemory(sizeof(TokEntry));
|
|
|
|
|
if (e2 == NULL) {
|
|
|
|
|
Yap_ErrorMessage = "not enough stack space to read in term";
|
|
|
|
|
p->TokInfo = eot_tok;
|
|
|
|
|
/* serious error now */
|
|
|
|
|
return(l);
|
2004-02-05 16:57:02 +00:00
|
|
|
|
} else {
|
|
|
|
|
e2->TokNext = NULL;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
}
|
2003-01-08 16:45:35 +00:00
|
|
|
|
t->TokNext = e2;
|
|
|
|
|
t = e2;
|
|
|
|
|
p = e2;
|
|
|
|
|
if (cherr == '=')
|
|
|
|
|
och = '+';
|
|
|
|
|
else
|
|
|
|
|
och = '-';
|
|
|
|
|
}
|
|
|
|
|
goto enter_symbol;
|
|
|
|
|
case '+':
|
|
|
|
|
case '-':
|
|
|
|
|
/* handle error while parsing a float */
|
|
|
|
|
{
|
|
|
|
|
TokEntry *e2;
|
|
|
|
|
|
|
|
|
|
t->Tok = Name_tok;
|
|
|
|
|
if (ch == '(')
|
|
|
|
|
solo_flag = FALSE;
|
|
|
|
|
t->TokInfo = Unsigned(Yap_LookupAtom("e"));
|
2003-02-14 10:56:56 +00:00
|
|
|
|
t->TokPos = GetCurInpPos(inp_stream);
|
2003-01-08 16:45:35 +00:00
|
|
|
|
e2 = (TokEntry *) AllocScannerMemory(sizeof(TokEntry));
|
|
|
|
|
if (e2 == NULL) {
|
|
|
|
|
Yap_ErrorMessage = "not enough stack space to read in term";
|
|
|
|
|
p->TokInfo = eot_tok;
|
|
|
|
|
/* serious error now */
|
|
|
|
|
return(l);
|
2004-02-05 16:57:02 +00:00
|
|
|
|
} else {
|
|
|
|
|
e2->TokNext = NULL;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
}
|
2003-01-08 16:45:35 +00:00
|
|
|
|
t->TokNext = e2;
|
|
|
|
|
t = e2;
|
|
|
|
|
p = e2;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
}
|
2003-01-08 16:45:35 +00:00
|
|
|
|
default:
|
|
|
|
|
och = cherr;
|
|
|
|
|
goto enter_symbol;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
}
|
2003-01-08 16:45:35 +00:00
|
|
|
|
} else {
|
|
|
|
|
t->Tok = Ord(kind = Number_tok);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
}
|
2003-01-08 16:45:35 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case QT:
|
|
|
|
|
case DC:
|
|
|
|
|
TokImage = ((AtomEntry *) ( Yap_PreAllocCodeSpace()))->StrOfAE;
|
|
|
|
|
charp = TokImage;
|
|
|
|
|
quote = ch;
|
|
|
|
|
len = 0;
|
|
|
|
|
ch = QuotedNxtch(inp_stream);
|
|
|
|
|
while (1) {
|
|
|
|
|
if (charp + 1024 > (char *)AuxSp) {
|
|
|
|
|
Yap_ErrorMessage = "Heap Overflow While Scanning: please increase code space (-h)";
|
|
|
|
|
break;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
}
|
2003-01-08 16:45:35 +00:00
|
|
|
|
if (ch == 10 && yap_flags[CHARACTER_ESCAPE_FLAG] == ISO_CHARACTER_ESCAPES) {
|
|
|
|
|
/* in ISO a new line terminates a string */
|
|
|
|
|
Yap_ErrorMessage = "layout character \n inside quotes";
|
|
|
|
|
break;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
}
|
2003-01-08 16:45:35 +00:00
|
|
|
|
if (ch == quote) {
|
|
|
|
|
ch = QuotedNxtch(inp_stream);
|
|
|
|
|
if (ch != quote)
|
2001-04-09 20:54:03 +01:00
|
|
|
|
break;
|
2003-01-08 16:45:35 +00:00
|
|
|
|
*charp++ = ch;
|
|
|
|
|
ch = QuotedNxtch(inp_stream);
|
|
|
|
|
} else if (ch == '\\' && yap_flags[CHARACTER_ESCAPE_FLAG] != CPROLOG_CHARACTER_ESCAPES) {
|
|
|
|
|
int scan_next = TRUE;
|
|
|
|
|
*charp++ = read_quoted_char(&scan_next, inp_stream, QuotedNxtch);
|
|
|
|
|
if (scan_next) {
|
|
|
|
|
ch = QuotedNxtch(inp_stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
}
|
2003-01-08 16:45:35 +00:00
|
|
|
|
} else if (chtype[ch] == EF) {
|
2002-11-18 18:18:05 +00:00
|
|
|
|
Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage);
|
2003-01-08 16:45:35 +00:00
|
|
|
|
t->Tok = Ord(kind = eot_tok);
|
2003-03-13 18:30:33 +00:00
|
|
|
|
break;
|
2003-01-08 16:45:35 +00:00
|
|
|
|
} else {
|
|
|
|
|
*charp++ = ch;
|
|
|
|
|
ch = QuotedNxtch(inp_stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
}
|
2003-01-08 16:45:35 +00:00
|
|
|
|
++len;
|
|
|
|
|
if (charp > (char *)AuxSp - 1024) {
|
|
|
|
|
/* Not enough space to read in the string. */
|
|
|
|
|
Yap_ErrorMessage = "not enough heap space to read in string or quoted atom";
|
|
|
|
|
/* serious error now */
|
|
|
|
|
Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage);
|
|
|
|
|
t->Tok = Ord(kind = eot_tok);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
}
|
2003-01-08 16:45:35 +00:00
|
|
|
|
}
|
|
|
|
|
*charp = '\0';
|
|
|
|
|
if (quote == '"') {
|
|
|
|
|
mp = AllocScannerMemory(len + 1);
|
|
|
|
|
if (mp == NULL) {
|
|
|
|
|
Yap_ErrorMessage = "not enough stack space to read in string or quoted atom";
|
|
|
|
|
Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage);
|
|
|
|
|
t->Tok = Ord(kind = eot_tok);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
}
|
2003-01-08 16:45:35 +00:00
|
|
|
|
strcpy(mp, TokImage);
|
|
|
|
|
t->TokInfo = Unsigned(mp);
|
|
|
|
|
Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage);
|
|
|
|
|
t->Tok = Ord(kind = String_tok);
|
|
|
|
|
} else {
|
|
|
|
|
t->TokInfo = Unsigned(Yap_LookupAtom(TokImage));
|
|
|
|
|
Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage);
|
|
|
|
|
t->Tok = Ord(kind = Name_tok);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
if (ch == '(')
|
|
|
|
|
solo_flag = FALSE;
|
2003-01-08 16:45:35 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SY:
|
|
|
|
|
och = ch;
|
|
|
|
|
ch = Nxtch(inp_stream);
|
|
|
|
|
if (och == '/' && ch == '*') {
|
|
|
|
|
while ((och != '*' || ch != '/') && chtype[ch] != EF) {
|
|
|
|
|
och = ch;
|
|
|
|
|
ch = Nxtch(inp_stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
}
|
2003-01-08 16:45:35 +00:00
|
|
|
|
if (chtype[ch] == EF) {
|
|
|
|
|
t->Tok = Ord(kind = eot_tok);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
}
|
2003-01-08 16:45:35 +00:00
|
|
|
|
ch = Nxtch(inp_stream);
|
|
|
|
|
goto restart;
|
|
|
|
|
}
|
|
|
|
|
enter_symbol:
|
|
|
|
|
if (och == '.' && (chtype[ch] == BS || chtype[ch] == EF
|
|
|
|
|
|| chtype[ch] == CC)) {
|
|
|
|
|
Yap_eot_before_eof = TRUE;
|
|
|
|
|
if (chtype[ch] == CC)
|
|
|
|
|
while ((ch = Nxtch(inp_stream)) != 10 && chtype[ch] != EF);
|
|
|
|
|
t->Tok = Ord(kind = eot_tok);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
TokImage = ((AtomEntry *) ( Yap_PreAllocCodeSpace()))->StrOfAE;
|
|
|
|
|
charp = TokImage;
|
|
|
|
|
*charp++ = och;
|
|
|
|
|
for (; chtype[ch] == SY; ch = Nxtch(inp_stream))
|
|
|
|
|
*charp++ = ch;
|
|
|
|
|
*charp = '\0';
|
|
|
|
|
t->TokInfo = Unsigned(Yap_LookupAtom(TokImage));
|
|
|
|
|
Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage);
|
|
|
|
|
t->Tok = Ord(kind = Name_tok);
|
|
|
|
|
if (ch == '(')
|
|
|
|
|
solo_flag = FALSE;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SL:
|
|
|
|
|
{
|
|
|
|
|
char chs[2];
|
|
|
|
|
chs[0] = ch;
|
|
|
|
|
chs[1] = '\0';
|
|
|
|
|
ch = Nxtch(inp_stream);
|
|
|
|
|
t->TokInfo = Unsigned(Yap_LookupAtom(chs));
|
|
|
|
|
t->Tok = Ord(kind = Name_tok);
|
|
|
|
|
if (ch == '(')
|
|
|
|
|
solo_flag = FALSE;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case BK:
|
|
|
|
|
och = ch;
|
|
|
|
|
do {
|
|
|
|
|
ch = Nxtch(inp_stream);
|
|
|
|
|
} while (chtype[ch] == BS);
|
|
|
|
|
if (och == '[' && ch == ']') {
|
|
|
|
|
t->TokInfo = Unsigned(AtomNil);
|
|
|
|
|
ch = Nxtch(inp_stream);
|
|
|
|
|
t->Tok = Ord(kind = Name_tok);
|
|
|
|
|
if (ch == '(')
|
|
|
|
|
solo_flag = FALSE;
|
|
|
|
|
} else {
|
|
|
|
|
t->TokInfo = och;
|
|
|
|
|
if (t->TokInfo == '(' && !solo_flag) {
|
|
|
|
|
t->TokInfo = 'l';
|
|
|
|
|
solo_flag = TRUE;
|
|
|
|
|
}
|
|
|
|
|
t->Tok = Ord(kind = Ponctuation_tok);
|
|
|
|
|
}
|
|
|
|
|
break;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
2003-01-08 16:45:35 +00:00
|
|
|
|
case EF:
|
|
|
|
|
t->Tok = Ord(kind = eot_tok);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
2001-04-09 20:54:03 +01:00
|
|
|
|
#ifdef DEBUG
|
2003-01-08 16:45:35 +00:00
|
|
|
|
fprintf(Yap_stderr, "\n++++ token: wrong char type %c %d\n", ch, chtype[ch]);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
#endif
|
2003-01-08 16:45:35 +00:00
|
|
|
|
t->Tok = Ord(kind = eot_tok);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
}
|
|
|
|
|
#ifdef DEBUG
|
2003-01-08 16:45:35 +00:00
|
|
|
|
if(Yap_Option[2]) fprintf(Yap_stderr,"[Token %d %ld]",Ord(kind),(unsigned long int)t->TokInfo);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
#endif
|
2002-11-19 17:10:45 +00:00
|
|
|
|
if (Yap_ErrorMessage) {
|
2003-10-06 15:16:23 +01:00
|
|
|
|
/* insert an error token to inform the system of what happened */
|
2002-11-19 17:10:45 +00:00
|
|
|
|
TokEntry *e = (TokEntry *) AllocScannerMemory(sizeof(TokEntry));
|
|
|
|
|
if (e == NULL) {
|
|
|
|
|
Yap_ErrorMessage = "not enough stack space to read in term";
|
|
|
|
|
p->TokInfo = eot_tok;
|
|
|
|
|
/* serious error now */
|
|
|
|
|
return(l);
|
|
|
|
|
}
|
|
|
|
|
p->TokNext = e;
|
|
|
|
|
e->Tok = Error_tok;
|
|
|
|
|
e->TokInfo = MkAtomTerm(Yap_LookupAtom(Yap_ErrorMessage));
|
2003-02-14 10:56:56 +00:00
|
|
|
|
e->TokPos = GetCurInpPos(inp_stream);
|
2004-02-05 16:57:02 +00:00
|
|
|
|
e->TokNext = NULL;
|
2002-11-19 17:10:45 +00:00
|
|
|
|
Yap_ErrorMessage = NULL;
|
|
|
|
|
p = e;
|
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
|
} while (kind != eot_tok);
|
|
|
|
|
return (l);
|
|
|
|
|
}
|
2004-02-05 16:57:02 +00:00
|
|
|
|
|
|
|
|
|
#if USE_SYSTEM_MALLOC
|
|
|
|
|
static
|
|
|
|
|
void clean_vtable(VarEntry *vt)
|
|
|
|
|
{
|
|
|
|
|
if (vt == NULL)
|
|
|
|
|
return;
|
|
|
|
|
clean_vtable(vt->VarLeft);
|
|
|
|
|
clean_vtable(vt->VarRight);
|
|
|
|
|
free(vt);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static
|
|
|
|
|
void clean_tokens(TokEntry *tk)
|
|
|
|
|
{
|
|
|
|
|
while (tk != NULL) {
|
|
|
|
|
TokEntry *ntk = tk->TokNext;
|
|
|
|
|
if (tk->Tok == Ord(String_tok)) {
|
|
|
|
|
free((void *)(tk->TokInfo));
|
|
|
|
|
}
|
|
|
|
|
free(tk);
|
|
|
|
|
tk = ntk;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
Yap_clean_tokenizer(TokEntry *tokstart, VarEntry *vartable, VarEntry *anonvartable)
|
|
|
|
|
{
|
|
|
|
|
clean_vtable(vartable);
|
|
|
|
|
clean_vtable(anonvartable);
|
|
|
|
|
clean_tokens(tokstart);
|
|
|
|
|
}
|
|
|
|
|
#endif
|