Rizin
unix-like reverse engineering framework and cli tools
reusable_node.h File Reference
#include "./subtree.h"

Go to the source code of this file.

Classes

struct  StackEntry
 
struct  ReusableNode
 

Functions

static ReusableNode reusable_node_new (void)
 
static void reusable_node_clear (ReusableNode *self)
 
static Subtree reusable_node_tree (ReusableNode *self)
 
static uint32_t reusable_node_byte_offset (ReusableNode *self)
 
static void reusable_node_delete (ReusableNode *self)
 
static void reusable_node_advance (ReusableNode *self)
 
static bool reusable_node_descend (ReusableNode *self)
 
static void reusable_node_advance_past_leaf (ReusableNode *self)
 
static void reusable_node_reset (ReusableNode *self, Subtree tree)
 

Function Documentation

◆ reusable_node_advance()

static void reusable_node_advance ( ReusableNode self)
inlinestatic

Definition at line 39 of file reusable_node.h.

39  {
40  StackEntry last_entry = *array_back(&self->stack);
41  uint32_t byte_offset = last_entry.byte_offset + ts_subtree_total_bytes(last_entry.tree);
42  if (ts_subtree_has_external_tokens(last_entry.tree)) {
43  self->last_external_token = ts_subtree_last_external_token(last_entry.tree);
44  }
45 
46  Subtree tree;
47  uint32_t next_index;
48  do {
49  StackEntry popped_entry = array_pop(&self->stack);
50  next_index = popped_entry.child_index + 1;
51  if (self->stack.size == 0) return;
52  tree = array_back(&self->stack)->tree;
53  } while (ts_subtree_child_count(tree) <= next_index);
54 
55  array_push(&self->stack, ((StackEntry) {
56  .tree = ts_subtree_children(tree)[next_index],
57  .child_index = next_index,
58  .byte_offset = byte_offset,
59  }));
60 }
#define array_back(self)
Definition: array.h:33
#define array_push(self, element)
Definition: array.h:43
#define array_pop(self)
Definition: array.h:82
unsigned int uint32_t
Definition: sftypes.h:29
uint32_t byte_offset
Definition: reusable_node.h:6
Subtree tree
Definition: reusable_node.h:4
uint32_t child_index
Definition: reusable_node.h:5
Subtree ts_subtree_last_external_token(Subtree tree)
Definition: subtree.c:802
static bool ts_subtree_has_external_tokens(Subtree self)
Definition: subtree.h:330
static uint32_t ts_subtree_total_bytes(Subtree self)
Definition: subtree.h:278
static uint32_t ts_subtree_child_count(Subtree self)
Definition: subtree.h:282

References array_back, array_pop, array_push, StackEntry::byte_offset, StackEntry::child_index, StackEntry::tree, ts_subtree_child_count(), ts_subtree_has_external_tokens(), ts_subtree_last_external_token(), and ts_subtree_total_bytes().

Referenced by reusable_node_advance_past_leaf(), ts_parser__advance(), and ts_parser__reuse_node().

◆ reusable_node_advance_past_leaf()

static void reusable_node_advance_past_leaf ( ReusableNode self)
inlinestatic

Definition at line 76 of file reusable_node.h.

76  {
77  while (reusable_node_descend(self)) {}
79 }
static void reusable_node_advance(ReusableNode *self)
Definition: reusable_node.h:39
static bool reusable_node_descend(ReusableNode *self)
Definition: reusable_node.h:62

References reusable_node_advance(), and reusable_node_descend().

Referenced by ts_parser__reuse_node().

◆ reusable_node_byte_offset()

static uint32_t reusable_node_byte_offset ( ReusableNode self)
inlinestatic

Definition at line 29 of file reusable_node.h.

29  {
30  return self->stack.size > 0
31  ? self->stack.contents[self->stack.size - 1].byte_offset
32  : UINT32_MAX;
33 }
#define UINT32_MAX

References UINT32_MAX.

Referenced by ts_parser__reuse_node().

◆ reusable_node_clear()

static void reusable_node_clear ( ReusableNode self)
inlinestatic

Definition at line 18 of file reusable_node.h.

18  {
19  array_clear(&self->stack);
20  self->last_external_token = NULL_SUBTREE;
21 }
#define array_clear(self)
Definition: array.h:35
#define NULL_SUBTREE
Definition: subtree.h:19

References array_clear, and NULL_SUBTREE.

Referenced by reusable_node_reset(), ts_parser_parse(), and ts_parser_reset().

◆ reusable_node_delete()

static void reusable_node_delete ( ReusableNode self)
inlinestatic

Definition at line 35 of file reusable_node.h.

35  {
36  array_delete(&self->stack);
37 }
#define array_delete(self)
Definition: array.h:41

References array_delete.

Referenced by ts_parser_delete().

◆ reusable_node_descend()

static bool reusable_node_descend ( ReusableNode self)
inlinestatic

Definition at line 62 of file reusable_node.h.

62  {
63  StackEntry last_entry = *array_back(&self->stack);
64  if (ts_subtree_child_count(last_entry.tree) > 0) {
65  array_push(&self->stack, ((StackEntry) {
66  .tree = ts_subtree_children(last_entry.tree)[0],
67  .child_index = 0,
68  .byte_offset = last_entry.byte_offset,
69  }));
70  return true;
71  } else {
72  return false;
73  }
74 }

References array_back, array_push, StackEntry::tree, and ts_subtree_child_count().

Referenced by reusable_node_advance_past_leaf(), reusable_node_reset(), ts_parser__breakdown_lookahead(), and ts_parser__reuse_node().

◆ reusable_node_new()

static ReusableNode reusable_node_new ( void  )
inlinestatic

Definition at line 14 of file reusable_node.h.

14  {
15  return (ReusableNode) {array_new(), NULL_SUBTREE};
16 }
#define array_new()
Definition: array.h:25

References array_new, and NULL_SUBTREE.

Referenced by parser__start(), and ts_parser_new().

◆ reusable_node_reset()

static void reusable_node_reset ( ReusableNode self,
Subtree  tree 
)
inlinestatic

Definition at line 81 of file reusable_node.h.

81  {
82  reusable_node_clear(self);
83  array_push(&self->stack, ((StackEntry) {
84  .tree = tree,
85  .child_index = 0,
86  .byte_offset = 0,
87  }));
88 
89  // Never reuse the root node, because it has a non-standard internal structure
90  // due to transformations that are applied when it is accepted: adding the EOF
91  // child and any extra children.
92  if (!reusable_node_descend(self)) {
93  reusable_node_clear(self);
94  }
95 }
static void reusable_node_clear(ReusableNode *self)
Definition: reusable_node.h:18

References array_push, reusable_node_clear(), and reusable_node_descend().

Referenced by ts_parser_parse().

◆ reusable_node_tree()

static Subtree reusable_node_tree ( ReusableNode self)
inlinestatic

Definition at line 23 of file reusable_node.h.

23  {
24  return self->stack.size > 0
25  ? self->stack.contents[self->stack.size - 1].tree
26  : NULL_SUBTREE;
27 }

References NULL_SUBTREE.

Referenced by ts_parser__breakdown_lookahead(), and ts_parser__reuse_node().