static foreign_t turtle_read_relative_uri(term_t C0, term_t Stream, term_t C, term_t Value) { int c; charbuf b; IOSTREAM *in; if ( !PL_get_integer(C0, &c) ) return type_error(C0, "code"); if ( c != '<' ) return FALSE; if ( !PL_get_stream_handle(Stream, &in) ) return FALSE; init_charbuf(&b); c = Sgetcode(in); for(; ; c = Sgetcode(in)) { if ( c == '>' ) { int rc; c = Sgetcode(in); rc = (PL_unify_integer(C, c) && PL_unify_wchars(Value, PL_ATOM, b.here-b.base, b.base)); PL_release_stream(in); free_charbuf(&b); return rc; } else if ( c == '\\' ) { int esc; c = Sgetcode(in); if ( c == '>' ) { add_charbuf(&b, c); } else if ( string_escape(in, c, &esc) ) { add_charbuf(&b, esc); } else { free_charbuf(&b); PL_release_stream(in); return FALSE; } } else if ( c == -1 ) { free_charbuf(&b); PL_release_stream(in); return syntax_error("eof_in_uri", in); } else { add_charbuf(&b, c); } } }
static foreign_t turtle_read_name(term_t C0, term_t Stream, term_t C, term_t Name) { int c; charbuf b; IOSTREAM *in; if ( !PL_get_integer(C0, &c) ) return type_error(C0, "code"); if ( !wcis_name_start_char(c) ) return FALSE; if ( !PL_get_stream_handle(Stream, &in) ) return FALSE; init_charbuf(&b); add_charbuf(&b, c); for(;;) { int c = Sgetcode(in); if ( wcis_name_char(c) ) { add_charbuf(&b, c); } else { int rc = ( PL_unify_integer(C, c) && PL_unify_wchars(Name, PL_ATOM, b.here-b.base, b.base) ); free_charbuf(&b); PL_release_stream(in); return rc; } } }
static int read_hN(IOSTREAM *in, int digits, int *value) { int d = digits; int v = 0; while ( d-- > 0 ) { int c = Sgetcode(in); if ( c >= '0' && c <= '9' ) v = (v<<4) + c - '0'; else if ( c >= 'A' && c <= 'F' ) v = (v<<4) + c + 10 - 'A'; else if ( c >= 'a' && c <= 'f' ) v = (v<<4) + c + 10 - 'a'; else { if ( digits == 4 ) return syntax_error("Illegal \\uNNNN in string", in); else return syntax_error("Illegal \\UNNNNNNNN in string", in); } } *value = v; return TRUE; }
static int syntax_error(IOSTREAM *in, const char *msg) { term_t ex = PL_new_term_refs(2); IOPOS *pos; if ( !PL_unify_term(ex+0, PL_FUNCTOR, FUNCTOR_syntax_error1, PL_CHARS, msg) ) return FALSE; if ( (pos=in->position) ) { term_t stream; if ( !(stream = PL_new_term_ref()) || !PL_unify_stream(stream, in) || !PL_unify_term(ex+1, PL_FUNCTOR, FUNCTOR_stream4, PL_TERM, stream, PL_INT, (int)pos->lineno, PL_INT, (int)(pos->linepos-1), /* one too late */ PL_INT64, (int64_t)(pos->charno-1)) ) return FALSE; } if ( PL_cons_functor_v(ex, FUNCTOR_error2, ex) ) { int c; do { c = Sgetcode(in); } while(c != '\n' && c != -1); return PL_raise_exception(ex); } return FALSE; }
static foreign_t turtle_read_string(term_t C0, term_t Stream, term_t C, term_t Value) { int c; charbuf b; IOSTREAM *in; int endlen = 1; if ( !PL_get_integer(C0, &c) ) return type_error(C0, "code"); if ( c != '"' ) return FALSE; if ( !PL_get_stream_handle(Stream, &in) ) return FALSE; init_charbuf(&b); c = Sgetcode(in); if ( c == '"' ) { c = Sgetcode(in); if ( c == '"' ) /* """...""" */ { endlen = 3; c = Sgetcode(in); } else { PL_release_stream(in); return (PL_unify_integer(C, c) && PL_unify_atom(Value, ATOM_)); } } for(;;c = Sgetcode(in)) { if ( c == -1 ) { free_charbuf(&b); PL_release_stream(in); return syntax_error("eof_in_string", in); } else if ( c == '"' ) { int count = 1; for(count=1; count<endlen; ) { if ( (c=Sgetcode(in)) == '"' ) count++; else break; } if ( count == endlen ) { int rc; c = Sgetcode(in); rc = (PL_unify_integer(C, c) && PL_unify_wchars(Value, PL_ATOM, b.here-b.base, b.base)); free_charbuf(&b); PL_release_stream(in); return rc; } while(count-- > 0) add_charbuf(&b, '"'); add_charbuf(&b, c); } else if ( c == '\\' ) { int esc; c = Sgetcode(in); if ( !string_escape(in, c, &esc) ) { free_charbuf(&b); PL_release_stream(in); return FALSE; } add_charbuf(&b, esc); } else { add_charbuf(&b, c); } } }