rubinius-melbourne 1.0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,180 @@
1
+ #ifndef MEL_PARSER_STATE_HPP
2
+ #define MEL_PARSER_STATE_HPP
3
+
4
+ #include "namespace.h"
5
+ #include "node.hpp"
6
+ #include "node_types.hpp"
7
+ #include "local_state.hpp"
8
+
9
+ #include <vector>
10
+ #include <list>
11
+
12
+ namespace MELBOURNE {
13
+
14
+ enum lex_state_e {
15
+ EXPR_BEG, /* ignore newline, +/- is a sign. */
16
+ EXPR_END, /* newline significant, +/- is a operator. */
17
+ EXPR_ARG, /* newline significant, +/- is a operator. */
18
+ EXPR_CMDARG, /* newline significant, +/- is a operator. */
19
+ EXPR_ENDARG, /* newline significant, +/- is a operator. */
20
+ EXPR_MID, /* newline significant, +/- is a operator. */
21
+ EXPR_FNAME, /* ignore newline, no reserved words. */
22
+ EXPR_DOT, /* right after `.' or `::', no reserved words. */
23
+ EXPR_CLASS, /* immediate after `class', no here document. */
24
+ };
25
+
26
+ typedef VALUE stack_type;
27
+
28
+ struct StartPosition {
29
+ int line;
30
+ const char* kind;
31
+
32
+ StartPosition(int l, const char* k)
33
+ : line(l)
34
+ , kind(k)
35
+ {}
36
+ };
37
+
38
+ typedef struct rb_parser_state {
39
+ int end_seen;
40
+ int debug_lines;
41
+ int heredoc_end;
42
+ int command_start;
43
+ NODE *lex_strterm;
44
+ int paren_nest;
45
+ int lpar_beg;
46
+ int class_nest;
47
+ int in_single;
48
+ int in_def;
49
+ int compile_for_eval;
50
+ ID cur_mid;
51
+ char *token_buffer;
52
+ int tokidx;
53
+ int toksiz;
54
+ int emit_warnings;
55
+ /* Mirror'ing the 1.8 parser, There are 2 input methods,
56
+ from IO and directly from a string. */
57
+
58
+ /* this function reads a line from lex_io and stores it in
59
+ * line_buffer.
60
+ */
61
+ bool (*lex_gets)(rb_parser_state*);
62
+ bstring line_buffer;
63
+
64
+ /* If this is set, we use the io method. */
65
+ FILE *lex_io;
66
+ /* Otherwise, we use this. */
67
+ bstring lex_string;
68
+ bstring lex_lastline;
69
+ bstring lex_nextline;
70
+
71
+ char *lex_pbeg;
72
+ char *lex_p;
73
+ char *lex_pend;
74
+ int lex_str_used;
75
+
76
+ enum lex_state_e lex_state;
77
+ int in_defined;
78
+ stack_type cond_stack;
79
+ stack_type cmdarg_stack;
80
+
81
+ void *lval; /* the parser's yylval */
82
+ bool eofp;
83
+
84
+ std::vector<bstring>* magic_comments;
85
+ int column;
86
+ NODE *top_node;
87
+
88
+ LocalState* variables;
89
+
90
+ int ternary_colon;
91
+
92
+ void **memory_pools;
93
+ int pool_size, current_pool;
94
+ char *memory_cur;
95
+ char *memory_last_addr;
96
+ int memory_size;
97
+
98
+ bool verbose;
99
+
100
+ bool parse_error;
101
+ VALUE processor;
102
+
103
+ char *sourcefile;
104
+ int sourceline;
105
+
106
+ // Keeps track of lines that 'end' starters are on, to enable
107
+ // better error reporting.
108
+ std::list<StartPosition>* start_lines;
109
+
110
+ // Tracks quarks
111
+ quark_map* quark_indexes;
112
+ quark_vector* quarks;
113
+ } rb_parser_state;
114
+
115
+
116
+ #define PARSER_STATE ((rb_parser_state*)parser_state)
117
+ #define PARSER_VAR(var) (PARSER_STATE->var)
118
+
119
+ #define end_seen PARSER_VAR(end_seen)
120
+ #define ruby__end__seen PARSER_VAR(ruby__end__seen)
121
+ #define ruby_debug_lines PARSER_VAR(debug_lines)
122
+ #define heredoc_end PARSER_VAR(heredoc_end)
123
+ #define command_start PARSER_VAR(command_start)
124
+ #define lex_strterm PARSER_VAR(lex_strterm)
125
+ #define paren_nest PARSER_VAR(paren_nest)
126
+ #define lpar_beg PARSER_VAR(lpar_beg)
127
+ #define class_nest PARSER_VAR(class_nest)
128
+ #define in_single PARSER_VAR(in_single)
129
+ #define in_def PARSER_VAR(in_def)
130
+ #define compile_for_eval PARSER_VAR(compile_for_eval)
131
+ #define cur_mid PARSER_VAR(cur_mid)
132
+ #define tokenbuf PARSER_VAR(token_buffer)
133
+ #define tokidx PARSER_VAR(tokidx)
134
+ #define toksiz PARSER_VAR(toksiz)
135
+ #define emit_warnings PARSER_VAR(emit_warnings)
136
+ #define lex_gets PARSER_VAR(lex_gets)
137
+ #define line_buffer PARSER_VAR(line_buffer)
138
+ #define line_count PARSER_VAR(line_count)
139
+ #define lex_io PARSER_VAR(lex_io)
140
+ #define lex_string PARSER_VAR(lex_string)
141
+ #define lex_gets_ptr PARSER_VAR(lex_gets_ptr)
142
+ #define lex_input PARSER_VAR(lex_input)
143
+ #define lex_lastline PARSER_VAR(lex_lastline)
144
+ #define lex_nextline PARSER_VAR(lex_nextline)
145
+ #define lex_pbeg PARSER_VAR(lex_pbeg)
146
+ #define lex_p PARSER_VAR(lex_p)
147
+ #define lex_pend PARSER_VAR(lex_pend)
148
+ #define lex_str_used PARSER_VAR(lex_str_used)
149
+ #define lex_state PARSER_VAR(lex_state)
150
+ #define in_defined PARSER_VAR(in_defined)
151
+ #define cond_stack PARSER_VAR(cond_stack)
152
+ #define cmdarg_stack PARSER_VAR(cmdarg_stack)
153
+ #define lval PARSER_VAR(lval)
154
+ #define eofp PARSER_VAR(eofp)
155
+ #define magic_comments PARSER_VAR(magic_comments)
156
+ #define column PARSER_VAR(column)
157
+ #define top_node PARSER_VAR(top_node)
158
+ #define variables PARSER_VAR(variables)
159
+ #define ternary_colon PARSER_VAR(ternary_colon)
160
+ #define memory_pools PARSER_VAR(memory_pools)
161
+ #define pool_size PARSER_VAR(pool_size)
162
+ #define current_pool PARSER_VAR(current_pool)
163
+ #define memory_cur PARSER_VAR(memory_cur)
164
+ #define memory_last_addr PARSER_VAR(memory_last_addr)
165
+ #define memory_size PARSER_VAR(memory_size)
166
+ #define verbose PARSER_VAR(verbose)
167
+ #define parse_error PARSER_VAR(parse_error)
168
+ #define processor PARSER_VAR(processor)
169
+ #define start_lines PARSER_VAR(start_lines)
170
+ #define sourcefile PARSER_VAR(sourcefile)
171
+ #define sourceline PARSER_VAR(sourceline)
172
+ #define quark_indexes PARSER_VAR(quark_indexes)
173
+ #define quarks PARSER_VAR(quarks)
174
+
175
+ #define node_newnode(t, a, b, c) parser_node_newnode((rb_parser_state*)parser_state, t, a, b, c)
176
+
177
+ quark id_to_quark(rb_parser_state* parser_state, QUID id);
178
+ };
179
+
180
+ #endif
@@ -0,0 +1,43 @@
1
+ #include "namespace.h"
2
+ #include "quark.hpp"
3
+ #include "parser_state.hpp"
4
+
5
+ #include <string.h>
6
+ #include <stdlib.h>
7
+
8
+ using namespace MELBOURNE;
9
+
10
+ quark MELBOURNE::quark_from_string(rb_parser_state* parser_state, const char* str) {
11
+ if (str == NULL) {
12
+ return QUARK_NOT_FOUND;
13
+ }
14
+
15
+ /* attempt to find it in our cache */
16
+ quark_map::iterator it = quark_indexes->find(str);
17
+ if (it != quark_indexes->end()) {
18
+ return it->second;
19
+ }
20
+
21
+ /* otherwise, we need to duplicate and store the string */
22
+ const char* new_quark = strdup(str);
23
+ quarks->push_back(new_quark);
24
+ size_t index = quarks->size() - 1;
25
+ quark_indexes->insert(quark_map::value_type(new_quark,index));
26
+ return index;
27
+ }
28
+
29
+ const char* MELBOURNE::quark_to_string(rb_parser_state* parser_state, quark q) {
30
+ if (q >= quarks->size())
31
+ return NULL;
32
+ return quarks->at(q);
33
+ }
34
+
35
+ void MELBOURNE::quark_cleanup(rb_parser_state* parser_state) {
36
+ delete quark_indexes;
37
+
38
+ for (quark_vector::iterator it = quarks->begin(); it != quarks->end(); ++it) {
39
+ free((char *)*it);
40
+ }
41
+
42
+ delete quarks;
43
+ }
@@ -0,0 +1,49 @@
1
+ #ifndef MEL_QUARK_HPP
2
+ #define MEL_QUARK_HPP
3
+
4
+ #include <stddef.h>
5
+ #include <cmath>
6
+ #include <vector>
7
+ #include <string.h>
8
+
9
+ #if defined(__APPLE_CC__) && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1090
10
+ #include <unordered_map>
11
+ #define std_unordered_map std::unordered_map
12
+ #else
13
+ #include <tr1/unordered_map>
14
+ #define std_unordered_map std::tr1::unordered_map
15
+ #endif
16
+
17
+ namespace MELBOURNE {
18
+
19
+ typedef size_t quark;
20
+
21
+ struct ConstCharHash {
22
+ size_t operator() (const char* value) const {
23
+ size_t length = strlen(value);
24
+ quark hash = 0;
25
+ for (size_t i = 0; i < length; i++) {
26
+ hash = hash * 5 + value[i];
27
+ }
28
+ return hash;
29
+ }
30
+ };
31
+
32
+ struct ConstCharEqualTo {
33
+ bool operator() ( const char* lhs, const char* rhs) const {
34
+ return strcmp(lhs, rhs) == 0;
35
+ }
36
+ };
37
+
38
+ struct rb_parser_state;
39
+ enum {QUARK_NOT_FOUND = ~0L};
40
+
41
+ quark quark_from_string(rb_parser_state* parser_state, const char* str);
42
+ const char* quark_to_string(rb_parser_state* parser_state, const quark quark);
43
+ void quark_cleanup(rb_parser_state* parser_state);
44
+
45
+ typedef std_unordered_map<const char*, quark, ConstCharHash, ConstCharEqualTo> quark_map;
46
+ typedef std::vector<const char*> quark_vector;
47
+ };
48
+ #endif
49
+
@@ -0,0 +1,225 @@
1
+ #include "namespace.h"
2
+ #include "melbourne.hpp"
3
+ #include "symbols.hpp"
4
+
5
+ #ifdef __cplusplus
6
+ extern "C" {
7
+ #endif
8
+
9
+ namespace MELBOURNE {
10
+ ID rb_sAlias;
11
+ ID rb_sAnd;
12
+ ID rb_sArgs;
13
+ ID rb_sArgsCat;
14
+ ID rb_sArgsPush;
15
+ ID rb_sArray;
16
+ ID rb_sAttrAsgn;
17
+ ID rb_sBackRef;
18
+ ID rb_sBegin;
19
+ ID rb_sBlock;
20
+ ID rb_sBlockArg;
21
+ ID rb_sBlockPass;
22
+ ID rb_sBreak;
23
+ ID rb_sCall;
24
+ ID rb_sCase;
25
+ ID rb_sCDecl;
26
+ ID rb_sClass;
27
+ ID rb_sColon2;
28
+ ID rb_sColon3;
29
+ ID rb_sConst;
30
+ ID rb_sCVar;
31
+ ID rb_sCVAsgn;
32
+ ID rb_sCVDecl;
33
+ ID rb_sData;
34
+ ID rb_sDefined;
35
+ ID rb_sDefn;
36
+ ID rb_sDefs;
37
+ ID rb_sDot2;
38
+ ID rb_sDot3;
39
+ ID rb_sDRegx;
40
+ ID rb_sDRegxOnce;
41
+ ID rb_sDStr;
42
+ ID rb_sDSym;
43
+ ID rb_sDXStr;
44
+ ID rb_sEncoding;
45
+ ID rb_sEnsure;
46
+ ID rb_sEvStr;
47
+ ID rb_sFalse;
48
+ ID rb_sFCall;
49
+ ID rb_sFile;
50
+ ID rb_sFixnum;
51
+ ID rb_sFlip2;
52
+ ID rb_sFlip3;
53
+ ID rb_sFloat;
54
+ ID rb_sFor;
55
+ ID rb_sGAsgn;
56
+ ID rb_sGVar;
57
+ ID rb_sHash;
58
+ ID rb_sIAsgn;
59
+ ID rb_sIf;
60
+ ID rb_sIter;
61
+ ID rb_sIVar;
62
+ ID rb_sLAsgn;
63
+ ID rb_sLambda;
64
+ ID rb_sLit;
65
+ ID rb_sLVar;
66
+ ID rb_sMAsgn;
67
+ ID rb_sMatch;
68
+ ID rb_sMatch2;
69
+ ID rb_sMatch3;
70
+ ID rb_sModule;
71
+ ID rb_sNegate;
72
+ ID rb_sNext;
73
+ ID rb_sNil;
74
+ ID rb_sNot;
75
+ ID rb_sNthRef;
76
+ ID rb_sNumber;
77
+ ID rb_sOpAsgn1;
78
+ ID rb_sOpAsgn2;
79
+ ID rb_sOpAsgnAnd;
80
+ ID rb_sOpAsgnOr;
81
+ ID rb_sOptArg;
82
+ ID rb_sOr;
83
+ ID rb_sPostExe;
84
+ ID rb_sPostArg;
85
+ ID rb_sPreExe;
86
+ ID rb_sRedo;
87
+ ID rb_sRegex;
88
+ ID rb_sResbody;
89
+ ID rb_sRescue;
90
+ ID rb_sRetry;
91
+ ID rb_sReturn;
92
+ ID rb_sSClass;
93
+ ID rb_sScope;
94
+ ID rb_sSelf;
95
+ ID rb_sSplat;
96
+ ID rb_sStr;
97
+ ID rb_sSuper;
98
+ ID rb_sSValue;
99
+ ID rb_sToAry;
100
+ ID rb_sTrue;
101
+ ID rb_sUndef;
102
+ ID rb_sUntil;
103
+ ID rb_sVAlias;
104
+ ID rb_sValues;
105
+ ID rb_sVCall;
106
+ ID rb_sWhen;
107
+ ID rb_sWhile;
108
+ ID rb_sXStr;
109
+ ID rb_sYield;
110
+ ID rb_sZArray;
111
+ ID rb_sZSuper;
112
+
113
+ };
114
+
115
+ #ifdef __cplusplus
116
+ } /* extern "C" { */
117
+ #endif
118
+
119
+ namespace MELBOURNE {
120
+
121
+ void init_symbols() {
122
+ rb_sAlias = rb_intern("process_alias");
123
+ rb_sAnd = rb_intern("process_and");
124
+ rb_sArgs = rb_intern("process_args");
125
+ rb_sArgsCat = rb_intern("process_argscat");
126
+ rb_sArgsPush = rb_intern("process_argspush");
127
+ rb_sArray = rb_intern("process_array");
128
+ rb_sAttrAsgn = rb_intern("process_attrasgn");
129
+ rb_sBackRef = rb_intern("process_back_ref");
130
+ rb_sBegin = rb_intern("process_begin");
131
+ rb_sBlock = rb_intern("process_block");
132
+ rb_sBlockArg = rb_intern("process_block_arg");
133
+ rb_sBlockPass = rb_intern("process_block_pass");
134
+ rb_sBreak = rb_intern("process_break");
135
+ rb_sCall = rb_intern("process_call");
136
+ rb_sCase = rb_intern("process_case");
137
+ rb_sCDecl = rb_intern("process_cdecl");
138
+ rb_sClass = rb_intern("process_class");
139
+ rb_sColon2 = rb_intern("process_colon2");
140
+ rb_sColon3 = rb_intern("process_colon3");
141
+ rb_sConst = rb_intern("process_const");
142
+ rb_sCVar = rb_intern("process_cvar");
143
+ rb_sCVAsgn = rb_intern("process_cvasgn");
144
+ rb_sCVDecl = rb_intern("process_cvdecl");
145
+ rb_sData = rb_intern("process_data");
146
+ rb_sDefined = rb_intern("process_defined");
147
+ rb_sDefn = rb_intern("process_defn");
148
+ rb_sDefs = rb_intern("process_defs");
149
+ rb_sDot2 = rb_intern("process_dot2");
150
+ rb_sDot3 = rb_intern("process_dot3");
151
+ rb_sDRegx = rb_intern("process_dregx");
152
+ rb_sDRegxOnce = rb_intern("process_dregx_once");
153
+ rb_sDStr = rb_intern("process_dstr");
154
+ rb_sDSym = rb_intern("process_dsym");
155
+ rb_sDXStr = rb_intern("process_dxstr");
156
+ rb_sEncoding = rb_intern("process_encoding");
157
+ rb_sEnsure = rb_intern("process_ensure");
158
+ rb_sEvStr = rb_intern("process_evstr");
159
+ rb_sFalse = rb_intern("process_false");
160
+ rb_sFCall = rb_intern("process_fcall");
161
+ rb_sFile = rb_intern("process_file");
162
+ rb_sFixnum = rb_intern("process_fixnum");
163
+ rb_sFlip2 = rb_intern("process_flip2");
164
+ rb_sFlip3 = rb_intern("process_flip3");
165
+ rb_sFloat = rb_intern("process_float");
166
+ rb_sFor = rb_intern("process_for");
167
+ rb_sGAsgn = rb_intern("process_gasgn");
168
+ rb_sGVar = rb_intern("process_gvar");
169
+ rb_sHash = rb_intern("process_hash");
170
+ rb_sIAsgn = rb_intern("process_iasgn");
171
+ rb_sIf = rb_intern("process_if");
172
+ rb_sIter = rb_intern("process_iter");
173
+ rb_sIVar = rb_intern("process_ivar");
174
+ rb_sLAsgn = rb_intern("process_lasgn");
175
+ rb_sLambda = rb_intern("process_lambda");
176
+ rb_sLit = rb_intern("process_lit");
177
+ rb_sLVar = rb_intern("process_lvar");
178
+ rb_sMAsgn = rb_intern("process_masgn");
179
+ rb_sMatch = rb_intern("process_match");
180
+ rb_sMatch2 = rb_intern("process_match2");
181
+ rb_sMatch3 = rb_intern("process_match3");
182
+ rb_sModule = rb_intern("process_module");
183
+ rb_sNegate = rb_intern("process_negate");
184
+ rb_sNext = rb_intern("process_next");
185
+ rb_sNil = rb_intern("process_nil");
186
+ rb_sNot = rb_intern("process_not");
187
+ rb_sNthRef = rb_intern("process_nth_ref");
188
+ rb_sNumber = rb_intern("process_number");
189
+ rb_sOpAsgn1 = rb_intern("process_op_asgn1");
190
+ rb_sOpAsgn2 = rb_intern("process_op_asgn2");
191
+ rb_sOpAsgnAnd = rb_intern("process_op_asgn_and");
192
+ rb_sOpAsgnOr = rb_intern("process_op_asgn_or");
193
+ rb_sOptArg = rb_intern("process_opt_arg");
194
+ rb_sOr = rb_intern("process_or");
195
+ rb_sPostExe = rb_intern("process_postexe");
196
+ rb_sPostArg = rb_intern("process_postarg");
197
+ rb_sPreExe = rb_intern("process_preexe");
198
+ rb_sRedo = rb_intern("process_redo");
199
+ rb_sRegex = rb_intern("process_regex");
200
+ rb_sResbody = rb_intern("process_resbody");
201
+ rb_sRescue = rb_intern("process_rescue");
202
+ rb_sRetry = rb_intern("process_retry");
203
+ rb_sReturn = rb_intern("process_return");
204
+ rb_sSClass = rb_intern("process_sclass");
205
+ rb_sScope = rb_intern("process_scope");
206
+ rb_sSelf = rb_intern("process_self");
207
+ rb_sSplat = rb_intern("process_splat");
208
+ rb_sStr = rb_intern("process_str");
209
+ rb_sSuper = rb_intern("process_super");
210
+ rb_sSValue = rb_intern("process_svalue");
211
+ rb_sToAry = rb_intern("process_to_ary");
212
+ rb_sTrue = rb_intern("process_true");
213
+ rb_sUndef = rb_intern("process_undef");
214
+ rb_sUntil = rb_intern("process_until");
215
+ rb_sVAlias = rb_intern("process_valias");
216
+ rb_sValues = rb_intern("process_values");
217
+ rb_sVCall = rb_intern("process_vcall");
218
+ rb_sWhen = rb_intern("process_when");
219
+ rb_sWhile = rb_intern("process_while");
220
+ rb_sXStr = rb_intern("process_xstr");
221
+ rb_sYield = rb_intern("process_yield");
222
+ rb_sZArray = rb_intern("process_zarray");
223
+ rb_sZSuper = rb_intern("process_zsuper");
224
+ }
225
+ };