|
3 | 3 | Author: Jan Wielemaker
|
4 | 4 |
|
5 | 5 | WWW: http://www.swi-prolog.org
|
6 |
| - Copyright (c) 2007-2021, University of Amsterdam |
| 6 | + Copyright (c) 2007-2023, University of Amsterdam |
7 | 7 | VU University Amsterdam
|
8 | 8 | CWI, Amsterdam
|
9 | 9 | SWI-Prolog Solutions b.v.
|
|
387 | 387 | escape(0'r, _, 0'\r) :- !.
|
388 | 388 | escape(0't, _, 0'\t) :- !.
|
389 | 389 | escape(0'u, Stream, C) :-
|
390 |
| - !, |
391 |
| - get_code(Stream, C1), |
392 |
| - get_code(Stream, C2), |
393 |
| - get_code(Stream, C3), |
394 |
| - get_code(Stream, C4), |
395 |
| - code_type(C1, xdigit(D1)), |
396 |
| - code_type(C2, xdigit(D2)), |
397 |
| - code_type(C3, xdigit(D3)), |
398 |
| - code_type(C4, xdigit(D4)), |
| 390 | + get_XXXX(Stream, H), |
| 391 | + ( hi_surrogate(H) |
| 392 | + -> get_surrogate_tail(Stream, H, C) |
| 393 | + ; C = H |
| 394 | + ). |
| 395 | + |
| 396 | +get_XXXX(Stream, C) :- |
| 397 | + get_xdigit(Stream, D1), |
| 398 | + get_xdigit(Stream, D2), |
| 399 | + get_xdigit(Stream, D3), |
| 400 | + get_xdigit(Stream, D4), |
399 | 401 | C is D1<<12+D2<<8+D3<<4+D4.
|
400 | 402 |
|
| 403 | +get_xdigit(Stream, D) :- |
| 404 | + get_code(Stream, C), |
| 405 | + code_type(C, xdigit(D)), |
| 406 | + !. |
| 407 | +get_xdigit(Stream, _) :- |
| 408 | + syntax_error(hexdigit_expected, Stream). |
| 409 | + |
| 410 | +get_surrogate_tail(Stream, Hi, Codepoint) :- |
| 411 | + ( get_code(Stream, 0'\\), |
| 412 | + get_code(Stream, 0'u), |
| 413 | + get_XXXX(Stream, Lo), |
| 414 | + surrogate([Hi, Lo], Codepoint) |
| 415 | + -> true |
| 416 | + ; syntax_error(illegal_surrogate_pair, Stream) |
| 417 | + ). |
| 418 | + |
| 419 | + |
| 420 | +hi_surrogate(C) :- |
| 421 | + C >= 0xD800, C < 0xDC00. |
| 422 | + |
| 423 | +lo_surrogate(C) :- |
| 424 | + C >= 0xDC00, C < 0xE000. |
| 425 | + |
| 426 | +surrogate([Hi, Lo], Codepoint) :- |
| 427 | + hi_surrogate(Hi), |
| 428 | + lo_surrogate(Lo), |
| 429 | + Codepoint is (Hi - 0xD800) * 0x400 + (Lo - 0xDC00) + 0x10000. |
| 430 | + |
401 | 431 | json_read_constant(0't, Stream, true) :-
|
402 | 432 | !,
|
403 | 433 | must_see(`rue`, Stream, true).
|
|
1091 | 1121 | [ 'Illegal comment' ].
|
1092 | 1122 | json_syntax_error(illegal_string_escape) -->
|
1093 | 1123 | [ 'Illegal escape sequence in string' ].
|
| 1124 | +json_syntax_error(illegal_surrogate_pair) --> |
| 1125 | + [ 'Illegal escaped surrogate pair in string' ]. |
| 1126 | + |
0 commit comments