sabme ua / mruby-mbed

Dependents:   mruby_mbed_web mirb_mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers compile.h Source File

compile.h

00001 /*
00002 ** mruby/compile.h - mruby parser
00003 **
00004 ** See Copyright Notice in mruby.h
00005 */
00006 
00007 #ifndef MRUBY_COMPILE_H
00008 #define MRUBY_COMPILE_H
00009 
00010 #if defined(__cplusplus)
00011 extern "C" {
00012 #endif
00013 
00014 #include "mruby.h"
00015 
00016 struct mrb_jmpbuf;
00017 
00018 struct mrb_parser_state;
00019 /* load context */
00020 typedef struct mrbc_context {
00021   mrb_sym *syms;
00022   int slen;
00023   char *filename;
00024   short lineno;
00025   int (*partial_hook)(struct mrb_parser_state*);
00026   void *partial_data;
00027   struct RClass *target_class;
00028   mrb_bool capture_errors:1;
00029   mrb_bool dump_result:1;
00030   mrb_bool no_exec:1;
00031   mrb_bool keep_lv:1;
00032   mrb_bool no_optimize:1;
00033 } mrbc_context;
00034 
00035 MRB_API mrbc_context* mrbc_context_new(mrb_state *mrb);
00036 MRB_API void mrbc_context_free(mrb_state *mrb, mrbc_context *cxt);
00037 MRB_API const char *mrbc_filename(mrb_state *mrb, mrbc_context *c, const char *s);
00038 MRB_API void mrbc_partial_hook(mrb_state *mrb, mrbc_context *c, int (*partial_hook)(struct mrb_parser_state*), void*data);
00039 
00040 MRB_API mrb_value mrb_toplevel_run_keep(mrb_state*, struct RProc*, unsigned int);
00041 
00042 /* AST node structure */
00043 typedef struct mrb_ast_node {
00044   struct mrb_ast_node *car, *cdr;
00045   uint16_t lineno, filename_index;
00046 } mrb_ast_node;
00047 
00048 /* lexer states */
00049 enum mrb_lex_state_enum {
00050   EXPR_BEG,                   /* ignore newline, +/- is a sign. */
00051   EXPR_END,                   /* newline significant, +/- is an operator. */
00052   EXPR_ENDARG,                /* ditto, and unbound braces. */
00053   EXPR_ENDFN,                 /* ditto, and unbound braces. */
00054   EXPR_ARG,                   /* newline significant, +/- is an operator. */
00055   EXPR_CMDARG,                /* newline significant, +/- is an operator. */
00056   EXPR_MID,                   /* newline significant, +/- is an operator. */
00057   EXPR_FNAME,                 /* ignore newline, no reserved words. */
00058   EXPR_DOT,                   /* right after `.' or `::', no reserved words. */
00059   EXPR_CLASS,                 /* immediate after `class', no here document. */
00060   EXPR_VALUE,                 /* alike EXPR_BEG but label is disallowed. */
00061   EXPR_MAX_STATE
00062 };
00063 
00064 /* saved error message */
00065 struct mrb_parser_message {
00066   int lineno;
00067   int column;
00068   char* message;
00069 };
00070 
00071 #define STR_FUNC_PARSING 0x01
00072 #define STR_FUNC_EXPAND  0x02
00073 #define STR_FUNC_REGEXP  0x04
00074 #define STR_FUNC_WORD    0x08
00075 #define STR_FUNC_SYMBOL  0x10
00076 #define STR_FUNC_ARRAY   0x20
00077 #define STR_FUNC_HEREDOC 0x40
00078 #define STR_FUNC_XQUOTE  0x80
00079 
00080 enum mrb_string_type {
00081   str_not_parsing  = (0),
00082   str_squote   = (STR_FUNC_PARSING),
00083   str_dquote   = (STR_FUNC_PARSING|STR_FUNC_EXPAND),
00084   str_regexp   = (STR_FUNC_PARSING|STR_FUNC_REGEXP|STR_FUNC_EXPAND),
00085   str_sword    = (STR_FUNC_PARSING|STR_FUNC_WORD|STR_FUNC_ARRAY),
00086   str_dword    = (STR_FUNC_PARSING|STR_FUNC_WORD|STR_FUNC_ARRAY|STR_FUNC_EXPAND),
00087   str_ssym     = (STR_FUNC_PARSING|STR_FUNC_SYMBOL),
00088   str_ssymbols = (STR_FUNC_PARSING|STR_FUNC_SYMBOL|STR_FUNC_ARRAY),
00089   str_dsymbols = (STR_FUNC_PARSING|STR_FUNC_SYMBOL|STR_FUNC_ARRAY|STR_FUNC_EXPAND),
00090   str_heredoc  = (STR_FUNC_PARSING|STR_FUNC_HEREDOC),
00091   str_xquote   = (STR_FUNC_PARSING|STR_FUNC_XQUOTE|STR_FUNC_EXPAND),
00092 };
00093 
00094 /* heredoc structure */
00095 struct mrb_parser_heredoc_info {
00096   mrb_bool allow_indent:1;
00097   mrb_bool line_head:1;
00098   enum mrb_string_type type;
00099   const char *term;
00100   int term_len;
00101   mrb_ast_node *doc;
00102 };
00103 
00104 #define MRB_PARSER_BUF_SIZE 1024
00105 
00106 /* parser structure */
00107 struct mrb_parser_state {
00108   mrb_state *mrb;
00109   struct mrb_pool *pool;
00110   mrb_ast_node *cells;
00111   const char *s, *send;
00112 #ifdef ENABLE_STDIO
00113   FILE *f;
00114 #endif
00115   mrbc_context *cxt;
00116   char const *filename;
00117   int lineno;
00118   int column;
00119 
00120   enum mrb_lex_state_enum lstate;
00121   mrb_ast_node *lex_strterm; /* (type nest_level beg . end) */
00122 
00123   unsigned int cond_stack;
00124   unsigned int cmdarg_stack;
00125   int paren_nest;
00126   int lpar_beg;
00127   int in_def, in_single;
00128   mrb_bool cmd_start:1;
00129   mrb_ast_node *locals;
00130 
00131   mrb_ast_node *pb;
00132   char buf[MRB_PARSER_BUF_SIZE];
00133   int bidx;
00134 
00135   mrb_ast_node *all_heredocs;   /* list of mrb_parser_heredoc_info* */
00136   mrb_ast_node *heredocs_from_nextline;
00137   mrb_ast_node *parsing_heredoc;
00138   mrb_ast_node *lex_strterm_before_heredoc;
00139   mrb_bool heredoc_end_now:1; /* for mirb */
00140 
00141   void *ylval;
00142 
00143   size_t nerr;
00144   size_t nwarn;
00145   mrb_ast_node *tree;
00146 
00147   mrb_bool no_optimize:1;
00148   mrb_bool capture_errors:1;
00149   struct mrb_parser_message error_buffer[10];
00150   struct mrb_parser_message warn_buffer[10];
00151 
00152   mrb_sym* filename_table;
00153   size_t filename_table_length;
00154   int current_filename_index;
00155 
00156   struct mrb_jmpbuf* jmp;
00157 };
00158 
00159 MRB_API struct mrb_parser_state* mrb_parser_new(mrb_state*);
00160 MRB_API void mrb_parser_free(struct mrb_parser_state*);
00161 MRB_API void mrb_parser_parse(struct mrb_parser_state*,mrbc_context*);
00162 
00163 MRB_API void mrb_parser_set_filename(struct mrb_parser_state*, char const*);
00164 MRB_API char const* mrb_parser_get_filename(struct mrb_parser_state*, uint16_t idx);
00165 
00166 /* utility functions */
00167 #ifdef ENABLE_STDIO
00168 MRB_API struct mrb_parser_state* mrb_parse_file(mrb_state*,FILE*,mrbc_context*);
00169 #endif
00170 MRB_API struct mrb_parser_state* mrb_parse_string(mrb_state*,const char*,mrbc_context*);
00171 MRB_API struct mrb_parser_state* mrb_parse_nstring(mrb_state*,const char*,int,mrbc_context*);
00172 MRB_API struct RProc* mrb_generate_code(mrb_state*, struct mrb_parser_state*);
00173 
00174 /* program load functions */
00175 #ifdef ENABLE_STDIO
00176 MRB_API mrb_value mrb_load_file(mrb_state*,FILE*);
00177 #endif
00178 MRB_API mrb_value mrb_load_string(mrb_state *mrb, const char *s);
00179 MRB_API mrb_value mrb_load_nstring(mrb_state *mrb, const char *s, int len);
00180 #ifdef ENABLE_STDIO
00181 MRB_API mrb_value mrb_load_file_cxt(mrb_state*,FILE*, mrbc_context *cxt);
00182 #endif
00183 MRB_API mrb_value mrb_load_string_cxt(mrb_state *mrb, const char *s, mrbc_context *cxt);
00184 MRB_API mrb_value mrb_load_nstring_cxt(mrb_state *mrb, const char *s, int len, mrbc_context *cxt);
00185 
00186 #if defined(__cplusplus)
00187 }  /* extern "C" { */
00188 #endif
00189 
00190 #endif /* MRUBY_COMPILE_H */
00191