Skip to content

Commit b4c5c4d

Browse files
committed
Use interpreter for parsing JSON instead of Parsing package
1 parent 029cf8d commit b4c5c4d

File tree

1 file changed

+3
-83
lines changed

1 file changed

+3
-83
lines changed

M2/Macaulay2/packages/JSON.m2

Lines changed: 3 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ newPackage(
2424
Email => "dtorrance@piedmont.edu",
2525
HomePage => "https://webwork.piedmont.edu/~dtorrance"}},
2626
Keywords => {"System"},
27-
PackageImports => {"Parsing"},
2827
PackageExports => {"Text"},
2928
AuxiliaryFiles => true)
3029

@@ -72,90 +71,11 @@ export {
7271
"ValueSeparator",
7372
}
7473

75-
exportFrom_Parsing "nil"
76-
77-
----------------------------------------------------------------
78-
-- parser based on https://datatracker.ietf.org/doc/html/rfc8259
79-
----------------------------------------------------------------
80-
81-
-- whitespace
82-
wsP = *orP(" ", "\t", "\n", "\r")
83-
strip = p -> first % (p @ wsP)
84-
85-
-- structural characters
86-
beginArrayP = strip "["
87-
beginObjectP = strip "{"
88-
endArrayP = strip "]"
89-
endObjectP = strip "}"
90-
nameSeparatorP = strip ":"
91-
valueSeparatorP = strip ","
92-
93-
-- literals
94-
falseP = (x -> false) % constParser "false"
95-
nullP = (x -> nil) % constParser "null" -- using null would break parsing
96-
trueP = (x -> true) % constParser "true"
97-
98-
-- numbers
99-
digit19P = orP("1", "2", "3", "4", "5", "6", "7", "8", "9")
100-
digitP = "0" | digit19P
101-
expP = andP(orP("e", "E"), optP(orP("-", "+")), +digitP)
102-
fracP = andP(".", +digitP)
103-
intP = orP("0", digit19P @ *digitP)
104-
numberP = (x -> value concatenate delete(nil, deepSplice x)
105-
) % andP(optP("-"), intP, optP(fracP), optP(expP))
106-
107-
-- strings
108-
hexDigitP = digitP | orP("a", "b", "c", "d", "e", "f",
109-
"A", "B", "C", "D", "E", "F");
110-
unescapedP = Parser(c -> if c === null then null else (
111-
x := first utf8 c;
112-
if x < 0x20 or x == 0x22 or x == 0x5c or x > 0x10ffff
113-
then null
114-
else terminalParser c))
115-
deformat = x -> (
116-
if last x === "/" then "/"
117-
else value concatenate("\"", x, "\""))
118-
escapedP = deformat % ("\\" @
119-
orP("\"", "\\", "/", "b", "f", "n", "r", "t",
120-
andP("u", hexDigitP, hexDigitP, hexDigitP, hexDigitP)))
121-
charP = unescapedP | escapedP
122-
stringP = ((l, x, r) -> concatenate x) % andP("\"", *charP, "\"")
123-
124-
-- objects
125-
memberP = ((k, gets, v) -> k => v) % andP(
126-
stringP, nameSeparatorP, futureParser valueP)
127-
objectP = ((l, x, r) -> hashTable (
128-
if x === nil then {}
129-
else toList deepSplice x)) % andP(
130-
beginObjectP,
131-
optP(memberP @ * (last % valueSeparatorP @ memberP)),
132-
endObjectP)
133-
134-
-- arrays
135-
arrayP = ((l, x, r) -> (
136-
if x === nil then {}
137-
else toList deepSplice x)) % andP(
138-
beginArrayP,
139-
optP(futureParser valueP @
140-
*(last % valueSeparatorP @ futureParser valueP)),
141-
endArrayP)
142-
143-
-- values
144-
valueP = strip orP(falseP, nullP, trueP, objectP, arrayP, numberP, stringP)
145-
jsonTextP = (last @@ last) % (*wsP @ valueP)
146-
147-
utf8Analyzer = Analyzer(s -> (
148-
if not instance(s, String) then error "analyzer expected a string";
149-
chars := characters s;
150-
i := 0;
151-
() -> if chars#?i then (
152-
r := (i, chars#i);
153-
i = i + 1;
154-
r)))
74+
importFrom(Core, "fromJSON0")
15575

15676
fromJSON = method()
157-
fromJSON String := jsonTextP : utf8Analyzer
158-
fromJSON File := fromJSON @@ get
77+
fromJSON String :=
78+
fromJSON File := fromJSON0
15979

16080
--------------
16181
-- encoding --

0 commit comments

Comments
 (0)