don't consume char after '.', do peek instead.

early errors should leave early.
This commit is contained in:
Vítor Santos Costa 2016-02-14 04:11:55 +00:00
parent df0b3d3a0d
commit 92cb3e34eb

View File

@ -563,44 +563,38 @@ typedef struct scanner_extra_alloc {
void *filler; void *filler;
} ScannerExtraBlock; } ScannerExtraBlock;
static TokEntry * static TokEntry *CodeSpaceError(TokEntry *t, TokEntry *p, TokEntry *l) {
CodeSpaceError(TokEntry *t, TokEntry *p, TokEntry *l) LOCAL_Error_TYPE = RESOURCE_ERROR_HEAP;
{ LOCAL_ErrorMessage = "Code Space Overflow";
LOCAL_Error_TYPE = RESOURCE_ERROR_HEAP; if (t) {
LOCAL_ErrorMessage = "Code Space Overflow"; t->Tok = eot_tok;
if (t) { t->TokInfo = TermOutOfHeapError;
t->Tok = eot_tok; }
t->TokInfo = TermOutOfHeapError; /* serious error now */
} return l;
/* 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 * static TokEntry *TrailSpaceError(TokEntry *t, TokEntry *l) {
AuxSpaceError(TokEntry *p, TokEntry *l, const char *msg) LOCAL_ErrorMessage = "Trail Overflow";
{ LOCAL_Error_TYPE = RESOURCE_ERROR_TRAIL;
/* huge atom or variable, we are in trouble */ if (t) {
LOCAL_ErrorMessage = (char *)msg; t->Tok = eot_tok;
LOCAL_Error_TYPE = RESOURCE_ERROR_AUXILIARY_STACK; t->TokInfo = TermOutOfTrailError;
Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage); }
if (p) { return l;
p->Tok = eot_tok; }
p->TokInfo = TermOutOfAuxspaceError;
} static TokEntry *AuxSpaceError(TokEntry *p, TokEntry *l, const char *msg) {
/* serious error now */ /* huge atom or variable, we are in trouble */
return l; 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) {
@ -1200,7 +1194,7 @@ Term Yap_scan_num(StreamDesc *inp) {
e->TokInfo = MkAtomTerm(Yap_LookupAtom(LOCAL_ErrorMessage)); e->TokInfo = MkAtomTerm(Yap_LookupAtom(LOCAL_ErrorMessage));
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->TokInfo = TermSyntaxError;
ef->TokPos = GetCurInpPos(inp); ef->TokPos = GetCurInpPos(inp);
ef->TokNext = NULL; ef->TokNext = NULL;
@ -1221,10 +1215,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; \ p->TokInfo = TermOutOfStackError; \
} \ } \
/* serious error now */ \ /* serious error now */ \
return l; \ return l; \
} }
@ -1403,7 +1397,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
LOCAL_StartLineCount = inp_stream->linecount; LOCAL_StartLineCount = inp_stream->linecount;
LOCAL_StartLinePos = inp_stream->linepos; LOCAL_StartLinePos = inp_stream->linepos;
do { do {
wchar_t och; wchar_t och, pch;
int quote, isvar; int quote, isvar;
char *charp, *mp; char *charp, *mp;
unsigned int len; unsigned int len;
@ -1465,7 +1459,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; t->TokInfo = TermEof;
} }
break; break;
@ -1506,7 +1500,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) {
return CodeSpaceError(t, p, l); return CodeSpaceError(t, p, l);
} }
t->TokInfo = Unsigned(ae); t->TokInfo = Unsigned(ae);
Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage); Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage);
@ -1531,8 +1525,8 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
cherr = 0; cherr = 0;
if (!(ptr = AllocScannerMemory(4096))) { if (!(ptr = AllocScannerMemory(4096))) {
return TrailSpaceError(t, l); return TrailSpaceError(t, 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) {
@ -1551,7 +1545,7 @@ 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) {
return TrailSpaceError(p, l); return TrailSpaceError(p, l);
} else { } else {
e->TokNext = NULL; e->TokNext = NULL;
@ -1576,7 +1570,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) {
return TrailSpaceError(p, l); return TrailSpaceError(p, l);
} else { } else {
e2->TokNext = NULL; e2->TokNext = NULL;
} }
@ -1602,7 +1596,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) {
return TrailSpaceError(p, l); return TrailSpaceError(p, l);
} else { } else {
e2->TokNext = NULL; e2->TokNext = NULL;
} }
@ -1669,8 +1663,9 @@ 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. */
return AuxSpaceError(t, l, "not enough space to read in string or quoted atom"); return AuxSpaceError(
} t, l, "not enough space to read in string or quoted atom");
}
} }
if (wcharp) { if (wcharp) {
*wcharp = '\0'; *wcharp = '\0';
@ -1718,8 +1713,8 @@ 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)) {
return CodeSpaceError(t, p, l); return CodeSpaceError(t, p, l);
} }
Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage); Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage);
t->Tok = Ord(kind = Name_tok); t->Tok = Ord(kind = Name_tok);
if (ch == '(') if (ch == '(')
@ -1730,12 +1725,29 @@ 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);
if (chtype(pch) == EF) {
mark_eof(inp_stream);
t->TokInfo = TermEof;
} else {
t->TokInfo = TermNewLine;
}
t->TokInfo = TermEof; t->TokInfo = TermEof;
return l; return l;
} else } else
ch = getchr(inp_stream); ch = getchr(inp_stream);
break; break;
case SY: case SY:
if (ch == '.' && (pch = Yap_peek(inp_stream - GLOBAL_Stream)) &&
(chtype(pch) == BS || chtype(pch) == EF || pch == '%')) {
t->Tok = Ord(kind = eot_tok);
if (chtype(ch) == EF) {
mark_eof(inp_stream);
t->TokInfo = TermEof;
} else {
t->TokInfo = TermNewLine;
}
return l;
}
if (ch == '`') if (ch == '`')
goto quoted_string; goto quoted_string;
och = ch; och = ch;
@ -1747,7 +1759,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
mark_eof(inp_stream); mark_eof(inp_stream);
t->TokInfo = TermEof; t->TokInfo = TermEof;
} else { } else {
t->TokInfo = TermNewLine; t->TokInfo = TermNewLine;
} }
return l; return l;
} }
@ -1797,10 +1809,10 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
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; t->TokInfo = TermEof;
} else { } else {
t->TokInfo = TermNl; t->TokInfo = TermNl;
} }
return l; return l;
} else { } else {
Atom ae; Atom ae;
@ -1822,12 +1834,12 @@ 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) {
return CodeSpaceError(t, p, l); return CodeSpaceError(t, p, l);
} }
t->TokInfo = Unsigned(ae); t->TokInfo = Unsigned(ae);
if (t->TokInfo == (CELL)NIL) { if (t->TokInfo == (CELL)NIL) {
return CodeSpaceError(t, p, l); return CodeSpaceError(t, p, l);
} }
Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage); Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage);
t->Tok = Ord(kind = Name_tok); t->Tok = Ord(kind = Name_tok);
if (ch == '(') if (ch == '(')
@ -1891,7 +1903,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
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; t->TokInfo = TermOutOfHeapError;
free(qq); free(qq);
return l; return l;
} else { } else {
@ -1951,7 +1963,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; t->TokInfo = TermError;
return l; return l;
} }
charp = TokImage; charp = TokImage;
@ -1974,7 +1986,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; t->TokInfo = TermOutOfHeapError;
break; break;
} else { } else {
charp = (char *)put_utf8((unsigned char *)charp, ch); charp = (char *)put_utf8((unsigned char *)charp, ch);
@ -1982,7 +1994,8 @@ 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. */
return AuxSpaceError(t, l, "not enough space to read in string or quoted atom"); return AuxSpaceError(
t, l, "not enough space to read in string or quoted atom");
} }
} }
len = charp - TokImage; len = charp - TokImage;
@ -1991,7 +2004,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; t->TokInfo = TermOutOfHeapError;
return l; return l;
} }
strncpy(mp, TokImage, len + 1); strncpy(mp, TokImage, len + 1);
@ -2006,7 +2019,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)) {
return CodeSpaceError(t, p, l); return CodeSpaceError(t, p, l);
} }
Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage); Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage);
solo_flag = FALSE; solo_flag = FALSE;
@ -2018,19 +2031,19 @@ 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; t->TokInfo = TermEof;
return l; return l;
default: default: {
{ char err[1024];
char err[1024]; snprintf(err, 1023, "\n++++ token: unrecognised char %c (%d), type %c\n",
snprintf( err, 1023, "\n++++ token: unrecognised char %c (%d), type %c\n", ch, ch, chtype(ch) ); ch, ch, chtype(ch));
#if DEBUG #if DEBUG
fprintf(stderr, "%s", err); fprintf(stderr, "%s", err);
#endif #endif
} }
t->Tok = Ord(kind = eot_tok); t->Tok = Ord(kind = eot_tok);
t->TokInfo = TermEof; t->TokInfo = TermEof;
} }
#if DEBUG #if DEBUG
if (GLOBAL_Option[2]) if (GLOBAL_Option[2])
@ -2040,8 +2053,8 @@ 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) {
return TrailSpaceError(p, l); return TrailSpaceError(p, l);
} }
p->TokNext = e; p->TokNext = e;
e->Tok = Error_tok; e->Tok = Error_tok;
e->TokInfo = MkAtomTerm(Yap_LookupAtom(LOCAL_ErrorMessage)); e->TokInfo = MkAtomTerm(Yap_LookupAtom(LOCAL_ErrorMessage));