cast19 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/ext/cast19/extconf.rb +1 -1
  2. data/ext/cast19/yylex.re +330 -0
  3. metadata +2 -1
@@ -1,2 +1,2 @@
1
1
  require 'mkmf'
2
- create_makefile('cast_ext')
2
+ create_makefile('cast19/cast_ext')
@@ -0,0 +1,330 @@
1
+ /* -*- mode: c -*- */
2
+ /* Given to re2c to generate the lexer `yylex'.
3
+ *
4
+ * Based on c.re in the exmaples distributed with re2c.
5
+ */
6
+ #include <string.h>
7
+ #include "cast.h"
8
+
9
+ /*
10
+ * -------------------------------------------------------------------
11
+ * Helpers
12
+ * -------------------------------------------------------------------
13
+ */
14
+
15
+ #define new_func(Foo) \
16
+ VALUE cast_new_##Foo##_at(long pos) { \
17
+ VALUE c##Foo; \
18
+ c##Foo = rb_const_get(cast_mC, rb_intern(#Foo)); \
19
+ return rb_funcall2(c##Foo, rb_intern("new"), 0, NULL); \
20
+ }
21
+ #define set_func(Foo, field) \
22
+ VALUE cast_##Foo##_set_##field(VALUE self, VALUE value) { \
23
+ return rb_funcall2(self, rb_intern(#field "="), 1, &value); \
24
+ }
25
+
26
+ new_func(IntLiteral);
27
+ set_func(IntLiteral, format);
28
+ set_func(IntLiteral, val);
29
+ set_func(IntLiteral, suffix);
30
+
31
+ new_func(FloatLiteral);
32
+ set_func(FloatLiteral, format);
33
+ set_func(FloatLiteral, val);
34
+ set_func(FloatLiteral, suffix);
35
+
36
+ new_func(CharLiteral);
37
+ set_func(CharLiteral, wide);
38
+ set_func(CharLiteral, val);
39
+
40
+ new_func(StringLiteral);
41
+ set_func(StringLiteral, wide);
42
+ set_func(StringLiteral, val);
43
+
44
+ /*
45
+ * -------------------------------------------------------------------
46
+ * yylex
47
+ * -------------------------------------------------------------------
48
+ */
49
+ #define BSIZE 8192
50
+
51
+ #define YYLTYPE VALUE
52
+
53
+ #define YYCTYPE char
54
+ #define YYCURSOR cursor
55
+ #define YYLIMIT p->lim
56
+ #define YYMARKER p->ptr
57
+ #define YYFILL(n) {}
58
+
59
+ #define RET(sym) {p->cur = cursor; rb_ary_store(p->token, 0, sym); rb_ary_store(p->token, 1, sym ); return;}
60
+ #define RETVALUE(sym) {p->cur = cursor; rb_ary_store(p->token, 0, sym); rb_ary_store(p->token, 1, value); return;}
61
+
62
+ /* Raise a ParseError. `s' is the format string for the exception
63
+ * message, which must contain exactly one '%s', which is replaced by
64
+ * the string delimited by `b' and `e'.
65
+ */
66
+ static void error1(char *s, char *b, char *e) {
67
+ char *str;
68
+ str = ALLOCA_N(char, e - b + 1);
69
+ memcpy(str, b, e-b);
70
+ str[e-b] = '\0';
71
+ rb_raise(cast_eParseError, s, str);
72
+ }
73
+
74
+ /* `token' is assumed to be a two element array, which is filled in.
75
+ */
76
+ void yylex(VALUE self, cast_Parser *p) {
77
+ char *cursor = p->cur;
78
+ char *cp;
79
+ VALUE value;
80
+ std:
81
+ p->tok = cursor;
82
+ /*!re2c
83
+ any = [\000-\377];
84
+ O = [0-7];
85
+ D = [0-9];
86
+ H = [a-fA-F0-9];
87
+ N = [1-9];
88
+ L = [a-zA-Z_];
89
+ E = [Ee] [+-]? D+;
90
+ P = [Pp] [+-]? D+;
91
+ FS = [fFlL];
92
+ IS = [uUlL]+;
93
+
94
+ ESC = [\\] ([abfnrtv?'"\\] | O (O O?)? | "x" H+);
95
+ */
96
+ /*!re2c
97
+ "/*" { goto comment; }
98
+ "//" { goto comment2; }
99
+
100
+ "auto" { RET(cast_sym_AUTO); }
101
+ "break" { RET(cast_sym_BREAK); }
102
+ "case" { RET(cast_sym_CASE); }
103
+ "char" { RET(cast_sym_CHAR); }
104
+ "const" { RET(cast_sym_CONST); }
105
+ "continue" { RET(cast_sym_CONTINUE); }
106
+ "default" { RET(cast_sym_DEFAULT); }
107
+ "do" { RET(cast_sym_DO); }
108
+ "double" { RET(cast_sym_DOUBLE); }
109
+ "else" { RET(cast_sym_ELSE); }
110
+ "enum" { RET(cast_sym_ENUM); }
111
+ "extern" { RET(cast_sym_EXTERN); }
112
+ "float" { RET(cast_sym_FLOAT); }
113
+ "for" { RET(cast_sym_FOR); }
114
+ "goto" { RET(cast_sym_GOTO); }
115
+ "if" { RET(cast_sym_IF); }
116
+ "int" { RET(cast_sym_INT); }
117
+ "long" { RET(cast_sym_LONG); }
118
+ "register" { RET(cast_sym_REGISTER); }
119
+ "return" { RET(cast_sym_RETURN); }
120
+ "short" { RET(cast_sym_SHORT); }
121
+ "signed" { RET(cast_sym_SIGNED); }
122
+ "sizeof" { RET(cast_sym_SIZEOF); }
123
+ "static" { RET(cast_sym_STATIC); }
124
+ "struct" { RET(cast_sym_STRUCT); }
125
+ "switch" { RET(cast_sym_SWITCH); }
126
+ "typedef" { RET(cast_sym_TYPEDEF); }
127
+ "union" { RET(cast_sym_UNION); }
128
+ "unsigned" { RET(cast_sym_UNSIGNED); }
129
+ "void" { RET(cast_sym_VOID); }
130
+ "volatile" { RET(cast_sym_VOLATILE); }
131
+ "while" { RET(cast_sym_WHILE); }
132
+ "inline" { RET(cast_sym_INLINE); }
133
+ "restrict" { RET(cast_sym_RESTRICT); }
134
+ "_Bool" { RET(cast_sym_BOOL); }
135
+ "_Complex" { RET(cast_sym_COMPLEX); }
136
+ "_Imaginary" { RET(cast_sym_IMAGINARY); }
137
+
138
+ L (L|D)* {
139
+ value = rb_str_new(p->tok, cursor - p->tok);
140
+ if (rb_funcall2(rb_funcall2(self, rb_intern("type_names"), 0, NULL),
141
+ rb_intern("include?"), 1, &value) == Qtrue) {
142
+ RETVALUE(cast_sym_TYPENAME);
143
+ } else {
144
+ RETVALUE(cast_sym_ID);
145
+ }
146
+ }
147
+
148
+ SUF = L(L|D)*;
149
+
150
+ "0" [xX] H+ SUF? {
151
+ value = cast_new_IntLiteral_at(p->lineno);
152
+ cast_IntLiteral_set_format(value, ID2SYM(rb_intern("hex")));
153
+ cast_IntLiteral_set_val(value, LONG2NUM(strtol(p->tok, (char **)&cp, 16)));
154
+ if (cp < cursor)
155
+ cast_IntLiteral_set_suffix(value, rb_str_new(cp, cursor - cp));
156
+ RETVALUE(cast_sym_ICON);
157
+ }
158
+ "0" D+ SUF? {
159
+ value = cast_new_IntLiteral_at(p->lineno);
160
+ cast_IntLiteral_set_format(value, ID2SYM(rb_intern("oct")));
161
+ cast_IntLiteral_set_val(value, LONG2NUM(strtol(p->tok, (char **)&cp, 8)));
162
+ if (cp < cursor) {
163
+ if (cp[0] == '8' || cp[0] == '9')
164
+ rb_raise(cast_eParseError, "bad octal digit: %c", cp[0]);
165
+ cast_IntLiteral_set_suffix(value, rb_str_new(cp, cursor - cp));
166
+ }
167
+ RETVALUE(cast_sym_ICON);
168
+ }
169
+ ( "0" | [1-9] D* ) SUF? {
170
+ value = cast_new_IntLiteral_at(p->lineno);
171
+ cast_IntLiteral_set_format(value, ID2SYM(rb_intern("dec")));
172
+ cast_IntLiteral_set_val(value, LONG2NUM(strtol(p->tok, (char **)&cp, 10)));
173
+ if (cp < cursor)
174
+ cast_IntLiteral_set_suffix(value, rb_str_new(cp, cursor - cp));
175
+ RETVALUE(cast_sym_ICON);
176
+ }
177
+
178
+ ( D+ E | D* "." D+ E? | D+ "." D* E? ) SUF? {
179
+ value = cast_new_FloatLiteral_at(p->lineno);
180
+ cast_FloatLiteral_set_format(value, ID2SYM(rb_intern("dec")));
181
+ cast_FloatLiteral_set_val(value, rb_float_new(strtod(p->tok, (char **)&cp)));
182
+ if (cp < cursor)
183
+ cast_FloatLiteral_set_suffix(value, rb_str_new(cp, cursor - cp));
184
+ RETVALUE(cast_sym_FCON);
185
+ }
186
+ ( "0" [Xx] (H+ P | H* "." H+ P? | H+ "." H* P?) ) SUF? {
187
+ value = cast_new_FloatLiteral_at(p->lineno);
188
+ cast_FloatLiteral_set_format(value, ID2SYM(rb_intern("hex")));
189
+ cast_FloatLiteral_set_val(value, rb_float_new(strtod(p->tok, (char **)&cp)));
190
+ if (cp < cursor)
191
+ cast_FloatLiteral_set_suffix(value, rb_str_new(cp, cursor - cp));
192
+ RETVALUE(cast_sym_FCON);
193
+ }
194
+
195
+ L? ['] (ESC|any\[\n\\'])+ ['] {
196
+ value = cast_new_CharLiteral_at(p->lineno);
197
+ if (p->tok[0] == 'L') {
198
+ cast_CharLiteral_set_wide(value, Qtrue);
199
+ cp = p->tok + 1;
200
+ } else {
201
+ cast_CharLiteral_set_wide(value, Qfalse);
202
+ cp = p->tok;
203
+ }
204
+ cast_CharLiteral_set_val(value, rb_str_new(cp + 1, cursor - cp - 2));
205
+ RETVALUE(cast_sym_CCON);
206
+ }
207
+ L? ["] (ESC|any\[\n\\"])* ["] {
208
+ value = cast_new_StringLiteral_at(p->lineno);
209
+ if (p->tok[0] == 'L') {
210
+ cast_StringLiteral_set_wide(value, Qtrue);
211
+ cp = p->tok + 1;
212
+ } else {
213
+ cast_StringLiteral_set_wide(value, Qfalse);
214
+ cp = p->tok;
215
+ }
216
+ cast_StringLiteral_set_val(value, rb_str_new(cp + 1, cursor - cp - 2));
217
+ RETVALUE(cast_sym_SCON);
218
+ }
219
+
220
+ "..." { RET(cast_sym_ELLIPSIS); }
221
+ ">>=" { RET(cast_sym_RSHIFTEQ); }
222
+ "<<=" { RET(cast_sym_LSHIFTEQ); }
223
+ "+=" { RET(cast_sym_ADDEQ); }
224
+ "-=" { RET(cast_sym_SUBEQ); }
225
+ "*=" { RET(cast_sym_MULEQ); }
226
+ "/=" { RET(cast_sym_DIVEQ); }
227
+ "%=" { RET(cast_sym_MODEQ); }
228
+ "&=" { RET(cast_sym_ANDEQ); }
229
+ "^=" { RET(cast_sym_XOREQ); }
230
+ "|=" { RET(cast_sym_OREQ); }
231
+ ">>" { RET(cast_sym_RSHIFT); }
232
+ "<<" { RET(cast_sym_LSHIFT); }
233
+ "++" { RET(cast_sym_INC); }
234
+ "--" { RET(cast_sym_DEC); }
235
+ "->" { RET(cast_sym_ARROW); }
236
+ "&&" { RET(cast_sym_ANDAND); }
237
+ "||" { RET(cast_sym_OROR); }
238
+ "<=" { RET(cast_sym_LEQ); }
239
+ ">=" { RET(cast_sym_GEQ); }
240
+ "==" { RET(cast_sym_EQEQ); }
241
+ "!=" { RET(cast_sym_NEQ); }
242
+ ";" { RET(cast_sym_SEMICOLON); }
243
+ "{" { RET(cast_sym_LBRACE); }
244
+ "}" { RET(cast_sym_RBRACE); }
245
+ "," { RET(cast_sym_COMMA); }
246
+ ":" { RET(cast_sym_COLON); }
247
+ "=" { RET(cast_sym_EQ); }
248
+ "(" { RET(cast_sym_LPAREN); }
249
+ ")" { RET(cast_sym_RPAREN); }
250
+ "[" { RET(cast_sym_LBRACKET); }
251
+ "]" { RET(cast_sym_RBRACKET); }
252
+ "." { RET(cast_sym_DOT); }
253
+ "&" { RET(cast_sym_AND); }
254
+ "!" { RET(cast_sym_BANG); }
255
+ "~" { RET(cast_sym_NOT); }
256
+ "-" { RET(cast_sym_SUB); }
257
+ "+" { RET(cast_sym_ADD); }
258
+ "*" { RET(cast_sym_MUL); }
259
+ "/" { RET(cast_sym_DIV); }
260
+ "%" { RET(cast_sym_MOD); }
261
+ "<" { RET(cast_sym_LT); }
262
+ ">" { RET(cast_sym_GT); }
263
+ "^" { RET(cast_sym_XOR); }
264
+ "|" { RET(cast_sym_OR); }
265
+ "?" { RET(cast_sym_QUESTION); }
266
+
267
+ "<:" { RET(cast_sym_LBRACKET); }
268
+ "<%" { RET(cast_sym_LBRACE); }
269
+ ":>" { RET(cast_sym_RBRACKET); }
270
+ "%>" { RET(cast_sym_RBRACE); }
271
+
272
+ [ \t\v\f]+ { goto std; }
273
+
274
+ [\000]
275
+ {
276
+ if(cursor == p->eof) RET(Qnil);
277
+ goto std;
278
+ }
279
+
280
+ "\n"
281
+ {
282
+ p->pos = cursor; ++p->lineno;
283
+ goto std;
284
+ }
285
+
286
+ any
287
+ {
288
+ //printf("unexpected character: %c\n", *p->tok);
289
+ rb_raise(cast_eParseError, "%d: unexpected character: %c (ASCII %d)\n", p->lineno, *p->tok, (int)*p->tok);
290
+ goto std;
291
+ }
292
+ */
293
+
294
+ comment:
295
+ /*!re2c
296
+ "*" "/" { goto std; }
297
+ "\n"
298
+ {
299
+ p->tok = p->pos = cursor; ++p->lineno;
300
+ goto comment;
301
+ }
302
+
303
+ [\000]
304
+ {
305
+ if (cursor == p->eof)
306
+ rb_raise(cast_eParseError,
307
+ "%d: unclosed multiline comment",
308
+ p->lineno);
309
+ }
310
+
311
+ any { goto comment; }
312
+ */
313
+
314
+ comment2:
315
+ /*!re2c
316
+ "\n"
317
+ {
318
+ p->tok = p->pos = cursor; ++p->lineno;
319
+ goto std;
320
+ }
321
+
322
+ [\000]
323
+ {
324
+ if (cursor == p->eof) RET(Qnil);
325
+ goto std;
326
+ }
327
+
328
+ any { goto comment2; }
329
+ */
330
+ }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cast19
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -24,6 +24,7 @@ files:
24
24
  - ext/cast19/yylex.c
25
25
  - ext/cast19/cast.h
26
26
  - ext/cast19/extconf.rb
27
+ - ext/cast19/yylex.re
27
28
  - lib/cast19/c.tab.rb
28
29
  - lib/cast19/c_nodes.rb
29
30
  - lib/cast19/inspect.rb