2001-04-09 20:54:03 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* *
|
|
|
|
* YAP Prolog *
|
|
|
|
* *
|
|
|
|
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
|
|
|
* *
|
|
|
|
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
|
|
|
|
* *
|
|
|
|
**************************************************************************
|
|
|
|
* *
|
|
|
|
* File: parser.c *
|
|
|
|
* Last rev: *
|
|
|
|
* mods: *
|
|
|
|
* comments: Prolog's parser *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
|
|
|
#ifdef SCCS
|
|
|
|
static char SccsId[] = "%W% %G%";
|
|
|
|
#endif
|
2014-09-15 09:13:50 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
@defgroup Syntax YAP Syntax
|
|
|
|
@ingroup YAPProgramming
|
|
|
|
@{
|
|
|
|
|
|
|
|
We will describe the syntax of YAP at two levels. We first will
|
|
|
|
describe the syntax for Prolog terms. In a second level we describe
|
|
|
|
the \a tokens from which Prolog \a terms are
|
|
|
|
built.
|
|
|
|
|
|
|
|
@defgroup Formal_Syntax Syntax of Terms
|
|
|
|
@ingroup Syntax
|
|
|
|
@{
|
|
|
|
|
|
|
|
Below, we describe the syntax of YAP terms from the different
|
|
|
|
classes of tokens defined above. The formalism used will be <em>BNF</em>,
|
|
|
|
extended where necessary with attributes denoting integer precedence or
|
|
|
|
operator type.
|
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
term ----> subterm(1200) end_of_term_marker
|
|
|
|
|
|
|
|
subterm(N) ----> term(M) [M <= N]
|
|
|
|
|
|
|
|
term(N) ----> op(N, fx) subterm(N-1)
|
|
|
|
| op(N, fy) subterm(N)
|
|
|
|
| subterm(N-1) op(N, xfx) subterm(N-1)
|
|
|
|
| subterm(N-1) op(N, xfy) subterm(N)
|
|
|
|
| subterm(N) op(N, yfx) subterm(N-1)
|
|
|
|
| subterm(N-1) op(N, xf)
|
|
|
|
| subterm(N) op(N, yf)
|
|
|
|
|
|
|
|
term(0) ----> atom '(' arguments ')'
|
|
|
|
| '(' subterm(1200) ')'
|
|
|
|
| '{' subterm(1200) '}'
|
|
|
|
| list
|
|
|
|
| string
|
|
|
|
| number
|
|
|
|
| atom
|
|
|
|
| variable
|
|
|
|
|
|
|
|
arguments ----> subterm(999)
|
|
|
|
| subterm(999) ',' arguments
|
|
|
|
|
|
|
|
list ----> '[]'
|
|
|
|
| '[' list_expr ']'
|
|
|
|
|
|
|
|
list_expr ----> subterm(999)
|
|
|
|
| subterm(999) list_tail
|
|
|
|
|
|
|
|
list_tail ----> ',' list_expr
|
|
|
|
| ',..' subterm(999)
|
|
|
|
| '|' subterm(999)
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Notes:
|
|
|
|
|
|
|
|
+ \a op(N,T) denotes an atom which has been previously declared with type
|
|
|
|
\a T and base precedence \a N.
|
|
|
|
|
|
|
|
+ Since ',' is itself a pre-declared operator with type \a xfy and
|
|
|
|
precedence 1000, is \a subterm starts with a '(', \a op must be
|
|
|
|
followed by a space to avoid ambiguity with the case of a functor
|
|
|
|
followed by arguments, e.g.:
|
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
+ (a,b) [the same as '+'(','(a,b)) of arity one]
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
versus
|
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
+(a,b) [the same as '+'(a,b) of arity two]
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
+
|
|
|
|
In the first rule for term(0) no blank space should exist between
|
|
|
|
\a atom and '('.
|
|
|
|
|
|
|
|
+
|
|
|
|
Each term to be read by the YAP parser must end with a single
|
|
|
|
dot, followed by a blank (in the sense mentioned in the previous
|
|
|
|
paragraph). When a name consisting of a single dot could be taken for
|
|
|
|
the end of term marker, the ambiguity should be avoided by surrounding the
|
|
|
|
dot with single quotes.
|
|
|
|
|
|
|
|
@}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
/*
|
|
|
|
* Description:
|
|
|
|
*
|
|
|
|
* parser: produces a prolog term from an array of tokens
|
|
|
|
*
|
|
|
|
* parser usage: the parser takes its input from an array of token descriptions
|
|
|
|
* addressed by the global variable 'tokptr' and produces a Term as result. A
|
|
|
|
* macro 'NextToken' should be defined in 'yap.h' for advancing 'tokptr' from
|
|
|
|
* one token to the next. In the distributed version this macro also updates
|
|
|
|
* a variable named 'toktide' for keeping track of how far the parser went
|
|
|
|
* before failling with a syntax error. The parser should be invoked with
|
|
|
|
* 'tokptr' pointing to the first token. The last token should have type
|
|
|
|
* 'eot_tok'. The parser return either a Term. Syntactic errors are signaled
|
|
|
|
* by a return value 0. The parser builds new terms on the 'global stack' and
|
|
|
|
* also uses an auxiliary stack pointed to by 'AuxSp'. In the distributed
|
|
|
|
* version this auxiliary stack is assumed to grow downwards. This
|
|
|
|
* assumption, however, is only relevant to routine 'ParseArgs', and to the
|
|
|
|
* variable toktide. conclusion: set tokptr pointing to first token set AuxSp
|
|
|
|
* Call Parse
|
|
|
|
*
|
|
|
|
* VSC: Working whithout known bugs in 87/4/6
|
|
|
|
*
|
|
|
|
* LD: -I or +I evaluated by parser 87/4/28
|
|
|
|
*
|
|
|
|
* LD: parser extended 87/4/28
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "Yap.h"
|
|
|
|
#include "Yatom.h"
|
2009-10-23 14:22:17 +01:00
|
|
|
#include "YapHeap.h"
|
2001-04-09 20:54:03 +01:00
|
|
|
#include "yapio.h"
|
2010-05-28 15:29:20 +01:00
|
|
|
#include "eval.h"
|
2013-11-13 12:57:52 +00:00
|
|
|
/* stuff we want to use in standard YAP code */
|
|
|
|
#include "pl-shared.h"
|
2013-12-08 19:12:24 +00:00
|
|
|
#include "YapText.h"
|
2013-11-25 10:24:13 +00:00
|
|
|
#include "pl-read.h"
|
|
|
|
#include "pl-text.h"
|
2001-04-09 20:54:03 +01:00
|
|
|
#if HAVE_STRING_H
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __STDC__XXX
|
|
|
|
#define Volatile volatile
|
|
|
|
#else
|
|
|
|
#define Volatile
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* weak backtraking mechanism based on long_jump */
|
|
|
|
|
2004-01-23 02:23:51 +00:00
|
|
|
typedef struct jmp_buff_struct {
|
2010-12-16 01:22:10 +00:00
|
|
|
sigjmp_buf JmpBuff;
|
2001-04-09 20:54:03 +01:00
|
|
|
} JMPBUFF;
|
|
|
|
|
2013-04-25 23:15:04 +01:00
|
|
|
static void GNextToken( CACHE_TYPE1 );
|
2013-09-13 11:44:26 +01:00
|
|
|
static void checkfor(wchar_t, JMPBUFF * CACHE_TYPE);
|
2013-11-25 10:24:13 +00:00
|
|
|
static Term ParseArgs(read_data *, Atom, wchar_t, JMPBUFF *, Term CACHE_TYPE);
|
|
|
|
static Term ParseList(read_data *, JMPBUFF * CACHE_TYPE);
|
|
|
|
static Term ParseTerm(read_data *, int, JMPBUFF * CACHE_TYPE);
|
2004-01-23 02:23:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
#define TRY(S,P) \
|
|
|
|
{ Volatile JMPBUFF *saveenv, newenv; \
|
2011-05-23 16:19:47 +01:00
|
|
|
Volatile TokEntry *saveT=LOCAL_tokptr; \
|
2014-01-19 21:15:05 +00:00
|
|
|
Volatile CELL *saveH=HR; \
|
2004-01-23 02:23:51 +00:00
|
|
|
Volatile int savecurprio=curprio; \
|
|
|
|
saveenv=FailBuff; \
|
2010-12-16 01:22:10 +00:00
|
|
|
if(!sigsetjmp(newenv.JmpBuff, 0)) { \
|
2004-01-23 02:23:51 +00:00
|
|
|
FailBuff = &newenv; \
|
|
|
|
S; \
|
|
|
|
FailBuff=saveenv; \
|
|
|
|
P; \
|
|
|
|
} \
|
|
|
|
else { FailBuff=saveenv; \
|
2014-01-19 21:15:05 +00:00
|
|
|
HR=saveH; \
|
2004-01-23 02:23:51 +00:00
|
|
|
curprio = savecurprio; \
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_tokptr=saveT; \
|
2004-01-23 02:23:51 +00:00
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define TRY3(S,P,F) \
|
|
|
|
{ Volatile JMPBUFF *saveenv, newenv; \
|
2011-05-23 16:19:47 +01:00
|
|
|
Volatile TokEntry *saveT=LOCAL_tokptr; \
|
2014-01-19 21:15:05 +00:00
|
|
|
Volatile CELL *saveH=HR; \
|
2004-01-23 02:23:51 +00:00
|
|
|
saveenv=FailBuff; \
|
2010-12-16 01:22:10 +00:00
|
|
|
if(!sigsetjmp(newenv.JmpBuff, 0)) { \
|
2004-01-23 02:23:51 +00:00
|
|
|
FailBuff = &newenv; \
|
|
|
|
S; \
|
|
|
|
FailBuff=saveenv; \
|
|
|
|
P; \
|
|
|
|
} \
|
|
|
|
else { \
|
|
|
|
FailBuff=saveenv; \
|
2014-01-19 21:15:05 +00:00
|
|
|
HR=saveH; \
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_tokptr=saveT; \
|
2004-01-23 02:23:51 +00:00
|
|
|
F } \
|
|
|
|
}
|
|
|
|
|
2005-11-16 01:55:03 +00:00
|
|
|
|
2010-12-16 01:22:10 +00:00
|
|
|
#define FAIL siglongjmp(FailBuff->JmpBuff,1)
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
VarEntry *
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_LookupVar(char *var) /* lookup variable in variables table */
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2001-04-09 20:54:03 +01:00
|
|
|
VarEntry *p;
|
|
|
|
|
2013-11-13 17:04:34 +00:00
|
|
|
#if DEBUG
|
2011-05-25 16:40:36 +01:00
|
|
|
if (GLOBAL_Option[4])
|
2014-03-06 02:09:48 +00:00
|
|
|
fprintf(stderr,"[LookupVar %s]", var);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
if (var[0] != '_' || var[1] != '\0') {
|
2011-05-23 16:19:47 +01:00
|
|
|
VarEntry **op = &LOCAL_VarTable;
|
2001-04-09 20:54:03 +01:00
|
|
|
unsigned char *vp = (unsigned char *)var;
|
2003-10-06 14:49:38 +01:00
|
|
|
UInt hv;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2011-05-23 16:19:47 +01:00
|
|
|
p = LOCAL_VarTable;
|
2003-10-28 01:16:03 +00:00
|
|
|
hv = HashFunction(vp) % AtomHashTableSize;
|
2001-04-09 20:54:03 +01:00
|
|
|
while (p != NULL) {
|
|
|
|
CELL hpv = p->hv;
|
|
|
|
if (hv == hpv) {
|
|
|
|
Int scmp;
|
|
|
|
if ((scmp = strcmp(var, p->VarRep)) == 0) {
|
2013-11-18 12:57:09 +00:00
|
|
|
p->refs++;
|
2001-04-09 20:54:03 +01:00
|
|
|
return(p);
|
|
|
|
} else if (scmp < 0) {
|
|
|
|
op = &(p->VarLeft);
|
|
|
|
p = p->VarLeft;
|
|
|
|
} else {
|
|
|
|
op = &(p->VarRight);
|
|
|
|
p = p->VarRight;
|
|
|
|
}
|
|
|
|
} else if (hv < hpv) {
|
|
|
|
op = &(p->VarLeft);
|
|
|
|
p = p->VarLeft;
|
|
|
|
} else {
|
|
|
|
op = &(p->VarRight);
|
|
|
|
p = p->VarRight;
|
|
|
|
}
|
|
|
|
}
|
2002-11-18 18:18:05 +00:00
|
|
|
p = (VarEntry *) Yap_AllocScannerMemory(strlen(var) + sizeof(VarEntry));
|
2001-04-09 20:54:03 +01:00
|
|
|
*op = p;
|
|
|
|
p->VarLeft = p->VarRight = NULL;
|
|
|
|
p->hv = hv;
|
2013-11-18 12:57:09 +00:00
|
|
|
p->refs = 1L;
|
2001-04-09 20:54:03 +01:00
|
|
|
strcpy(p->VarRep, var);
|
|
|
|
} else {
|
|
|
|
/* anon var */
|
2002-11-18 18:18:05 +00:00
|
|
|
p = (VarEntry *) Yap_AllocScannerMemory(sizeof(VarEntry) + 2);
|
2011-05-23 16:19:47 +01:00
|
|
|
p->VarLeft = LOCAL_AnonVarTable;
|
|
|
|
LOCAL_AnonVarTable = p;
|
2001-04-09 20:54:03 +01:00
|
|
|
p->VarRight = NULL;
|
2013-11-18 12:57:09 +00:00
|
|
|
p->refs = 0L;
|
|
|
|
p->hv = 1L;
|
2001-04-09 20:54:03 +01:00
|
|
|
p->VarRep[0] = '_';
|
|
|
|
p->VarRep[1] = '\0';
|
|
|
|
}
|
|
|
|
p->VarAdr = TermNil;
|
|
|
|
return (p);
|
|
|
|
}
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
static Term
|
2011-03-07 16:02:55 +00:00
|
|
|
VarNames(VarEntry *p,Term l USES_REGS)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
if (p != NULL) {
|
|
|
|
if (strcmp(p->VarRep, "_") != 0) {
|
2012-10-08 18:25:17 +01:00
|
|
|
Term t[2];
|
|
|
|
Term o;
|
|
|
|
|
|
|
|
t[0] = MkAtomTerm(Yap_LookupAtom(p->VarRep));
|
|
|
|
t[1] = p->VarAdr;
|
|
|
|
o = Yap_MkApplTerm(FunctorEq, 2, t);
|
|
|
|
o = MkPairTerm(o, VarNames(p->VarRight,
|
|
|
|
VarNames(p->VarLeft,l PASS_REGS) PASS_REGS));
|
2014-01-19 21:15:05 +00:00
|
|
|
if (HR > ASP-4096) {
|
2006-01-02 02:16:19 +00:00
|
|
|
save_machine_regs();
|
2011-05-23 16:19:47 +01:00
|
|
|
siglongjmp(LOCAL_IOBotch,1);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
return(o);
|
|
|
|
} else {
|
2011-03-07 16:02:55 +00:00
|
|
|
return VarNames(p->VarRight,VarNames(p->VarLeft,l PASS_REGS) PASS_REGS);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return (l);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
Term
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_VarNames(VarEntry *p,Term l)
|
2002-11-11 17:38:10 +00:00
|
|
|
{
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
|
|
|
return VarNames(p,l PASS_REGS);
|
2002-11-11 17:38:10 +00:00
|
|
|
}
|
|
|
|
|
2013-11-18 12:57:09 +00:00
|
|
|
static Term
|
|
|
|
Singletons(VarEntry *p,Term l USES_REGS)
|
|
|
|
{
|
|
|
|
if (p != NULL) {
|
2013-11-21 22:48:27 +00:00
|
|
|
if (p->VarRep && p->VarRep[0] != '_' && p->refs == 1) {
|
2013-11-18 12:57:09 +00:00
|
|
|
Term t[2];
|
|
|
|
Term o;
|
|
|
|
|
|
|
|
t[0] = MkAtomTerm(Yap_LookupAtom(p->VarRep));
|
|
|
|
t[1] = p->VarAdr;
|
|
|
|
o = Yap_MkApplTerm(FunctorEq, 2, t);
|
|
|
|
o = MkPairTerm(o, Singletons(p->VarRight,
|
|
|
|
Singletons(p->VarLeft,l PASS_REGS) PASS_REGS));
|
2014-01-19 21:15:05 +00:00
|
|
|
if (HR > ASP-4096) {
|
2013-11-18 12:57:09 +00:00
|
|
|
save_machine_regs();
|
|
|
|
siglongjmp(LOCAL_IOBotch,1);
|
|
|
|
}
|
|
|
|
return(o);
|
|
|
|
} else {
|
|
|
|
return Singletons(p->VarRight,Singletons(p->VarLeft,l PASS_REGS) PASS_REGS);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return (l);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Term
|
|
|
|
Yap_Singletons(VarEntry *p,Term l)
|
|
|
|
{
|
|
|
|
CACHE_REGS
|
|
|
|
return Singletons(p,l PASS_REGS);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Term
|
|
|
|
Variables(VarEntry *p,Term l USES_REGS)
|
|
|
|
{
|
|
|
|
if (p != NULL) {
|
|
|
|
Term o;
|
|
|
|
o = MkPairTerm(p->VarAdr, Variables(p->VarRight,Variables(p->VarLeft,l PASS_REGS) PASS_REGS));
|
2014-01-19 21:15:05 +00:00
|
|
|
if (HR > ASP-4096) {
|
2013-11-18 12:57:09 +00:00
|
|
|
save_machine_regs();
|
|
|
|
siglongjmp(LOCAL_IOBotch,1);
|
|
|
|
}
|
|
|
|
return(o);
|
|
|
|
} else {
|
|
|
|
return (l);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Term
|
|
|
|
Yap_Variables(VarEntry *p,Term l)
|
|
|
|
{
|
|
|
|
CACHE_REGS
|
|
|
|
return Variables(p,l PASS_REGS);
|
|
|
|
}
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
static int
|
2011-03-07 16:02:55 +00:00
|
|
|
IsPrefixOp(Atom op,int *pptr, int *rpptr USES_REGS)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
int p;
|
|
|
|
|
2011-03-07 16:02:55 +00:00
|
|
|
OpEntry *opp = Yap_GetOpProp(op, PREFIX_OP PASS_REGS);
|
2009-11-20 00:33:14 +00:00
|
|
|
if (!opp)
|
|
|
|
return FALSE;
|
2005-10-21 17:09:03 +01:00
|
|
|
if (opp->OpModule &&
|
2011-09-20 11:36:49 +01:00
|
|
|
opp->OpModule != CurrentModule) {
|
|
|
|
READ_UNLOCK(opp->OpRWLock);
|
2005-10-21 17:09:03 +01:00
|
|
|
return FALSE;
|
2011-09-20 11:36:49 +01:00
|
|
|
}
|
2005-10-21 17:09:03 +01:00
|
|
|
if ((p = opp->Prefix) != 0) {
|
|
|
|
READ_UNLOCK(opp->OpRWLock);
|
2001-04-09 20:54:03 +01:00
|
|
|
*pptr = *rpptr = p & MaskPrio;
|
|
|
|
if (p & DcrrpFlag)
|
|
|
|
--* rpptr;
|
2005-10-21 17:09:03 +01:00
|
|
|
return TRUE;
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2005-10-21 17:09:03 +01:00
|
|
|
READ_UNLOCK(opp->OpRWLock);
|
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-11-20 00:33:14 +00:00
|
|
|
Yap_IsPrefixOp(Atom op,int *pptr, int *rpptr)
|
2002-11-11 17:38:10 +00:00
|
|
|
{
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
|
|
|
return IsPrefixOp(op,pptr,rpptr PASS_REGS);
|
2002-11-11 17:38:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-03-07 16:02:55 +00:00
|
|
|
IsInfixOp(Atom op, int *pptr, int *lpptr, int *rpptr USES_REGS)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
int p;
|
|
|
|
|
2011-03-07 16:02:55 +00:00
|
|
|
OpEntry *opp = Yap_GetOpProp(op, INFIX_OP PASS_REGS);
|
2009-11-20 00:33:14 +00:00
|
|
|
if (!opp)
|
|
|
|
return FALSE;
|
2005-10-21 17:09:03 +01:00
|
|
|
if (opp->OpModule &&
|
2011-09-20 11:36:49 +01:00
|
|
|
opp->OpModule != CurrentModule) {
|
|
|
|
READ_UNLOCK(opp->OpRWLock);
|
2005-10-21 17:09:03 +01:00
|
|
|
return FALSE;
|
2011-09-20 11:36:49 +01:00
|
|
|
}
|
2005-10-21 17:09:03 +01:00
|
|
|
if ((p = opp->Infix) != 0) {
|
|
|
|
READ_UNLOCK(opp->OpRWLock);
|
2001-04-09 20:54:03 +01:00
|
|
|
*pptr = *rpptr = *lpptr = p & MaskPrio;
|
|
|
|
if (p & DcrrpFlag)
|
|
|
|
--* rpptr;
|
|
|
|
if (p & DcrlpFlag)
|
|
|
|
--* lpptr;
|
2005-10-21 17:09:03 +01:00
|
|
|
return TRUE;
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2005-10-21 17:09:03 +01:00
|
|
|
READ_UNLOCK(opp->OpRWLock);
|
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-11-20 00:33:14 +00:00
|
|
|
Yap_IsInfixOp(Atom op, int *pptr, int *lpptr, int *rpptr)
|
2002-11-11 17:38:10 +00:00
|
|
|
{
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
|
|
|
return IsInfixOp(op, pptr, lpptr, rpptr PASS_REGS);
|
2002-11-11 17:38:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-03-07 16:02:55 +00:00
|
|
|
IsPosfixOp(Atom op, int *pptr, int *lpptr USES_REGS)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
int p;
|
2005-10-21 17:09:03 +01:00
|
|
|
|
2011-06-14 08:58:10 +01:00
|
|
|
OpEntry *opp = Yap_GetOpProp(op, POSFIX_OP PASS_REGS);
|
2009-11-20 00:33:14 +00:00
|
|
|
if (!opp)
|
|
|
|
return FALSE;
|
2005-10-21 17:09:03 +01:00
|
|
|
if (opp->OpModule &&
|
2011-09-20 11:36:49 +01:00
|
|
|
opp->OpModule != CurrentModule) {
|
|
|
|
READ_UNLOCK(opp->OpRWLock);
|
2005-10-21 17:09:03 +01:00
|
|
|
return FALSE;
|
2011-09-20 11:36:49 +01:00
|
|
|
}
|
2005-10-21 17:09:03 +01:00
|
|
|
if ((p = opp->Posfix) != 0) {
|
|
|
|
READ_UNLOCK(opp->OpRWLock);
|
2001-04-09 20:54:03 +01:00
|
|
|
*pptr = *lpptr = p & MaskPrio;
|
|
|
|
if (p & DcrlpFlag)
|
|
|
|
--* lpptr;
|
|
|
|
return (TRUE);
|
|
|
|
} else {
|
2005-10-21 17:09:03 +01:00
|
|
|
READ_UNLOCK(opp->OpRWLock);
|
2001-04-09 20:54:03 +01:00
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
int
|
2009-11-20 00:33:14 +00:00
|
|
|
Yap_IsPosfixOp(Atom op, int *pptr, int *lpptr)
|
2002-11-11 17:38:10 +00:00
|
|
|
{
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
|
|
|
return IsPosfixOp(op, pptr, lpptr PASS_REGS);
|
2002-11-11 17:38:10 +00:00
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
inline static void
|
2011-03-07 16:02:55 +00:00
|
|
|
GNextToken( USES_REGS1 )
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_tokptr->Tok == Ord(eot_tok))
|
2001-04-09 20:54:03 +01:00
|
|
|
return;
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_tokptr == LOCAL_toktide)
|
|
|
|
LOCAL_toktide = LOCAL_tokptr = LOCAL_tokptr->TokNext;
|
2001-04-09 20:54:03 +01:00
|
|
|
else
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_tokptr = LOCAL_tokptr->TokNext;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline static void
|
2013-09-13 11:44:26 +01:00
|
|
|
checkfor(wchar_t c, JMPBUFF *FailBuff USES_REGS)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_tokptr->Tok != Ord(Ponctuation_tok)
|
2013-09-13 11:44:26 +01:00
|
|
|
|| LOCAL_tokptr->TokInfo != (Term)c)
|
2001-04-09 20:54:03 +01:00
|
|
|
FAIL;
|
|
|
|
NextToken;
|
|
|
|
}
|
|
|
|
|
2013-11-25 10:24:13 +00:00
|
|
|
#ifdef O_QUASIQUOTATIONS
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
is_quasi_quotation_syntax(Term goal, ReadData _PL_rd, Atom *pat)
|
2013-12-02 14:49:41 +00:00
|
|
|
{ CACHE_REGS
|
2013-11-25 10:24:13 +00:00
|
|
|
Term m = CurrentModule, t;
|
|
|
|
Atom at;
|
|
|
|
UInt arity;
|
|
|
|
Functor f;
|
|
|
|
|
|
|
|
t = Yap_StripModule(goal, &m);
|
|
|
|
f = FunctorOfTerm( t );
|
|
|
|
*pat = at = NameOfFunctor( f );
|
|
|
|
arity = ArityOfFunctor( f );
|
|
|
|
if ( arity > 0 )
|
|
|
|
return TRUE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
get_quasi_quotation(term_t t, unsigned char **here, unsigned char *ein,
|
|
|
|
ReadData _PL_rd)
|
|
|
|
{ unsigned char *in, *start = *here;
|
|
|
|
|
|
|
|
for(in=start; in <= ein; in++)
|
|
|
|
{ if ( in[0] == '}' &&
|
|
|
|
in[-1] == '|' )
|
|
|
|
{ *here = in+1; /* after } */
|
|
|
|
in--; /* Before | */
|
|
|
|
|
|
|
|
if ( _PL_rd->quasi_quotations ) /* option; must return strings */
|
|
|
|
{ PL_chars_t txt;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
txt.text.t = (char*)start;
|
|
|
|
txt.length = in-start;
|
|
|
|
txt.storage = PL_CHARS_HEAP;
|
|
|
|
txt.encoding = ENC_UTF8;
|
|
|
|
txt.canonical = FALSE;
|
|
|
|
|
|
|
|
rc = PL_unify_text(t, 0, &txt, PL_CODE_LIST);
|
|
|
|
PL_free_text(&txt);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
} else
|
|
|
|
{ return PL_unify_term(t, PL_FUNCTOR, FUNCTOR_dquasi_quotation3,
|
|
|
|
PL_POINTER, _PL_rd,
|
|
|
|
PL_INTPTR, (intptr_t)(start),
|
|
|
|
PL_INTPTR, (intptr_t)(in-start));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE; //errorWarning("end_of_file_in_quasi_quotation", 0, _PL_rd);
|
|
|
|
}
|
|
|
|
#endif /*O_QUASIQUOTATIONS*/
|
|
|
|
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
static Term
|
2013-11-25 10:24:13 +00:00
|
|
|
ParseArgs(read_data *rd, Atom a, wchar_t close, JMPBUFF *FailBuff, Term arg1 USES_REGS)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
int nargs = 0;
|
|
|
|
Term *p, t;
|
2007-04-18 07:30:41 +01:00
|
|
|
Functor func;
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef SFUNC
|
2002-11-18 18:18:05 +00:00
|
|
|
SFEntry *pe = (SFEntry *) Yap_GetAProp(a, SFProperty);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
NextToken;
|
|
|
|
p = (Term *) ParserAuxSp;
|
2013-07-07 22:15:25 +01:00
|
|
|
if (arg1) {
|
|
|
|
*p = arg1;
|
|
|
|
nargs++;
|
|
|
|
ParserAuxSp = (char *)(p+1);
|
|
|
|
if (LOCAL_tokptr->Tok == Ord(Ponctuation_tok)
|
|
|
|
&& LOCAL_tokptr->TokInfo == close) {
|
|
|
|
|
|
|
|
func = Yap_MkFunctor(a, 1);
|
|
|
|
if (func == NULL) {
|
|
|
|
LOCAL_ErrorMessage = "Heap Overflow";
|
|
|
|
FAIL;
|
|
|
|
}
|
|
|
|
t = Yap_MkApplTerm(func, nargs, p);
|
2014-01-19 21:15:05 +00:00
|
|
|
if (HR > ASP-4096) {
|
2013-07-07 22:15:25 +01:00
|
|
|
LOCAL_ErrorMessage = "Stack Overflow";
|
|
|
|
return TermNil;
|
|
|
|
}
|
|
|
|
NextToken;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
while (1) {
|
|
|
|
Term *tp = (Term *)ParserAuxSp;
|
2011-05-23 16:19:47 +01:00
|
|
|
if (ParserAuxSp+1 > LOCAL_TrailTop) {
|
|
|
|
LOCAL_ErrorMessage = "Trail Overflow";
|
2004-10-28 21:12:23 +01:00
|
|
|
FAIL;
|
|
|
|
}
|
2013-11-25 10:24:13 +00:00
|
|
|
*tp++ = Unsigned(ParseTerm(rd, 999, FailBuff PASS_REGS));
|
2004-11-19 17:14:15 +00:00
|
|
|
ParserAuxSp = (char *)tp;
|
2001-04-09 20:54:03 +01:00
|
|
|
++nargs;
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_tokptr->Tok != Ord(Ponctuation_tok))
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
2011-05-23 16:19:47 +01:00
|
|
|
if (((int) LOCAL_tokptr->TokInfo) != ',')
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
NextToken;
|
|
|
|
}
|
2004-10-28 21:12:23 +01:00
|
|
|
ParserAuxSp = (char *)p;
|
2001-04-09 20:54:03 +01:00
|
|
|
/*
|
|
|
|
* Needed because the arguments for the functor are placed in reverse
|
|
|
|
* order
|
|
|
|
*/
|
2014-01-19 21:15:05 +00:00
|
|
|
if (HR > ASP-(nargs+1)) {
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_ErrorMessage = "Stack Overflow";
|
2002-10-29 03:10:00 +00:00
|
|
|
FAIL;
|
|
|
|
}
|
2007-04-18 07:30:41 +01:00
|
|
|
func = Yap_MkFunctor(a, nargs);
|
|
|
|
if (func == NULL) {
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_ErrorMessage = "Heap Overflow";
|
2007-04-18 07:30:41 +01:00
|
|
|
FAIL;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef SFUNC
|
|
|
|
if (pe)
|
2002-11-18 18:18:05 +00:00
|
|
|
t = MkSFTerm(Yap_MkFunctor(a, SFArity), nargs, p, pe->NilValue);
|
2001-04-09 20:54:03 +01:00
|
|
|
else
|
2002-11-18 18:18:05 +00:00
|
|
|
t = Yap_MkApplTerm(Yap_MkFunctor(a, nargs), nargs, p);
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2008-12-23 01:53:52 +00:00
|
|
|
if (a == AtomDBref && nargs == 2)
|
2004-02-13 18:39:29 +00:00
|
|
|
t = MkDBRefTerm((DBRef)IntegerOfTerm(p[0]));
|
|
|
|
else
|
2007-04-18 07:30:41 +01:00
|
|
|
t = Yap_MkApplTerm(func, nargs, p);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2014-01-19 21:15:05 +00:00
|
|
|
if (HR > ASP-4096) {
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_ErrorMessage = "Stack Overflow";
|
2005-11-22 12:42:39 +00:00
|
|
|
return TermNil;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
/* check for possible overflow against local stack */
|
2013-07-07 22:15:25 +01:00
|
|
|
checkfor(close, FailBuff PASS_REGS);
|
2005-11-22 12:42:39 +00:00
|
|
|
return t;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2013-09-13 15:02:00 +01:00
|
|
|
static Term MakeAccessor( Term t, Functor f USES_REGS )
|
2013-09-13 11:44:26 +01:00
|
|
|
{
|
|
|
|
UInt arity = ArityOfFunctor(FunctorOfTerm(t)), i;
|
|
|
|
Term tf[2], tl= TermNil;
|
|
|
|
|
|
|
|
tf[1] = ArgOfTerm(1, t);
|
|
|
|
for (i = arity; i > 1; i--) {
|
|
|
|
tl = MkPairTerm(ArgOfTerm(i, t), tl);
|
|
|
|
}
|
|
|
|
tf[0] = tl;
|
|
|
|
return Yap_MkApplTerm( f, 2, tf );
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
static Term
|
2013-11-25 10:24:13 +00:00
|
|
|
ParseList(read_data *rd, JMPBUFF *FailBuff USES_REGS)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2002-11-11 17:38:10 +00:00
|
|
|
Term o;
|
2002-10-28 17:46:55 +00:00
|
|
|
CELL *to_store;
|
2014-01-19 21:15:05 +00:00
|
|
|
o = AbsPair(HR);
|
2002-10-28 17:46:55 +00:00
|
|
|
loop:
|
2014-01-19 21:15:05 +00:00
|
|
|
to_store = HR;
|
|
|
|
HR+=2;
|
2013-11-25 10:24:13 +00:00
|
|
|
to_store[0] = ParseTerm(rd, 999, FailBuff PASS_REGS);
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_tokptr->Tok == Ord(Ponctuation_tok)) {
|
|
|
|
if (((int) LOCAL_tokptr->TokInfo) == ',') {
|
2001-04-09 20:54:03 +01:00
|
|
|
NextToken;
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_tokptr->Tok == Ord(Name_tok)
|
|
|
|
&& strcmp(RepAtom((Atom)(LOCAL_tokptr->TokInfo))->StrOfAE, "..") == 0) {
|
2001-04-09 20:54:03 +01:00
|
|
|
NextToken;
|
2013-11-25 10:24:13 +00:00
|
|
|
to_store[1] = ParseTerm(rd, 999, FailBuff PASS_REGS);
|
2002-10-28 17:46:55 +00:00
|
|
|
} else {
|
|
|
|
/* check for possible overflow against local stack */
|
2014-01-19 21:15:05 +00:00
|
|
|
if (HR > ASP-4096) {
|
2002-10-28 17:46:55 +00:00
|
|
|
to_store[1] = TermNil;
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_ErrorMessage = "Stack Overflow";
|
2002-10-28 17:46:55 +00:00
|
|
|
FAIL;
|
|
|
|
} else {
|
2014-01-19 21:15:05 +00:00
|
|
|
to_store[1] = AbsPair(HR);
|
2002-10-28 17:46:55 +00:00
|
|
|
goto loop;
|
|
|
|
}
|
|
|
|
}
|
2011-05-23 16:19:47 +01:00
|
|
|
} else if (((int) LOCAL_tokptr->TokInfo) == '|') {
|
2001-04-09 20:54:03 +01:00
|
|
|
NextToken;
|
2013-11-25 10:24:13 +00:00
|
|
|
to_store[1] = ParseTerm(rd, 999, FailBuff PASS_REGS);
|
2002-10-28 17:46:55 +00:00
|
|
|
} else {
|
|
|
|
to_store[1] = MkAtomTerm(AtomNil);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
FAIL;
|
2002-10-28 17:46:55 +00:00
|
|
|
return (o);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static Term
|
2013-11-25 10:24:13 +00:00
|
|
|
ParseTerm(read_data *rd, int prio, JMPBUFF *FailBuff USES_REGS)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
/* parse term with priority prio */
|
|
|
|
Volatile Term t;
|
|
|
|
Volatile Functor func;
|
|
|
|
Volatile VarEntry *varinfo;
|
|
|
|
Volatile int curprio = 0, opprio, oplprio, oprprio;
|
2009-11-20 00:33:14 +00:00
|
|
|
Volatile Atom opinfo;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2011-05-23 16:19:47 +01:00
|
|
|
switch (LOCAL_tokptr->Tok) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case Name_tok:
|
2011-05-23 16:19:47 +01:00
|
|
|
t = LOCAL_tokptr->TokInfo;
|
2001-04-09 20:54:03 +01:00
|
|
|
NextToken;
|
2011-04-26 18:51:02 +01:00
|
|
|
/* special rules apply for +1, -2.3, etc... */
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_tokptr->Tok == Number_tok) {
|
2011-04-26 18:51:02 +01:00
|
|
|
if ((Atom)t == AtomMinus) {
|
2011-05-23 16:19:47 +01:00
|
|
|
t = LOCAL_tokptr->TokInfo;
|
2011-04-26 18:51:02 +01:00
|
|
|
if (IsIntTerm(t))
|
|
|
|
t = MkIntTerm(-IntOfTerm(t));
|
|
|
|
else if (IsFloatTerm(t))
|
|
|
|
t = MkFloatTerm(-FloatOfTerm(t));
|
|
|
|
#ifdef USE_GMP
|
|
|
|
else if (IsBigIntTerm(t)) {
|
|
|
|
t = Yap_gmp_neg_big(t);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
else
|
|
|
|
t = MkLongIntTerm(-LongIntOfTerm(t));
|
|
|
|
NextToken;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-05-23 16:19:47 +01:00
|
|
|
if ((LOCAL_tokptr->Tok != Ord(Ponctuation_tok)
|
|
|
|
|| Unsigned(LOCAL_tokptr->TokInfo) != 'l')
|
2011-03-07 16:02:55 +00:00
|
|
|
&& IsPrefixOp((Atom)t, &opprio, &oprprio PASS_REGS)
|
2001-04-09 20:54:03 +01:00
|
|
|
) {
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_tokptr->Tok == Name_tok) {
|
|
|
|
Atom at = (Atom)LOCAL_tokptr->TokInfo;
|
2002-02-04 16:12:54 +00:00
|
|
|
#ifndef _MSC_VER
|
2011-04-26 18:51:02 +01:00
|
|
|
if ((Atom)t == AtomPlus) {
|
|
|
|
if (at == AtomInf) {
|
|
|
|
t = MkFloatTerm(INFINITY);
|
|
|
|
NextToken;
|
2001-06-19 17:31:59 +01:00
|
|
|
break;
|
2011-04-26 18:51:02 +01:00
|
|
|
} else if (at == AtomNan) {
|
|
|
|
t = MkFloatTerm(NAN);
|
|
|
|
NextToken;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if ((Atom)t == AtomMinus) {
|
|
|
|
if (at == AtomInf) {
|
|
|
|
t = MkFloatTerm(-INFINITY);
|
|
|
|
NextToken;
|
|
|
|
break;
|
|
|
|
} else if (at == AtomNan) {
|
|
|
|
t = MkFloatTerm(NAN);
|
|
|
|
NextToken;
|
|
|
|
break;
|
|
|
|
}
|
2001-06-19 17:31:59 +01:00
|
|
|
}
|
2002-02-04 16:12:54 +00:00
|
|
|
#endif
|
2011-04-26 18:51:02 +01:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
if (opprio <= prio) {
|
|
|
|
/* try to parse as a prefix operator */
|
|
|
|
TRY(
|
|
|
|
/* build appl on the heap */
|
2002-11-18 18:18:05 +00:00
|
|
|
func = Yap_MkFunctor((Atom) t, 1);
|
2007-04-18 07:30:41 +01:00
|
|
|
if (func == NULL) {
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_ErrorMessage = "Heap Overflow";
|
2007-04-18 07:30:41 +01:00
|
|
|
FAIL;
|
2010-02-19 14:16:57 +00:00
|
|
|
}
|
2013-11-25 10:24:13 +00:00
|
|
|
t = ParseTerm(rd, oprprio, FailBuff PASS_REGS);
|
2002-11-18 18:18:05 +00:00
|
|
|
t = Yap_MkApplTerm(func, 1, &t);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* check for possible overflow against local stack */
|
2014-01-19 21:15:05 +00:00
|
|
|
if (HR > ASP-4096) {
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_ErrorMessage = "Stack Overflow";
|
2001-04-09 20:54:03 +01:00
|
|
|
FAIL;
|
|
|
|
}
|
|
|
|
curprio = opprio;
|
|
|
|
,
|
|
|
|
break;
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_tokptr->Tok == Ord(Ponctuation_tok)
|
|
|
|
&& Unsigned(LOCAL_tokptr->TokInfo) == 'l')
|
2013-11-25 10:24:13 +00:00
|
|
|
t = ParseArgs(rd, (Atom) t, ')', FailBuff, 0L PASS_REGS);
|
2001-04-09 20:54:03 +01:00
|
|
|
else
|
|
|
|
t = MkAtomTerm((Atom)t);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Number_tok:
|
2011-05-23 16:19:47 +01:00
|
|
|
t = LOCAL_tokptr->TokInfo;
|
2001-04-09 20:54:03 +01:00
|
|
|
NextToken;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case String_tok: /* build list on the heap */
|
|
|
|
{
|
2011-05-23 16:19:47 +01:00
|
|
|
Volatile char *p = (char *) LOCAL_tokptr->TokInfo;
|
2013-12-02 14:49:41 +00:00
|
|
|
t = Yap_CharsToTDQ(p, CurrentModule PASS_REGS);
|
|
|
|
if (!t) {
|
|
|
|
FAIL;
|
2013-11-13 10:38:20 +00:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
NextToken;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2006-11-27 17:42:03 +00:00
|
|
|
case WString_tok: /* build list on the heap */
|
|
|
|
{
|
2011-05-23 16:19:47 +01:00
|
|
|
Volatile wchar_t *p = (wchar_t *) LOCAL_tokptr->TokInfo;
|
2013-12-02 14:49:41 +00:00
|
|
|
t = Yap_WCharsToTDQ(p, CurrentModule PASS_REGS);
|
|
|
|
if (!t) {
|
2012-10-16 08:44:26 +01:00
|
|
|
FAIL;
|
|
|
|
}
|
2006-11-27 17:42:03 +00:00
|
|
|
NextToken;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
case Var_tok:
|
2011-05-23 16:19:47 +01:00
|
|
|
varinfo = (VarEntry *) (LOCAL_tokptr->TokInfo);
|
2001-04-09 20:54:03 +01:00
|
|
|
if ((t = varinfo->VarAdr) == TermNil) {
|
|
|
|
t = varinfo->VarAdr = MkVarTerm();
|
|
|
|
}
|
|
|
|
NextToken;
|
|
|
|
break;
|
|
|
|
|
2002-11-19 17:10:45 +00:00
|
|
|
case Error_tok:
|
|
|
|
FAIL;
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
case Ponctuation_tok:
|
2011-05-23 16:19:47 +01:00
|
|
|
switch ((int) LOCAL_tokptr->TokInfo) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case '(':
|
|
|
|
case 'l': /* non solo ( */
|
|
|
|
NextToken;
|
2013-11-25 10:24:13 +00:00
|
|
|
t = ParseTerm(rd, 1200, FailBuff PASS_REGS);
|
2013-09-13 11:44:26 +01:00
|
|
|
checkfor(')', FailBuff PASS_REGS);
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
case '[':
|
|
|
|
NextToken;
|
2012-02-13 09:37:33 +00:00
|
|
|
if (LOCAL_tokptr->Tok == Ponctuation_tok &&
|
|
|
|
(int) LOCAL_tokptr->TokInfo == ']') {
|
|
|
|
t = TermNil;
|
|
|
|
NextToken;
|
|
|
|
break;
|
|
|
|
}
|
2013-11-25 10:24:13 +00:00
|
|
|
t = ParseList(rd, FailBuff PASS_REGS);
|
2013-09-13 11:44:26 +01:00
|
|
|
checkfor(']', FailBuff PASS_REGS);
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
case '{':
|
|
|
|
NextToken;
|
2012-02-13 09:37:33 +00:00
|
|
|
if (LOCAL_tokptr->Tok == Ponctuation_tok &&
|
|
|
|
(int) LOCAL_tokptr->TokInfo == '}') {
|
|
|
|
t = MkAtomTerm(AtomBraces);
|
|
|
|
NextToken;
|
|
|
|
break;
|
|
|
|
}
|
2013-11-25 10:24:13 +00:00
|
|
|
t = ParseTerm(rd, 1200, FailBuff PASS_REGS);
|
2009-12-03 22:51:29 +00:00
|
|
|
t = Yap_MkApplTerm(FunctorBraces, 1, &t);
|
|
|
|
/* check for possible overflow against local stack */
|
2014-01-19 21:15:05 +00:00
|
|
|
if (HR > ASP-4096) {
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_ErrorMessage = "Stack Overflow";
|
2009-12-03 22:51:29 +00:00
|
|
|
FAIL;
|
|
|
|
}
|
2013-09-13 11:44:26 +01:00
|
|
|
checkfor('}', FailBuff PASS_REGS);
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
FAIL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-11-25 10:24:13 +00:00
|
|
|
case QuasiQuotes_tok:
|
|
|
|
{
|
|
|
|
qq_t *qq = (qq_t *)(LOCAL_tokptr->TokInfo);
|
|
|
|
term_t pv, positions = rd->subtpos, to;
|
|
|
|
Atom at;
|
|
|
|
Term tn;
|
|
|
|
CELL *tnp;
|
|
|
|
|
|
|
|
// from SWI, enter the list
|
|
|
|
/* prepare (if we are the first in term) */
|
|
|
|
if ( !rd->varnames )
|
|
|
|
rd->varnames = PL_new_term_ref();
|
|
|
|
if ( !rd->qq )
|
|
|
|
{ if ( rd->quasi_quotations )
|
|
|
|
{ rd->qq = rd->quasi_quotations;
|
|
|
|
} else
|
|
|
|
{ if ( !(rd->qq = PL_new_term_ref()) )
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
// create positions term
|
|
|
|
if ( positions )
|
|
|
|
{ if ( !(pv = PL_new_term_refs(3)) ||
|
|
|
|
!PL_unify_term(positions,
|
|
|
|
PL_FUNCTOR, FUNCTOR_quasi_quotation_position5,
|
|
|
|
PL_INTPTR, qq->start.charno,
|
|
|
|
PL_VARIABLE,
|
|
|
|
PL_TERM, pv+0, // leave three open slots
|
|
|
|
PL_TERM, pv+1,
|
|
|
|
PL_TERM, pv+2) )
|
|
|
|
return FALSE;
|
|
|
|
} else
|
|
|
|
pv = 0;
|
|
|
|
/* push type */
|
|
|
|
|
|
|
|
if ( !(rd->qq_tail = PL_copy_term_ref(rd->qq)) )
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NextToken;
|
|
|
|
t = ParseTerm(rd, 1200, FailBuff PASS_REGS);
|
|
|
|
if (LOCAL_tokptr->Tok != QuasiQuotes_tok) {
|
|
|
|
FAIL;
|
|
|
|
}
|
|
|
|
if ( !( is_quasi_quotation_syntax(t, rd, &at)) )
|
|
|
|
FAIL;
|
|
|
|
/* Arg 2: the content */
|
|
|
|
tn = Yap_MkNewApplTerm( SWIFunctorToFunctor(FUNCTOR_quasi_quotation4), 4 );
|
|
|
|
tnp = RepAppl(tn)+1;
|
|
|
|
tnp[0] = MkAtomTerm(at);
|
|
|
|
if ( !get_quasi_quotation(Yap_InitSlot( ArgOfTerm(2, tn) PASS_REGS), &qq->text, qq->text+strlen((const char *)qq->text), rd) )
|
|
|
|
FAIL;
|
|
|
|
|
|
|
|
if ( positions )
|
|
|
|
{ intptr_t qqend = qq->end.charno;
|
|
|
|
|
|
|
|
// set_range_position(positions, -1, qqend PASS_LD);
|
|
|
|
if ( !PL_unify_term( Yap_InitSlot( ArgOfTerm(2, t) PASS_REGS),
|
|
|
|
PL_FUNCTOR, FUNCTOR_minus2,
|
|
|
|
PL_INTPTR, qq->mid.charno+2, /* end of | token */
|
|
|
|
PL_INTPTR, qqend-2) ) /* end minus "|}" */
|
|
|
|
FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
tnp[2] = Yap_GetFromSlot(rd->varnames PASS_REGS); /* Arg 3: the var dictionary */
|
|
|
|
/* Arg 4: the result */
|
|
|
|
t = ArgOfTerm(4, tn);
|
|
|
|
if ( !(to = PL_new_term_ref()) ||
|
|
|
|
!PL_unify_list(rd->qq_tail, to, rd->qq_tail) ||
|
|
|
|
!PL_unify(to, Yap_InitSlot(tn PASS_REGS)) )
|
|
|
|
FAIL;
|
|
|
|
}
|
|
|
|
NextToken;
|
|
|
|
break;
|
2001-04-09 20:54:03 +01:00
|
|
|
default:
|
|
|
|
|
|
|
|
FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* main loop to parse infix and posfix operators starts here */
|
|
|
|
while (TRUE) {
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_tokptr->Tok == Ord(Name_tok)
|
|
|
|
&& Yap_HasOp((Atom)(LOCAL_tokptr->TokInfo))) {
|
|
|
|
Atom save_opinfo = opinfo = (Atom)(LOCAL_tokptr->TokInfo);
|
2011-03-07 16:02:55 +00:00
|
|
|
if (IsInfixOp(save_opinfo, &opprio, &oplprio, &oprprio PASS_REGS)
|
2001-04-09 20:54:03 +01:00
|
|
|
&& opprio <= prio && oplprio >= curprio) {
|
|
|
|
/* try parsing as infix operator */
|
|
|
|
Volatile int oldprio = curprio;
|
|
|
|
TRY3(
|
2011-05-23 16:19:47 +01:00
|
|
|
func = Yap_MkFunctor((Atom) LOCAL_tokptr->TokInfo, 2);
|
2007-04-18 07:30:41 +01:00
|
|
|
if (func == NULL) {
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_ErrorMessage = "Heap Overflow";
|
2007-04-18 07:30:41 +01:00
|
|
|
FAIL;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
NextToken;
|
|
|
|
{
|
|
|
|
Term args[2];
|
|
|
|
args[0] = t;
|
2013-11-25 10:24:13 +00:00
|
|
|
args[1] = ParseTerm(rd, oprprio, FailBuff PASS_REGS);
|
2002-11-18 18:18:05 +00:00
|
|
|
t = Yap_MkApplTerm(func, 2, args);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* check for possible overflow against local stack */
|
2014-01-19 21:15:05 +00:00
|
|
|
if (HR > ASP-4096) {
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_ErrorMessage = "Stack Overflow";
|
2001-04-09 20:54:03 +01:00
|
|
|
FAIL;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
curprio = opprio;
|
|
|
|
opinfo = save_opinfo;
|
|
|
|
continue;
|
|
|
|
,
|
|
|
|
opinfo = save_opinfo;
|
|
|
|
curprio = oldprio;
|
|
|
|
)
|
|
|
|
}
|
2011-03-07 16:02:55 +00:00
|
|
|
if (IsPosfixOp(opinfo, &opprio, &oplprio PASS_REGS)
|
2001-04-09 20:54:03 +01:00
|
|
|
&& opprio <= prio && oplprio >= curprio) {
|
|
|
|
/* parse as posfix operator */
|
2011-05-23 16:19:47 +01:00
|
|
|
Functor func = Yap_MkFunctor((Atom) LOCAL_tokptr->TokInfo, 1);
|
2007-04-18 07:30:41 +01:00
|
|
|
if (func == NULL) {
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_ErrorMessage = "Heap Overflow";
|
2007-04-18 07:30:41 +01:00
|
|
|
FAIL;
|
|
|
|
}
|
|
|
|
t = Yap_MkApplTerm(func, 1, &t);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* check for possible overflow against local stack */
|
2014-01-19 21:15:05 +00:00
|
|
|
if (HR > ASP-4096) {
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_ErrorMessage = "Stack Overflow";
|
2001-04-09 20:54:03 +01:00
|
|
|
FAIL;
|
|
|
|
}
|
|
|
|
curprio = opprio;
|
|
|
|
NextToken;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_tokptr->Tok == Ord(Ponctuation_tok)) {
|
|
|
|
if (Unsigned(LOCAL_tokptr->TokInfo) == ',' &&
|
2001-04-09 20:54:03 +01:00
|
|
|
prio >= 1000 && curprio <= 999) {
|
|
|
|
Volatile Term args[2];
|
|
|
|
NextToken;
|
|
|
|
args[0] = t;
|
2013-11-25 10:24:13 +00:00
|
|
|
args[1] = ParseTerm(rd, 1000, FailBuff PASS_REGS);
|
2008-12-23 01:53:52 +00:00
|
|
|
t = Yap_MkApplTerm(FunctorComma, 2, args);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* check for possible overflow against local stack */
|
2014-01-19 21:15:05 +00:00
|
|
|
if (HR > ASP-4096) {
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_ErrorMessage = "Stack Overflow";
|
2001-04-09 20:54:03 +01:00
|
|
|
FAIL;
|
|
|
|
}
|
|
|
|
curprio = 1000;
|
|
|
|
continue;
|
2011-05-23 16:19:47 +01:00
|
|
|
} else if (Unsigned(LOCAL_tokptr->TokInfo) == '|' &&
|
2011-03-07 16:02:55 +00:00
|
|
|
IsInfixOp(AtomVBar, &opprio, &oplprio, &oprprio PASS_REGS)
|
2010-01-14 15:57:16 +00:00
|
|
|
&& opprio <= prio && oplprio >= curprio) {
|
2001-04-09 20:54:03 +01:00
|
|
|
Volatile Term args[2];
|
|
|
|
NextToken;
|
|
|
|
args[0] = t;
|
2013-11-25 10:24:13 +00:00
|
|
|
args[1] = ParseTerm(rd, oprprio, FailBuff PASS_REGS);
|
2002-11-18 18:18:05 +00:00
|
|
|
t = Yap_MkApplTerm(FunctorVBar, 2, args);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* check for possible overflow against local stack */
|
2014-01-19 21:15:05 +00:00
|
|
|
if (HR > ASP-4096) {
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_ErrorMessage = "Stack Overflow";
|
2001-04-09 20:54:03 +01:00
|
|
|
FAIL;
|
|
|
|
}
|
2010-01-14 15:57:16 +00:00
|
|
|
curprio = opprio;
|
2001-04-09 20:54:03 +01:00
|
|
|
continue;
|
2013-07-07 22:15:25 +01:00
|
|
|
} else if (Unsigned(LOCAL_tokptr->TokInfo) == '(' &&
|
|
|
|
IsPosfixOp(AtomEmptyBrackets, &opprio, &oplprio PASS_REGS)
|
|
|
|
&& opprio <= prio && oplprio >= curprio) {
|
2013-11-25 10:24:13 +00:00
|
|
|
t = ParseArgs(rd, AtomEmptyBrackets, ')', FailBuff, t PASS_REGS);
|
2013-07-07 22:15:25 +01:00
|
|
|
curprio = opprio;
|
|
|
|
continue;
|
|
|
|
} else if (Unsigned(LOCAL_tokptr->TokInfo) == '[' &&
|
|
|
|
IsPosfixOp(AtomEmptySquareBrackets, &opprio, &oplprio PASS_REGS)
|
|
|
|
&& opprio <= prio && oplprio >= curprio) {
|
2013-11-25 10:24:13 +00:00
|
|
|
t = ParseArgs(rd, AtomEmptySquareBrackets, ']', FailBuff, t PASS_REGS);
|
2013-09-13 15:02:00 +01:00
|
|
|
t = MakeAccessor(t, FunctorEmptySquareBrackets PASS_REGS);
|
2013-07-07 22:15:25 +01:00
|
|
|
curprio = opprio;
|
|
|
|
continue;
|
|
|
|
} else if (Unsigned(LOCAL_tokptr->TokInfo) == '{' &&
|
|
|
|
IsPosfixOp(AtomEmptyCurlyBrackets, &opprio, &oplprio PASS_REGS)
|
|
|
|
&& opprio <= prio && oplprio >= curprio) {
|
2013-11-25 10:24:13 +00:00
|
|
|
t = ParseArgs(rd, AtomEmptyCurlyBrackets, '}', FailBuff, t PASS_REGS);
|
2013-09-13 15:02:00 +01:00
|
|
|
t = MakeAccessor(t, FunctorEmptyCurlyBrackets PASS_REGS);
|
2013-07-07 22:15:25 +01:00
|
|
|
curprio = opprio;
|
|
|
|
continue;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_tokptr->Tok <= Ord(WString_tok))
|
2001-04-09 20:54:03 +01:00
|
|
|
FAIL;
|
|
|
|
break;
|
|
|
|
}
|
2013-11-13 17:04:34 +00:00
|
|
|
#if DEBUG
|
2011-05-25 16:40:36 +01:00
|
|
|
if (GLOBAL_Option['p' - 'a' + 1]) {
|
2011-05-04 10:11:41 +01:00
|
|
|
Yap_DebugPutc(LOCAL_c_error_stream,'[');
|
2009-05-22 19:24:27 +01:00
|
|
|
Yap_DebugPlWrite(t);
|
2011-05-04 10:11:41 +01:00
|
|
|
Yap_DebugPutc(LOCAL_c_error_stream,']');
|
|
|
|
Yap_DebugPutc(LOCAL_c_error_stream,'\n');
|
2005-11-16 02:45:48 +00:00
|
|
|
}
|
|
|
|
#endif
|
2005-11-16 01:55:03 +00:00
|
|
|
return t;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Term
|
2013-11-25 10:24:13 +00:00
|
|
|
Yap_Parse(read_data *rd)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2001-04-09 20:54:03 +01:00
|
|
|
Volatile Term t;
|
2004-01-23 02:23:51 +00:00
|
|
|
JMPBUFF FailBuff;
|
|
|
|
|
2010-12-16 01:22:10 +00:00
|
|
|
if (!sigsetjmp(FailBuff.JmpBuff, 0)) {
|
2013-11-25 10:24:13 +00:00
|
|
|
t = ParseTerm(rd, 1200, &FailBuff PASS_REGS);
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_tokptr->Tok != Ord(eot_tok))
|
2001-04-09 20:54:03 +01:00
|
|
|
return (0L);
|
|
|
|
return (t);
|
|
|
|
} else
|
|
|
|
return (0);
|
|
|
|
}
|
2014-09-15 19:10:49 +01:00
|
|
|
|
|
|
|
//! @}
|
|
|
|
|