Skip to content

Commit 5c87e03

Browse files
committed
pickle->json
1 parent 92efaef commit 5c87e03

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
.idea
33
venv/
44
__pycache__/
5+
*.json

sly/yacc.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import sys
3535
import os
3636
import inspect
37-
import pickle
37+
import json
3838
from collections import OrderedDict, defaultdict, Counter
3939

4040
__all__ = [ 'Parser' ]
@@ -937,7 +937,7 @@ class LALRError(YaccError):
937937
# -----------------------------------------------------------------------------
938938

939939
class LRTable(object):
940-
def __init__(self, grammar, lr_vars=None):
940+
def __init__(self, grammar, lr_dump=None):
941941
self.grammar = grammar
942942

943943
# Internal attributes
@@ -963,8 +963,8 @@ def __init__(self, grammar, lr_vars=None):
963963
self.grammar.compute_first()
964964
self.grammar.compute_follow()
965965

966-
if lr_vars is not None:
967-
self.lr_action, self.lr_goto = lr_vars
966+
if lr_dump is not None:
967+
self.load(lr_dump)
968968
else:
969969
self.lr_action = {} # Action table
970970
self.lr_goto = {} # Goto table
@@ -988,6 +988,17 @@ def __init__(self, grammar, lr_vars=None):
988988
self.lr_goto_cache2 = None
989989
self.lr0_cidhash = None
990990

991+
def dump(self):
992+
return [
993+
list(self.lr_action.items()),
994+
list(self.lr_goto.items()),
995+
]
996+
997+
def load(self, obj):
998+
action, goto = obj
999+
self.lr_action = dict(action)
1000+
self.lr_goto = dict(goto)
1001+
9911002
# Compute the LR(0) closure operation on I, where I is a set of LR(0) items.
9921003
def lr0_closure(self, I):
9931004
self._add_count += 1
@@ -1994,15 +2005,14 @@ def __build_grammar(cls, rules):
19942005
@classmethod
19952006
def _get_build_file(cls):
19962007
path = inspect.getfile(cls)
1997-
return path[: path.rfind('.')] + '.pkl'
2008+
return path[: path.rfind('.')] + '.json'
19982009

19992010
@classmethod
20002011
def build_to_file(self):
20012012
path = self._get_build_file()
20022013

2003-
lr_table = self._lrtable
2004-
lr_vars = (lr_table.lr_action, lr_table.lr_goto)
2005-
pickle.dump(lr_vars, open(path, 'wb'))
2014+
lr_dump = self._lrtable.dump()
2015+
json.dump(lr_dump, open(path, 'w'))
20062016
print('Built to', path)
20072017

20082018
@classmethod
@@ -2012,15 +2022,14 @@ def __build_lrtables(cls):
20122022
'''
20132023
# use cache
20142024
build_file = cls._get_build_file()
2015-
lr_vars = None
2025+
lr_dump = None
20162026
if os.path.exists(build_file):
20172027
try:
2018-
lr_vars = pickle.load(open(build_file, 'rb'))
2019-
assert len(lr_vars) == 2
2028+
lr_dump = json.load(open(build_file, 'r'))
20202029
except Exception:
20212030
pass
20222031

2023-
lrtable = LRTable(cls._grammar, lr_vars=lr_vars)
2032+
lrtable = LRTable(cls._grammar, lr_dump=lr_dump)
20242033
num_sr = len(lrtable.sr_conflicts)
20252034

20262035
# Report shift/reduce and reduce/reduce conflicts

0 commit comments

Comments
 (0)