Oops, we want to keep digits as characters in library(abnf), but as numbers in library(json).

This commit is contained in:
panasenco
2021-04-28 22:58:04 -07:00
parent dfbf291725
commit baed12f45e
3 changed files with 47 additions and 31 deletions

View File

@@ -116,37 +116,37 @@ abnf_alpha('X') --> "X".
abnf_alpha('Y') --> "Y".
abnf_alpha('Z') --> "Z".
abnf_bit(0) --> "0".
abnf_bit(1) --> "1".
abnf_bit('0') --> "0".
abnf_bit('1') --> "1".
abnf_char(C) --> [C], { dif(C, '\x0000\'), char_type(C, ascii) }. %'
abnf_char(Char) --> [Char], { dif(Char, '\x0000\'), char_type(Char, ascii) }. %'
abnf_cr --> "\r".
abnf_crlf --> "\r\n".
abnf_ctl(C) --> [C], { char_type(C, ascii), char_type(C, control) }.
abnf_ctl(Char) --> [Char], { char_type(Char, ascii), char_type(Char, control) }.
abnf_digit(0) --> "0".
abnf_digit(1) --> "1".
abnf_digit(2) --> "2".
abnf_digit(3) --> "3".
abnf_digit(4) --> "4".
abnf_digit(5) --> "5".
abnf_digit(6) --> "6".
abnf_digit(7) --> "7".
abnf_digit(8) --> "8".
abnf_digit(9) --> "9".
abnf_digit('0') --> "0".
abnf_digit('1') --> "1".
abnf_digit('2') --> "2".
abnf_digit('3') --> "3".
abnf_digit('4') --> "4".
abnf_digit('5') --> "5".
abnf_digit('6') --> "6".
abnf_digit('7') --> "7".
abnf_digit('8') --> "8".
abnf_digit('9') --> "9".
abnf_dquote --> "\"".
abnf_hexdig(Digit) --> abnf_digit(Digit).
abnf_hexdig(10) --> "A".
abnf_hexdig(11) --> "B".
abnf_hexdig(12) --> "C".
abnf_hexdig(13) --> "D".
abnf_hexdig(14) --> "E".
abnf_hexdig(15) --> "F".
abnf_hexdig(Char) --> abnf_digit(Char).
abnf_hexdig('A') --> "A".
abnf_hexdig('B') --> "B".
abnf_hexdig('C') --> "C".
abnf_hexdig('D') --> "D".
abnf_hexdig('E') --> "E".
abnf_hexdig('F') --> "F".
abnf_htab --> "\t".
@@ -156,11 +156,11 @@ abnf_lwsp --> "".
abnf_lwsp --> abnf_wsp, abnf_lwsp.
abnf_lwsp --> abnf_clrf, abnf_wsp, abnf_lwsp.
abnf_octet(C) --> [C], char_type(C, octet).
abnf_octet(Char) --> [Char], char_type(Char, octet).
abnf_sp --> " ".
abnf_vchar(C) --> [C], char_type(C, ascii_graphic).
abnf_vchar(Char) --> [Char], char_type(Char, ascii_graphic).
abnf_wsp --> abnf_sp.
abnf_wsp --> abnf_htab.

View File

@@ -1,7 +1,7 @@
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Written Apr 2021 by Aram Panasenco (panasenco@ucla.edu)
Part of Scryer Prolog.
`json_chars//1` can be used with [`phrase_from_file/2`](src/lib/pio.pl)
or [`phrase/2`](src/lib/dcgs.pl) to parse and generate [JSON](https://www.json.org/json-en.html).
@@ -43,7 +43,6 @@
:- use_module(library(dcgs)).
:- use_module(library(dif)).
:- use_module(library(lists)).
:- use_module(library(serialization/abnf)).
/* The DCGs are written to match the McKeeman form presented on the right side of https://www.json.org/json-en.html
as closely as possible. Note that the names in the McKeeman form conflict with the pictures on the site. */
@@ -165,13 +164,19 @@ json_character(EscapeChar) -->
H4 is (EscapeCharCode // 16^0) mod 16
) }.
json_hex(Hex) --> abnf_hexdig(Hex).
json_hex(Digit) --> json_digit(Digit).
json_hex(10) --> "a".
json_hex(11) --> "b".
json_hex(12) --> "c".
json_hex(13) --> "d".
json_hex(14) --> "e".
json_hex(15) --> "f".
json_hex(10) --> "A".
json_hex(11) --> "B".
json_hex(12) --> "C".
json_hex(13) --> "D".
json_hex(14) --> "E".
json_hex(15) --> "F".
/* I can't think of any alternatives to using `number_chars/2` when generating, though this leads
to under-reporting of correct solutions. At least matching solutions unify when both are instantiated...
@@ -203,20 +208,31 @@ json_number(Number) -->
NumberChars
).
json_integer(Digit) --> abnf_digit(Digit).
json_integer(Digit) --> json_digit(Digit).
json_integer(TotalValue) -->
json_onenine(FirstDigit),
json_digits(RemainingValue, Power),
{ TotalValue is FirstDigit * 10 ^ (Power + 1) + RemainingValue }.
json_digits(Digit, 0) --> abnf_digit(Digit).
json_digits(Digit, 0) --> json_digit(Digit).
json_digits(Value, Power) -->
abnf_digit(FirstDigit),
json_digit(FirstDigit),
json_digits(RemainingValue, NextPower),
{ Power is NextPower + 1,
Value is FirstDigit * 10^Power + RemainingValue }.
json_onenine(Digit) --> abnf_digit(Digit), { dif(Digit, 0) }.
json_digit(0) --> "0".
json_digit(Digit) --> json_onenine(Digit).
json_onenine(1) --> "1".
json_onenine(2) --> "2".
json_onenine(3) --> "3".
json_onenine(4) --> "4".
json_onenine(5) --> "5".
json_onenine(6) --> "6".
json_onenine(7) --> "7".
json_onenine(8) --> "8".
json_onenine(9) --> "9".
json_fraction(0) --> "".
json_fraction(Fraction) -->