Skip to content

Commit 0c28b4c

Browse files
committed
SyntaxNodes: implement the hash function for syntax nodes.
This hash function will concatenate all interesting pieces of information of node definitions in a single string and call hash() on this string.
1 parent 9bc5bd2 commit 0c28b4c

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

utils/gyb_syntax_support/NodeSerializationCodes.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,3 +247,7 @@ def verify_syntax_node_serialization_codes(nodes, serialization_codes):
247247
if serialization_code in used_codes:
248248
error("Serialization code %d used twice" % serialization_code)
249249
used_codes.add(serialization_code)
250+
251+
252+
def get_serialization_code(syntax_kind):
253+
return SYNTAX_NODE_SERIALIZATION_CODES[syntax_kind]

utils/gyb_syntax_support/__init__.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
from ExprNodes import EXPR_NODES # noqa: I201
88
from GenericNodes import GENERIC_NODES # noqa: I201
99
from NodeSerializationCodes import SYNTAX_NODE_SERIALIZATION_CODES, \
10+
get_serialization_code, \
1011
verify_syntax_node_serialization_codes
12+
1113
from PatternNodes import PATTERN_NODES # noqa: I201
1214
from StmtNodes import STMT_NODES # noqa: I201
1315
import Token
@@ -141,5 +143,28 @@ def dedented_lines(description):
141143
return textwrap.dedent(description).split('\n')
142144

143145

146+
def hash_syntax_node(node):
147+
# Hash into the syntax name and serialization code
148+
result = hash((node.name, str(get_serialization_code(node.syntax_kind))))
149+
for child in node.children:
150+
# Hash into the expected child syntax
151+
result = hash((result, child.syntax_kind))
152+
# Hash into the child name
153+
result = hash((result, child.name))
154+
# Hash into whether the child is optional
155+
result = hash((result, str(child.is_optional)))
156+
return result
157+
158+
159+
def hash_token_syntax(token):
160+
# Hash into the token name and serialization code
161+
return hash((token.name, str(token.serialization_code)))
162+
163+
144164
def calculate_node_hash():
145-
return "abc"
165+
result = 0
166+
for node in SYNTAX_NODES:
167+
result = hash((result, hash_syntax_node(node=node)))
168+
for token in SYNTAX_TOKENS:
169+
result = hash((result, hash_token_syntax(token=token)))
170+
return result

0 commit comments

Comments
 (0)