1 /** 2 ** @file mruby/compile.h - mruby parser 3 ** 4 ** See Copyright Notice in mruby.h 5 */ 6 7 #ifndef MRUBY_COMPILE_H 8 #define MRUBY_COMPILE_H 9 10 #include "common.h" 11 12 /** 13 * MRuby Compiler 14 */ 15 MRB_BEGIN_DECL 16 17 #include <mruby.h> 18 19 struct mrb_jmpbuf; 20 21 struct mrb_parser_state; 22 /* load context */ 23 typedef struct mrbc_context { 24 mrb_sym *syms; 25 int slen; 26 char *filename; 27 uint16_t lineno; 28 int (*partial_hook)(struct mrb_parser_state*); 29 void *partial_data; 30 struct RClass *target_class; 31 mrb_bool capture_errors:1; 32 mrb_bool dump_result:1; 33 mrb_bool no_exec:1; 34 mrb_bool keep_lv:1; 35 mrb_bool no_optimize:1; 36 struct RProc *upper; 37 38 size_t parser_nerr; 39 } mrbc_context; 40 41 MRB_API mrbc_context* mrbc_context_new(mrb_state *mrb); 42 MRB_API void mrbc_context_free(mrb_state *mrb, mrbc_context *cxt); 43 MRB_API const char *mrbc_filename(mrb_state *mrb, mrbc_context *c, const char *s); 44 MRB_API void mrbc_partial_hook(mrb_state *mrb, mrbc_context *c, int (*partial_hook)(struct mrb_parser_state*), void*data); 45 46 /* AST node structure */ 47 typedef struct mrb_ast_node { 48 struct mrb_ast_node *car, *cdr; 49 uint16_t lineno, filename_index; 50 } mrb_ast_node; 51 52 /* lexer states */ 53 enum mrb_lex_state_enum { 54 EXPR_BEG, /* ignore newline, +/- is a sign. */ 55 EXPR_END, /* newline significant, +/- is an operator. */ 56 EXPR_ENDARG, /* ditto, and unbound braces. */ 57 EXPR_ENDFN, /* ditto, and unbound braces. */ 58 EXPR_ARG, /* newline significant, +/- is an operator. */ 59 EXPR_CMDARG, /* newline significant, +/- is an operator. */ 60 EXPR_MID, /* newline significant, +/- is an operator. */ 61 EXPR_FNAME, /* ignore newline, no reserved words. */ 62 EXPR_DOT, /* right after '.' or '::', no reserved words. */ 63 EXPR_CLASS, /* immediate after 'class', no here document. */ 64 EXPR_VALUE, /* alike EXPR_BEG but label is disallowed. */ 65 EXPR_MAX_STATE 66 }; 67 68 /* saved error message */ 69 struct mrb_parser_message { 70 uint16_t lineno; 71 int column; 72 char* message; 73 }; 74 75 #define STR_FUNC_PARSING 0x01 76 #define STR_FUNC_EXPAND 0x02 77 #define STR_FUNC_REGEXP 0x04 78 #define STR_FUNC_WORD 0x08 79 #define STR_FUNC_SYMBOL 0x10 80 #define STR_FUNC_ARRAY 0x20 81 #define STR_FUNC_HEREDOC 0x40 82 #define STR_FUNC_XQUOTE 0x80 83 84 enum mrb_string_type { 85 str_not_parsing = (0), 86 str_squote = (STR_FUNC_PARSING), 87 str_dquote = (STR_FUNC_PARSING|STR_FUNC_EXPAND), 88 str_regexp = (STR_FUNC_PARSING|STR_FUNC_REGEXP|STR_FUNC_EXPAND), 89 str_sword = (STR_FUNC_PARSING|STR_FUNC_WORD|STR_FUNC_ARRAY), 90 str_dword = (STR_FUNC_PARSING|STR_FUNC_WORD|STR_FUNC_ARRAY|STR_FUNC_EXPAND), 91 str_ssym = (STR_FUNC_PARSING|STR_FUNC_SYMBOL), 92 str_ssymbols = (STR_FUNC_PARSING|STR_FUNC_SYMBOL|STR_FUNC_ARRAY), 93 str_dsymbols = (STR_FUNC_PARSING|STR_FUNC_SYMBOL|STR_FUNC_ARRAY|STR_FUNC_EXPAND), 94 str_heredoc = (STR_FUNC_PARSING|STR_FUNC_HEREDOC), 95 str_xquote = (STR_FUNC_PARSING|STR_FUNC_XQUOTE|STR_FUNC_EXPAND), 96 }; 97 98 /* heredoc structure */ 99 struct mrb_parser_heredoc_info { 100 mrb_bool allow_indent:1; 101 mrb_bool line_head:1; 102 enum mrb_string_type type; 103 const char *term; 104 int term_len; 105 mrb_ast_node *doc; 106 }; 107 108 #define MRB_PARSER_TOKBUF_MAX (UINT16_MAX-1) 109 #define MRB_PARSER_TOKBUF_SIZE 256 110 111 /* parser structure */ 112 struct mrb_parser_state { 113 mrb_state *mrb; 114 struct mrb_pool *pool; 115 mrb_ast_node *cells; 116 const char *s, *send; 117 #ifndef MRB_DISABLE_STDIO 118 FILE *f; 119 #endif 120 mrbc_context *cxt; 121 mrb_sym filename_sym; 122 uint16_t lineno; 123 int column; 124 125 enum mrb_lex_state_enum lstate; 126 mrb_ast_node *lex_strterm; /* (type nest_level beg . end) */ 127 128 unsigned int cond_stack; 129 unsigned int cmdarg_stack; 130 int paren_nest; 131 int lpar_beg; 132 int in_def, in_single; 133 mrb_bool cmd_start:1; 134 mrb_ast_node *locals; 135 136 mrb_ast_node *pb; 137 char *tokbuf; 138 char buf[MRB_PARSER_TOKBUF_SIZE]; 139 int tidx; 140 int tsiz; 141 142 mrb_ast_node *all_heredocs; /* list of mrb_parser_heredoc_info* */ 143 mrb_ast_node *heredocs_from_nextline; 144 mrb_ast_node *parsing_heredoc; 145 mrb_ast_node *lex_strterm_before_heredoc; 146 147 void *ylval; 148 149 size_t nerr; 150 size_t nwarn; 151 mrb_ast_node *tree; 152 153 mrb_bool no_optimize:1; 154 mrb_bool capture_errors:1; 155 struct RProc *upper; 156 struct mrb_parser_message error_buffer[10]; 157 struct mrb_parser_message warn_buffer[10]; 158 159 mrb_sym* filename_table; 160 uint16_t filename_table_length; 161 uint16_t current_filename_index; 162 163 struct mrb_jmpbuf* jmp; 164 mrb_ast_node *nvars; 165 }; 166 167 MRB_API struct mrb_parser_state* mrb_parser_new(mrb_state*); 168 MRB_API void mrb_parser_free(struct mrb_parser_state*); 169 MRB_API void mrb_parser_parse(struct mrb_parser_state*,mrbc_context*); 170 171 MRB_API void mrb_parser_set_filename(struct mrb_parser_state*, char const*); 172 MRB_API mrb_sym mrb_parser_get_filename(struct mrb_parser_state*, uint16_t idx); 173 174 /* utility functions */ 175 #ifndef MRB_DISABLE_STDIO 176 MRB_API struct mrb_parser_state* mrb_parse_file(mrb_state*,FILE*,mrbc_context*); 177 #endif 178 MRB_API struct mrb_parser_state* mrb_parse_string(mrb_state*,const char*,mrbc_context*); 179 MRB_API struct mrb_parser_state* mrb_parse_nstring(mrb_state*,const char*,size_t,mrbc_context*); 180 MRB_API struct RProc* mrb_generate_code(mrb_state*, struct mrb_parser_state*); 181 MRB_API mrb_value mrb_load_exec(mrb_state *mrb, struct mrb_parser_state *p, mrbc_context *c); 182 183 /** program load functions 184 * Please note! Currently due to interactions with the GC calling these functions will 185 * leak one RProc object per function call. 186 * To prevent this save the current memory arena before calling and restore the arena 187 * right after, like so 188 * int ai = mrb_gc_arena_save(mrb); 189 * mrb_value status = mrb_load_string(mrb, buffer); 190 * mrb_gc_arena_restore(mrb, ai); 191 */ 192 #ifndef MRB_DISABLE_STDIO 193 MRB_API mrb_value mrb_load_file(mrb_state*,FILE*); 194 MRB_API mrb_value mrb_load_file_cxt(mrb_state*,FILE*, mrbc_context *cxt); 195 #endif 196 MRB_API mrb_value mrb_load_string(mrb_state *mrb, const char *s); 197 MRB_API mrb_value mrb_load_nstring(mrb_state *mrb, const char *s, size_t len); 198 MRB_API mrb_value mrb_load_string_cxt(mrb_state *mrb, const char *s, mrbc_context *cxt); 199 MRB_API mrb_value mrb_load_nstring_cxt(mrb_state *mrb, const char *s, size_t len, mrbc_context *cxt); 200 201 /** @} */ 202 MRB_END_DECL 203 204 #endif /* MRUBY_COMPILE_H */ 205