mbed I/F binding for mruby
Dependents: mruby_mbed_web mirb_mbed
mbed-mruby
How to use
Class
include/mruby/compile.h@0:158c61bb030f, 2015-03-25 (annotated)
- Committer:
- mzta
- Date:
- Wed Mar 25 17:36:16 2015 +0000
- Revision:
- 0:158c61bb030f
mirb_mbed initial commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mzta | 0:158c61bb030f | 1 | /* |
mzta | 0:158c61bb030f | 2 | ** mruby/compile.h - mruby parser |
mzta | 0:158c61bb030f | 3 | ** |
mzta | 0:158c61bb030f | 4 | ** See Copyright Notice in mruby.h |
mzta | 0:158c61bb030f | 5 | */ |
mzta | 0:158c61bb030f | 6 | |
mzta | 0:158c61bb030f | 7 | #ifndef MRUBY_COMPILE_H |
mzta | 0:158c61bb030f | 8 | #define MRUBY_COMPILE_H |
mzta | 0:158c61bb030f | 9 | |
mzta | 0:158c61bb030f | 10 | #if defined(__cplusplus) |
mzta | 0:158c61bb030f | 11 | extern "C" { |
mzta | 0:158c61bb030f | 12 | #endif |
mzta | 0:158c61bb030f | 13 | |
mzta | 0:158c61bb030f | 14 | #include "mruby.h" |
mzta | 0:158c61bb030f | 15 | |
mzta | 0:158c61bb030f | 16 | struct mrb_jmpbuf; |
mzta | 0:158c61bb030f | 17 | |
mzta | 0:158c61bb030f | 18 | struct mrb_parser_state; |
mzta | 0:158c61bb030f | 19 | /* load context */ |
mzta | 0:158c61bb030f | 20 | typedef struct mrbc_context { |
mzta | 0:158c61bb030f | 21 | mrb_sym *syms; |
mzta | 0:158c61bb030f | 22 | int slen; |
mzta | 0:158c61bb030f | 23 | char *filename; |
mzta | 0:158c61bb030f | 24 | short lineno; |
mzta | 0:158c61bb030f | 25 | int (*partial_hook)(struct mrb_parser_state*); |
mzta | 0:158c61bb030f | 26 | void *partial_data; |
mzta | 0:158c61bb030f | 27 | struct RClass *target_class; |
mzta | 0:158c61bb030f | 28 | mrb_bool capture_errors:1; |
mzta | 0:158c61bb030f | 29 | mrb_bool dump_result:1; |
mzta | 0:158c61bb030f | 30 | mrb_bool no_exec:1; |
mzta | 0:158c61bb030f | 31 | mrb_bool keep_lv:1; |
mzta | 0:158c61bb030f | 32 | mrb_bool no_optimize:1; |
mzta | 0:158c61bb030f | 33 | } mrbc_context; |
mzta | 0:158c61bb030f | 34 | |
mzta | 0:158c61bb030f | 35 | MRB_API mrbc_context* mrbc_context_new(mrb_state *mrb); |
mzta | 0:158c61bb030f | 36 | MRB_API void mrbc_context_free(mrb_state *mrb, mrbc_context *cxt); |
mzta | 0:158c61bb030f | 37 | MRB_API const char *mrbc_filename(mrb_state *mrb, mrbc_context *c, const char *s); |
mzta | 0:158c61bb030f | 38 | MRB_API void mrbc_partial_hook(mrb_state *mrb, mrbc_context *c, int (*partial_hook)(struct mrb_parser_state*), void*data); |
mzta | 0:158c61bb030f | 39 | |
mzta | 0:158c61bb030f | 40 | MRB_API mrb_value mrb_toplevel_run_keep(mrb_state*, struct RProc*, unsigned int); |
mzta | 0:158c61bb030f | 41 | |
mzta | 0:158c61bb030f | 42 | /* AST node structure */ |
mzta | 0:158c61bb030f | 43 | typedef struct mrb_ast_node { |
mzta | 0:158c61bb030f | 44 | struct mrb_ast_node *car, *cdr; |
mzta | 0:158c61bb030f | 45 | uint16_t lineno, filename_index; |
mzta | 0:158c61bb030f | 46 | } mrb_ast_node; |
mzta | 0:158c61bb030f | 47 | |
mzta | 0:158c61bb030f | 48 | /* lexer states */ |
mzta | 0:158c61bb030f | 49 | enum mrb_lex_state_enum { |
mzta | 0:158c61bb030f | 50 | EXPR_BEG, /* ignore newline, +/- is a sign. */ |
mzta | 0:158c61bb030f | 51 | EXPR_END, /* newline significant, +/- is an operator. */ |
mzta | 0:158c61bb030f | 52 | EXPR_ENDARG, /* ditto, and unbound braces. */ |
mzta | 0:158c61bb030f | 53 | EXPR_ENDFN, /* ditto, and unbound braces. */ |
mzta | 0:158c61bb030f | 54 | EXPR_ARG, /* newline significant, +/- is an operator. */ |
mzta | 0:158c61bb030f | 55 | EXPR_CMDARG, /* newline significant, +/- is an operator. */ |
mzta | 0:158c61bb030f | 56 | EXPR_MID, /* newline significant, +/- is an operator. */ |
mzta | 0:158c61bb030f | 57 | EXPR_FNAME, /* ignore newline, no reserved words. */ |
mzta | 0:158c61bb030f | 58 | EXPR_DOT, /* right after `.' or `::', no reserved words. */ |
mzta | 0:158c61bb030f | 59 | EXPR_CLASS, /* immediate after `class', no here document. */ |
mzta | 0:158c61bb030f | 60 | EXPR_VALUE, /* alike EXPR_BEG but label is disallowed. */ |
mzta | 0:158c61bb030f | 61 | EXPR_MAX_STATE |
mzta | 0:158c61bb030f | 62 | }; |
mzta | 0:158c61bb030f | 63 | |
mzta | 0:158c61bb030f | 64 | /* saved error message */ |
mzta | 0:158c61bb030f | 65 | struct mrb_parser_message { |
mzta | 0:158c61bb030f | 66 | int lineno; |
mzta | 0:158c61bb030f | 67 | int column; |
mzta | 0:158c61bb030f | 68 | char* message; |
mzta | 0:158c61bb030f | 69 | }; |
mzta | 0:158c61bb030f | 70 | |
mzta | 0:158c61bb030f | 71 | #define STR_FUNC_PARSING 0x01 |
mzta | 0:158c61bb030f | 72 | #define STR_FUNC_EXPAND 0x02 |
mzta | 0:158c61bb030f | 73 | #define STR_FUNC_REGEXP 0x04 |
mzta | 0:158c61bb030f | 74 | #define STR_FUNC_WORD 0x08 |
mzta | 0:158c61bb030f | 75 | #define STR_FUNC_SYMBOL 0x10 |
mzta | 0:158c61bb030f | 76 | #define STR_FUNC_ARRAY 0x20 |
mzta | 0:158c61bb030f | 77 | #define STR_FUNC_HEREDOC 0x40 |
mzta | 0:158c61bb030f | 78 | #define STR_FUNC_XQUOTE 0x80 |
mzta | 0:158c61bb030f | 79 | |
mzta | 0:158c61bb030f | 80 | enum mrb_string_type { |
mzta | 0:158c61bb030f | 81 | str_not_parsing = (0), |
mzta | 0:158c61bb030f | 82 | str_squote = (STR_FUNC_PARSING), |
mzta | 0:158c61bb030f | 83 | str_dquote = (STR_FUNC_PARSING|STR_FUNC_EXPAND), |
mzta | 0:158c61bb030f | 84 | str_regexp = (STR_FUNC_PARSING|STR_FUNC_REGEXP|STR_FUNC_EXPAND), |
mzta | 0:158c61bb030f | 85 | str_sword = (STR_FUNC_PARSING|STR_FUNC_WORD|STR_FUNC_ARRAY), |
mzta | 0:158c61bb030f | 86 | str_dword = (STR_FUNC_PARSING|STR_FUNC_WORD|STR_FUNC_ARRAY|STR_FUNC_EXPAND), |
mzta | 0:158c61bb030f | 87 | str_ssym = (STR_FUNC_PARSING|STR_FUNC_SYMBOL), |
mzta | 0:158c61bb030f | 88 | str_ssymbols = (STR_FUNC_PARSING|STR_FUNC_SYMBOL|STR_FUNC_ARRAY), |
mzta | 0:158c61bb030f | 89 | str_dsymbols = (STR_FUNC_PARSING|STR_FUNC_SYMBOL|STR_FUNC_ARRAY|STR_FUNC_EXPAND), |
mzta | 0:158c61bb030f | 90 | str_heredoc = (STR_FUNC_PARSING|STR_FUNC_HEREDOC), |
mzta | 0:158c61bb030f | 91 | str_xquote = (STR_FUNC_PARSING|STR_FUNC_XQUOTE|STR_FUNC_EXPAND), |
mzta | 0:158c61bb030f | 92 | }; |
mzta | 0:158c61bb030f | 93 | |
mzta | 0:158c61bb030f | 94 | /* heredoc structure */ |
mzta | 0:158c61bb030f | 95 | struct mrb_parser_heredoc_info { |
mzta | 0:158c61bb030f | 96 | mrb_bool allow_indent:1; |
mzta | 0:158c61bb030f | 97 | mrb_bool line_head:1; |
mzta | 0:158c61bb030f | 98 | enum mrb_string_type type; |
mzta | 0:158c61bb030f | 99 | const char *term; |
mzta | 0:158c61bb030f | 100 | int term_len; |
mzta | 0:158c61bb030f | 101 | mrb_ast_node *doc; |
mzta | 0:158c61bb030f | 102 | }; |
mzta | 0:158c61bb030f | 103 | |
mzta | 0:158c61bb030f | 104 | #define MRB_PARSER_BUF_SIZE 1024 |
mzta | 0:158c61bb030f | 105 | |
mzta | 0:158c61bb030f | 106 | /* parser structure */ |
mzta | 0:158c61bb030f | 107 | struct mrb_parser_state { |
mzta | 0:158c61bb030f | 108 | mrb_state *mrb; |
mzta | 0:158c61bb030f | 109 | struct mrb_pool *pool; |
mzta | 0:158c61bb030f | 110 | mrb_ast_node *cells; |
mzta | 0:158c61bb030f | 111 | const char *s, *send; |
mzta | 0:158c61bb030f | 112 | #ifdef ENABLE_STDIO |
mzta | 0:158c61bb030f | 113 | FILE *f; |
mzta | 0:158c61bb030f | 114 | #endif |
mzta | 0:158c61bb030f | 115 | mrbc_context *cxt; |
mzta | 0:158c61bb030f | 116 | char const *filename; |
mzta | 0:158c61bb030f | 117 | int lineno; |
mzta | 0:158c61bb030f | 118 | int column; |
mzta | 0:158c61bb030f | 119 | |
mzta | 0:158c61bb030f | 120 | enum mrb_lex_state_enum lstate; |
mzta | 0:158c61bb030f | 121 | mrb_ast_node *lex_strterm; /* (type nest_level beg . end) */ |
mzta | 0:158c61bb030f | 122 | |
mzta | 0:158c61bb030f | 123 | unsigned int cond_stack; |
mzta | 0:158c61bb030f | 124 | unsigned int cmdarg_stack; |
mzta | 0:158c61bb030f | 125 | int paren_nest; |
mzta | 0:158c61bb030f | 126 | int lpar_beg; |
mzta | 0:158c61bb030f | 127 | int in_def, in_single; |
mzta | 0:158c61bb030f | 128 | mrb_bool cmd_start:1; |
mzta | 0:158c61bb030f | 129 | mrb_ast_node *locals; |
mzta | 0:158c61bb030f | 130 | |
mzta | 0:158c61bb030f | 131 | mrb_ast_node *pb; |
mzta | 0:158c61bb030f | 132 | char buf[MRB_PARSER_BUF_SIZE]; |
mzta | 0:158c61bb030f | 133 | int bidx; |
mzta | 0:158c61bb030f | 134 | |
mzta | 0:158c61bb030f | 135 | mrb_ast_node *all_heredocs; /* list of mrb_parser_heredoc_info* */ |
mzta | 0:158c61bb030f | 136 | mrb_ast_node *heredocs_from_nextline; |
mzta | 0:158c61bb030f | 137 | mrb_ast_node *parsing_heredoc; |
mzta | 0:158c61bb030f | 138 | mrb_ast_node *lex_strterm_before_heredoc; |
mzta | 0:158c61bb030f | 139 | mrb_bool heredoc_end_now:1; /* for mirb */ |
mzta | 0:158c61bb030f | 140 | |
mzta | 0:158c61bb030f | 141 | void *ylval; |
mzta | 0:158c61bb030f | 142 | |
mzta | 0:158c61bb030f | 143 | size_t nerr; |
mzta | 0:158c61bb030f | 144 | size_t nwarn; |
mzta | 0:158c61bb030f | 145 | mrb_ast_node *tree; |
mzta | 0:158c61bb030f | 146 | |
mzta | 0:158c61bb030f | 147 | mrb_bool no_optimize:1; |
mzta | 0:158c61bb030f | 148 | mrb_bool capture_errors:1; |
mzta | 0:158c61bb030f | 149 | struct mrb_parser_message error_buffer[10]; |
mzta | 0:158c61bb030f | 150 | struct mrb_parser_message warn_buffer[10]; |
mzta | 0:158c61bb030f | 151 | |
mzta | 0:158c61bb030f | 152 | mrb_sym* filename_table; |
mzta | 0:158c61bb030f | 153 | size_t filename_table_length; |
mzta | 0:158c61bb030f | 154 | int current_filename_index; |
mzta | 0:158c61bb030f | 155 | |
mzta | 0:158c61bb030f | 156 | struct mrb_jmpbuf* jmp; |
mzta | 0:158c61bb030f | 157 | }; |
mzta | 0:158c61bb030f | 158 | |
mzta | 0:158c61bb030f | 159 | MRB_API struct mrb_parser_state* mrb_parser_new(mrb_state*); |
mzta | 0:158c61bb030f | 160 | MRB_API void mrb_parser_free(struct mrb_parser_state*); |
mzta | 0:158c61bb030f | 161 | MRB_API void mrb_parser_parse(struct mrb_parser_state*,mrbc_context*); |
mzta | 0:158c61bb030f | 162 | |
mzta | 0:158c61bb030f | 163 | MRB_API void mrb_parser_set_filename(struct mrb_parser_state*, char const*); |
mzta | 0:158c61bb030f | 164 | MRB_API char const* mrb_parser_get_filename(struct mrb_parser_state*, uint16_t idx); |
mzta | 0:158c61bb030f | 165 | |
mzta | 0:158c61bb030f | 166 | /* utility functions */ |
mzta | 0:158c61bb030f | 167 | #ifdef ENABLE_STDIO |
mzta | 0:158c61bb030f | 168 | MRB_API struct mrb_parser_state* mrb_parse_file(mrb_state*,FILE*,mrbc_context*); |
mzta | 0:158c61bb030f | 169 | #endif |
mzta | 0:158c61bb030f | 170 | MRB_API struct mrb_parser_state* mrb_parse_string(mrb_state*,const char*,mrbc_context*); |
mzta | 0:158c61bb030f | 171 | MRB_API struct mrb_parser_state* mrb_parse_nstring(mrb_state*,const char*,int,mrbc_context*); |
mzta | 0:158c61bb030f | 172 | MRB_API struct RProc* mrb_generate_code(mrb_state*, struct mrb_parser_state*); |
mzta | 0:158c61bb030f | 173 | |
mzta | 0:158c61bb030f | 174 | /* program load functions */ |
mzta | 0:158c61bb030f | 175 | #ifdef ENABLE_STDIO |
mzta | 0:158c61bb030f | 176 | MRB_API mrb_value mrb_load_file(mrb_state*,FILE*); |
mzta | 0:158c61bb030f | 177 | #endif |
mzta | 0:158c61bb030f | 178 | MRB_API mrb_value mrb_load_string(mrb_state *mrb, const char *s); |
mzta | 0:158c61bb030f | 179 | MRB_API mrb_value mrb_load_nstring(mrb_state *mrb, const char *s, int len); |
mzta | 0:158c61bb030f | 180 | #ifdef ENABLE_STDIO |
mzta | 0:158c61bb030f | 181 | MRB_API mrb_value mrb_load_file_cxt(mrb_state*,FILE*, mrbc_context *cxt); |
mzta | 0:158c61bb030f | 182 | #endif |
mzta | 0:158c61bb030f | 183 | MRB_API mrb_value mrb_load_string_cxt(mrb_state *mrb, const char *s, mrbc_context *cxt); |
mzta | 0:158c61bb030f | 184 | MRB_API mrb_value mrb_load_nstring_cxt(mrb_state *mrb, const char *s, int len, mrbc_context *cxt); |
mzta | 0:158c61bb030f | 185 | |
mzta | 0:158c61bb030f | 186 | #if defined(__cplusplus) |
mzta | 0:158c61bb030f | 187 | } /* extern "C" { */ |
mzta | 0:158c61bb030f | 188 | #endif |
mzta | 0:158c61bb030f | 189 | |
mzta | 0:158c61bb030f | 190 | #endif /* MRUBY_COMPILE_H */ |
mzta | 0:158c61bb030f | 191 |