| 1 | /** |
| 2 | * @file static_literals.h |
| 3 | * |
| 4 | * A set of static literal nodes that can be checked for duplicates. |
| 5 | */ |
| 6 | #ifndef PRISM_STATIC_LITERALS_H |
| 7 | #define PRISM_STATIC_LITERALS_H |
| 8 | |
| 9 | #include "prism/defines.h" |
| 10 | #include "prism/ast.h" |
| 11 | #include "prism/util/pm_newline_list.h" |
| 12 | |
| 13 | #include <assert.h> |
| 14 | #include <stdbool.h> |
| 15 | |
| 16 | /** |
| 17 | * An internal hash table for a set of nodes. |
| 18 | */ |
| 19 | typedef struct { |
| 20 | /** The array of nodes in the hash table. */ |
| 21 | pm_node_t **nodes; |
| 22 | |
| 23 | /** The size of the hash table. */ |
| 24 | uint32_t size; |
| 25 | |
| 26 | /** The space that has been allocated in the hash table. */ |
| 27 | uint32_t capacity; |
| 28 | } pm_node_hash_t; |
| 29 | |
| 30 | /** |
| 31 | * Certain sets of nodes (hash keys and when clauses) check for duplicate nodes |
| 32 | * to alert the user of potential issues. To do this, we keep a set of the nodes |
| 33 | * that have been seen so far, and compare whenever we find a new node. |
| 34 | * |
| 35 | * We bucket the nodes based on their type to minimize the number of comparisons |
| 36 | * that need to be performed. |
| 37 | */ |
| 38 | typedef struct { |
| 39 | /** |
| 40 | * This is the set of IntegerNode and SourceLineNode instances. |
| 41 | */ |
| 42 | pm_node_hash_t integer_nodes; |
| 43 | |
| 44 | /** |
| 45 | * This is the set of FloatNode instances. |
| 46 | */ |
| 47 | pm_node_hash_t float_nodes; |
| 48 | |
| 49 | /** |
| 50 | * This is the set of RationalNode and ImaginaryNode instances. |
| 51 | */ |
| 52 | pm_node_hash_t number_nodes; |
| 53 | |
| 54 | /** |
| 55 | * This is the set of StringNode and SourceFileNode instances. |
| 56 | */ |
| 57 | pm_node_hash_t string_nodes; |
| 58 | |
| 59 | /** |
| 60 | * This is the set of RegularExpressionNode instances. |
| 61 | */ |
| 62 | pm_node_hash_t regexp_nodes; |
| 63 | |
| 64 | /** |
| 65 | * This is the set of SymbolNode instances. |
| 66 | */ |
| 67 | pm_node_hash_t symbol_nodes; |
| 68 | |
| 69 | /** |
| 70 | * A pointer to the last TrueNode instance that was inserted, or NULL. |
| 71 | */ |
| 72 | pm_node_t *true_node; |
| 73 | |
| 74 | /** |
| 75 | * A pointer to the last FalseNode instance that was inserted, or NULL. |
| 76 | */ |
| 77 | pm_node_t *false_node; |
| 78 | |
| 79 | /** |
| 80 | * A pointer to the last NilNode instance that was inserted, or NULL. |
| 81 | */ |
| 82 | pm_node_t *nil_node; |
| 83 | |
| 84 | /** |
| 85 | * A pointer to the last SourceEncodingNode instance that was inserted, or |
| 86 | * NULL. |
| 87 | */ |
| 88 | pm_node_t *source_encoding_node; |
| 89 | } pm_static_literals_t; |
| 90 | |
| 91 | /** |
| 92 | * Add a node to the set of static literals. |
| 93 | * |
| 94 | * @param newline_list The list of newline offsets to use to calculate lines. |
| 95 | * @param start_line The line number that the parser starts on. |
| 96 | * @param literals The set of static literals to add the node to. |
| 97 | * @param node The node to add to the set. |
| 98 | * @param replace Whether to replace the previous node if one already exists. |
| 99 | * @return A pointer to the node that is being overwritten, if there is one. |
| 100 | */ |
| 101 | pm_node_t * pm_static_literals_add(const pm_newline_list_t *newline_list, int32_t start_line, pm_static_literals_t *literals, pm_node_t *node, bool replace); |
| 102 | |
| 103 | /** |
| 104 | * Free the internal memory associated with the given static literals set. |
| 105 | * |
| 106 | * @param literals The set of static literals to free. |
| 107 | */ |
| 108 | void pm_static_literals_free(pm_static_literals_t *literals); |
| 109 | |
| 110 | /** |
| 111 | * Create a string-based representation of the given static literal. |
| 112 | * |
| 113 | * @param buffer The buffer to write the string to. |
| 114 | * @param newline_list The list of newline offsets to use to calculate lines. |
| 115 | * @param start_line The line number that the parser starts on. |
| 116 | * @param encoding_name The name of the encoding of the source being parsed. |
| 117 | * @param node The node to create a string representation of. |
| 118 | */ |
| 119 | void pm_static_literal_inspect(pm_buffer_t *buffer, const pm_newline_list_t *newline_list, int32_t start_line, const char *encoding_name, const pm_node_t *node); |
| 120 | |
| 121 | #endif |