Debian Codegraph @

vendor/mruby-source/include/mruby/compile.h

raw
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_parser_state;
20 /* load context */
21 typedef struct mrbc_context {
22 mrb_sym *syms;
23 int slen;
24 char *filename;
25 uint16_t lineno;
26 int (*partial_hook)(struct mrb_parser_state*);
27 void *partial_data;
28 struct RClass *target_class;
29 mrb_bool capture_errors:1;
30 mrb_bool dump_result:1;
31 mrb_bool no_exec:1;
32 mrb_bool keep_lv:1;
33 mrb_bool no_optimize:1;
34 mrb_bool no_ext_ops:1;
35 const struct RProc *upper;
36
37 size_t parser_nerr;
38 } mrbc_context;
39
40 MRB_API mrbc_context* mrbc_context_new(mrb_state *mrb);
41 MRB_API void mrbc_context_free(mrb_state *mrb, mrbc_context *cxt);
42 MRB_API const char *mrbc_filename(mrb_state *mrb, mrbc_context *c, const char *s);
43 MRB_API void mrbc_partial_hook(mrb_state *mrb, mrbc_context *c, int (*partial_hook)(struct mrb_parser_state*), void*data);
44 MRB_API void mrbc_cleanup_local_variables(mrb_state *mrb, mrbc_context *c);
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 a sign. */
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 remove_indent:1;
102 mrb_bool line_head:1;
103 size_t indent;
104 mrb_ast_node *indented;
105 enum mrb_string_type type;
106 const char *term;
107 int term_len;
108 mrb_ast_node *doc;
109 };
110
111 #define MRB_PARSER_TOKBUF_MAX (UINT16_MAX-1)
112 #define MRB_PARSER_TOKBUF_SIZE 256
113
114 /* parser structure */
115 struct mrb_parser_state {
116 mrb_state *mrb;
117 struct mrb_pool *pool;
118 mrb_ast_node *cells;
119 const char *s, *send;
120 #ifndef MRB_NO_STDIO
121 /* If both f and s are non-null, it will be taken preferentially from s until s < send. */
122 FILE *f;
123 #endif
124 mrbc_context *cxt;
125 mrb_sym filename_sym;
126 uint16_t lineno;
127 int column;
128
129 enum mrb_lex_state_enum lstate;
130 struct parser_lex_strterm *lex_strterm;
131
132 unsigned int cond_stack;
133 unsigned int cmdarg_stack;
134 int paren_nest;
135 int lpar_beg;
136 int in_def, in_single;
137 mrb_bool cmd_start:1;
138 mrb_ast_node *locals;
139
140 mrb_ast_node *pb;
141 char *tokbuf;
142 char buf[MRB_PARSER_TOKBUF_SIZE];
143 int tidx;
144 int tsiz;
145
146 mrb_ast_node *heredocs_from_nextline;
147 mrb_ast_node *parsing_heredoc;
148
149 void *ylval;
150
151 size_t nerr;
152 size_t nwarn;
153 mrb_ast_node *tree;
154
155 mrb_bool no_optimize:1;
156 mrb_bool capture_errors:1;
157 mrb_bool no_ext_ops:1;
158 const struct RProc *upper;
159 struct mrb_parser_message error_buffer[10];
160 struct mrb_parser_message warn_buffer[10];
161
162 mrb_sym* filename_table;
163 uint16_t filename_table_length;
164 uint16_t current_filename_index;
165
166 mrb_ast_node *nvars;
167 };
168
169 MRB_API struct mrb_parser_state* mrb_parser_new(mrb_state*);
170 MRB_API void mrb_parser_free(struct mrb_parser_state*);
171 MRB_API void mrb_parser_parse(struct mrb_parser_state*,mrbc_context*);
172
173 MRB_API void mrb_parser_set_filename(struct mrb_parser_state*, char const*);
174 MRB_API mrb_sym mrb_parser_get_filename(struct mrb_parser_state*, uint16_t idx);
175
176 /* utility functions */
177 #ifndef MRB_NO_STDIO
178 MRB_API struct mrb_parser_state* mrb_parse_file(mrb_state*,FILE*,mrbc_context*);
179 #endif
180 MRB_API struct mrb_parser_state* mrb_parse_string(mrb_state*,const char*,mrbc_context*);
181 MRB_API struct mrb_parser_state* mrb_parse_nstring(mrb_state*,const char*,size_t,mrbc_context*);
182 MRB_API struct RProc* mrb_generate_code(mrb_state*, struct mrb_parser_state*);
183 MRB_API mrb_value mrb_load_exec(mrb_state *mrb, struct mrb_parser_state *p, mrbc_context *c);
184
185 /** program load functions
186 * Please note! Currently due to interactions with the GC calling these functions will
187 * leak one RProc object per function call.
188 * To prevent this save the current memory arena before calling and restore the arena
189 * right after, like so
190 * int ai = mrb_gc_arena_save(mrb);
191 * mrb_value status = mrb_load_string(mrb, buffer);
192 * mrb_gc_arena_restore(mrb, ai);
193 */
194 #ifndef MRB_NO_STDIO
195 MRB_API mrb_value mrb_load_file(mrb_state*,FILE*);
196 MRB_API mrb_value mrb_load_file_cxt(mrb_state*,FILE*, mrbc_context *cxt);
197 MRB_API mrb_value mrb_load_detect_file_cxt(mrb_state *mrb, FILE *fp, mrbc_context *c);
198 #endif
199 MRB_API mrb_value mrb_load_string(mrb_state *mrb, const char *s);
200 MRB_API mrb_value mrb_load_nstring(mrb_state *mrb, const char *s, size_t len);
201 MRB_API mrb_value mrb_load_string_cxt(mrb_state *mrb, const char *s, mrbc_context *cxt);
202 MRB_API mrb_value mrb_load_nstring_cxt(mrb_state *mrb, const char *s, size_t len, mrbc_context *cxt);
203
204 /** @} */
205 MRB_END_DECL
206
207 #endif /* MRUBY_COMPILE_H */