Rizin
unix-like reverse engineering framework and cli tools
scanner.c File Reference
#include <tree_sitter/parser.h>
#include <ctype.h>
#include <wctype.h>
#include <stdio.h>
#include <string.h>

Go to the source code of this file.

Macros

#define CMD_IDENTIFIER_MAX_LENGTH   32
 
#define ESCAPE_CHAR   '\\'
 

Enumerations

enum  TokenType {
  CMD_IDENTIFIER , HELP_STMT , FILE_DESCRIPTOR , EQ_SEP_CONCAT ,
  CONCAT , CONCAT_PF_DOT , SPEC_SEP
}
 

Functions

void * tree_sitter_rzcmd_external_scanner_create ()
 
void tree_sitter_rzcmd_external_scanner_destroy (void *payload)
 
unsigned tree_sitter_rzcmd_external_scanner_serialize (void *payload, char *buffer)
 
void tree_sitter_rzcmd_external_scanner_deserialize (void *payload, const char *buffer, unsigned length)
 
static bool is_pf_cmd (const char *s)
 
static bool is_env_cmd (const char *s)
 
static bool is_at_cmd (const char *s)
 
static bool is_remote_cmd (const char *s)
 
static bool is_comment (const char *s)
 
static bool is_interpret_cmd (const char *s)
 
static bool is_special_start (const int32_t ch)
 
static bool is_start_of_command (const int32_t ch)
 
static bool is_mid_command (const char *res, int len, const int32_t ch)
 
static bool is_concat (const int32_t ch)
 
static bool is_concat_pf_dot (const int32_t ch)
 
static bool is_concat_eq_sep (const int32_t ch)
 
static bool is_recursive_help (const int32_t before_last_ch, const int32_t last_ch)
 
static bool is_recursive_help_json (const int32_t trd_last_ch, const int32_t snd_last_ch, const int32_t last_ch)
 
static bool scan_number (TSLexer *lexer, const bool *valid_symbols)
 
bool tree_sitter_rzcmd_external_scanner_scan (void *payload, TSLexer *lexer, const bool *valid_symbols)
 

Macro Definition Documentation

◆ CMD_IDENTIFIER_MAX_LENGTH

#define CMD_IDENTIFIER_MAX_LENGTH   32

Definition at line 10 of file scanner.c.

◆ ESCAPE_CHAR

#define ESCAPE_CHAR   '\\'

Definition at line 11 of file scanner.c.

Enumeration Type Documentation

◆ TokenType

enum TokenType
Enumerator
CMD_IDENTIFIER 
HELP_STMT 
FILE_DESCRIPTOR 
EQ_SEP_CONCAT 
CONCAT 
CONCAT_PF_DOT 
SPEC_SEP 

Definition at line 13 of file scanner.c.

13  {
15  HELP_STMT,
18  CONCAT,
20  SPEC_SEP,
21 };
@ EQ_SEP_CONCAT
Definition: scanner.c:17
@ HELP_STMT
Definition: scanner.c:15
@ CMD_IDENTIFIER
Definition: scanner.c:14
@ FILE_DESCRIPTOR
Definition: scanner.c:16
@ SPEC_SEP
Definition: scanner.c:20
@ CONCAT
Definition: scanner.c:18
@ CONCAT_PF_DOT
Definition: scanner.c:19

Function Documentation

◆ is_at_cmd()

static bool is_at_cmd ( const char *  s)
static

Definition at line 45 of file scanner.c.

45  {
46  return s[0] == '@';
47 }
static RzSocket * s
Definition: rtr.c:28

References s.

Referenced by is_mid_command(), and tree_sitter_rzcmd_external_scanner_scan().

◆ is_comment()

static bool is_comment ( const char *  s)
static

Definition at line 53 of file scanner.c.

53  {
54  return !strncmp (s, "/*", 2) || !strcmp (s, "#");
55 }

References s.

Referenced by ds_esc_str(), and tree_sitter_rzcmd_external_scanner_scan().

◆ is_concat()

static bool is_concat ( const int32_t  ch)
static

Definition at line 92 of file scanner.c.

92  {
93  return ch != '\0' && !iswspace(ch) && ch != '#' && ch != '@' &&
94  ch != '|' && ch != '>' && ch != ';' &&
95  ch != ')' && ch != '`' && ch != '~' && ch != '\\';
96 }

Referenced by is_concat_eq_sep(), is_concat_pf_dot(), and tree_sitter_rzcmd_external_scanner_scan().

◆ is_concat_eq_sep()

static bool is_concat_eq_sep ( const int32_t  ch)
static

Definition at line 102 of file scanner.c.

102  {
103  return is_concat(ch) && ch != '=';
104 }
static bool is_concat(const int32_t ch)
Definition: scanner.c:92

References is_concat().

Referenced by tree_sitter_rzcmd_external_scanner_scan().

◆ is_concat_pf_dot()

static bool is_concat_pf_dot ( const int32_t  ch)
static

Definition at line 98 of file scanner.c.

98  {
99  return is_concat(ch) && ch != '=';
100 }

References is_concat().

Referenced by tree_sitter_rzcmd_external_scanner_scan().

◆ is_env_cmd()

static bool is_env_cmd ( const char *  s)
static

Definition at line 41 of file scanner.c.

41  {
42  return !strncmp (s, "env", 3);
43 }

References s.

Referenced by tree_sitter_rzcmd_external_scanner_scan().

◆ is_interpret_cmd()

static bool is_interpret_cmd ( const char *  s)
static

Definition at line 57 of file scanner.c.

57  {
58  return s[0] == '.';
59 }

References s.

Referenced by is_mid_command().

◆ is_mid_command()

static bool is_mid_command ( const char *  res,
int  len,
const int32_t  ch 
)
static

Definition at line 73 of file scanner.c.

73  {
74  if (ch == ESCAPE_CHAR) {
75  return true;
76  }
77  if (res[0] == '#') {
78  if (len == 1) {
79  return ch == '!' || ch == '?';
80  }
81  return ch == '?';
82  } else if (res[0] == '<') {
83  return ch == '?';
84  }
85  return iswalnum (ch) || ch == '$' || ch == '?' || ch == '.' || ch == '!' ||
86  ch == '+' || ch == '=' || ch == '/' || ch == '*' ||
87  ch == '-' || ch == '&' || ch == '_' ||
88  (is_interpret_cmd (res) && ch == '(') ||
89  (is_remote_cmd (res) && ch == '<') || (is_at_cmd (res) && ch == '@');
90 }
size_t len
Definition: 6502dis.c:15
static bool is_interpret_cmd(const char *s)
Definition: scanner.c:57
static bool is_at_cmd(const char *s)
Definition: scanner.c:45
#define ESCAPE_CHAR
Definition: scanner.c:11
static bool is_remote_cmd(const char *s)
Definition: scanner.c:49

References ESCAPE_CHAR, is_at_cmd(), is_interpret_cmd(), is_remote_cmd(), and len.

Referenced by tree_sitter_rzcmd_external_scanner_scan().

◆ is_pf_cmd()

static bool is_pf_cmd ( const char *  s)
static

Definition at line 37 of file scanner.c.

37  {
38  return (strcmp (s, "pfo") && !strncmp (s, "pf", 2)) || !strcmp (s, "Cf");
39 }

References s.

Referenced by tree_sitter_rzcmd_external_scanner_scan().

◆ is_recursive_help()

static bool is_recursive_help ( const int32_t  before_last_ch,
const int32_t  last_ch 
)
static

Definition at line 106 of file scanner.c.

106  {
107  return before_last_ch == '?' && last_ch == '*';
108 }

Referenced by tree_sitter_rzcmd_external_scanner_scan().

◆ is_recursive_help_json()

static bool is_recursive_help_json ( const int32_t  trd_last_ch,
const int32_t  snd_last_ch,
const int32_t  last_ch 
)
static

Definition at line 110 of file scanner.c.

110  {
111  return trd_last_ch == '?' && snd_last_ch == '*' && last_ch == 'j';
112 }

Referenced by tree_sitter_rzcmd_external_scanner_scan().

◆ is_remote_cmd()

static bool is_remote_cmd ( const char *  s)
static

Definition at line 49 of file scanner.c.

49  {
50  return s[0] == 'R';
51 }

References s.

Referenced by is_mid_command().

◆ is_special_start()

static bool is_special_start ( const int32_t  ch)
static

Definition at line 61 of file scanner.c.

61  {
62  return ch == '*' || ch == '(' || ch == '@' || ch == '|' || ch == '>' ||
63  ch == '.' || ch == '|' || ch == '%' || ch == '~' ||
64  ch == '!';
65 }

Referenced by is_start_of_command(), and tree_sitter_rzcmd_external_scanner_scan().

◆ is_start_of_command()

static bool is_start_of_command ( const int32_t  ch)
static

Definition at line 67 of file scanner.c.

67  {
68  return iswalpha (ch) || ch == '$' || ch == '?' || ch == ':' || ch == '+' ||
69  ch == '=' || ch == '/' || ch == '_' || ch == '#' || ch == '\\' ||
70  ch == '-' || ch == '<' || ch == '&' || is_special_start (ch);
71 }
static bool is_special_start(const int32_t ch)
Definition: scanner.c:61

References is_special_start().

Referenced by tree_sitter_rzcmd_external_scanner_scan().

◆ scan_number()

static bool scan_number ( TSLexer lexer,
const bool valid_symbols 
)
static

Definition at line 114 of file scanner.c.

114  {
115  if (!valid_symbols[FILE_DESCRIPTOR]) {
116  return false;
117  }
118 
119  // skip spaces at the beginning
120  while (iswspace (lexer->lookahead)) {
121  lexer->advance (lexer, true);
122  }
123 
124  if (!iswdigit (lexer->lookahead)) {
125  return false;
126  }
127  lexer->advance (lexer, false);
128  for (;;) {
129  if (iswdigit (lexer->lookahead)) {
130  lexer->advance (lexer, false);
131  } else if (lexer->lookahead != '>') {
132  return false;
133  } else {
134  break;
135  }
136  }
137  if (lexer->lookahead == '>') {
139  return true;
140  }
141  return false;
142 }
void(* advance)(TSLexer *, bool)
Definition: parser.h:46
int32_t lookahead
Definition: parser.h:44
TSSymbol result_symbol
Definition: parser.h:45

References TSLexer::advance, FILE_DESCRIPTOR, TSLexer::lookahead, and TSLexer::result_symbol.

Referenced by tree_sitter_rzcmd_external_scanner_scan().

◆ tree_sitter_rzcmd_external_scanner_create()

void* tree_sitter_rzcmd_external_scanner_create ( void  )

Definition at line 23 of file scanner.c.

23  {
24  return NULL;
25 }
#define NULL
Definition: cris-opc.c:27

References NULL.

Referenced by tree_sitter_rzcmd().

◆ tree_sitter_rzcmd_external_scanner_deserialize()

void tree_sitter_rzcmd_external_scanner_deserialize ( void *  payload,
const char *  buffer,
unsigned  length 
)

Definition at line 34 of file scanner.c.

34  {
35 }

Referenced by tree_sitter_rzcmd().

◆ tree_sitter_rzcmd_external_scanner_destroy()

void tree_sitter_rzcmd_external_scanner_destroy ( void *  payload)

Definition at line 27 of file scanner.c.

27  {
28 }

Referenced by tree_sitter_rzcmd().

◆ tree_sitter_rzcmd_external_scanner_scan()

bool tree_sitter_rzcmd_external_scanner_scan ( void *  payload,
TSLexer lexer,
const bool valid_symbols 
)

Definition at line 144 of file scanner.c.

144  {
145  if (valid_symbols[SPEC_SEP] && lexer->lookahead == ':') {
146  lexer->advance(lexer, false);
147  lexer->result_symbol = SPEC_SEP;
148  return true;
149  } else if (valid_symbols[CONCAT] && is_concat(lexer->lookahead)) {
150  lexer->result_symbol = CONCAT;
151  return true;
152  } else if (valid_symbols[CONCAT_PF_DOT] && is_concat_pf_dot(lexer->lookahead)) {
153  lexer->result_symbol = CONCAT_PF_DOT;
154  return true;
155  } else if (valid_symbols[EQ_SEP_CONCAT] && is_concat_eq_sep(lexer->lookahead)) {
156  lexer->result_symbol = EQ_SEP_CONCAT;
157  return true;
158  }
159  if (valid_symbols[CMD_IDENTIFIER] || valid_symbols[HELP_STMT]) {
160  char res[CMD_IDENTIFIER_MAX_LENGTH + 1];
161  int i_res = 0;
162 
163  while (iswspace (lexer->lookahead)) {
164  lexer->advance (lexer, true);
165  }
166 
167  if (!is_start_of_command (lexer->lookahead)) {
168  return false;
169  }
170  res[i_res++] = lexer->lookahead;
171  lexer->advance (lexer, false);
172  while (lexer->lookahead && i_res < CMD_IDENTIFIER_MAX_LENGTH && is_mid_command (res, i_res, lexer->lookahead)) {
173  if (lexer->lookahead == ESCAPE_CHAR) {
174  // ignore escape char and just get the next one, whatever it is
175  lexer->advance (lexer, false);
176  }
177  res[i_res++] = lexer->lookahead;
178  lexer->advance (lexer, false);
179  }
180  res[i_res] = '\0';
181  if (is_comment (res)) {
182  return false;
183  }
184  // ?? is not considered an help command, just a regular one
185  if ((res[i_res - 1] == '?' && strcmp (res, "??") != 0) ||
186  (i_res > 2 && is_recursive_help (res[i_res - 2], res[i_res - 1])) ||
187  (i_res > 3 && is_recursive_help_json (res[i_res - 3], res[i_res - 2], res[i_res - 1]))) {
188  if (i_res == 1) {
189  return false;
190  }
191  lexer->result_symbol = HELP_STMT;
192  } else {
193  if ((is_special_start(res[0]) && strcmp(res, "R=!")) || is_pf_cmd(res) || is_env_cmd(res) || is_at_cmd(res) || !valid_symbols[CMD_IDENTIFIER]) {
194  return false;
195  }
196  lexer->result_symbol = CMD_IDENTIFIER;
197  }
198  return true;
199  }
200  if (valid_symbols[FILE_DESCRIPTOR]) {
201  return scan_number (lexer, valid_symbols);
202  }
203  return false;
204 }
static bool scan_number(TSLexer *lexer, const bool *valid_symbols)
Definition: scanner.c:114
#define CMD_IDENTIFIER_MAX_LENGTH
Definition: scanner.c:10
static bool is_recursive_help_json(const int32_t trd_last_ch, const int32_t snd_last_ch, const int32_t last_ch)
Definition: scanner.c:110
static bool is_concat_eq_sep(const int32_t ch)
Definition: scanner.c:102
static bool is_recursive_help(const int32_t before_last_ch, const int32_t last_ch)
Definition: scanner.c:106
static bool is_mid_command(const char *res, int len, const int32_t ch)
Definition: scanner.c:73
static bool is_pf_cmd(const char *s)
Definition: scanner.c:37
static bool is_env_cmd(const char *s)
Definition: scanner.c:41
static bool is_comment(const char *s)
Definition: scanner.c:53
static bool is_concat_pf_dot(const int32_t ch)
Definition: scanner.c:98
static bool is_start_of_command(const int32_t ch)
Definition: scanner.c:67

References TSLexer::advance, CMD_IDENTIFIER, CMD_IDENTIFIER_MAX_LENGTH, CONCAT, CONCAT_PF_DOT, EQ_SEP_CONCAT, ESCAPE_CHAR, FILE_DESCRIPTOR, HELP_STMT, is_at_cmd(), is_comment(), is_concat(), is_concat_eq_sep(), is_concat_pf_dot(), is_env_cmd(), is_mid_command(), is_pf_cmd(), is_recursive_help(), is_recursive_help_json(), is_special_start(), is_start_of_command(), TSLexer::lookahead, TSLexer::result_symbol, scan_number(), and SPEC_SEP.

Referenced by tree_sitter_rzcmd().

◆ tree_sitter_rzcmd_external_scanner_serialize()

unsigned tree_sitter_rzcmd_external_scanner_serialize ( void *  payload,
char *  buffer 
)

Definition at line 30 of file scanner.c.

30  {
31  return 0;
32 }

Referenced by tree_sitter_rzcmd().