Rizin
unix-like reverse engineering framework and cli tools
language.c File Reference
#include "./language.h"
#include "./subtree.h"
#include "./error_costs.h"
#include <string.h>

Go to the source code of this file.

Functions

uint32_t ts_language_symbol_count (const TSLanguage *self)
 
uint32_t ts_language_version (const TSLanguage *self)
 
uint32_t ts_language_field_count (const TSLanguage *self)
 
void ts_language_table_entry (const TSLanguage *self, TSStateId state, TSSymbol symbol, TableEntry *result)
 
TSSymbolMetadata ts_language_symbol_metadata (const TSLanguage *self, TSSymbol symbol)
 
TSSymbol ts_language_public_symbol (const TSLanguage *self, TSSymbol symbol)
 
const char * ts_language_symbol_name (const TSLanguage *self, TSSymbol symbol)
 
TSSymbol ts_language_symbol_for_name (const TSLanguage *self, const char *string, uint32_t length, bool is_named)
 
TSSymbolType ts_language_symbol_type (const TSLanguage *self, TSSymbol symbol)
 
const char * ts_language_field_name_for_id (const TSLanguage *self, TSFieldId id)
 
TSFieldId ts_language_field_id_for_name (const TSLanguage *self, const char *name, uint32_t name_length)
 

Function Documentation

◆ ts_language_field_count()

uint32_t ts_language_field_count ( const TSLanguage self)

Get the number of distinct field names in the language.

Definition at line 14 of file language.c.

14  {
15  return self->field_count;
16 }

Referenced by ts_language_field_id_for_name(), and ts_language_field_name_for_id().

◆ ts_language_field_id_for_name()

TSFieldId ts_language_field_id_for_name ( const TSLanguage self,
const char *  name,
uint32_t  name_length 
)

Get the numerical id for the given field name string.

Definition at line 119 of file language.c.

123  {
125  for (TSSymbol i = 1; i < count + 1; i++) {
126  switch (strncmp(name, self->field_names[i], name_length)) {
127  case 0:
128  if (self->field_names[i][name_length] == 0) return i;
129  break;
130  case -1:
131  return 0;
132  default:
133  break;
134  }
135  }
136  return 0;
137 }
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 count
Definition: sflib.h:98
uint32_t ts_language_field_count(const TSLanguage *self)
Definition: language.c:14
uint16_t TSSymbol
Definition: parser.h:19
unsigned int uint32_t
Definition: sftypes.h:29
const char *const * field_names
Definition: parser.h:106
Definition: z80asm.h:102

References count, i, and ts_language_field_count().

Referenced by ts_node_child_by_field_name(), and ts_query__parse_pattern().

◆ ts_language_field_name_for_id()

const char* ts_language_field_name_for_id ( const TSLanguage self,
TSFieldId  id 
)

Get the field name string for the given numerical id.

Definition at line 107 of file language.c.

110  {
112  if (count && id <= count) {
113  return self->field_names[id];
114  } else {
115  return NULL;
116  }
117 }
#define NULL
Definition: cris-opc.c:27
int id
Definition: op.c:540

References count, id, NULL, and ts_language_field_count().

Referenced by ts_query__analyze_patterns(), and ts_query_cursor__advance().

◆ 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 }
#define ts_builtin_sym_error
Definition: parser.h:12

References ts_builtin_sym_error.

Referenced by ts_node_symbol().

◆ ts_language_symbol_count()

uint32_t ts_language_symbol_count ( const TSLanguage self)

Get the number of distinct node types in the language.

Definition at line 6 of file language.c.

6  {
7  return self->symbol_count + self->alias_count;
8 }

Referenced by ts_language_symbol_for_name(), and ts_language_symbol_name().

◆ ts_language_symbol_for_name()

TSSymbol ts_language_symbol_for_name ( const TSLanguage self,
const char *  string,
uint32_t  length,
bool  is_named 
)

Get the numerical id for the given node type string.

Definition at line 74 of file language.c.

79  {
80  if (!strncmp(string, "ERROR", length)) return ts_builtin_sym_error;
82  for (TSSymbol i = 0; i < count; i++) {
84  if ((!metadata.visible && !metadata.supertype) || metadata.named != is_named) continue;
85  const char *symbol_name = self->symbol_names[i];
86  if (!strncmp(symbol_name, string, length) && !symbol_name[length]) {
87  return self->public_symbol_map[i];
88  }
89  }
90  return 0;
91 }
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 length
Definition: sflib.h:133
uint32_t ts_language_symbol_count(const TSLanguage *self)
Definition: language.c:6
TSSymbolMetadata ts_language_symbol_metadata(const TSLanguage *self, TSSymbol symbol)
Definition: language.c:38
bool visible
Definition: parser.h:36
bool supertype
Definition: parser.h:38

References count, i, length, TSSymbolMetadata::named, TSSymbolMetadata::supertype, ts_builtin_sym_error, ts_language_symbol_count(), ts_language_symbol_metadata(), and TSSymbolMetadata::visible.

Referenced by rz_core_cmd_new(), and ts_query__parse_pattern().

◆ ts_language_symbol_metadata()

TSSymbolMetadata ts_language_symbol_metadata ( const TSLanguage self,
TSSymbol  symbol 
)

◆ ts_language_symbol_name()

const char* ts_language_symbol_name ( const TSLanguage self,
TSSymbol  symbol 
)

Get a node type string for the given numerical id.

Definition at line 59 of file language.c.

62  {
63  if (symbol == ts_builtin_sym_error) {
64  return "ERROR";
65  } else if (symbol == ts_builtin_sym_error_repeat) {
66  return "_ERROR";
67  } else if (symbol < ts_language_symbol_count(self)) {
68  return self->symbol_names[symbol];
69  } else {
70  return NULL;
71  }
72 }

References NULL, ts_builtin_sym_error, ts_builtin_sym_error_repeat, and ts_language_symbol_count().

Referenced by ts_node_type(), ts_query__analyze_patterns(), ts_stack_print_dot_graph(), ts_subtree__print_dot_graph(), and ts_subtree__write_to_string().

◆ ts_language_symbol_type()

TSSymbolType ts_language_symbol_type ( const TSLanguage self,
TSSymbol  symbol 
)

Check whether the given node type id belongs to named nodes, anonymous nodes, or a hidden nodes.

See also ts_node_is_named. Hidden nodes are never returned from the API.

Definition at line 93 of file language.c.

96  {
97  TSSymbolMetadata metadata = ts_language_symbol_metadata(self, symbol);
98  if (metadata.named && metadata.visible) {
99  return TSSymbolTypeRegular;
100  } else if (metadata.visible) {
101  return TSSymbolTypeAnonymous;
102  } else {
103  return TSSymbolTypeAuxiliary;
104  }
105 }
@ TSSymbolTypeAuxiliary
Definition: api.h:52
@ TSSymbolTypeRegular
Definition: api.h:50
@ TSSymbolTypeAnonymous
Definition: api.h:51

References TSSymbolMetadata::named, ts_language_symbol_metadata(), TSSymbolTypeAnonymous, TSSymbolTypeAuxiliary, TSSymbolTypeRegular, and TSSymbolMetadata::visible.

Referenced by ts_language_type_is_named_wasm(), and ts_language_type_is_visible_wasm().

◆ 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 }
static uint16_t ts_language_lookup(const TSLanguage *self, TSStateId state, TSSymbol symbol)
Definition: language.h:74
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
Definition: zipcmp.c:77
Definition: dis.h:43

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_language_version()

uint32_t ts_language_version ( const TSLanguage self)

Get the ABI version number for this language. This version number is used to ensure that languages were generated by a compatible version of Tree-sitter.

See also ts_parser_set_language.

Definition at line 10 of file language.c.

10  {
11  return self->version;
12 }