-
Notifications
You must be signed in to change notification settings - Fork 10.5k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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)))) | ||
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))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Recommend |
||
for token in SYNTAX_TOKENS: | ||
result = hash((result, hash_token_syntax(token=token))) | ||
return result |
There was a problem hiding this comment.
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)
andstr(token.serialization_code)
below.