From: panasenco Date: Thu, 29 Apr 2021 05:58:04 +0000 (-0700) Subject: Oops, we want to keep digits as characters in library(abnf), but as numbers in librar... X-Git-Tag: v0.9.0~96^2 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=baed12f45e8133ee7dde14a76b3d9dbbe1251319;p=scryer-prolog.git Oops, we want to keep digits as characters in library(abnf), but as numbers in library(json). --- diff --git a/src/lib/serialization/abnf.pl b/src/lib/serialization/abnf.pl index 40e5d9f6..bf71ab9c 100644 --- a/src/lib/serialization/abnf.pl +++ b/src/lib/serialization/abnf.pl @@ -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. diff --git a/src/lib/serialization/json.pl b/src/lib/serialization/json.pl index 50672e1f..e1bebe11 100644 --- a/src/lib/serialization/json.pl +++ b/src/lib/serialization/json.pl @@ -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) --> diff --git a/src/tests/json/pass_everything.min.json b/src/tests/json/pass_everything.min.json index 5d5659ea..27f37e62 100644 --- a/src/tests/json/pass_everything.min.json +++ b/src/tests/json/pass_everything.min.json @@ -1 +1 @@ -["JSON Test Pattern pass1",{"object with 1 member":["array with 1 element"]},{},[],-42,true,false,null,{"integer":1234567890,"real":-9876.54321,"e":0.000000000000123456789,"E":12345678900000000000000000000000000.0,"":23456789012000000000000000000000000000000000000000000000000000000000000000000,"zero":0,"one":1,"space":" ","quote":"\"","backslash":"\\","controls":"\b\f\n\r\t","slash":"\/ & \/","alpha":"abcdefghijklmnopqrstuvwyz","ALPHA":"ABCDEFGHIJKLMNOPQRSTUVWYZ","digit":"0123456789","special":"`1~!@#$%^&*()_+-={':[,]}|;.<\/>?","hex":"ģ䕧覫\u0001췯ꯍ\u001A","true":true,"false":false,"null":null,"array":[],"object":{},"address":"50 St. James Street","url":"http:\/\/www.JSON.org\/","comment":"\/\/ \/* *\/":" "," s p a c e d ":[1,2,3,4,5,6,7],"compact":[1,2,3,4,5,6,7],"jsontext":"{\"object with 1 member\":[\"array with 1 element\"]}","quotes":"" \" %22 0x22 034 "","\/\\\"쫾몾ꮘﳞ볚\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',.\/<>?":"A key can be any string"},0.5,98.6,99.44,1066,"rosebud"] +["JSON Test Pattern pass1",{"object with 1 member":["array with 1 element"]},{},[],-42,true,false,null,{"integer":1234567890,"real":-9876.54321,"e":0.000000000000123456789,"E":12345678900000000000000000000000000.0,"":23456789012000000000000000000000000000000000000000000000000000000000000000000,"zero":0,"one":1,"space":" ","quote":"\"","backslash":"\\","controls":"\b\f\n\r\t","slash":"\/ & \/","alpha":"abcdefghijklmnopqrstuvwyz","ALPHA":"ABCDEFGHIJKLMNOPQRSTUVWYZ","digit":"0123456789","special":"`1~!@#$%^&*()_+-={':[,]}|;.<\/>?","hex":"ģ䕧覫\u0001췯ꯍ\u001a","true":true,"false":false,"null":null,"array":[],"object":{},"address":"50 St. James Street","url":"http:\/\/www.JSON.org\/","comment":"\/\/ \/* *\/":" "," s p a c e d ":[1,2,3,4,5,6,7],"compact":[1,2,3,4,5,6,7],"jsontext":"{\"object with 1 member\":[\"array with 1 element\"]}","quotes":"" \" %22 0x22 034 "","\/\\\"쫾몾ꮘﳞ볚\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',.\/<>?":"A key can be any string"},0.5,98.6,99.44,1066,"rosebud"]