Skip to content

SyntaxNodes: implement the hash function for syntax nodes. #22457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions utils/gyb_syntax_support/NodeSerializationCodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,7 @@ def verify_syntax_node_serialization_codes(nodes, serialization_codes):
if serialization_code in used_codes:
error("Serialization code %d used twice" % serialization_code)
used_codes.add(serialization_code)


def get_serialization_code(syntax_kind):
return SYNTAX_NODE_SERIALIZATION_CODES[syntax_kind]
27 changes: 26 additions & 1 deletion utils/gyb_syntax_support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
from ExprNodes import EXPR_NODES # noqa: I201
from GenericNodes import GENERIC_NODES # noqa: I201
from NodeSerializationCodes import SYNTAX_NODE_SERIALIZATION_CODES, \
get_serialization_code, \
verify_syntax_node_serialization_codes

from PatternNodes import PATTERN_NODES # noqa: I201
from StmtNodes import STMT_NODES # noqa: I201
import Token
Expand Down Expand Up @@ -141,5 +143,28 @@ def dedented_lines(description):
return textwrap.dedent(description).split('\n')


def hash_syntax_node(node):
# Hash into the syntax name and serialization code
result = hash((node.name, str(get_serialization_code(node.syntax_kind))))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops sorry, missed a nitpick, you don't have to convert the integer to a string in order to hash it.
Also no need for str(child.is_optional) and str(token.serialization_code) below.

for child in node.children:
# Hash into the expected child syntax
result = hash((result, child.syntax_kind))
# Hash into the child name
result = hash((result, child.name))
# Hash into whether the child is optional
result = hash((result, str(child.is_optional)))
return result


def hash_token_syntax(token):
# Hash into the token name and serialization code
return hash((token.name, str(token.serialization_code)))


def calculate_node_hash():
return "abc"
result = 0
for node in SYNTAX_NODES:
result = hash((result, hash_syntax_node(node=node)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommend hash_syntax_node(node=node) -> hash_syntax_node(node), named arguments are not required in python, you usually put them for readability when there's a lot of them, but it's not offering any value here
Also remove named argument for hash_token_syntax(token=token) below.

for token in SYNTAX_TOKENS:
result = hash((result, hash_token_syntax(token=token)))
return result