2 ** mruby/compile.h - mruby parser
4 ** See Copyright Notice in mruby.h
7 #ifndef MRUBY_COMPILE_H
8 #define MRUBY_COMPILE_H 1
10 #if defined(__cplusplus)
18 typedef struct mrbc_context
{
23 mrb_bool capture_errors
:1;
24 mrb_bool dump_result
:1;
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
;
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. */
54 /* saved error message */
55 struct mrb_parser_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;
86 enum mrb_string_type type
;
92 /* parser structure */
93 struct mrb_parser_state
{
95 struct mrb_pool
*pool
;
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
;
110 int in_def
, in_single
, cmd_start
;
111 mrb_ast_node
*locals
;
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 */
129 struct mrb_parser_message error_buffer
[10];
130 struct mrb_parser_message warn_buffer
[10];
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)
157 #endif /* MRUBY_COMPILE_H */