Skip to content

Commit 7bc9cbc

Browse files
committed
Added logic for dynamically rebuilding parser on changes.
1 parent d2cd9e3 commit 7bc9cbc

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,9 @@ dmypy.json
137137

138138
*.cproject
139139

140-
# generated parser
140+
# generated parser and checksum
141141
src/scenic/syntax/parser.py
142+
src/scenic/syntax/.parser_checksum
142143

143144
# generated media/output
144145
simulation.gif

src/scenic/syntax/__init__.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""The Scenic compiler and associated support code."""
22

3+
import hashlib as _hashlib
34
import pathlib as _pathlib
45
import subprocess as _subprocess
56
import sys as _sys
@@ -8,6 +9,7 @@
89
_projectRootDir = _syntaxDir.parent.parent.parent
910
_grammarPath = _syntaxDir / "scenic.gram"
1011
_parserPath = _syntaxDir / "parser.py"
12+
_checksumPath = _syntaxDir / ".parser_checksum"
1113

1214

1315
def buildParser():
@@ -30,10 +32,30 @@ def buildParser():
3032
capture_output=True,
3133
text=True,
3234
)
35+
36+
with open(_checksumPath, "wb") as f:
37+
f.write(getParserHash())
38+
3339
return result
3440

3541

36-
if not _parserPath.exists():
42+
def getParserHash():
43+
with open(_parserPath, "rb") as f:
44+
data = f.read()
45+
return _hashlib.blake2b(data).digest()
46+
47+
48+
def checksumValid():
49+
if not _checksumPath.exists():
50+
return False
51+
52+
with open(_checksumPath, "rb") as f:
53+
checksum = f.read()
54+
55+
return checksum == getParserHash()
56+
57+
58+
if not _parserPath.exists() or not checksumValid():
3759
_result = buildParser()
3860
_retcode = _result.returncode
3961
if _retcode != 0:

0 commit comments

Comments
 (0)