fix comment.
[mruby.git] / include / mruby / compile.h
blob8b0743fa347f6d37d7ab76190110bce1255e53b0
1 /*
2 ** mruby/compile.h - mruby parser
3 **
4 ** See Copyright Notice in mruby.h
5 */
7 #ifndef MRUBY_COMPILE_H
8 #define MRUBY_COMPILE_H 1
10 #if defined(__cplusplus)
11 extern "C" {
12 #endif
14 #include "mruby.h"
15 #include <setjmp.h>
17 /* load context */
18 typedef struct mrbc_context {
19 mrb_sym *syms;
20 int slen;
21 char *filename;
22 short lineno;
23 mrb_bool capture_errors:1;
24 mrb_bool dump_result:1;
25 mrb_bool no_exec:1;
26 } mrbc_context;
28 mrbc_context* mrbc_context_new(mrb_state *mrb);
29 void mrbc_context_free(mrb_state *mrb, mrbc_context *cxt);
30 const char *mrbc_filename(mrb_state *mrb, mrbc_context *c, const char *s);
32 /* AST node structure */
33 typedef struct mrb_ast_node {
34 struct mrb_ast_node *car, *cdr;
35 short lineno;
36 } mrb_ast_node;
38 /* lexer states */
39 enum mrb_lex_state_enum {
40 EXPR_BEG, /* ignore newline, +/- is a sign. */
41 EXPR_END, /* newline significant, +/- is an operator. */
42 EXPR_ENDARG, /* ditto, and unbound braces. */
43 EXPR_ENDFN, /* ditto, and unbound braces. */
44 EXPR_ARG, /* newline significant, +/- is an operator. */
45 EXPR_CMDARG, /* newline significant, +/- is an operator. */
46 EXPR_MID, /* newline significant, +/- is an operator. */
47 EXPR_FNAME, /* ignore newline, no reserved words. */
48 EXPR_DOT, /* right after `.' or `::', no reserved words. */
49 EXPR_CLASS, /* immediate after `class', no here document. */
50 EXPR_VALUE, /* alike EXPR_BEG but label is disallowed. */
51 EXPR_MAX_STATE
54 /* saved error message */
55 struct mrb_parser_message {
56 int lineno;
57 int column;
58 char* message;
61 #define STR_FUNC_PARSING 0x01
62 #define STR_FUNC_EXPAND 0x02
63 #define STR_FUNC_REGEXP 0x04
64 #define STR_FUNC_WORD 0x08
65 #define STR_FUNC_SYMBOL 0x10
66 #define STR_FUNC_ARRAY 0x20
67 #define STR_FUNC_HEREDOC 0x40
69 enum mrb_string_type {
70 str_not_parsing = (0),
71 str_squote = (STR_FUNC_PARSING),
72 str_dquote = (STR_FUNC_PARSING|STR_FUNC_EXPAND),
73 str_regexp = (STR_FUNC_PARSING|STR_FUNC_REGEXP|STR_FUNC_EXPAND),
74 str_sword = (STR_FUNC_PARSING|STR_FUNC_WORD|STR_FUNC_ARRAY),
75 str_dword = (STR_FUNC_PARSING|STR_FUNC_WORD|STR_FUNC_ARRAY|STR_FUNC_EXPAND),
76 str_ssym = (STR_FUNC_PARSING|STR_FUNC_SYMBOL),
77 str_ssymbols = (STR_FUNC_PARSING|STR_FUNC_SYMBOL|STR_FUNC_ARRAY),
78 str_dsymbols = (STR_FUNC_PARSING|STR_FUNC_SYMBOL|STR_FUNC_ARRAY|STR_FUNC_EXPAND),
79 str_heredoc = (STR_FUNC_PARSING|STR_FUNC_HEREDOC),
82 /* heredoc structure */
83 struct mrb_parser_heredoc_info {
84 mrb_bool allow_indent:1;
85 mrb_bool line_head:1;
86 enum mrb_string_type type;
87 const char *term;
88 int term_len;
89 mrb_ast_node *doc;
92 /* parser structure */
93 struct mrb_parser_state {
94 mrb_state *mrb;
95 struct mrb_pool *pool;
96 mrb_ast_node *cells;
97 const char *s, *send;
98 FILE *f;
99 char *filename;
100 int lineno;
101 int column;
103 enum mrb_lex_state_enum lstate;
104 mrb_ast_node *lex_strterm; /* (type nest_level beg . end) */
106 unsigned int cond_stack;
107 unsigned int cmdarg_stack;
108 int paren_nest;
109 int lpar_beg;
110 int in_def, in_single, cmd_start;
111 mrb_ast_node *locals;
113 mrb_ast_node *pb;
114 char buf[1024];
115 int bidx;
117 mrb_ast_node *heredocs; /* list of mrb_parser_heredoc_info* */
118 mrb_ast_node *parsing_heredoc;
119 mrb_bool heredoc_starts_nextline:1;
120 mrb_bool heredoc_end_now:1; /* for mirb */
122 void *ylval;
124 int nerr;
125 int nwarn;
126 mrb_ast_node *tree;
128 int capture_errors;
129 struct mrb_parser_message error_buffer[10];
130 struct mrb_parser_message warn_buffer[10];
132 jmp_buf jmp;
135 struct mrb_parser_state* mrb_parser_new(mrb_state*);
136 void mrb_parser_free(struct mrb_parser_state*);
137 void mrb_parser_parse(struct mrb_parser_state*,mrbc_context*);
139 /* utility functions */
140 struct mrb_parser_state* mrb_parse_file(mrb_state*,FILE*,mrbc_context*);
141 struct mrb_parser_state* mrb_parse_string(mrb_state*,const char*,mrbc_context*);
142 struct mrb_parser_state* mrb_parse_nstring(mrb_state*,const char*,int,mrbc_context*);
143 int mrb_generate_code(mrb_state*, struct mrb_parser_state*);
145 /* program load functions */
146 mrb_value mrb_load_file(mrb_state*,FILE*);
147 mrb_value mrb_load_string(mrb_state *mrb, const char *s);
148 mrb_value mrb_load_nstring(mrb_state *mrb, const char *s, int len);
149 mrb_value mrb_load_file_cxt(mrb_state*,FILE*, mrbc_context *cxt);
150 mrb_value mrb_load_string_cxt(mrb_state *mrb, const char *s, mrbc_context *cxt);
151 mrb_value mrb_load_nstring_cxt(mrb_state *mrb, const char *s, int len, mrbc_context *cxt);
153 #if defined(__cplusplus)
154 } /* extern "C" { */
155 #endif
157 #endif /* MRUBY_COMPILE_H */