2 ** @file mruby/compile.h - mruby parser
4 ** See Copyright Notice in mruby.h
7 #ifndef MRUBY_COMPILE_H
8 #define MRUBY_COMPILE_H
21 struct mrb_parser_state;
23 typedef struct mrbc_context {
28 int (*partial_hook)(struct mrb_parser_state*);
30 struct RClass *target_class;
31 mrb_bool capture_errors:1;
32 mrb_bool dump_result:1;
35 mrb_bool no_optimize:1;
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);
46 /* AST node structure */
47 typedef struct mrb_ast_node {
48 struct mrb_ast_node *car, *cdr;
49 uint16_t lineno, filename_index;
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. */
68 /* saved error message */
69 struct mrb_parser_message {
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
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),
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;
108 #define MRB_PARSER_TOKBUF_MAX (UINT16_MAX-1)
109 #define MRB_PARSER_TOKBUF_SIZE 256
111 /* parser structure */
112 struct mrb_parser_state {
114 struct mrb_pool *pool;
116 const char *s, *send;
117 #ifndef MRB_DISABLE_STDIO
121 mrb_sym filename_sym;
125 enum mrb_lex_state_enum lstate;
126 mrb_ast_node *lex_strterm; /* (type nest_level beg . end) */
128 unsigned int cond_stack;
129 unsigned int cmdarg_stack;
132 int in_def, in_single;
133 mrb_bool cmd_start:1;
134 mrb_ast_node *locals;
138 char buf[MRB_PARSER_TOKBUF_SIZE];
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;
153 mrb_bool no_optimize:1;
154 mrb_bool capture_errors:1;
156 struct mrb_parser_message error_buffer[10];
157 struct mrb_parser_message warn_buffer[10];
159 mrb_sym* filename_table;
160 uint16_t filename_table_length;
161 uint16_t current_filename_index;
163 struct mrb_jmpbuf* jmp;
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*);
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);
174 /* utility functions */
175 #ifndef MRB_DISABLE_STDIO
176 MRB_API struct mrb_parser_state* mrb_parse_file(mrb_state*,FILE*,mrbc_context*);
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);
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);
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);
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);
204 #endif /* MRUBY_COMPILE_H */