File indexing completed on 2024-04-14 04:31:15
0001 /* This file is part of KDevelop 0002 * 0003 * Copyright (C) 2010-2015 Miquel Sabaté Solà <mikisabate@gmail.com> 0004 * 0005 * This program is free software: you can redistribute it and/or modify 0006 * it under the terms of the GNU General Public License as published by 0007 * the Free Software Foundation, either version 3 of the License, or 0008 * (at your option) any later version. 0009 * 0010 * This program is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 * GNU General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU General Public License 0016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0017 */ 0018 0019 0020 #ifndef NODE_H_ 0021 #define NODE_H_ 0022 0023 0024 #ifdef __cplusplus 0025 0026 namespace ruby { 0027 extern "C" { 0028 #endif 0029 0030 #include <parser/export.h> 0031 0032 /** 0033 * This enumeration contains all the available tokens 0034 * used by this parser. This values are important for the 0035 * tests, so don't touch it unless you are really aware of 0036 * its side effects. 0037 */ 0038 enum node_t { 0039 token_invalid = 0, 0040 token_comment = 1, /* Not used anymore */ 0041 token_plus, 0042 token_minus, 0043 token_mul, 0044 token_pow, 0045 token_div, 0046 token_mod, 0047 token_bit_and, 0048 token_bit_or, 0049 token_bit_xor, /* 10 */ 0050 token_kw_and, 0051 token_kw_or, 0052 token_kw_not, 0053 token_or, 0054 token_and, 0055 token_lshift, 0056 token_rshift, 0057 token_neg, 0058 token_not, 0059 token_unary_plus, /* 20 */ 0060 token_unary_minus, 0061 token_assign, 0062 token_op_assign, 0063 token_assoc, 0064 token_cmp, 0065 token_eq, 0066 token_neq, 0067 token_eqq, 0068 token_match, 0069 token_nmatch, /* 30 */ 0070 token_greater, 0071 token_geq, 0072 token_lesser, 0073 token_leq, 0074 token_dot2, 0075 token_dot3, 0076 token_ternary, 0077 token_if, 0078 token_unless, 0079 token_while, /* 40 */ 0080 token_until, 0081 token_case, 0082 token_when, 0083 token_up_begin, 0084 token_up_end, 0085 token_for, 0086 token_begin, 0087 token_rescue_arg, 0088 token_rescue, 0089 token_ensure, /* 50 */ 0090 token_object, 0091 token_numeric, 0092 token_symbol, 0093 token_body, 0094 token_function, 0095 token_module, 0096 token_class, 0097 token_singleton_class, 0098 token_super, 0099 token_string, /* 60 */ 0100 token_regexp, 0101 token_key, 0102 token_array, 0103 token_hash, 0104 token_block, 0105 token_method_call, 0106 token_heredoc, 0107 token_break, 0108 token_redo, 0109 token_retry, /* 70 */ 0110 token_next, 0111 token_return, 0112 token_yield, 0113 token_alias, 0114 token_defined, 0115 token_undef, 0116 token_array_value, 0117 token__end__, 0118 token_true, 0119 token_false, /* 80 */ 0120 token_nil, 0121 token_self, 0122 token_encoding, 0123 token_file, 0124 token_line, /* 85 */ 0125 }; 0126 0127 0128 /** 0129 * This structure stores errors/warnings on the parsing. 0130 */ 0131 struct error_t { 0132 char *msg; 0133 int line, column; 0134 unsigned char warning : 1; 0135 struct error_t *next; 0136 }; 0137 0138 /** 0139 * This is the AST generated by the parser. It contains the tree 0140 * and some stats that make things easier to the program that is 0141 * using this parser. 0142 */ 0143 struct ast_t { 0144 struct Node *tree; 0145 struct error_t *errors; 0146 unsigned char unrecoverable : 1; 0147 }; 0148 0149 /** 0150 * This structure represents a position inside the document 0151 * that is being parsed. 0152 */ 0153 struct pos_t { 0154 int start_line, end_line; 0155 int start_col, end_col; 0156 unsigned long long offset; 0157 }; 0158 0159 /** 0160 * These are the values that should go inside the flags 0161 * attribute of the node struct. 0162 */ 0163 enum flags_t { 0164 /* Numeric literals. */ 0165 int_l = 0, 0166 float_l, 0167 rational_l, 0168 imaginary_l, 0169 0170 /* Variables */ 0171 var, 0172 ivar, 0173 cvar, 0174 constant, 0175 global, 0176 0177 /* Args */ 0178 kwrest, 0179 star, 0180 label, 0181 opt, 0182 block, 0183 }; 0184 0185 /** 0186 * This structure defines a node 0187 * in the abstract syntax tree 0188 */ 0189 struct Node { 0190 /* Node info */ 0191 int kind; 0192 enum flags_t flags; 0193 char *name; 0194 void *context; 0195 0196 /* Node's position */ 0197 struct pos_t pos; 0198 char *comment; 0199 0200 /* Left/Right childs */ 0201 struct Node *l; 0202 struct Node *r; 0203 0204 /* Condition expression */ 0205 struct Node *cond; 0206 0207 /* Ensure expression (Exception-AST only) */ 0208 struct Node *ensure; 0209 0210 /* List of inner statements */ 0211 struct Node *next; 0212 struct Node *last; 0213 }; 0214 0215 /** 0216 * This is a convenient enum to store the version of Ruby to be used. 0217 */ 0218 enum ruby_version { 0219 ruby18 = 0, /* 1.8.x branch */ 0220 ruby19 = 1, /* 1.9.x branch */ 0221 ruby20 = 2, /* 2.0.x branch */ 0222 ruby21 = 3, /* 2.1.x branch */ 0223 }; 0224 0225 /* 0226 * The different access specifiers available in Ruby. 0227 */ 0228 enum access_t { 0229 public_a = 0, 0230 protected_a = 1, 0231 private_a = 2, 0232 }; 0233 0234 /** 0235 * This struct contains all the options that may be passed 0236 * to the parser. 0237 */ 0238 struct options_t { 0239 const char *path; 0240 char *contents; 0241 enum ruby_version version; 0242 }; 0243 0244 /* Interface to the parser */ 0245 0246 /** 0247 * Generate the AST of a given ruby file. 0248 * 0249 * @param opts The options passed to the parser. 0250 * @return an AST that represents the code. 0251 */ 0252 KDEVRUBYPARSER_EXPORT struct ast_t * rb_compile_file(struct options_t *opts); 0253 0254 /** 0255 * Free an ast_t. 0256 * 0257 * @param ra the ast_t you want to free. 0258 */ 0259 KDEVRUBYPARSER_EXPORT void rb_free(struct ast_t *ra); 0260 0261 /** 0262 * Get the name node. 0263 * 0264 * @param n The root node. 0265 */ 0266 KDEVRUBYPARSER_EXPORT struct Node * rb_name_node(struct Node *n); 0267 0268 /** 0269 * Free the node of an ast_t. Note that this function is already called 0270 * by the rb_free function. 0271 * 0272 * @param n The root node. 0273 */ 0274 void free_ast(struct Node *n); 0275 0276 /** 0277 * Free the errors of an ast_t. Note that this function is already called 0278 * by the rb_free function. 0279 * 0280 * @param ra the ast_t containing the errors to be freed. 0281 */ 0282 void free_errors(struct ast_t *ra); 0283 0284 /* 0285 * There are three ways to allocate a node. The simplest one is 0286 * alloc_node. If the node has also a conditional expression, we 0287 * should use alloc_cond. Moreover, if the node has an ensure statement, 0288 * we should use alloc_ensure. 0289 */ 0290 struct Node * alloc_node(int kind, struct Node *l, struct Node *r); 0291 struct Node * alloc_cond(int kind, struct Node *cond, struct Node *l, 0292 struct Node *r); 0293 struct Node * alloc_ensure(int kind, struct Node * l, struct Node *r, 0294 struct Node *els, struct Node *ensure); 0295 0296 0297 /* 0298 * These functions play with the next and last attributes of a node. 0299 * They're useful to create or update a list of nodes or to concatenate 0300 * two different lists, 0301 */ 0302 struct Node * create_list(struct Node *head, struct Node *tail); 0303 struct Node * update_list(struct Node *head, struct Node *tail); 0304 struct Node * concat_list(struct Node *head, struct Node *tail); 0305 #define pop_list(head, tail) \ 0306 (tail->last == NULL) ? update_list(head, tail) : create_list(head, tail) 0307 0308 0309 /* Debugging utilities */ 0310 0311 #ifdef BUILD_TESTS 0312 void print_node(struct Node *n); 0313 KDEVRUBYPARSER_EXPORT void print_errors(struct error_t *errors); 0314 #endif 0315 0316 0317 /* 0318 * Auxiliar macros. 0319 */ 0320 0321 #define get_last_expr(n) ((n->last) ? n->last : n) 0322 #define is_valid(n) (n->pos.start_line >= 0) 0323 #define is_rest_arg(n) (n->flags == kwrest) 0324 #define is_block_arg(n) (n->flags == block) 0325 #define is_global_var(n) (n->flags == global) 0326 #define is_ivar(n) (n->flags == ivar) 0327 #define is_cvar(n) (n->flags == cvar) 0328 #define is_constant (n->flags == constant) 0329 #define valid_children(n) (n->r && n->l) 0330 #define is_super(n) (!n->l) 0331 #define has_star(n) (n->flags == kwrest || n->flags == star) 0332 #define is_just_a_star(n) (n->flags == star) 0333 0334 0335 #ifdef __cplusplus 0336 } 0337 } 0338 #endif 0339 0340 0341 #endif /* NODE_H_ */