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

Go to the source code of this file.

Classes

struct  TableEntry
 
struct  LookaheadIterator
 

Macros

#define ts_builtin_sym_error_repeat   (ts_builtin_sym_error - 1)
 

Functions

void ts_language_table_entry (const TSLanguage *, TSStateId, TSSymbol, TableEntry *)
 
TSSymbolMetadata ts_language_symbol_metadata (const TSLanguage *, TSSymbol)
 
TSSymbol ts_language_public_symbol (const TSLanguage *, TSSymbol)
 
static bool ts_language_is_symbol_external (const TSLanguage *self, TSSymbol symbol)
 
static const TSParseActionts_language_actions (const TSLanguage *self, TSStateId state, TSSymbol symbol, uint32_t *count)
 
static bool ts_language_has_reduce_action (const TSLanguage *self, TSStateId state, TSSymbol symbol)
 
static uint16_t ts_language_lookup (const TSLanguage *self, TSStateId state, TSSymbol symbol)
 
static bool ts_language_has_actions (const TSLanguage *self, TSStateId state, TSSymbol symbol)
 
static LookaheadIterator ts_language_lookaheads (const TSLanguage *self, TSStateId state)
 
static bool ts_lookahead_iterator_next (LookaheadIterator *self)
 
static TSStateId ts_language_next_state (const TSLanguage *self, TSStateId state, TSSymbol symbol)
 
static bool ts_language_state_is_primary (const TSLanguage *self, TSStateId state)
 
static const boolts_language_enabled_external_tokens (const TSLanguage *self, unsigned external_scanner_state)
 
static const TSSymbolts_language_alias_sequence (const TSLanguage *self, uint32_t production_id)
 
static TSSymbol ts_language_alias_at (const TSLanguage *self, uint32_t production_id, uint32_t child_index)
 
static void ts_language_field_map (const TSLanguage *self, uint32_t production_id, const TSFieldMapEntry **start, const TSFieldMapEntry **end)
 
static void ts_language_aliases_for_symbol (const TSLanguage *self, TSSymbol original_symbol, const TSSymbol **start, const TSSymbol **end)
 

Macro Definition Documentation

◆ ts_builtin_sym_error_repeat

#define ts_builtin_sym_error_repeat   (ts_builtin_sym_error - 1)

Definition at line 11 of file language.h.

Function Documentation

◆ ts_language_actions()

static const TSParseAction* ts_language_actions ( const TSLanguage self,
TSStateId  state,
TSSymbol  symbol,
uint32_t count 
)
inlinestatic

Definition at line 45 of file language.h.

50  {
52  ts_language_table_entry(self, state, symbol, &entry);
53  *count = entry.action_count;
54  return entry.actions;
55 }
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void count
Definition: sflib.h:98
void ts_language_table_entry(const TSLanguage *, TSStateId, TSSymbol, TableEntry *)
Definition: language.c:18
Definition: zipcmp.c:77
Definition: dis.h:43

References count, and ts_language_table_entry().

Referenced by parser__reductions_after_sequence(), parser__repair_error_callback(), parser__skip_preceding_trees_callback(), ts_language_next_state(), and ts_parser__recover().

◆ ts_language_alias_at()

static TSSymbol ts_language_alias_at ( const TSLanguage self,
uint32_t  production_id,
uint32_t  child_index 
)
inlinestatic

Definition at line 236 of file language.h.

240  {
241  return production_id ?
242  self->alias_sequences[production_id * self->max_alias_sequence_length + child_index] :
243  0;
244 }

Referenced by iterator_get_visible_state(), iterator_tree_is_visible(), ts_query__analyze_patterns(), ts_tree_cursor_current_field_id(), ts_tree_cursor_current_node(), ts_tree_cursor_goto_parent(), and ts_tree_cursor_parent_node().

◆ ts_language_alias_sequence()

static const TSSymbol* ts_language_alias_sequence ( const TSLanguage self,
uint32_t  production_id 
)
inlinestatic

Definition at line 227 of file language.h.

230  {
231  return production_id ?
232  &self->alias_sequences[production_id * self->max_alias_sequence_length] :
233  NULL;
234 }
#define NULL
Definition: cris-opc.c:27

References NULL.

Referenced by ts_node_iterate_children(), ts_subtree__write_to_string(), ts_subtree_summarize_children(), ts_tree_cursor_current_status(), and ts_tree_cursor_iterate_children().

◆ ts_language_aliases_for_symbol()

static void ts_language_aliases_for_symbol ( const TSLanguage self,
TSSymbol  original_symbol,
const TSSymbol **  start,
const TSSymbol **  end 
)
inlinestatic

Definition at line 263 of file language.h.

268  {
269  *start = &self->public_symbol_map[original_symbol];
270  *end = *start + 1;
271 
272  unsigned i = 0;
273  for (;;) {
274  TSSymbol symbol = self->alias_map[i++];
275  if (symbol == 0 || symbol > original_symbol) break;
276  uint16_t count = self->alias_map[i++];
277  if (symbol == original_symbol) {
278  *start = &self->alias_map[i];
279  *end = &self->alias_map[i + count];
280  break;
281  }
282  i += count;
283  }
284 }
lzma_index ** i
Definition: index.h:629
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void start
Definition: sflib.h:133
uint16_t TSSymbol
Definition: parser.h:19
unsigned short uint16_t
Definition: sftypes.h:30

References count, test_evm::end, i, and start.

Referenced by ts_query__analyze_patterns().

◆ ts_language_enabled_external_tokens()

static const bool* ts_language_enabled_external_tokens ( const TSLanguage self,
unsigned  external_scanner_state 
)
inlinestatic

Definition at line 216 of file language.h.

219  {
220  if (external_scanner_state == 0) {
221  return NULL;
222  } else {
223  return self->external_scanner.states + self->external_token_count * external_scanner_state;
224  }
225 }

References NULL.

Referenced by parser__lex(), and ts_parser__lex().

◆ ts_language_field_map()

static void ts_language_field_map ( const TSLanguage self,
uint32_t  production_id,
const TSFieldMapEntry **  start,
const TSFieldMapEntry **  end 
)
inlinestatic

Definition at line 246 of file language.h.

251  {
252  if (self->field_count == 0) {
253  *start = NULL;
254  *end = NULL;
255  return;
256  }
257 
258  TSFieldMapSlice slice = self->field_map_slices[production_id];
259  *start = &self->field_map_entries[slice.index];
260  *end = &self->field_map_entries[slice.index] + slice.length;
261 }
uint16_t length
Definition: parser.h:32
uint16_t index
Definition: parser.h:31
uint32_t field_count
Definition: parser.h:99

References test_evm::end, TSFieldMapSlice::index, TSFieldMapSlice::length, NULL, and start.

Referenced by ts_node_child_by_field_id(), ts_node_field_name_for_child(), ts_query__analyze_patterns(), ts_subtree__write_to_string(), ts_tree_cursor_current_field_id(), and ts_tree_cursor_current_status().

◆ ts_language_has_actions()

static bool ts_language_has_actions ( const TSLanguage self,
TSStateId  state,
TSSymbol  symbol 
)
inlinestatic

Definition at line 96 of file language.h.

100  {
101  return ts_language_lookup(self, state, symbol) != 0;
102 }
static uint16_t ts_language_lookup(const TSLanguage *self, TSStateId state, TSSymbol symbol)
Definition: language.h:74

References ts_language_lookup().

Referenced by ts_parser__lex(), and ts_parser__recover().

◆ ts_language_has_reduce_action()

static bool ts_language_has_reduce_action ( const TSLanguage self,
TSStateId  state,
TSSymbol  symbol 
)
inlinestatic

Definition at line 57 of file language.h.

61  {
63  ts_language_table_entry(self, state, symbol, &entry);
64  return entry.action_count > 0 && entry.actions[0].type == TSParseActionTypeReduce;
65 }
@ TSParseActionTypeReduce
Definition: parser.h:55

References ts_language_table_entry(), and TSParseActionTypeReduce.

Referenced by ts_parser__handle_error().

◆ ts_language_is_symbol_external()

static bool ts_language_is_symbol_external ( const TSLanguage self,
TSSymbol  symbol 
)
inlinestatic

Definition at line 41 of file language.h.

41  {
42  return 0 < symbol && symbol < self->external_token_count + 1;
43 }

◆ ts_language_lookaheads()

static LookaheadIterator ts_language_lookaheads ( const TSLanguage self,
TSStateId  state 
)
inlinestatic

Definition at line 110 of file language.h.

113  {
114  bool is_small_state = state >= self->large_state_count;
115  const uint16_t *data;
116  const uint16_t *group_end = NULL;
117  uint16_t group_count = 0;
118  if (is_small_state) {
119  uint32_t index = self->small_parse_table_map[state - self->large_state_count];
120  data = &self->small_parse_table[index];
121  group_end = data + 1;
122  group_count = *data;
123  } else {
124  data = &self->parse_table[state * self->symbol_count] - 1;
125  }
126  return (LookaheadIterator) {
127  .language = self,
128  .data = data,
129  .group_end = group_end,
130  .group_count = group_count,
131  .is_small_state = is_small_state,
132  .symbol = UINT16_MAX,
133  .next_state = 0,
134  };
135 }
unsigned int uint32_t
Definition: sftypes.h:29
#define UINT16_MAX

References NULL, and UINT16_MAX.

Referenced by ts_query__analyze_patterns().

◆ ts_language_lookup()

static uint16_t ts_language_lookup ( const TSLanguage self,
TSStateId  state,
TSSymbol  symbol 
)
inlinestatic

Definition at line 74 of file language.h.

78  {
79  if (state >= self->large_state_count) {
80  uint32_t index = self->small_parse_table_map[state - self->large_state_count];
81  const uint16_t *data = &self->small_parse_table[index];
82  uint16_t group_count = *(data++);
83  for (unsigned i = 0; i < group_count; i++) {
84  uint16_t section_value = *(data++);
85  uint16_t symbol_count = *(data++);
86  for (unsigned i = 0; i < symbol_count; i++) {
87  if (*(data++) == symbol) return section_value;
88  }
89  }
90  return 0;
91  } else {
92  return self->parse_table[state * self->symbol_count + symbol];
93  }
94 }
uint32_t large_state_count
Definition: parser.h:97

References i.

Referenced by ts_language_has_actions(), ts_language_next_state(), and ts_language_table_entry().

◆ ts_language_next_state()

static TSStateId ts_language_next_state ( const TSLanguage self,
TSStateId  state,
TSSymbol  symbol 
)
inlinestatic

Definition at line 181 of file language.h.

185  {
186  if (symbol == ts_builtin_sym_error || symbol == ts_builtin_sym_error_repeat) {
187  return 0;
188  } else if (symbol < self->token_count) {
189  uint32_t count;
190  const TSParseAction *actions = ts_language_actions(self, state, symbol, &count);
191  if (count > 0) {
192  TSParseAction action = actions[count - 1];
193  if (action.type == TSParseActionTypeShift) {
194  return action.shift.extra ? state : action.shift.state;
195  }
196  }
197  return 0;
198  } else {
199  return ts_language_lookup(self, state, symbol);
200  }
201 }
static const TSParseAction * ts_language_actions(const TSLanguage *self, TSStateId state, TSSymbol symbol, uint32_t *count)
Definition: language.h:45
#define ts_builtin_sym_error_repeat
Definition: language.h:11
@ TSParseActionTypeShift
Definition: parser.h:54
#define ts_builtin_sym_error
Definition: parser.h:12

References test-lz4-speed::action, count, ts_builtin_sym_error, ts_builtin_sym_error_repeat, ts_language_actions(), ts_language_lookup(), and TSParseActionTypeShift.

Referenced by parser__advance(), parser__breakdown_top_of_stack(), parser__reduce(), parser__reductions_after_sequence(), parser__repair_error_callback(), ts_parser__advance(), ts_parser__breakdown_top_of_stack(), ts_parser__handle_error(), and ts_parser__reduce().

◆ ts_language_public_symbol()

TSSymbol ts_language_public_symbol ( const TSLanguage self,
TSSymbol  symbol 
)

Definition at line 51 of file language.c.

54  {
55  if (symbol == ts_builtin_sym_error) return symbol;
56  return self->public_symbol_map[symbol];
57 }

References ts_builtin_sym_error.

Referenced by ts_node_symbol().

◆ ts_language_state_is_primary()

static bool ts_language_state_is_primary ( const TSLanguage self,
TSStateId  state 
)
inlinestatic

Definition at line 205 of file language.h.

208  {
209  if (self->version >= 14) {
210  return state == self->primary_state_ids[state];
211  } else {
212  return true;
213  }
214 }
uint32_t version
Definition: parser.h:91

Referenced by ts_query__analyze_patterns().

◆ ts_language_symbol_metadata()

◆ ts_language_table_entry()

void ts_language_table_entry ( const TSLanguage self,
TSStateId  state,
TSSymbol  symbol,
TableEntry result 
)

Definition at line 18 of file language.c.

23  {
24  if (symbol == ts_builtin_sym_error || symbol == ts_builtin_sym_error_repeat) {
25  result->action_count = 0;
26  result->is_reusable = false;
27  result->actions = NULL;
28  } else {
29  assert(symbol < self->token_count);
30  uint32_t action_index = ts_language_lookup(self, state, symbol);
31  const TSParseActionEntry *entry = &self->parse_actions[action_index];
32  result->action_count = entry->entry.count;
33  result->is_reusable = entry->entry.reusable;
34  result->actions = (const TSParseAction *)(entry + 1);
35  }
36 }
assert(limit<=UINT32_MAX/2)
bool is_reusable
Definition: language.h:16
uint32_t action_count
Definition: language.h:15
const TSParseAction * actions
Definition: language.h:14

References TableEntry::action_count, TableEntry::actions, assert(), TableEntry::is_reusable, NULL, ts_builtin_sym_error, ts_builtin_sym_error_repeat, and ts_language_lookup().

Referenced by parser__advance(), parser__do_potential_reductions(), ts_language_actions(), ts_language_has_reduce_action(), ts_parser__advance(), ts_parser__do_all_potential_reductions(), ts_parser__get_cached_token(), and ts_parser__reuse_node().

◆ ts_lookahead_iterator_next()

static bool ts_lookahead_iterator_next ( LookaheadIterator self)
inlinestatic

Definition at line 137 of file language.h.

137  {
138  // For small parse states, valid symbols are listed explicitly,
139  // grouped by their value. There's no need to look up the actions
140  // again until moving to the next group.
141  if (self->is_small_state) {
142  self->data++;
143  if (self->data == self->group_end) {
144  if (self->group_count == 0) return false;
145  self->group_count--;
146  self->table_value = *(self->data++);
147  unsigned symbol_count = *(self->data++);
148  self->group_end = self->data + symbol_count;
149  self->symbol = *self->data;
150  } else {
151  self->symbol = *self->data;
152  return true;
153  }
154  }
155 
156  // For large parse states, iterate through every symbol until one
157  // is found that has valid actions.
158  else {
159  do {
160  self->data++;
161  self->symbol++;
162  if (self->symbol >= self->language->symbol_count) return false;
163  self->table_value = *self->data;
164  } while (!self->table_value);
165  }
166 
167  // Depending on if the symbols is terminal or non-terminal, the table value either
168  // represents a list of actions or a successor state.
169  if (self->symbol < self->language->token_count) {
170  const TSParseActionEntry *entry = &self->language->parse_actions[self->table_value];
171  self->action_count = entry->entry.count;
172  self->actions = (const TSParseAction *)(entry + 1);
173  self->next_state = 0;
174  } else {
175  self->action_count = 0;
176  self->next_state = self->table_value;
177  }
178  return true;
179 }
const uint16_t * group_end
Definition: language.h:22
TSSymbol symbol
Definition: language.h:30
bool is_small_state
Definition: language.h:27
uint16_t group_count
Definition: language.h:26
const TSLanguage * language
Definition: language.h:20
uint16_t table_value
Definition: language.h:24
const uint16_t * data
Definition: language.h:21
uint32_t token_count
Definition: parser.h:94
uint32_t symbol_count
Definition: parser.h:92

Referenced by ts_query__analyze_patterns().