|
| 1 | +#undef RUBY_EXPORT |
| 2 | +#include "ruby.h" |
| 3 | +#include "vm_debug.h" |
| 4 | +#ifdef HAVE_LOCALE_H |
| 5 | +#include <locale.h> |
| 6 | +#endif |
| 7 | + |
| 8 | +#define RUBY_PARSER_PRINT_TOKENS 1 |
| 9 | +void mri_tokenizer_print_token(void *token); |
| 10 | +#include "parse.c" |
| 11 | +#include <stdbool.h> |
| 12 | + |
| 13 | +#ifdef yydebug |
| 14 | +#undef yydebug |
| 15 | +#endif |
| 16 | + |
| 17 | +void mri_tokenizer_print_token(void *token) |
| 18 | +{ |
| 19 | + enum yytokentype *t = token; |
| 20 | + const char *token_name = yytname[YYTRANSLATE(*t)]; |
| 21 | + |
| 22 | +#define map_token(from, to) \ |
| 23 | + if (strcmp(token_name, from) == 0) { \ |
| 24 | + token_name = to; \ |
| 25 | + } |
| 26 | + |
| 27 | + // keywords |
| 28 | + map_token("\"`def'\"", "kDEF"); |
| 29 | + map_token("\"`module'\"", "kMODULE"); |
| 30 | + map_token("\"`def'\"", "kDEF"); |
| 31 | + map_token("\"`undef'\"", "kUNDEF"); |
| 32 | + map_token("\"`begin'\"", "kBEGIN"); |
| 33 | + map_token("\"`rescue'\"", "kRESCUE"); |
| 34 | + map_token("\"`ensure'\"", "kENSURE"); |
| 35 | + map_token("\"`end'\"", "kEND"); |
| 36 | + map_token("\"`if'\"", "kIF"); |
| 37 | + map_token("\"`unless'\"", "kUNLESS"); |
| 38 | + map_token("\"`then'\"", "kTHEN"); |
| 39 | + map_token("\"`elsif'\"", "kELSIF"); |
| 40 | + map_token("\"`else'\"", "kELSE"); |
| 41 | + map_token("\"`case'\"", "kCASE"); |
| 42 | + map_token("\"`when'\"", "kWHEN"); |
| 43 | + map_token("\"`while'\"", "kWHILE"); |
| 44 | + map_token("\"`until'\"", "kUNTIL"); |
| 45 | + map_token("\"`for'\"", "kFOR"); |
| 46 | + map_token("\"`break'\"", "kBREAK"); |
| 47 | + map_token("\"`next'\"", "kNEXT"); |
| 48 | + map_token("\"`redo'\"", "kREDO"); |
| 49 | + map_token("\"`retry'\"", "kRETRY"); |
| 50 | + map_token("\"`in'\"", "kIN"); |
| 51 | + map_token("\"`do'\"", "kDO"); |
| 52 | + map_token("\"`do'\" for condition", "kDO_COND"); |
| 53 | + map_token("\"`do'\" for block", "kDO_BLOCK"); |
| 54 | + map_token("\"`do'\" for lambda", "kDO_LAMBDA"); |
| 55 | + map_token("\"`return'\"", "kRETURN"); |
| 56 | + map_token("\"`yield'\"", "kYIELD"); |
| 57 | + map_token("\"`super'\"", "kSUPER"); |
| 58 | + map_token("\"`self'\"", "kSELF"); |
| 59 | + map_token("\"`nil'\"", "kNIL"); |
| 60 | + map_token("\"`true'\"", "kTRUE"); |
| 61 | + map_token("\"`false'\"", "kFALSE"); |
| 62 | + map_token("\"`and'\"", "kAND"); |
| 63 | + map_token("\"`or'\"", "kOR"); |
| 64 | + map_token("\"`not'\"", "kNOT"); |
| 65 | + map_token("\"`if' modifier\"", "kIF_MOD"); |
| 66 | + map_token("\"`unless' modifier\"", "kUNLESS_MOD"); |
| 67 | + map_token("\"`while' modifier\"", "kWHILE_MOD"); |
| 68 | + map_token("\"`until' modifier\"", "kUNTIL_MOD"); |
| 69 | + map_token("\"`rescue' modifier\"", "kRESCUE_MOD"); |
| 70 | + map_token("\"`alias'\"", "kALIAS"); |
| 71 | + map_token("\"`defined?'\"", "kDEFINED"); |
| 72 | + map_token("\"`BEGIN'\"", "klBEGIN"); |
| 73 | + map_token("\"`END'\"", "klEND"); |
| 74 | + map_token("\"`__LINE__'\"", "k__LINE__"); |
| 75 | + map_token("\"`__FILE__'\"", "k__FILE__"); |
| 76 | + map_token("\"`__ENCODING__'\"", "k__ENCODING__"); |
| 77 | + |
| 78 | + map_token("\"local variable or method\"", "tIDENTIFIER"); |
| 79 | + map_token("\"method\"", "tFID"); |
| 80 | + map_token("\"global variable\"", "tGVAR"); |
| 81 | + map_token("\"instance variable\"", "tIVAR"); |
| 82 | + map_token("\"constant\"", "tCONSTANT"); |
| 83 | + map_token("\"class variable\"", "tCVAR"); |
| 84 | + map_token("\"label\"", "tLABEL"); |
| 85 | + map_token("\"integer literal\"", "tINTEGER"); |
| 86 | + map_token("\"float literal\"", "tFLOAT"); |
| 87 | + map_token("\"rational literal\"", "tRATIONAL"); |
| 88 | + map_token("\"imaginary literal\"", "tIMAGINARY"); |
| 89 | + map_token("\"char literal\"", "tCHAR"); |
| 90 | + map_token("\"numbered reference\"", "tNTH_REF"); |
| 91 | + map_token("\"back reference\"", "tBACK_REF"); |
| 92 | + map_token("\"literal content\"", "tSTRING_CONTENT"); |
| 93 | + map_token("tREGEXP_END", "tREGEXP_END"); |
| 94 | + |
| 95 | + map_token("'.'", "tDOT"); |
| 96 | + map_token("\"backslash\"", "tBACKSLASH"); |
| 97 | + map_token("\"escaped space\"", "tSP"); |
| 98 | + map_token("\"escaped horizontal tab\"", "tSLASH_T"); |
| 99 | + map_token("\"escaped form feed\"", "tSLASH_F"); |
| 100 | + map_token("\"escaped carriage return\"", "tSLASH_R"); |
| 101 | + map_token("\"escaped vertical tab\"", "tVTAB"); |
| 102 | + map_token("\"unary+\"", "tUPLUS"); |
| 103 | + map_token("\"unary-\"", "tUMINUS"); |
| 104 | + map_token("\"**\"", "tPOW"); |
| 105 | + map_token("\"<=>\"", "tCMP"); |
| 106 | + map_token("\"==\"", "tEQ"); |
| 107 | + map_token("\"===\"", "tEQQ"); |
| 108 | + map_token("\"!=\"", "tNEQ"); |
| 109 | + map_token("\">=\"", "tGEQ"); |
| 110 | + map_token("\"<=\"", "tLEQ"); |
| 111 | + map_token("\"&&\"", "tANDOP"); |
| 112 | + map_token("\"||\"", "tOROP"); |
| 113 | + map_token("\"=~\"", "tMATCH"); |
| 114 | + map_token("\"!~\"", "tNMATCH"); |
| 115 | + map_token("\"..\"", "tDOT2"); |
| 116 | + map_token("\"...\"", "tDOT3"); |
| 117 | + map_token("\"(..\"", "tBDOT2"); |
| 118 | + map_token("\"(...\"", "tBDOT3"); |
| 119 | + map_token("\"[]\"", "tAREF"); |
| 120 | + map_token("\"[]=\"", "tASET"); |
| 121 | + map_token("\"<<\"", "tLSHFT"); |
| 122 | + map_token("\">>\"", "tRSHFT"); |
| 123 | + map_token("\"&.\"", "tANDDOT"); |
| 124 | + map_token("\"::\"", "tCOLON2"); |
| 125 | + map_token("\":: at EXPR_BEG\"", "tCOLON3"); |
| 126 | + map_token("\"operator-assignment\"", "tOP_ASGN"); |
| 127 | + map_token("\"=>\"", "tASSOC"); |
| 128 | + map_token("\"(\"", "tLPAREN"); |
| 129 | + map_token("\"( arg\"", "tLPAREN_ARG"); |
| 130 | + map_token("\")\"", "tRPAREN"); |
| 131 | + map_token("\"[\"", "tLBRACK"); |
| 132 | + map_token("\"{\"", "tLBRACE"); |
| 133 | + map_token("\"{ arg\"", "tLBRACE_ARG"); |
| 134 | + map_token("\"*\"", "tSTAR"); |
| 135 | + map_token("\"**arg\"", "tDSTAR"); |
| 136 | + map_token("\"&\"", "tAMPER"); |
| 137 | + map_token("\"->\"", "tLAMBDA"); |
| 138 | + map_token("\"symbol literal\"", "tSYMBEG"); |
| 139 | + map_token("\"string literal\"", "tSTRING_BEG"); |
| 140 | + map_token("\"backtick literal\"", "tXSTRING_BEG"); |
| 141 | + map_token("\"regexp literal\"", "tREGEXP_BEG"); |
| 142 | + map_token("\"word list\"", "tWORDS_BEG"); |
| 143 | + map_token("\"verbatim word list\"", "tQWORDS_BEG"); |
| 144 | + map_token("\"symbol list\"", "tSYMBOLS_BEG"); |
| 145 | + map_token("\"verbatim symbol list\"", "tQSYMBOLS_BEG"); |
| 146 | + map_token("\"terminator\"", "tSTRING_END"); |
| 147 | + map_token("\"'}'\"", "tSTRING_DEND"); |
| 148 | + map_token("tSTRING_DBEG", "tSTRING_DBEG"); |
| 149 | + map_token("tSTRING_DVAR", "tSTRING_DVAR"); |
| 150 | + map_token("tLAMBEG", "tLAMBEG"); |
| 151 | + map_token("tLABEL_END", "tLABEL_END"); |
| 152 | + // map_token("tLOWEST", ""); |
| 153 | + map_token("'='", "tEQL"); |
| 154 | + map_token("'?'", "tEH"); |
| 155 | + map_token("':'", "tCOLON"); |
| 156 | + map_token("'>'", "tGT"); |
| 157 | + map_token("'<'", "tLT"); |
| 158 | + map_token("'|'", "tPIPE"); |
| 159 | + map_token("'^'", "tCARET"); |
| 160 | + map_token("'&'", "tAMPER2"); |
| 161 | + map_token("'+'", "tPLUS"); |
| 162 | + map_token("'-'", "tMINUS"); |
| 163 | + map_token("'*'", "tSTAR2"); |
| 164 | + map_token("'/'", "tDIVIDE"); |
| 165 | + map_token("'%'", "tPERCENT"); |
| 166 | + map_token("tUMINUS_NUM", "tUMINUS_NUM"); |
| 167 | + map_token("'!'", "tBANG"); |
| 168 | + map_token("'~'", "tTILDE"); |
| 169 | + // map_token("tLAST_TOKEN", ""); |
| 170 | + map_token("'{'", "tLCURLY"); |
| 171 | + map_token("'}'", "tRCURLY"); |
| 172 | + map_token("'['", "tLBRACK2"); |
| 173 | + map_token("','", "tCOMMA"); |
| 174 | + map_token("'`'", "tBACK_REF2"); |
| 175 | + map_token("'('", "tLPAREN2"); |
| 176 | + map_token("')'", "tRPAREN"); |
| 177 | + map_token("']'", "tRBRACK"); |
| 178 | + map_token("';'", "tSEMI"); |
| 179 | + map_token("' '", "tSPACE"); |
| 180 | + map_token("'\\n'", "tNL"); |
| 181 | + |
| 182 | + map_token("\"end-of-input\"", "END_OF_INPUT"); |
| 183 | + printf("%s\n", token_name); |
| 184 | +} |
| 185 | + |
| 186 | +void __parse(const char *filename, const char *src, bool yydebug) |
| 187 | +{ |
| 188 | + VALUE parser = rb_parser_new(); |
| 189 | + rb_parser_set_context(parser, NULL, FALSE); |
| 190 | + rb_parser_set_yydebug(parser, RBOOL(yydebug)); |
| 191 | + rb_parser_compile_string(parser, filename, rb_fstring_cstr(src), 0); |
| 192 | +} |
| 193 | + |
| 194 | +char *__read_file(const char *filepath) |
| 195 | +{ |
| 196 | + FILE *f = fopen(filepath, "rb"); |
| 197 | + if (!f) { |
| 198 | + return NULL; |
| 199 | + } |
| 200 | + fseek(f, 0, SEEK_END); |
| 201 | + long fsize = ftell(f); |
| 202 | + fseek(f, 0, SEEK_SET); |
| 203 | + |
| 204 | + char *fcontent = malloc(fsize + 1); |
| 205 | + (void)(fread(fcontent, 1, fsize, f)); |
| 206 | + fclose(f); |
| 207 | + fcontent[fsize] = 0; |
| 208 | + |
| 209 | + return fcontent; |
| 210 | +} |
| 211 | + |
| 212 | +int |
| 213 | +main(int argc, char **argv) |
| 214 | +{ |
| 215 | +#ifdef HAVE_LOCALE_H |
| 216 | + setlocale(LC_CTYPE, ""); |
| 217 | +#endif |
| 218 | + |
| 219 | + ruby_sysinit(&argc, &argv); |
| 220 | + { |
| 221 | + RUBY_INIT_STACK; |
| 222 | + ruby_init(); |
| 223 | + if (argc != 2) { |
| 224 | + printf("Usage: mri_tokenizer path/to/file.rb\n"); |
| 225 | + return 1; |
| 226 | + } |
| 227 | + char *filepath = argv[1]; |
| 228 | + char *code = __read_file(filepath); |
| 229 | + if (!code) { |
| 230 | + printf("Failed to read file '%s', aborting.\n", filepath); |
| 231 | + return 1; |
| 232 | + } |
| 233 | + __parse(filepath, code, false); |
| 234 | + free(code); |
| 235 | + return 0; |
| 236 | + } |
| 237 | +} |
0 commit comments