|
7 | 7 | from ExprNodes import EXPR_NODES # noqa: I201
|
8 | 8 | from GenericNodes import GENERIC_NODES # noqa: I201
|
9 | 9 | from NodeSerializationCodes import SYNTAX_NODE_SERIALIZATION_CODES, \
|
| 10 | + get_serialization_code, \ |
10 | 11 | verify_syntax_node_serialization_codes
|
| 12 | + |
11 | 13 | from PatternNodes import PATTERN_NODES # noqa: I201
|
12 | 14 | from StmtNodes import STMT_NODES # noqa: I201
|
13 | 15 | import Token
|
@@ -141,5 +143,28 @@ def dedented_lines(description):
|
141 | 143 | return textwrap.dedent(description).split('\n')
|
142 | 144 |
|
143 | 145 |
|
| 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 | + |
144 | 164 | 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