mruby 3.3.0
mruby is the lightweight implementation of the Ruby language
Loading...
Searching...
No Matches
compile.h
Go to the documentation of this file.
1
7#ifndef MRUBY_COMPILE_H
8#define MRUBY_COMPILE_H
9
10#include "common.h"
11
16
17#include <mruby.h>
18
19struct mrb_parser_state;
20/* load context */
21typedef struct mrb_ccontext {
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} mrb_ccontext; /* compiler context */
39
40MRB_API mrb_ccontext* mrb_ccontext_new(mrb_state *mrb);
41MRB_API void mrb_ccontext_free(mrb_state *mrb, mrb_ccontext *cxt);
42MRB_API const char *mrb_ccontext_filename(mrb_state *mrb, mrb_ccontext *c, const char *s);
43MRB_API void mrb_ccontext_partial_hook(mrb_state *mrb, mrb_ccontext *c, int (*partial_hook)(struct mrb_parser_state*), void*data);
44MRB_API void mrb_ccontext_cleanup_local_variables(mrb_state *mrb, mrb_ccontext *c);
45
46/* compatibility macros */
47#define mrbc_context mrb_ccontext
48#define mrbc_context_new mrb_ccontext_new
49#define mrbc_context_free mrb_ccontext_free
50#define mrbc_filename mrb_ccontext_filename
51#define mrbc_partial_hook mrb_ccontext_partial_hook
52#define mrbc_cleanup_local_variables mrb_ccontext_cleanup_local_variables
53
54/* AST node structure */
55typedef struct mrb_ast_node {
56 struct mrb_ast_node *car, *cdr;
57 uint16_t lineno, filename_index;
59
60/* lexer states */
61enum mrb_lex_state_enum {
62 EXPR_BEG, /* ignore newline, +/- is a sign. */
63 EXPR_END, /* newline significant, +/- is an operator. */
64 EXPR_ENDARG, /* ditto, and unbound braces. */
65 EXPR_ENDFN, /* ditto, and unbound braces. */
66 EXPR_ARG, /* newline significant, +/- is an operator. */
67 EXPR_CMDARG, /* newline significant, +/- is an operator. */
68 EXPR_MID, /* newline significant, +/- is a sign. */
69 EXPR_FNAME, /* ignore newline, no reserved words. */
70 EXPR_DOT, /* right after '.' or '::', no reserved words. */
71 EXPR_CLASS, /* immediate after 'class', no here document. */
72 EXPR_VALUE, /* alike EXPR_BEG but label is disallowed. */
73 EXPR_MAX_STATE
74};
75
76/* saved error message */
78 uint16_t lineno;
79 int column;
80 char* message;
81};
82
83#define STR_FUNC_PARSING 0x01
84#define STR_FUNC_EXPAND 0x02
85#define STR_FUNC_REGEXP 0x04
86#define STR_FUNC_WORD 0x08
87#define STR_FUNC_SYMBOL 0x10
88#define STR_FUNC_ARRAY 0x20
89#define STR_FUNC_HEREDOC 0x40
90#define STR_FUNC_XQUOTE 0x80
91
92enum mrb_string_type {
93 str_not_parsing = (0),
94 str_squote = (STR_FUNC_PARSING),
95 str_dquote = (STR_FUNC_PARSING|STR_FUNC_EXPAND),
96 str_regexp = (STR_FUNC_PARSING|STR_FUNC_REGEXP|STR_FUNC_EXPAND),
97 str_sword = (STR_FUNC_PARSING|STR_FUNC_WORD|STR_FUNC_ARRAY),
98 str_dword = (STR_FUNC_PARSING|STR_FUNC_WORD|STR_FUNC_ARRAY|STR_FUNC_EXPAND),
99 str_ssym = (STR_FUNC_PARSING|STR_FUNC_SYMBOL),
100 str_ssymbols = (STR_FUNC_PARSING|STR_FUNC_SYMBOL|STR_FUNC_ARRAY),
101 str_dsymbols = (STR_FUNC_PARSING|STR_FUNC_SYMBOL|STR_FUNC_ARRAY|STR_FUNC_EXPAND),
102 str_heredoc = (STR_FUNC_PARSING|STR_FUNC_HEREDOC),
103 str_xquote = (STR_FUNC_PARSING|STR_FUNC_XQUOTE|STR_FUNC_EXPAND),
104};
105
106/* heredoc structure */
108 mrb_bool allow_indent:1;
109 mrb_bool remove_indent:1;
110 mrb_bool line_head:1;
111 size_t indent;
112 mrb_ast_node *indented;
113 enum mrb_string_type type;
114 const char *term;
115 int term_len;
116 mrb_ast_node *doc;
117};
118
119#define MRB_PARSER_TOKBUF_MAX (UINT16_MAX-1)
120#define MRB_PARSER_TOKBUF_SIZE 256
121
122/* parser structure */
124 mrb_state *mrb;
125 struct mrb_pool *pool;
126 mrb_ast_node *cells;
127 const char *s, *send;
128#ifndef MRB_NO_STDIO
129 /* If both f and s are non-null, it will be taken preferentially from s until s < send. */
130 FILE *f;
131#endif
132 mrb_ccontext *cxt;
133 mrb_sym filename_sym;
134 uint16_t lineno;
135 int column;
136
137 enum mrb_lex_state_enum lstate;
138 struct parser_lex_strterm *lex_strterm;
139
140 unsigned int cond_stack;
141 unsigned int cmdarg_stack;
142 int paren_nest;
143 int lpar_beg;
144 int in_def, in_single;
145 mrb_bool cmd_start:1;
146 mrb_ast_node *locals;
147
148 mrb_ast_node *pb;
149 char *tokbuf;
150 char buf[MRB_PARSER_TOKBUF_SIZE];
151 int tidx;
152 int tsiz;
153
154 mrb_ast_node *heredocs_from_nextline;
155 mrb_ast_node *parsing_heredoc;
156
157 void *ylval;
158
159 size_t nerr;
160 size_t nwarn;
161 mrb_ast_node *tree;
162
163 mrb_bool no_optimize:1;
164 mrb_bool capture_errors:1;
165 mrb_bool no_ext_ops:1;
166 const struct RProc *upper;
167 struct mrb_parser_message error_buffer[10];
168 struct mrb_parser_message warn_buffer[10];
169
170 mrb_sym* filename_table;
171 uint16_t filename_table_length;
172 uint16_t current_filename_index;
173
174 mrb_ast_node *nvars;
175};
176
177MRB_API struct mrb_parser_state* mrb_parser_new(mrb_state*);
178MRB_API void mrb_parser_free(struct mrb_parser_state*);
179MRB_API void mrb_parser_parse(struct mrb_parser_state*,mrb_ccontext*);
180
181MRB_API void mrb_parser_set_filename(struct mrb_parser_state*, char const*);
182MRB_API mrb_sym mrb_parser_get_filename(struct mrb_parser_state*, uint16_t idx);
183
184/* utility functions */
185#ifndef MRB_NO_STDIO
186MRB_API struct mrb_parser_state* mrb_parse_file(mrb_state*,FILE*,mrb_ccontext*);
187#endif
188MRB_API struct mrb_parser_state* mrb_parse_string(mrb_state*,const char*,mrb_ccontext*);
189MRB_API struct mrb_parser_state* mrb_parse_nstring(mrb_state*,const char*,size_t,mrb_ccontext*);
190MRB_API struct RProc* mrb_generate_code(mrb_state*, struct mrb_parser_state*);
191MRB_API mrb_value mrb_load_exec(mrb_state *mrb, struct mrb_parser_state *p, mrb_ccontext *c);
192
202#ifndef MRB_NO_STDIO
204MRB_API mrb_value mrb_load_file_cxt(mrb_state*,FILE*, mrb_ccontext *cxt);
205MRB_API mrb_value mrb_load_detect_file_cxt(mrb_state *mrb, FILE *fp, mrb_ccontext *c);
206#endif
207MRB_API mrb_value mrb_load_string(mrb_state *mrb, const char *s);
208MRB_API mrb_value mrb_load_nstring(mrb_state *mrb, const char *s, size_t len);
209MRB_API mrb_value mrb_load_string_cxt(mrb_state *mrb, const char *s, mrb_ccontext *cxt);
210MRB_API mrb_value mrb_load_nstring_cxt(mrb_state *mrb, const char *s, size_t len, mrb_ccontext *cxt);
211
214
215#endif /* MRUBY_COMPILE_H */
mruby Boolean.
mruby Symbol.
mruby common platform definition"
#define MRB_END_DECL
End declarations in C mode.
Definition common.h:28
#define MRB_BEGIN_DECL
Start declarations in C mode.
Definition common.h:26
#define MRB_API
Declare a public mruby API function.
Definition common.h:79
mrb_value mrb_load_file(mrb_state *, FILE *)
program load functions Please note! Currently due to interactions with the GC calling these functions...
Class class.
Definition class.h:17
Definition proc.h:42
Definition compile.h:55
Definition compile.h:21
Definition compile.h:107
Definition compile.h:77
Definition compile.h:123
Definition pool.c:45
Definition mruby.h:256
Definition boxing_nan.h:40