cleanups on error handling and fix EOF.

This commit is contained in:
Vítor Santos Costa 2016-01-31 10:19:08 +00:00
parent 7491481e8e
commit fbf0c6bd0d

View File

@ -151,7 +151,7 @@ double-quoting. The implementation of YAP represents strings as
lists of integers. Since YAP 4.3.0 there is no static limit on string lists of integers. Since YAP 4.3.0 there is no static limit on string
size. size.
Escape sequences can be used to include the non-printable characters Escape sequences can be used anf include the non-printable characters
`a` (alert), `b` (backspace), `r` (carriage return), `a` (alert), `b` (backspace), `r` (carriage return),
`f` (form feed), `t` (horizontal tabulation), `n` (new `f` (form feed), `t` (horizontal tabulation), `n` (new
line), and `v` (vertical tabulation). Escape sequences also be line), and `v` (vertical tabulation). Escape sequences also be
@ -563,6 +563,46 @@ typedef struct scanner_extra_alloc {
void *filler; void *filler;
} ScannerExtraBlock; } ScannerExtraBlock;
static TokEntry *
CodeSpaceError(TokEntry *t, TokEntry *p, TokEntry *l)
{
LOCAL_Error_TYPE = RESOURCE_ERROR_HEAP;
LOCAL_ErrorMessage = "Code Space Overflow";
if (t) {
t->Tok = eot_tok;
t->TokInfo = TermOutOfHeapError;
}
/* serious error now */
return l;
}
static TokEntry *
TrailSpaceError(TokEntry *t, TokEntry *l)
{
LOCAL_ErrorMessage = "Trail Overflow";
LOCAL_Error_TYPE = RESOURCE_ERROR_TRAIL;
if (t) {
t->Tok = eot_tok;
t->TokInfo = TermOutOfTrailError;
}
return l;
}
static TokEntry *
AuxSpaceError(TokEntry *p, TokEntry *l, const char *msg)
{
/* huge atom or variable, we are in trouble */
LOCAL_ErrorMessage = (char *)msg;
LOCAL_Error_TYPE = RESOURCE_ERROR_AUXILIARY_STACK;
Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage);
if (p) {
p->Tok = eot_tok;
p->TokInfo = TermOutOfAuxspaceError;
}
/* serious error now */
return l;
}
static void InitScannerMemory(void) { static void InitScannerMemory(void) {
CACHE_REGS CACHE_REGS
LOCAL_ErrorMessage = NULL; LOCAL_ErrorMessage = NULL;
@ -1161,6 +1201,7 @@ Term Yap_scan_num(StreamDesc *inp) {
e->TokPos = GetCurInpPos(inp); e->TokPos = GetCurInpPos(inp);
e->TokNext = ef; e->TokNext = ef;
ef->Tok = Ord(kind = eot_tok); ef->Tok = Ord(kind = eot_tok);
ef->TokInfo = TermSyntaxError;
ef->TokPos = GetCurInpPos(inp); ef->TokPos = GetCurInpPos(inp);
ef->TokNext = NULL; ef->TokNext = NULL;
LOCAL_tokptr = tokptr; LOCAL_tokptr = tokptr;
@ -1180,8 +1221,10 @@ Term Yap_scan_num(StreamDesc *inp) {
LOCAL_ErrorMessage = "Stack Overflow"; \ LOCAL_ErrorMessage = "Stack Overflow"; \
LOCAL_Error_TYPE = RESOURCE_ERROR_STACK; \ LOCAL_Error_TYPE = RESOURCE_ERROR_STACK; \
LOCAL_Error_Size = 0L; \ LOCAL_Error_Size = 0L; \
if (p) \ if (p) { \
p->Tok = Ord(kind = eot_tok); \ p->Tok = Ord(kind = eot_tok); \
p->TokInfo = TermOutOfStackError; \
} \
/* serious error now */ \ /* serious error now */ \
return l; \ return l; \
} }
@ -1199,8 +1242,6 @@ const char *Yap_tokRep(TokEntry *tokptr) {
case Number_tok: case Number_tok:
if ((b = Yap_TermToString(info, buf, sze, &length, &LOCAL_encoding, if ((b = Yap_TermToString(info, buf, sze, &length, &LOCAL_encoding,
flags)) != buf) { flags)) != buf) {
if (b)
free(b);
return NULL; return NULL;
} }
return buf; return buf;
@ -1370,12 +1411,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
t = (TokEntry *)AllocScannerMemory(sizeof(TokEntry)); t = (TokEntry *)AllocScannerMemory(sizeof(TokEntry));
t->TokNext = NULL; t->TokNext = NULL;
if (t == NULL) { if (t == NULL) {
LOCAL_ErrorMessage = "Trail Overflow"; return TrailSpaceError(p, l);
LOCAL_Error_TYPE = RESOURCE_ERROR_TRAIL;
if (p)
p->Tok = Ord(kind = eot_tok);
/* serious error now */
return l;
} }
if (!l) if (!l)
l = t; l = t;
@ -1428,6 +1464,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
} else { } else {
t->Tok = Ord(kind = eot_tok); t->Tok = Ord(kind = eot_tok);
mark_eof(inp_stream); mark_eof(inp_stream);
t->TokInfo = TermEof;
} }
break; break;
@ -1445,14 +1482,8 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
for (; chtype(ch) <= NU; ch = getchr(inp_stream)) { for (; chtype(ch) <= NU; ch = getchr(inp_stream)) {
if (charp == (char *)AuxSp - 1024) { if (charp == (char *)AuxSp - 1024) {
huge_var_error: huge_var_error:
return AuxSpaceError(p, l, "Code Space Overflow due to huge atom");
/* huge atom or variable, we are in trouble */ /* huge atom or variable, we are in trouble */
LOCAL_ErrorMessage = "Code Space Overflow due to huge atom";
LOCAL_Error_TYPE = RESOURCE_ERROR_AUXILIARY_STACK;
Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage);
if (p)
p->Tok = Ord(kind = eot_tok);
/* serious error now */
return l;
} }
add_ch_to_buff(ch); add_ch_to_buff(ch);
} }
@ -1474,12 +1505,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
ae = Yap_LookupAtom(TokImage); ae = Yap_LookupAtom(TokImage);
} }
if (ae == NIL) { if (ae == NIL) {
LOCAL_Error_TYPE = RESOURCE_ERROR_HEAP; return CodeSpaceError(t, p, l);
LOCAL_ErrorMessage = "Code Space Overflow";
if (p)
t->Tok = Ord(kind = eot_tok);
/* serious error now */
return l;
} }
t->TokInfo = Unsigned(ae); t->TokInfo = Unsigned(ae);
Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage); Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage);
@ -1504,18 +1530,15 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
cherr = 0; cherr = 0;
if (!(ptr = AllocScannerMemory(4096))) { if (!(ptr = AllocScannerMemory(4096))) {
LOCAL_ErrorMessage = "Trail Overflow"; return TrailSpaceError(t, l);
LOCAL_Error_TYPE = RESOURCE_ERROR_TRAIL;
if (p)
t->Tok = Ord(kind = eot_tok);
/* serious error now */
return l;
} }
CHECK_SPACE(); CHECK_SPACE();
if ((t->TokInfo = get_num(&cha, &cherr, inp_stream, ptr, 4096, 1)) == if ((t->TokInfo = get_num(&cha, &cherr, inp_stream, ptr, 4096, 1)) ==
0L) { 0L) {
if (p) if (p) {
p->Tok = Ord(kind = eot_tok); p->Tok = eot_tok;
t->TokInfo = TermError;
}
/* serious error now */ /* serious error now */
return l; return l;
} }
@ -1527,12 +1550,8 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
t->TokPos = GetCurInpPos(inp_stream); t->TokPos = GetCurInpPos(inp_stream);
e = (TokEntry *)AllocScannerMemory(sizeof(TokEntry)); e = (TokEntry *)AllocScannerMemory(sizeof(TokEntry));
if (e == NULL) { if (e == NULL) {
LOCAL_ErrorMessage = "Trail Overflow"; return TrailSpaceError(p, l);
LOCAL_Error_TYPE = RESOURCE_ERROR_TRAIL;
if (p)
p->Tok = Ord(kind = eot_tok);
/* serious error now */
return l;
} else { } else {
e->TokNext = NULL; e->TokNext = NULL;
} }
@ -1556,12 +1575,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
t->TokPos = GetCurInpPos(inp_stream); t->TokPos = GetCurInpPos(inp_stream);
e2 = (TokEntry *)AllocScannerMemory(sizeof(TokEntry)); e2 = (TokEntry *)AllocScannerMemory(sizeof(TokEntry));
if (e2 == NULL) { if (e2 == NULL) {
LOCAL_ErrorMessage = "Trail Overflow"; return TrailSpaceError(p, l);
LOCAL_Error_TYPE = RESOURCE_ERROR_TRAIL;
if (p)
p->Tok = Ord(kind = eot_tok);
/* serious error now */
return l;
} else { } else {
e2->TokNext = NULL; e2->TokNext = NULL;
} }
@ -1587,11 +1601,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
t->TokPos = GetCurInpPos(inp_stream); t->TokPos = GetCurInpPos(inp_stream);
e2 = (TokEntry *)AllocScannerMemory(sizeof(TokEntry)); e2 = (TokEntry *)AllocScannerMemory(sizeof(TokEntry));
if (e2 == NULL) { if (e2 == NULL) {
LOCAL_ErrorMessage = "Trail Overflow"; return TrailSpaceError(p, l);
LOCAL_Error_TYPE = RESOURCE_ERROR_TRAIL;
t->Tok = Ord(kind = eot_tok);
/* serious error now */
return l;
} else { } else {
e2->TokNext = NULL; e2->TokNext = NULL;
} }
@ -1649,6 +1659,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage); Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage);
mark_eof(inp_stream); mark_eof(inp_stream);
t->Tok = Ord(kind = eot_tok); t->Tok = Ord(kind = eot_tok);
t->TokInfo = TermEof;
break; break;
} else { } else {
add_ch_to_buff(ch); add_ch_to_buff(ch);
@ -1657,13 +1668,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
++len; ++len;
if (charp > (char *)AuxSp - 1024) { if (charp > (char *)AuxSp - 1024) {
/* Not enough space to read in the string. */ /* Not enough space to read in the string. */
LOCAL_Error_TYPE = RESOURCE_ERROR_AUXILIARY_STACK; return AuxSpaceError(t, l, "not enough space to read in string or quoted atom");
LOCAL_ErrorMessage =
"not enough space to read in string or quoted atom";
/* serious error now */
Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage);
t->Tok = Ord(kind = eot_tok);
return l;
} }
} }
if (wcharp) { if (wcharp) {
@ -1682,6 +1687,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
"not enough heap space to read in string or quoted atom"; "not enough heap space to read in string or quoted atom";
Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage); Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage);
t->Tok = Ord(kind = eot_tok); t->Tok = Ord(kind = eot_tok);
t->TokInfo = TermOutOfHeapError;
return l; return l;
} }
if (wcharp) { if (wcharp) {
@ -1711,12 +1717,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
t->TokInfo = Unsigned(Yap_LookupAtom(TokImage)); t->TokInfo = Unsigned(Yap_LookupAtom(TokImage));
} }
if (!(t->TokInfo)) { if (!(t->TokInfo)) {
LOCAL_Error_TYPE = RESOURCE_ERROR_HEAP; return CodeSpaceError(t, p, l);
LOCAL_ErrorMessage = "Code Space Overflow";
if (p)
t->Tok = Ord(kind = eot_tok);
/* serious error now */
return l;
} }
Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage); Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage);
t->Tok = Ord(kind = Name_tok); t->Tok = Ord(kind = Name_tok);
@ -1728,6 +1729,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
case BS: case BS:
if (ch == '\0') { if (ch == '\0') {
t->Tok = Ord(kind = eot_tok); t->Tok = Ord(kind = eot_tok);
t->TokInfo = TermEof;
return l; return l;
} else } else
ch = getchr(inp_stream); ch = getchr(inp_stream);
@ -1740,8 +1742,12 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
if (och == '.') { if (och == '.') {
if (chtype(ch) == BS || chtype(ch) == EF || ch == '%') { if (chtype(ch) == BS || chtype(ch) == EF || ch == '%') {
t->Tok = Ord(kind = eot_tok); t->Tok = Ord(kind = eot_tok);
if (chtype(ch) == EF) if (chtype(ch) == EF) {
mark_eof(inp_stream); mark_eof(inp_stream);
t->TokInfo = TermEof;
} else {
t->TokInfo = TermNewLine;
}
return l; return l;
} }
} }
@ -1768,6 +1774,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
} }
if (chtype(ch) == EF) { if (chtype(ch) == EF) {
t->Tok = Ord(kind = eot_tok); t->Tok = Ord(kind = eot_tok);
t->TokInfo = TermEof;
break; break;
} else { } else {
/* leave comments */ /* leave comments */
@ -1787,8 +1794,12 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
enter_symbol: enter_symbol:
if (och == '.' && (chtype(ch) == BS || chtype(ch) == EF || ch == '%')) { if (och == '.' && (chtype(ch) == BS || chtype(ch) == EF || ch == '%')) {
t->Tok = Ord(kind = eot_tok); t->Tok = Ord(kind = eot_tok);
if (chtype(ch) == EF) if (chtype(ch) == EF) {
mark_eof(inp_stream); mark_eof(inp_stream);
t->TokInfo = TermEof;
} else {
t->TokInfo = TermNl;
}
return l; return l;
} else { } else {
Atom ae; Atom ae;
@ -1810,21 +1821,11 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
ae = Yap_LookupAtom(TokImage); ae = Yap_LookupAtom(TokImage);
} }
if (ae == NIL) { if (ae == NIL) {
LOCAL_Error_TYPE = RESOURCE_ERROR_HEAP; return CodeSpaceError(t, p, l);
LOCAL_ErrorMessage = "Code Space Overflow";
if (p)
t->Tok = Ord(kind = eot_tok);
/* serious error now */
return l;
} }
t->TokInfo = Unsigned(ae); t->TokInfo = Unsigned(ae);
if (t->TokInfo == (CELL)NIL) { if (t->TokInfo == (CELL)NIL) {
LOCAL_Error_TYPE = RESOURCE_ERROR_HEAP; return CodeSpaceError(t, p, l);
LOCAL_ErrorMessage = "Code Space Overflow";
if (p)
t->Tok = Ord(kind = eot_tok);
/* serious error now */
return l;
} }
Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage); Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage);
t->Tok = Ord(kind = Name_tok); t->Tok = Ord(kind = Name_tok);
@ -1882,12 +1883,14 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
LOCAL_ErrorMessage = "not enough heap space to read in quasi quote"; LOCAL_ErrorMessage = "not enough heap space to read in quasi quote";
Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage); Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage);
t->Tok = Ord(kind = eot_tok); t->Tok = Ord(kind = eot_tok);
t->TokInfo = TermOutOfHeapError;
return l; return l;
} }
if (cur_qq) { if (cur_qq) {
LOCAL_ErrorMessage = "quasi quote in quasi quote"; LOCAL_ErrorMessage = "quasi quote in quasi quote";
Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage); Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage);
t->Tok = Ord(kind = eot_tok); t->Tok = Ord(kind = eot_tok);
t->TokInfo = TermOutOfHeapError;
free(qq); free(qq);
return l; return l;
} else { } else {
@ -1925,6 +1928,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
free(cur_qq); free(cur_qq);
cur_qq = NULL; cur_qq = NULL;
t->Tok = Ord(kind = eot_tok); t->Tok = Ord(kind = eot_tok);
t->TokInfo = TermError;
return l; return l;
} }
cur_qq = NULL; cur_qq = NULL;
@ -1946,6 +1950,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
"not enough heap space to read in a quasi quoted atom"; "not enough heap space to read in a quasi quoted atom";
Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage); Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage);
t->Tok = Ord(kind = eot_tok); t->Tok = Ord(kind = eot_tok);
t->TokInfo = TermError;
return l; return l;
} }
charp = TokImage; charp = TokImage;
@ -1968,6 +1973,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage); Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage);
mark_eof(inp_stream); mark_eof(inp_stream);
t->Tok = Ord(kind = eot_tok); t->Tok = Ord(kind = eot_tok);
t->TokInfo = TermOutOfHeapError;
break; break;
} else { } else {
charp = (char *)put_utf8((unsigned char *)charp, ch); charp = (char *)put_utf8((unsigned char *)charp, ch);
@ -1975,13 +1981,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
} }
if (charp > (char *)AuxSp - 1024) { if (charp > (char *)AuxSp - 1024) {
/* Not enough space to read in the string. */ /* Not enough space to read in the string. */
LOCAL_Error_TYPE = RESOURCE_ERROR_AUXILIARY_STACK; return AuxSpaceError(t, l, "not enough space to read in string or quoted atom");
LOCAL_ErrorMessage =
"not enough space to read in string or quoted atom";
/* serious error now */
Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage);
t->Tok = Ord(kind = eot_tok);
return l;
} }
} }
len = charp - TokImage; len = charp - TokImage;
@ -1990,6 +1990,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
LOCAL_ErrorMessage = "not enough heap space to read in quasi quote"; LOCAL_ErrorMessage = "not enough heap space to read in quasi quote";
Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage); Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage);
t->Tok = Ord(kind = eot_tok); t->Tok = Ord(kind = eot_tok);
t->TokInfo = TermOutOfHeapError;
return l; return l;
} }
strncpy(mp, TokImage, len + 1); strncpy(mp, TokImage, len + 1);
@ -2004,12 +2005,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
qq->end.linepos = inp_stream->linepos - 1; qq->end.linepos = inp_stream->linepos - 1;
qq->end.charno = inp_stream->charcount - 1; qq->end.charno = inp_stream->charcount - 1;
if (!(t->TokInfo)) { if (!(t->TokInfo)) {
LOCAL_Error_TYPE = RESOURCE_ERROR_HEAP; return CodeSpaceError(t, p, l);
LOCAL_ErrorMessage = "Code Space Overflow";
if (p)
t->Tok = Ord(kind = eot_tok);
/* serious error now */
return l;
} }
Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage); Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage);
solo_flag = FALSE; solo_flag = FALSE;
@ -2021,6 +2017,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
case EF: case EF:
mark_eof(inp_stream); mark_eof(inp_stream);
t->Tok = Ord(kind = eot_tok); t->Tok = Ord(kind = eot_tok);
t->TokInfo = TermEof;
return l; return l;
default: default:
@ -2032,6 +2029,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
#endif #endif
} }
t->Tok = Ord(kind = eot_tok); t->Tok = Ord(kind = eot_tok);
t->TokInfo = TermEof;
} }
#if DEBUG #if DEBUG
if (GLOBAL_Option[2]) if (GLOBAL_Option[2])
@ -2041,11 +2039,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
/* insert an error token to inform the system of what happened */ /* insert an error token to inform the system of what happened */
TokEntry *e = (TokEntry *)AllocScannerMemory(sizeof(TokEntry)); TokEntry *e = (TokEntry *)AllocScannerMemory(sizeof(TokEntry));
if (e == NULL) { if (e == NULL) {
LOCAL_ErrorMessage = "Trail Overflow"; return TrailSpaceError(p, l);
LOCAL_Error_TYPE = RESOURCE_ERROR_TRAIL;
p->Tok = Ord(kind = eot_tok);
/* serious error now */
return l;
} }
p->TokNext = e; p->TokNext = e;
e->Tok = Error_tok; e->Tok = Error_tok;