56#if _MSC_VER || defined(__MINGW32__)
74#define my_isxdigit(C, SU, SL) \
75 (chtype(C) == NU || (C >= 'A' && C <= (SU)) || (C >= 'a' && C <= (SL)))
76#define my_isupper(C) (C >= 'A' && C <= 'Z')
77#define my_islower(C) (C >= 'a' && C <= 'z')
79static Term float_send(
char *,
int);
80static Term get_num(
int *,
int *,
struct stream_desc *,
int,
char **,
size_t *);
86static void Yap_setCurrentSourceLocation(
struct stream_desc *s) {
89 if (s->status & Socket_Stream_f)
90 LOCAL_SourceFileName = AtomSocket;
93 if (s->status & Pipe_Stream_f)
94 LOCAL_SourceFileName = AtomPipe;
95 else if (s->status & InMemory_Stream_f)
96 LOCAL_SourceFileName = s->name;
98 LOCAL_SourceFileName = s->name;
99 LOCAL_SourceFileLineno = s->linecount;
103char_kind_t Yap_chtype0[NUMBER_OF_CHARS + 1] = {
108 BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS,
112 BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS,
115 BS, SL, DC, SY, SY, CC, SY, QT, BK, BK, SY, SY, BK, SY, SY, SY,
118 NU, NU, NU, NU, NU, NU, NU, NU, NU, NU, SY, SL, SY, SY, SY, SY,
121 SY, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC,
124 UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, BK, SY, BK, SY, UL,
127 SY, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC,
130 LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, BK, BK, BK, SY, BS,
134 BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS,
138 BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS,
141 BS, SY, SY, SY, SY, SY, SY, SY, SY, SY, LC, SY, SY, SY, SY, SY,
144 SY, SY, LC, LC, SY, SY, SY, SY, SY, LC, LC, SY, SY, SY, SY, SY,
147 UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC,
151 UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, LC,
153 UC, UC, UC, UC, UC, UC, UC, SY, UC, UC, UC, UC, UC, UC, UC, LC,
156 LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC,
161 LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC
163 LC, LC, LC, LC, LC, LC, LC, SY, LC, LC, LC, LC, LC, LC, LC, LC
174 int sno = inp - GLOBAL_Stream;
181static inline int getchrq(
struct stream_desc *inp) {
192#define CodeSpaceError(t, p, l) CodeSpaceError__(t, p, l PASS_REGS)
195 LOCAL_Error_TYPE = RESOURCE_ERROR_HEAP;
196 LOCAL_ErrorMessage =
"Code Space Overflow";
199 t->TokInfo = TermOutOfHeapError;
205#define TrailSpaceError(t, l) TrailSpaceError__(t, l PASS_REGS)
207 LOCAL_ErrorMessage =
"Trail Overflow";
208 LOCAL_Error_TYPE = RESOURCE_ERROR_TRAIL;
211 t->TokInfo = TermOutOfTrailError;
215extern double atof(
const char *);
217static Term float_send(
char *s,
int sign) {
218 Float f = (Float)(sign * atof(s));
219#if HAVE_ISFINITE || defined(isfinite)
220 if (trueGlobalPrologFlag(ISO_FLAG)) {
223 LOCAL_ErrorMessage =
"Float overflow while scanning";
224 return (MkEvalFl(f));
228 if (trueGlobalPrologFlag(ISO_FLAG)) {
230 LOCAL_ErrorMessage =
"Float overflow while scanning";
231 return (MkEvalFl(f));
237 return MkFloatTerm(f);
242static Term read_int_overflow(
const char *s, Int base, Int val,
int sign) {
248 mpz_init_set_str(
new, s, base);
251 t = Yap_MkBigIntTerm(
new);
257 return MkIntegerTerm(val);
261static wchar_t read_quoted_char(
int *scan_nextp,
struct stream_desc *st) {
277 while (chtype((ch = getchrq(st))) == BS)
303 for (i = 0; i < 4; i++) {
305 if (ch >=
'0' && ch <=
'9') {
306 wc += (ch -
'0') << ((3 - i) * 4);
307 }
else if (ch >=
'a' && ch <=
'f') {
308 wc += ((ch -
'a') + 10) << ((3 - i) * 4);
309 }
else if (ch >=
'A' && ch <=
'F') {
310 wc += ((ch -
'A') + 10) << ((3 - i) * 4);
312 return Yap_encoding_error(wc, 1, st);
321 for (i = 0; i < 8; i++) {
323 if (ch >=
'0' && ch <=
'9') {
324 wc += (ch -
'0') << ((7 - i) * 4);
325 }
else if (ch >=
'a' && ch <=
'f') {
326 wc += ((ch -
'a') + 10) << ((7 - i) * 4);
327 }
else if (ch >=
'A' && ch <=
'F') {
328 wc += ((ch -
'A') + 10) << ((7 - i) * 4);
330 return Yap_encoding_error(wc, 1, st);
338 return Yap_encoding_error(ch, 1, st);
346 if (trueGlobalPrologFlag(ISO_FLAG)) {
347 return Yap_encoding_error(ch, 1, st);
352 }
else if (ch >=
'a' && ch <
'z') {
354 }
else if (ch >=
'A' && ch <
'Z') {
371 unsigned char so_far = ch -
'0';
373 if (ch >=
'0' && ch <
'8') {
374 so_far = so_far * 8 + (ch -
'0');
376 if (ch >=
'0' && ch <
'8') {
377 so_far = so_far * 8 + (ch -
'0');
380 return Yap_encoding_error(ch, 1, st);
383 }
else if (ch ==
'\\') {
386 return Yap_encoding_error(ch, 1, st);
388 }
else if (ch ==
'\\') {
391 return Yap_encoding_error(ch, 1, st);
397 unsigned char so_far = 0;
399 if (my_isxdigit(ch,
'f',
'F')) {
401 so_far * 16 + (chtype(ch) == NU
403 : (my_isupper(ch) ? ch -
'A' : ch -
'a') + 10);
405 if (my_isxdigit(ch,
'f',
'F')) {
407 so_far * 16 + (chtype(ch) == NU
409 : (my_isupper(ch) ? ch -
'A' : ch -
'a') + 10);
414 return Yap_encoding_error(ch, 1, st);
416 }
else if (ch ==
'\\') {
419 return Yap_encoding_error(ch, 1, st);
421 }
else if (ch ==
'\\') {
424 return Yap_encoding_error(ch, 1, st);
431 return Yap_encoding_error(ch, 1, st);
435#define number_overflow() \
437 imgsz = Yap_Min(imgsz * 2, imgsz); \
439 nbuf = Realloc(buf, imgsz); \
440 left = imgsz - max_size; \
447static Term get_num(
int *chp,
int *chbuffp,
StreamDesc *st,
int sign,
448 char **bufp,
size_t *szp) {
450 Int val = 0L, base = ch -
'0';
451 int might_be_float = TRUE, has_overflow = FALSE;
452 const unsigned char *decimalpoint;
453 char *buf0 = *bufp, *sp = buf0, *buf = buf0;
454 size_t imgsz = *szp, max_size = imgsz, left = max_size - 2;
462 if (chtype(ch) == NU) {
466 base = 10 * base + ch -
'0';
473 might_be_float = FALSE;
481 int scan_extra = TRUE;
484 Yap_GetModuleEntry(CurrentModule)->flags & M_CHARESCAPE) {
485 ascii = read_quoted_char(&scan_extra, st);
491 return MkIntegerTerm(-ascii);
493 return MkIntegerTerm(ascii);
494 }
else if (base >= 10 && base <= 36) {
495 int upper_case =
'A' - 11 + base;
496 int lower_case =
'a' - 11 + base;
498 while (my_isxdigit(ch, upper_case, lower_case)) {
501 (chtype(ch) == NU ? ch -
'0'
502 : (my_isupper(ch) ? ch -
'A' : ch -
'a') + 10);
506 val = oval * base + chval;
507 if (oval != (val - chval) / base)
508 has_overflow = (has_overflow || TRUE);
512 }
else if (ch ==
'x' && base == 0) {
513 might_be_float = FALSE;
518 if (!my_isxdigit(ch,
'F',
'f')) {
521 while (my_isxdigit(ch,
'F',
'f')) {
524 (chtype(ch) == NU ? ch -
'0'
525 : (my_isupper(ch) ? ch -
'A' : ch -
'a') + 10);
529 val = val * 16 + chval;
530 if (oval != (val - chval) / 16)
535 }
else if (ch ==
'o' && base == 0) {
536 might_be_float =
false;
539 if (ch < '0' || ch >
'7') {
542 }
else if (ch ==
'b' && base == 0) {
543 might_be_float =
false;
546 if (ch < '0' || ch >
'1') {
554 while (chtype(ch) == NU) {
556 if (!(val == 0 && ch ==
'0') || has_overflow) {
561 if (ch -
'0' >= base) {
564 return MkIntegerTerm(-val);
565 return MkIntegerTerm(val);
567 if (oval > Int_MAX / 10 - (ch -
'0'))
570 val = val * base + ch -
'0';
573 if (might_be_float && (ch ==
'.' || ch ==
'e' || ch ==
'E')) {
574 int has_dot = (ch ==
'.');
578 if (chtype(ch = getchr(st)) != NU) {
579 if (ch ==
'e' || ch ==
'E') {
580 if (trueGlobalPrologFlag(ISO_FLAG))
588 return read_int_overflow(buf, base, val, sign);
590 return MkIntegerTerm(-val);
591 return MkIntegerTerm(val);
595 if ((decimalpoint = (
unsigned char *)(localeconv()->decimal_point)) ==
598 decimalpoint = (
const unsigned char *)
".";
599 dp = (
unsigned char *)decimalpoint;
601 while ((dc = *dp++) !=
'\0') {
607 if (chtype(ch) == NU) {
612 }
while (chtype(ch = getchr(st)) == NU);
615 if (ch ==
'e' || ch ==
'E') {
625 }
else if (ch ==
'+') {
628 if (chtype(ch) != NU) {
631 return float_send(buf, sign);
632 return MkIntegerTerm(sign * val);
638 }
while (chtype(ch = getchr(st)) == NU);
642 return float_send(buf, sign);
643 }
else if (has_overflow) {
647 if (buf[0] ==
'0' && buf[1] ==
'x')
648 return read_int_overflow(buf + 2, 16, val, sign);
649 else if (buf[0] ==
'0' && buf[1] ==
'o')
650 return read_int_overflow(buf + 2, 8, val, sign);
651 else if (buf[0] ==
'0' && buf[1] ==
'b')
652 return read_int_overflow(buf + 2, 2, val, sign);
654 return read_int_overflow(buf + 2, base, val, sign);
656 return read_int_overflow(buf + 3, base, val, sign);
657 return read_int_overflow(buf, base, val, sign);
661 return MkIntegerTerm(val * sign);
672 int lvl = push_text_stack();
673 LOCAL_VarTable = LOCAL_AnonVarTable = NULL;
674 LOCAL_VarList = LOCAL_VarTail = NULL;
675 if (!(ptr =
Malloc(4096))) {
677 Yap_ThrowError(RESOURCE_ERROR_HEAP, TermNil,
"scanner: failed to allocate token image");
681 while (iswspace(ch = getchr(inp)))
684 while (isspace(ch = getchr(inp)))
688 tokptr->TokLine = GetCurInpLine(inp);
689 tokptr->TokPos = GetCurInpPos(inp);
690 tokptr->TokOffset = GetCurInpOffset(inp);
694 }
else if (ch ==
'+') {
697 if (chtype(ch) == NU) {
699 if (ASP - HR < 1024) {
701 Yap_ThrowError(RESOURCE_ERROR_STACK, TermNil,
"scanner: failed to allocate token image");
706 out = get_num(&ch, &cherr, inp, sign, &buf, &sz);
712 while (iswspace(ch = getchr(inp)))
715 while (isspace(ch = getchr(inp)))
718 if (ch == EOFCHAR || (ch ==
'.' &&
720 (iswspace(ch = getchr(inp)) || ch == EOFCHAR)
722 (isspace(ch = getchr(inp)) || ch == EOFCHAR)
731#define CHECK_SPACE() \
732 if (ASP - HR < 1024) { \
733 LOCAL_ErrorMessage = "Stack Overflow"; \
734 LOCAL_Error_TYPE = RESOURCE_ERROR_STACK; \
735 LOCAL_Error_Size = 0L; \
737 p->Tok = Ord(kind = eot_tok); \
738 p->TokInfo = TermOutOfStackError; \
750 Term info = tokptr->TokInfo;
752 switch (tokptr->Tok) {
757 info = MkAtomTerm((
Atom)info);
759 return Yap_MkApplTerm(Yap_MkFunctor(AtomAtom, 1), 1, &info);
760 case QuasiQuotes_tok:
761 info = MkAtomTerm(Yap_LookupAtom(
"<QQ>"));
762 return Yap_MkApplTerm(Yap_MkFunctor(AtomAtom, 1), 1, &info);
764 return Yap_MkApplTerm(Yap_MkFunctor(AtomNumber, 1), 1, &info);
769 if ((t[0] = varinfo->VarAdr) == TermNil) {
770 t[0] = varinfo->VarAdr = MkVarTerm();
772 t[1] = MkAtomTerm((
Atom)(varinfo->VarRep));
773 return Yap_MkApplTerm(Yap_MkFunctor(AtomGVar, 2), 2, t);
776 return Yap_MkApplTerm(Yap_MkFunctor(AtomString, 1), 1, &info);
778 return Yap_MkApplTerm(Yap_MkFunctor(AtomString, 1), 1, &info);
780 return MkAtomTerm(AtomError);
782 return MkAtomTerm(Yap_LookupAtom(
"EOT"));
783 case Ponctuation_tok:
785 return TermBeginBracket;
792const char *Yap_tokText(
void *tokptre) {
795 Term info = tokptr->TokInfo;
797 switch (tokptr->Tok) {
800 case Ponctuation_tok:
807 return AtomOfTerm(info)->StrOfAE;
808 case QuasiQuotes_tok:
811 if (IsIntegerTerm(info)) {
813 snprintf(s, 35, Int_FORMAT, IntegerOfTerm(info));
815 }
else if (IsFloatTerm(info)) {
817 snprintf(s, 63,
"%6g", FloatOfTerm(info));
820 size_t len = Yap_gmp_to_size(info, 10);
821 char *s =
Malloc(len + 2);
822 return Yap_gmp_to_string(info, s, len + 1, 10);
828 return ((
Atom)info)->StrOfAE;
833static void open_comment(
int ch,
StreamDesc *st USES_REGS) {
836 h0[0] = AbsAppl(h0 + 2);
838 if (!LOCAL_CommentsTail) {
840 LOCAL_Comments = AbsPair(h0);
843 *LOCAL_CommentsTail = AbsPair(h0);
845 LOCAL_CommentsTail = h0 + 1;
847 h0[0] = (CELL)FunctorMinus;
848 h0[1] = Yap_StreamPosition(st - GLOBAL_Stream);
850 LOCAL_CommentsNextChar = h0 + 2;
851 LOCAL_CommentsBuff = (
wchar_t *)
Malloc(1024 *
sizeof(
wchar_t));
852 LOCAL_CommentsBuffLim = 1024;
853 LOCAL_CommentsBuff[0] = ch;
854 LOCAL_CommentsBuffPos = 1;
857static void extend_comment(
int ch USES_REGS) {
858 LOCAL_CommentsBuff[LOCAL_CommentsBuffPos] = ch;
859 LOCAL_CommentsBuffPos++;
860 if (LOCAL_CommentsBuffPos == LOCAL_CommentsBuffLim - 1) {
861 LOCAL_CommentsBuff = (
wchar_t *)realloc(
862 LOCAL_CommentsBuff,
sizeof(
wchar_t) * (LOCAL_CommentsBuffLim + 4096));
863 LOCAL_CommentsBuffLim += 4096;
867static void close_comment(USES_REGS1) {
868 LOCAL_CommentsBuff[LOCAL_CommentsBuffPos] =
'\0';
869 *LOCAL_CommentsNextChar = Yap_WCharsToString(LOCAL_CommentsBuff PASS_REGS);
870 Free(LOCAL_CommentsBuff);
871 LOCAL_CommentsBuff = NULL;
872 LOCAL_CommentsBuffLim = 0;
878 st->status |= Push_Eof_Stream_f;
882#define safe_add_ch_to_buff(ch) charp += put_utf8(charp, ch);
884#define add_ch_to_buff(ch) \
886 if (ch == 10 && (trueGlobalPrologFlag(ISO_FLAG) || \
887 falseLocalPrologFlag(MULTILINE_QUOTED_TEXT_FLAG)))\
891 if (t) { t->Tok = Ord(kind = eot_tok); \
893 charp += put_utf8(charp, ch); }
895TokEntry *Yap_tokenizer(
void *st_,
void *params_) {
900 enum TokenKinds kind;
901 int solo_flag = TRUE;
902 int32_t ch, och =
' ';
906 char *TokImage =
Malloc(imgsz PASS_REGS);
909 LOCAL_VarTable = NULL;
910 LOCAL_AnonVarTable = NULL;
914 while (chtype(ch) == BS) {
918 params->tposOUTPUT = Yap_StreamPosition(st - GLOBAL_Stream);
919 Yap_setCurrentSourceLocation(st);
920 LOCAL_StartLineCount = st->linecount;
921 LOCAL_StartLinePos = st->linestart;
924 unsigned char *charp, *mp;
930 return CodeSpaceError(t, p, l);
938 while (chtype(ch) == BS) {
941 t->TokPos = GetCurInpPos(st);
942 t->TokLine = GetCurInpLine(st);
943 t->TokOffset = GetCurInpOffset(st);
945 switch (chtype(ch)) {
948 if (store_comments) {
949 open_comment(ch, st PASS_REGS);
951 while ((ch = getchr(st)) != 10 && chtype(ch) != EF) {
952 extend_comment(ch PASS_REGS);
954 extend_comment(ch PASS_REGS);
955 if (chtype(ch) != EF) {
957 if (chtype(ch) == CC) {
958 extend_comment(ch PASS_REGS);
959 goto continue_comment;
962 close_comment(PASS_REGS1);
964 while ((ch = getchr(st)) != 10 && chtype(ch) != EF)
967 if (chtype(ch) != EF) {
971 while (chtype(ch) == BS) {
974 params->tposOUTPUT = Yap_StreamPosition(st - GLOBAL_Stream);
975 Yap_setCurrentSourceLocation(st);
979 t->Tok = Ord(kind = eot_tok);
981 t->TokInfo = TermEof;
991 charp = (
unsigned char *)TokImage;
992 isvar = (chtype(och) != LC);
994 for (; chtype(ch) <= NU; ch = getchr(st)) {
995 if (charp == (
unsigned char *)TokImage + (imgsz - 1)) {
996 unsigned char *p0 = (
unsigned char *)TokImage;
997 imgsz = Yap_Min(imgsz * 2, imgsz + 1024 * 1024 * 1024);
998 TokImage = Realloc(p0, imgsz);
999 if (TokImage == NULL) {
1000 return CodeSpaceError(t, p, l);
1002 charp = (
unsigned char *)TokImage + (charp - p0);
1006 while (ch ==
'\'' && isvar && params->ce) {
1007 if (charp == (
unsigned char *)AuxSp - 1024) {
1008 return CodeSpaceError(t, p, l);
1010 safe_add_ch_to_buff(ch);
1013 add_ch_to_buff(
'\0');
1014 if (!isvar || (ch ==
'(' && params->vn_asfl) ||
1015 (TokImage[0] !=
'_' && params->vprefix)) {
1018 ae = Yap_LookupAtom(TokImage);
1020 return CodeSpaceError(t, p, l);
1022 t->TokInfo = MkAtomTerm(ae);
1025 t->Tok = Ord(kind = Name_tok);
1027 VarEntry *ve = Yap_LookupVar((
const char *)TokImage);
1028 t->TokInfo = Unsigned(ve);
1032 t->Tok = Ord(kind = Var_tok);
1046 if ((t->TokInfo = get_num(&cha, &cherr, st, sign, &TokImage, &imgsz)) ==
1048 if (t->TokInfo == 0) {
1057 t->Tok = Number_tok;
1058 t->TokPos = GetCurInpPos(st);
1059 t->TokLine = GetCurInpLine(st);
1060 t->TokOffset = GetCurInpOffset(st);
1063 return TrailSpaceError(p, l);
1083 t->Tok = Ord(Var_tok);
1084 t->TokInfo = (Term)Yap_LookupVar(
"E");
1085 t->TokPos = GetCurInpPos(st);
1086 t->TokLine = GetCurInpLine(st);
1087 t->TokOffset= GetCurInpOffset(st);
1090 return TrailSpaceError(p, l);
1109 if (chtype(ch) == NU) {
1119 t->TokInfo = MkAtomTerm(AtomE);
1120 t->TokLine = GetCurInpLine(st);
1121 t->TokPos = GetCurInpPos(st);
1122 t->TokOffset= GetCurInpOffset(st);
1125 return TrailSpaceError(p, l);
1143 t->Tok = Ord(kind = Number_tok);
1150 charp = (
unsigned char *)TokImage;
1156 if (charp > (
unsigned char *)TokImage + (imgsz - 1)) {
1157 size_t sz = charp - (
unsigned char *)TokImage;
1159 Realloc(TokImage, (imgsz = Yap_Min(imgsz * 2, imgsz + MBYTE)));
1160 if (TokImage == NULL) {
1161 return CodeSpaceError(t, p, l);
1163 charp = (
unsigned char *)TokImage + sz;
1166 if (ch == 10 && (trueGlobalPrologFlag(ISO_FLAG) ||
1167 trueLocalPrologFlag(MULTILINE_QUOTED_TEXT_FLAG))) {
1168 t->TokInfo = Yap_CharsToTDQ((
char *)TokImage, CurrentModule,
1169 LOCAL_encoding PASS_REGS);
1170 Yap_bad_nl_error(t->TokInfo, st);
1173 else if (ch == EOFCHAR) {
1176 else if (ch == quote) {
1182 }
else if (ch ==
'\\' &&
1183 Yap_GetModuleEntry(CurrentModule)->flags & M_CHARESCAPE) {
1184 int scan_next = TRUE;
1185 if ((ch = read_quoted_char(&scan_next, st))) {
1186 safe_add_ch_to_buff(ch);
1199 t->TokInfo = Yap_CharsToTDQ((
char *)TokImage, CurrentModule,
1200 LOCAL_encoding PASS_REGS);
1202 if (!(t->TokInfo)) {
1203 return CodeSpaceError(t, p, l);
1205 if (IsAtomTerm(t->TokInfo)) {
1206 t->Tok = Ord(kind = Name_tok);
1208 t->Tok = Ord(kind = String_tok);
1210 }
else if (quote ==
'`') {
1211 t->TokInfo = Yap_CharsToTBQ((
char *)TokImage, CurrentModule,
1212 LOCAL_encoding PASS_REGS);
1213 if (!(t->TokInfo)) {
1214 return CodeSpaceError(t, p, l);
1216 if (IsAtomTerm(t->TokInfo)) {
1217 t->Tok = Ord(kind = Name_tok);
1219 t->Tok = Ord(kind = String_tok);
1222 t->TokInfo = MkAtomTerm(Yap_LookupAtom(TokImage));
1223 if (!(t->TokInfo)) {
1224 return CodeSpaceError(t, p, l);
1226 t->Tok = Ord(kind = Name_tok);
1235 t->Tok = Ord(kind = eot_tok);
1236 pch = Yap_peek(st - GLOBAL_Stream);
1237 if (chtype(pch) == EF) {
1240 if (params->get_eot_blank)
1243 t->TokInfo = TermEof;
1250 if (ch ==
'.' && (pch = getchr(st)) &&
1251 (chtype(pch) == BS || chtype(pch) == EF || pch ==
'%')) {
1252 if (chtype(ch) != EF)
1254 t->Tok = Ord(kind = eot_tok);
1257 t->TokInfo = TermNewLine;
1272 if (chtype(ch) == BS || chtype(ch) == EF || ch ==
'%') {
1273 t->Tok = Ord(kind = eot_tok);
1275 t->TokInfo = TermNewLine;
1278 if (chtype(ch) == EF) {
1280 t->TokInfo = TermEof;
1282 t->TokInfo = TermNewLine;
1287 if (och ==
'/' && ch ==
'*') {
1288 if (store_comments) {
1290 open_comment(
'/', st PASS_REGS);
1291 while ((och !=
'*' || ch !=
'/') && chtype(ch) != EF) {
1294 extend_comment(ch PASS_REGS);
1297 if (chtype(ch) != EF) {
1299 extend_comment(ch PASS_REGS);
1301 close_comment(PASS_REGS1);
1303 while ((och !=
'*' || ch !=
'/') && chtype(ch) != EF) {
1308 if (chtype(ch) == EF) {
1309 t->Tok = Ord(kind = eot_tok);
1310 t->TokInfo = TermEof;
1317 while (chtype(ch) == BS) {
1321 params->tposOUTPUT = Yap_StreamPosition(st - GLOBAL_Stream);
1322 Yap_setCurrentSourceLocation(st);
1329 if (och ==
'.' && (chtype(ch) == BS || chtype(ch) == EF || ch ==
'%')) {
1330 t->Tok = Ord(kind = eot_tok);
1332 t->TokInfo = TermNewLine;
1335 if (chtype(ch) == EF) {
1337 t->TokInfo = TermEof;
1339 t->TokInfo = TermNl;
1344 charp = (
unsigned char *)TokImage;
1345 add_ch_to_buff(och);
1346 for (; chtype(ch) == SY; ch = getchr(st)) {
1347 if (charp >= (
unsigned char *)TokImage + (imgsz - 10)) {
1348 size_t sz = charp - (
unsigned char *)TokImage;
1349 imgsz = Yap_Min(imgsz * 2, imgsz + MBYTE);
1350 TokImage = Realloc(TokImage, imgsz);
1352 return CodeSpaceError(t, p, l);
1354 charp = (
unsigned char *)TokImage + sz;
1358 add_ch_to_buff(
'\0');
1359 ae = Yap_LookupAtom(TokImage);
1361 return CodeSpaceError(t, p, l);
1363 t->TokInfo = MkAtomTerm(ae);
1364 if (t->TokInfo == (CELL)NIL) {
1365 return CodeSpaceError(t, p, l);
1367 t->Tok = Ord(kind = Name_tok);
1376 unsigned char chs[2];
1380 t->TokInfo = MkAtomTerm(Yap_ULookupAtom(chs));
1381 t->Tok = Ord(kind = Name_tok);
1391 charp = chs = (
unsigned char *)TokImage;
1392 add_ch_to_buff(och);
1394 t->TokInfo = MkAtomTerm(Yap_ULookupAtom(chs));
1397 while (chtype(ch) == BS) {
1401 t->TokInfo = TermEmptyBrackets;
1402 t->Tok = Ord(kind = Name_tok);
1406 }
else if (!solo_flag) {
1410 }
else if (och ==
'[') {
1411 while (chtype(ch) == BS) {
1415 t->TokInfo = TermNil;
1416 t->Tok = Ord(kind = Name_tok);
1421 }
else if (och ==
'{') {
1425 LOCAL_ErrorMessage =
"not enough heap space to read in quasi quote";
1426 t->Tok = Ord(kind = eot_tok);
1427 t->TokInfo = TermOutOfHeapError;
1431 LOCAL_ErrorMessage =
"quasi quote in quasi quote";
1433 t->Tok = Ord(kind = eot_tok);
1434 t->TokInfo = TermOutOfHeapError;
1439 t->TokInfo = (CELL)qq;
1440 if (st->status & Seekable_Stream_f) {
1441 qq->start.byteno = fseek(st->file, 0, 0);
1443 qq->start.byteno = st->charcount - 1;
1445 qq->start.lineno = st->linecount;
1446 qq->start.linepos = st->charcount + 1 - st->linestart;
1447 qq->start.charno = st->charcount + 1;
1448 t->Tok = Ord(kind = QuasiQuotes_tok);
1453 while (chtype(ch) == BS) {
1457 t->TokInfo = TermBraces;
1458 t->Tok = Ord(kind = Name_tok);
1463 }
else if (och ==
'|' && ch ==
'|') {
1466 LOCAL_ErrorMessage =
"quasi quoted's || without {|";
1469 t->Tok = Ord(kind = eot_tok);
1470 t->TokInfo = TermError;
1474 t->TokInfo = (CELL)qq;
1475 if (st->status & Seekable_Stream_f) {
1476 qq->mid.byteno = fseek(st->file, 0, 0);
1478 qq->mid.byteno = st->charcount - 1;
1480 qq->mid.lineno = st->linecount;
1481 qq->mid.linepos = st->charcount+1-st->linestart;
1482 qq->mid.charno = st->charcount - 1;
1483 t->Tok = Ord(kind = QuasiQuotes_tok);
1485 charp = (
unsigned char *)TokImage;
1495 charp += put_utf8((
unsigned char *)charp, och);
1496 charp += put_utf8((
unsigned char *)charp, ch);
1500 }
else if (chtype(ch) == EF) {
1502 t->Tok = Ord(kind = eot_tok);
1503 t->TokInfo = TermOutOfHeapError;
1506 charp += put_utf8(charp, ch);
1510 len = charp - (
unsigned char *)TokImage;
1511 mp = malloc(len + 1);
1513 LOCAL_ErrorMessage =
"not enough heap space to read in quasi quote";
1514 t->Tok = Ord(kind = eot_tok);
1515 t->TokInfo = TermOutOfHeapError;
1518 strncpy((
char *)mp, (
const char *)TokImage, len + 1);
1519 qq->text = (
unsigned char *)mp;
1520 if (st->status & Seekable_Stream_f) {
1521 qq->end.byteno = fseek(st->file, 0, 0);
1523 qq->end.byteno = st->charcount - 1;
1525 qq->end.lineno = st->linecount;
1526 qq->end.linepos = st->charcount - st->linestart;
1527 qq->end.charno = st->charcount - 1;
1528 if (!(t->TokInfo)) {
1529 return CodeSpaceError(t, p, l);
1536 t->Tok = Ord(kind = Ponctuation_tok);
1540 t->Tok = Ord(kind = eot_tok);
1541 t->TokInfo = TermEof;
1547 snprintf(err, 1023,
"\n++++ token: unrecognised char %c (%d), type %c\n",
1548 ch, ch, chtype(ch));
1550 t->Tok = Ord(kind = eot_tok);
1551 t->TokInfo = TermEof;
1553 if (LOCAL_ErrorMessage) {
1557 return TrailSpaceError(p, l);
1561 e->TokInfo = MkAtomTerm(Yap_LookupAtom(LOCAL_ErrorMessage));
1562 e->TokPos = GetCurInpPos(st);
1563 e->TokLine = GetCurInpLine(st);
1564 e->TokOffset= GetCurInpOffset(st);
1566 LOCAL_ErrorMessage = NULL;
1569 }
while (kind != eot_tok);
1579 LOCAL_Comments = TermNil;
1580 LOCAL_CommentsNextChar = LOCAL_CommentsTail = NULL;
1581 if (LOCAL_CommentsBuff) {
1582 LOCAL_CommentsBuff = NULL;
1584 LOCAL_CommentsBuffLim = 0;
int Yap_symbol_encoding_error(YAP_Int ch, int code, struct stream_desc *st, const char *s)
This is a bug while encoding a symbol, and should always result in a syntax error.
Term Yap_scan_num(StreamDesc *inp)
This routine is used when we need to parse a string into a number.
void Yap_clean_tokenizer(void)
terminate scanning: just closes the comment store
Term Yap_tokRep(void *tokptre)
convert a token to text
void * Malloc(size_t sz USES_REGS)
allocate a temporary text block
int(* stream_wgetc)(int)
function the stream uses for reading an octet
int(* stream_wgetc_for_read)(int)
direct handle to stream in that space