cast 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2014 @@
1
+ ###
2
+ ### ##################################################################
3
+ ###
4
+ ### Tests for the parse methods.
5
+ ###
6
+ ### ##################################################################
7
+ ###
8
+
9
+ require 'common'
10
+
11
+ class MatchTest < Test::Unit::TestCase
12
+ def setup
13
+ C.default_parser = C::Parser.new
14
+ end
15
+ def test_node_matches
16
+ i = C::Int.new
17
+ assert_same(true, i.match?(i))
18
+ assert_same(true, i.match?('int'))
19
+
20
+ l = C::IntLiteral.new(10)
21
+ assert_same(true, l.match?(l))
22
+ assert_same(true, l.match?(10))
23
+
24
+ i = C::Int.new
25
+ assert_same(false, i.match?('unsigned int'))
26
+ assert_same(false, i.match?('long int'))
27
+ assert_same(false, i.match?('no int here')) # shouldn't raise!
28
+
29
+ l = C::IntLiteral.new(10)
30
+ assert_same(false, i.match?(10.0))
31
+
32
+ t = C::CustomType.new('T')
33
+ ##
34
+ assert_same(false, t.match?('T'))
35
+ ##
36
+ parser = C::Parser.new
37
+ parser.type_names << 'T'
38
+ assert_same(true, t.match?('T', parser))
39
+ ##
40
+ assert_same(false, t.match?('T'))
41
+ ##
42
+ C.default_parser.type_names << 'T'
43
+ assert_same(true, t.match?('T'))
44
+ end
45
+
46
+ def test_nodelist_match
47
+ list = C::NodeArray[]
48
+ assert_same(true, list.match?(list))
49
+ assert_same(true, list.match?([]))
50
+
51
+ list = C::NodeArray[C::Int.new, C::IntLiteral.new(10)]
52
+ list2 = C::NodeChain[C::Int.new, C::IntLiteral.new(10)]
53
+ assert_same(true, list.match?(list))
54
+ assert_same(true, list.match?(list2))
55
+ assert_same(true, list.match?(['int', 10]))
56
+
57
+ list = C::NodeArray[C::NodeArray[C::Int.new], C::NodeChain[]]
58
+ list2 = C::NodeChain[C::NodeChain[C::Int.new], C::NodeArray[]]
59
+ assert_same(true, list.match?(list))
60
+ assert_same(true, list.match?(list2))
61
+ assert_same(true, list.match?([['int'], []]))
62
+ assert_same(false, list.match?([[], ['int']]))
63
+ assert_same(false, list.match?(['int']))
64
+ assert_same(false, list.match?([['int']]))
65
+
66
+ t = C::NodeArray[C::CustomType.new('T')]
67
+ ##
68
+ assert_same(false, t.match?(['T']))
69
+ ##
70
+ parser = C::Parser.new
71
+ parser.type_names << 'T'
72
+ assert_same(true, t.match?(['T'], parser))
73
+ ##
74
+ assert_same(false, t.match?(['T']))
75
+ ##
76
+ C.default_parser.type_names << 'T'
77
+ assert_same(true, t.match?(['T']))
78
+ end
79
+ end
80
+
81
+ class ParseTests < Test::Unit::TestCase
82
+ include CheckAst
83
+ def check klass, s
84
+ check_ast(s){|inp| klass.parse(inp)}
85
+ end
86
+
87
+ def test_translation_unit
88
+ check C::TranslationUnit, <<EOS
89
+ int i;
90
+ void (*f)(void *);
91
+ ----
92
+ TranslationUnit
93
+ entities:
94
+ - Declaration
95
+ type: Int
96
+ declarators:
97
+ - Declarator
98
+ name: "i"
99
+ - Declaration
100
+ type: Void
101
+ declarators:
102
+ - Declarator
103
+ indirect_type: Pointer
104
+ type: Function
105
+ params:
106
+ - Parameter
107
+ type: Pointer
108
+ type: Void
109
+ name: "f"
110
+ EOS
111
+ assert_raise(ParseError){C::TranslationUnit.parse('')}
112
+ end
113
+
114
+ def test_declaration
115
+ check C::Declaration, <<EOS
116
+ int i;
117
+ ----
118
+ Declaration
119
+ type: Int
120
+ declarators:
121
+ - Declarator
122
+ name: "i"
123
+ EOS
124
+ check C::Declaration, <<EOS
125
+ int i, j;
126
+ ----
127
+ Declaration
128
+ type: Int
129
+ declarators:
130
+ - Declarator
131
+ name: "i"
132
+ - Declarator
133
+ name: "j"
134
+ EOS
135
+ assert_raise(ParseError){C::Declaration.parse('int i; int j;')}
136
+ assert_raise(ParseError){C::Declaration.parse('int f() {}')}
137
+ assert_raise(ParseError){C::Declaration.parse('')}
138
+ end
139
+
140
+ def test_parameter
141
+ check C::Parameter, <<EOS
142
+ int i
143
+ ----
144
+ Parameter
145
+ type: Int
146
+ name: "i"
147
+ EOS
148
+ check C::Parameter, <<EOS
149
+ int
150
+ ----
151
+ Parameter
152
+ type: Int
153
+ EOS
154
+ check C::Parameter, <<EOS
155
+ i
156
+ ----
157
+ Parameter
158
+ name: "i"
159
+ EOS
160
+ check C::Parameter, <<EOS
161
+ void
162
+ ----
163
+ Parameter
164
+ type: Void
165
+ EOS
166
+ assert_raise(ParseError){C::Parameter.parse('...')}
167
+ assert_raise(ParseError){C::Parameter.parse(') {} void (')}
168
+ assert_raise(ParseError){C::Parameter.parse('); void(')}
169
+ assert_raise(ParseError){C::Parameter.parse('i,j')}
170
+ assert_raise(ParseError){C::Parameter.parse('int,float')}
171
+ assert_raise(ParseError){C::Parameter.parse('')}
172
+ end
173
+
174
+ def test_declarator
175
+ check C::Declarator, <<EOS
176
+ x
177
+ ----
178
+ Declarator
179
+ name: "x"
180
+ EOS
181
+ check C::Declarator, <<EOS
182
+ *x
183
+ ----
184
+ Declarator
185
+ indirect_type: Pointer
186
+ name: "x"
187
+ EOS
188
+ check C::Declarator, <<EOS
189
+ x[10]
190
+ ----
191
+ Declarator
192
+ indirect_type: Array
193
+ length: IntLiteral
194
+ val: 10
195
+ name: "x"
196
+ EOS
197
+ check C::Declarator, <<EOS
198
+ *x(int argc, char **argv)
199
+ ----
200
+ Declarator
201
+ indirect_type: Function
202
+ type: Pointer
203
+ params:
204
+ - Parameter
205
+ type: Int
206
+ name: "argc"
207
+ - Parameter
208
+ type: Pointer
209
+ type: Pointer
210
+ type: Char
211
+ name: "argv"
212
+ name: "x"
213
+ EOS
214
+ assert_raise(ParseError){C::Declarator.parse('f; int f;')}
215
+ assert_raise(ParseError){C::Declarator.parse('i,j')}
216
+ assert_raise(ParseError){C::Declarator.parse(';')}
217
+ assert_raise(ParseError){C::Declarator.parse('')}
218
+ end
219
+
220
+ def test_function_def
221
+ check C::FunctionDef, <<EOS
222
+ int f() {}
223
+ ----
224
+ FunctionDef
225
+ type: Function
226
+ type: Int
227
+ name: "f"
228
+ EOS
229
+ check C::FunctionDef, <<EOS
230
+ void *f(void *) {}
231
+ ----
232
+ FunctionDef
233
+ type: Function
234
+ type: Pointer
235
+ type: Void
236
+ params:
237
+ - Parameter
238
+ type: Pointer
239
+ type: Void
240
+ name: "f"
241
+ EOS
242
+ assert_raise(ParseError){C::FunctionDef.parse('void f(); void g();')}
243
+ assert_raise(ParseError){C::FunctionDef.parse('int i;')}
244
+ assert_raise(ParseError){C::FunctionDef.parse('void f();')}
245
+ assert_raise(ParseError){C::FunctionDef.parse(';')}
246
+ assert_raise(ParseError){C::FunctionDef.parse('')}
247
+ end
248
+
249
+ def test_enumerator
250
+ check C::Enumerator, <<EOS
251
+ X
252
+ ----
253
+ Enumerator
254
+ name: "X"
255
+ EOS
256
+ check C::Enumerator, <<EOS
257
+ X=10
258
+ ----
259
+ Enumerator
260
+ name: "X"
261
+ val: IntLiteral
262
+ val: 10
263
+ EOS
264
+ assert_raise(ParseError){C::Enumerator.parse('} enum {')}
265
+ assert_raise(ParseError){C::Enumerator.parse('} f() {')}
266
+ assert_raise(ParseError){C::Enumerator.parse('X, Y')}
267
+ assert_raise(ParseError){C::Enumerator.parse('')}
268
+ end
269
+
270
+ def test_member_initializer
271
+ check C::MemberInit, <<EOS
272
+ 1
273
+ ----
274
+ MemberInit
275
+ init: IntLiteral
276
+ val: 1
277
+ EOS
278
+ check C::MemberInit, <<EOS
279
+ 1,
280
+ ----
281
+ MemberInit
282
+ init: IntLiteral
283
+ val: 1
284
+ EOS
285
+ check C::MemberInit, <<EOS
286
+ i
287
+ ----
288
+ MemberInit
289
+ init: Variable
290
+ name: "i"
291
+ EOS
292
+ check C::MemberInit, <<EOS
293
+ .i = i
294
+ ----
295
+ MemberInit
296
+ member:
297
+ - Member
298
+ name: "i"
299
+ init: Variable
300
+ name: "i"
301
+ EOS
302
+ check C::MemberInit, <<EOS
303
+ .i [5] = 10.0
304
+ ----
305
+ MemberInit
306
+ member:
307
+ - Member
308
+ name: "i"
309
+ - IntLiteral
310
+ val: 5
311
+ init: FloatLiteral
312
+ val: 10.0
313
+ EOS
314
+ assert_raise(ParseError){C::MemberInit.parse('} int f() {')}
315
+ assert_raise(ParseError){C::MemberInit.parse('}} f() {{')}
316
+ assert_raise(ParseError){C::MemberInit.parse('1}; x = {1')}
317
+ assert_raise(ParseError){C::MemberInit.parse('1}, y')}
318
+ assert_raise(ParseError){C::MemberInit.parse('1, 2')}
319
+ assert_raise(ParseError){C::MemberInit.parse('')}
320
+ end
321
+
322
+ def test_block
323
+ check C::Block, <<EOS
324
+ {}
325
+ ----
326
+ Block
327
+ EOS
328
+ check C::Block, <<EOS
329
+ {{}}
330
+ ----
331
+ Block
332
+ stmts:
333
+ - Block
334
+ EOS
335
+ assert_raise(ParseError){C::Block.parse('} void f() {')}
336
+ assert_raise(ParseError){C::Block.parse(';;')}
337
+ assert_raise(ParseError){C::Block.parse('int i;')}
338
+ assert_raise(ParseError){C::Block.parse(';')}
339
+ assert_raise(ParseError){C::Block.parse('')}
340
+ end
341
+
342
+ def test_if
343
+ check C::If, <<EOS
344
+ if (1) 10;
345
+ ----
346
+ If
347
+ cond: IntLiteral
348
+ val: 1
349
+ then: ExpressionStatement
350
+ expr: IntLiteral
351
+ val: 10
352
+ EOS
353
+ check C::If, <<EOS
354
+ if (1) 10; else 20;
355
+ ----
356
+ If
357
+ cond: IntLiteral
358
+ val: 1
359
+ then: ExpressionStatement
360
+ expr: IntLiteral
361
+ val: 10
362
+ else: ExpressionStatement
363
+ expr: IntLiteral
364
+ val: 20
365
+ EOS
366
+ assert_raise(ParseError){C::If.parse('} void f() {')}
367
+ assert_raise(ParseError){C::If.parse(';;')}
368
+ assert_raise(ParseError){C::If.parse('int i;')}
369
+ assert_raise(ParseError){C::If.parse(';')}
370
+ assert_raise(ParseError){C::If.parse('')}
371
+ end
372
+
373
+ def test_switch
374
+ check C::Switch, <<EOS
375
+ switch (x);
376
+ ----
377
+ Switch
378
+ cond: Variable
379
+ name: "x"
380
+ stmt: ExpressionStatement
381
+ EOS
382
+ assert_raise(ParseError){C::Switch.parse('} void f() {')}
383
+ assert_raise(ParseError){C::Switch.parse(';;')}
384
+ assert_raise(ParseError){C::Switch.parse('int i;')}
385
+ assert_raise(ParseError){C::Switch.parse(';')}
386
+ assert_raise(ParseError){C::Switch.parse('')}
387
+ end
388
+
389
+ def test_while
390
+ check C::While, <<EOS
391
+ while (1);
392
+ ----
393
+ While
394
+ cond: IntLiteral
395
+ val: 1
396
+ stmt: ExpressionStatement
397
+ EOS
398
+ check C::While, <<EOS
399
+ do ; while (1);
400
+ ----
401
+ While (do)
402
+ cond: IntLiteral
403
+ val: 1
404
+ stmt: ExpressionStatement
405
+ EOS
406
+ assert_raise(ParseError){C::While.parse('} void f() {')}
407
+ assert_raise(ParseError){C::While.parse(';;')}
408
+ assert_raise(ParseError){C::While.parse('int i;')}
409
+ assert_raise(ParseError){C::While.parse(';')}
410
+ assert_raise(ParseError){C::While.parse('')}
411
+ end
412
+
413
+ def test_for
414
+ check C::For, <<EOS
415
+ for (;;);
416
+ ----
417
+ For
418
+ stmt: ExpressionStatement
419
+ EOS
420
+ check C::For, <<EOS
421
+ for (int i; ; );
422
+ ----
423
+ For
424
+ init: Declaration
425
+ type: Int
426
+ declarators:
427
+ - Declarator
428
+ name: "i"
429
+ stmt: ExpressionStatement
430
+ EOS
431
+ assert_raise(ParseError){C::For.parse('} void f() {')}
432
+ assert_raise(ParseError){C::For.parse(';;')}
433
+ assert_raise(ParseError){C::For.parse('int i;')}
434
+ assert_raise(ParseError){C::For.parse(';')}
435
+ assert_raise(ParseError){C::For.parse('')}
436
+ end
437
+
438
+ def test_goto
439
+ check C::Goto, <<EOS
440
+ goto x;
441
+ ----
442
+ Goto
443
+ target: "x"
444
+ EOS
445
+ assert_raise(ParseError){C::Goto.parse('} void f() {')}
446
+ assert_raise(ParseError){C::Goto.parse(';;')}
447
+ assert_raise(ParseError){C::Goto.parse('int i;')}
448
+ assert_raise(ParseError){C::Goto.parse(';')}
449
+ assert_raise(ParseError){C::Goto.parse('')}
450
+ end
451
+
452
+ def test_continue
453
+ check C::Continue, <<EOS
454
+ continue;
455
+ ----
456
+ Continue
457
+ EOS
458
+ assert_raise(ParseError){C::Continue.parse('} void f() {')}
459
+ assert_raise(ParseError){C::Continue.parse(';;')}
460
+ assert_raise(ParseError){C::Continue.parse('int i;')}
461
+ assert_raise(ParseError){C::Continue.parse(';')}
462
+ assert_raise(ParseError){C::Continue.parse('')}
463
+ end
464
+
465
+ def test_break
466
+ check C::Break, <<EOS
467
+ break;
468
+ ----
469
+ Break
470
+ EOS
471
+ assert_raise(ParseError){C::Break.parse('} void f() {')}
472
+ assert_raise(ParseError){C::Break.parse(';;')}
473
+ assert_raise(ParseError){C::Break.parse('int i;')}
474
+ assert_raise(ParseError){C::Break.parse(';')}
475
+ assert_raise(ParseError){C::Break.parse('')}
476
+ end
477
+
478
+ def test_return
479
+ check C::Return, <<EOS
480
+ return;
481
+ ----
482
+ Return
483
+ EOS
484
+ check C::Return, <<EOS
485
+ return 10;
486
+ ----
487
+ Return
488
+ expr: IntLiteral
489
+ val: 10
490
+ EOS
491
+ assert_raise(ParseError){C::Return.parse('} void f() {')}
492
+ assert_raise(ParseError){C::Return.parse(';;')}
493
+ assert_raise(ParseError){C::Return.parse('int i;')}
494
+ assert_raise(ParseError){C::Return.parse(';')}
495
+ assert_raise(ParseError){C::Return.parse('')}
496
+ end
497
+
498
+ def test_expression_statement
499
+ check C::ExpressionStatement, <<EOS
500
+ ;
501
+ ----
502
+ ExpressionStatement
503
+ EOS
504
+ check C::ExpressionStatement, <<EOS
505
+ 10;
506
+ ----
507
+ ExpressionStatement
508
+ expr: IntLiteral
509
+ val: 10
510
+ EOS
511
+ assert_raise(ParseError){C::ExpressionStatement.parse('} void f() {')}
512
+ assert_raise(ParseError){C::ExpressionStatement.parse(';;')}
513
+ assert_raise(ParseError){C::ExpressionStatement.parse('int i;')}
514
+ assert_raise(ParseError){C::ExpressionStatement.parse('return;')}
515
+ assert_raise(ParseError){C::ExpressionStatement.parse('')}
516
+ end
517
+
518
+ def test_statement
519
+ check C::Statement, <<EOS
520
+ {}
521
+ ----
522
+ Block
523
+ EOS
524
+ check C::Statement, <<EOS
525
+ if (1) 10; else 20;
526
+ ----
527
+ If
528
+ cond: IntLiteral
529
+ val: 1
530
+ then: ExpressionStatement
531
+ expr: IntLiteral
532
+ val: 10
533
+ else: ExpressionStatement
534
+ expr: IntLiteral
535
+ val: 20
536
+ EOS
537
+ check C::Statement, <<EOS
538
+ switch (x);
539
+ ----
540
+ Switch
541
+ cond: Variable
542
+ name: "x"
543
+ stmt: ExpressionStatement
544
+ EOS
545
+ check C::Statement, <<EOS
546
+ while (1) ;
547
+ ----
548
+ While
549
+ cond: IntLiteral
550
+ val: 1
551
+ stmt: ExpressionStatement
552
+ EOS
553
+ check C::Statement, <<EOS
554
+ do ; while (1);
555
+ ----
556
+ While (do)
557
+ cond: IntLiteral
558
+ val: 1
559
+ stmt: ExpressionStatement
560
+ EOS
561
+ check C::Statement, <<EOS
562
+ for (;;) ;
563
+ ----
564
+ For
565
+ stmt: ExpressionStatement
566
+ EOS
567
+ check C::Statement, <<EOS
568
+ goto x;
569
+ ----
570
+ Goto
571
+ target: "x"
572
+ EOS
573
+ check C::Statement, <<EOS
574
+ continue;
575
+ ----
576
+ Continue
577
+ EOS
578
+ check C::Statement, <<EOS
579
+ break;
580
+ ----
581
+ Break
582
+ EOS
583
+ check C::Statement, <<EOS
584
+ return;
585
+ ----
586
+ Return
587
+ EOS
588
+ check C::Statement, <<EOS
589
+ ;
590
+ ----
591
+ ExpressionStatement
592
+ EOS
593
+ assert_raise(ParseError){C::Statement.parse('} void f() {')}
594
+ assert_raise(ParseError){C::Statement.parse(';;')}
595
+ assert_raise(ParseError){C::Statement.parse('int i;')}
596
+ assert_raise(ParseError){C::Statement.parse('')}
597
+ end
598
+
599
+ def test_plain_label
600
+ check C::PlainLabel, <<EOS
601
+ x:
602
+ ----
603
+ PlainLabel
604
+ name: "x"
605
+ EOS
606
+ assert_raise(ParseError){C::PlainLabel.parse('} void f() {')}
607
+ assert_raise(ParseError){C::PlainLabel.parse(';')}
608
+ assert_raise(ParseError){C::PlainLabel.parse('')}
609
+ assert_raise(ParseError){C::PlainLabel.parse('x')}
610
+ assert_raise(ParseError){C::PlainLabel.parse('case 1:')}
611
+ assert_raise(ParseError){C::PlainLabel.parse('default:')}
612
+ end
613
+
614
+ def test_default
615
+ check C::Default, <<EOS
616
+ default:
617
+ ----
618
+ Default
619
+ EOS
620
+ assert_raise(ParseError){C::Default.parse('} void f() {')}
621
+ assert_raise(ParseError){C::Default.parse(';')}
622
+ assert_raise(ParseError){C::Default.parse('')}
623
+ assert_raise(ParseError){C::Default.parse('x')}
624
+ assert_raise(ParseError){C::Default.parse('x:')}
625
+ assert_raise(ParseError){C::Default.parse('case 1:')}
626
+ end
627
+
628
+ def test_case
629
+ check C::Case, <<EOS
630
+ case 1:
631
+ ----
632
+ Case
633
+ expr: IntLiteral
634
+ val: 1
635
+ EOS
636
+ assert_raise(ParseError){C::Case.parse('} void f() {')}
637
+ assert_raise(ParseError){C::Case.parse(';')}
638
+ assert_raise(ParseError){C::Case.parse('')}
639
+ assert_raise(ParseError){C::Case.parse('x:')}
640
+ assert_raise(ParseError){C::Case.parse('default:')}
641
+ end
642
+
643
+ def test_label
644
+ check C::Label, <<EOS
645
+ x:
646
+ ----
647
+ PlainLabel
648
+ name: "x"
649
+ EOS
650
+ check C::Label, <<EOS
651
+ default:
652
+ ----
653
+ Default
654
+ EOS
655
+ check C::Label, <<EOS
656
+ case 1:
657
+ ----
658
+ Case
659
+ expr: IntLiteral
660
+ val: 1
661
+ EOS
662
+ assert_raise(ParseError){C::Label.parse('} void f() {')}
663
+ assert_raise(ParseError){C::Label.parse(';')}
664
+ assert_raise(ParseError){C::Label.parse('')}
665
+ assert_raise(ParseError){C::Label.parse('x')}
666
+ end
667
+
668
+ def test_comma
669
+ check C::Comma, <<EOS
670
+ ++i, ++j
671
+ ----
672
+ Comma
673
+ exprs:
674
+ - PreInc
675
+ expr: Variable
676
+ name: "i"
677
+ - PreInc
678
+ expr: Variable
679
+ name: "j"
680
+ EOS
681
+ check C::Comma, <<EOS
682
+ (++i, ++j)
683
+ ----
684
+ Comma
685
+ exprs:
686
+ - PreInc
687
+ expr: Variable
688
+ name: "i"
689
+ - PreInc
690
+ expr: Variable
691
+ name: "j"
692
+ EOS
693
+ assert_raise(ParseError){C::Comma.parse('} void f() {')}
694
+ assert_raise(ParseError){C::Comma.parse(';')}
695
+ assert_raise(ParseError){C::Comma.parse('int i')}
696
+ assert_raise(ParseError){C::Comma.parse('int')}
697
+ assert_raise(ParseError){C::Comma.parse('if (0)')}
698
+ assert_raise(ParseError){C::Comma.parse('switch (0)')}
699
+ assert_raise(ParseError){C::Comma.parse('for (;;)')}
700
+ assert_raise(ParseError){C::Comma.parse('goto')}
701
+ assert_raise(ParseError){C::Comma.parse('return')}
702
+ end
703
+
704
+ def test_conditional
705
+ check C::Conditional, <<EOS
706
+ 1 ? 10 : 20
707
+ ----
708
+ Conditional
709
+ cond: IntLiteral
710
+ val: 1
711
+ then: IntLiteral
712
+ val: 10
713
+ else: IntLiteral
714
+ val: 20
715
+ EOS
716
+ assert_raise(ParseError){C::Conditional.parse('} void f() {')}
717
+ assert_raise(ParseError){C::Conditional.parse(';')}
718
+ assert_raise(ParseError){C::Conditional.parse('int i')}
719
+ assert_raise(ParseError){C::Conditional.parse('int')}
720
+ assert_raise(ParseError){C::Conditional.parse('if (0)')}
721
+ assert_raise(ParseError){C::Conditional.parse('switch (0)')}
722
+ assert_raise(ParseError){C::Conditional.parse('for (;;)')}
723
+ assert_raise(ParseError){C::Conditional.parse('goto')}
724
+ assert_raise(ParseError){C::Conditional.parse('return')}
725
+ end
726
+
727
+ def test_cast
728
+ check C::Cast, <<EOS
729
+ (int)10.0
730
+ ----
731
+ Cast
732
+ type: Int
733
+ expr: FloatLiteral
734
+ val: 10.0
735
+ EOS
736
+ assert_raise(ParseError){C::Cast.parse('} void f() {')}
737
+ assert_raise(ParseError){C::Cast.parse(';')}
738
+ assert_raise(ParseError){C::Cast.parse('int i')}
739
+ assert_raise(ParseError){C::Cast.parse('int')}
740
+ assert_raise(ParseError){C::Cast.parse('if (0)')}
741
+ assert_raise(ParseError){C::Cast.parse('switch (0)')}
742
+ assert_raise(ParseError){C::Cast.parse('for (;;)')}
743
+ assert_raise(ParseError){C::Cast.parse('goto')}
744
+ assert_raise(ParseError){C::Cast.parse('return')}
745
+ end
746
+
747
+ def test_address
748
+ check C::Address, <<EOS
749
+ &x
750
+ ----
751
+ Address
752
+ expr: Variable
753
+ name: "x"
754
+ EOS
755
+ assert_raise(ParseError){C::Address.parse('} void f() {')}
756
+ assert_raise(ParseError){C::Address.parse(';')}
757
+ assert_raise(ParseError){C::Address.parse('int i')}
758
+ assert_raise(ParseError){C::Address.parse('int')}
759
+ assert_raise(ParseError){C::Address.parse('if (0)')}
760
+ assert_raise(ParseError){C::Address.parse('switch (0)')}
761
+ assert_raise(ParseError){C::Address.parse('for (;;)')}
762
+ assert_raise(ParseError){C::Address.parse('goto')}
763
+ assert_raise(ParseError){C::Address.parse('return')}
764
+ end
765
+
766
+ def test_dereference
767
+ check C::Dereference, <<EOS
768
+ *x
769
+ ----
770
+ Dereference
771
+ expr: Variable
772
+ name: "x"
773
+ EOS
774
+ assert_raise(ParseError){C::Dereference.parse('} void f() {')}
775
+ assert_raise(ParseError){C::Dereference.parse(';')}
776
+ assert_raise(ParseError){C::Dereference.parse('int i')}
777
+ assert_raise(ParseError){C::Dereference.parse('int')}
778
+ assert_raise(ParseError){C::Dereference.parse('if (0)')}
779
+ assert_raise(ParseError){C::Dereference.parse('switch (0)')}
780
+ assert_raise(ParseError){C::Dereference.parse('for (;;)')}
781
+ assert_raise(ParseError){C::Dereference.parse('goto')}
782
+ assert_raise(ParseError){C::Dereference.parse('return')}
783
+ end
784
+
785
+ def test_sizeof
786
+ check C::Sizeof, <<EOS
787
+ sizeof i
788
+ ----
789
+ Sizeof
790
+ expr: Variable
791
+ name: "i"
792
+ EOS
793
+ check C::Sizeof, <<EOS
794
+ sizeof(int)
795
+ ----
796
+ Sizeof
797
+ expr: Int
798
+ EOS
799
+ assert_raise(ParseError){C::Sizeof.parse('} void f() {')}
800
+ assert_raise(ParseError){C::Sizeof.parse(';')}
801
+ assert_raise(ParseError){C::Sizeof.parse('int i')}
802
+ assert_raise(ParseError){C::Sizeof.parse('int')}
803
+ assert_raise(ParseError){C::Sizeof.parse('if (0)')}
804
+ assert_raise(ParseError){C::Sizeof.parse('switch (0)')}
805
+ assert_raise(ParseError){C::Sizeof.parse('for (;;)')}
806
+ assert_raise(ParseError){C::Sizeof.parse('goto')}
807
+ assert_raise(ParseError){C::Sizeof.parse('return')}
808
+ end
809
+
810
+ def test_index
811
+ check C::Index, <<EOS
812
+ x[10][20]
813
+ ----
814
+ Index
815
+ expr: Index
816
+ expr: Variable
817
+ name: "x"
818
+ index: IntLiteral
819
+ val: 10
820
+ index: IntLiteral
821
+ val: 20
822
+ EOS
823
+ assert_raise(ParseError){C::Index.parse('} void f() {')}
824
+ assert_raise(ParseError){C::Index.parse(';')}
825
+ assert_raise(ParseError){C::Index.parse('int i')}
826
+ assert_raise(ParseError){C::Index.parse('int')}
827
+ assert_raise(ParseError){C::Index.parse('if (0)')}
828
+ assert_raise(ParseError){C::Index.parse('switch (0)')}
829
+ assert_raise(ParseError){C::Index.parse('for (;;)')}
830
+ assert_raise(ParseError){C::Index.parse('goto')}
831
+ assert_raise(ParseError){C::Index.parse('return')}
832
+ end
833
+
834
+ def test_call
835
+ check C::Call, <<EOS
836
+ x(10, 20)()
837
+ ----
838
+ Call
839
+ expr: Call
840
+ expr: Variable
841
+ name: "x"
842
+ args:
843
+ - IntLiteral
844
+ val: 10
845
+ - IntLiteral
846
+ val: 20
847
+ EOS
848
+ assert_raise(ParseError){C::Call.parse('} void f() {')}
849
+ assert_raise(ParseError){C::Call.parse(';')}
850
+ assert_raise(ParseError){C::Call.parse('int i')}
851
+ assert_raise(ParseError){C::Call.parse('int')}
852
+ assert_raise(ParseError){C::Call.parse('if (0)')}
853
+ assert_raise(ParseError){C::Call.parse('switch (0)')}
854
+ assert_raise(ParseError){C::Call.parse('for (;;)')}
855
+ assert_raise(ParseError){C::Call.parse('goto')}
856
+ assert_raise(ParseError){C::Call.parse('return')}
857
+ end
858
+
859
+ def test_arrow
860
+ check C::Arrow, <<EOS
861
+ x->y
862
+ ----
863
+ Arrow
864
+ expr: Variable
865
+ name: "x"
866
+ member: Member
867
+ name: "y"
868
+ EOS
869
+ assert_raise(ParseError){C::Arrow.parse('} void f() {')}
870
+ assert_raise(ParseError){C::Arrow.parse(';')}
871
+ assert_raise(ParseError){C::Arrow.parse('int i')}
872
+ assert_raise(ParseError){C::Arrow.parse('int')}
873
+ assert_raise(ParseError){C::Arrow.parse('if (0)')}
874
+ assert_raise(ParseError){C::Arrow.parse('switch (0)')}
875
+ assert_raise(ParseError){C::Arrow.parse('for (;;)')}
876
+ assert_raise(ParseError){C::Arrow.parse('goto')}
877
+ assert_raise(ParseError){C::Arrow.parse('return')}
878
+ end
879
+
880
+ def test_dot
881
+ check C::Dot, <<EOS
882
+ x.y
883
+ ----
884
+ Dot
885
+ expr: Variable
886
+ name: "x"
887
+ member: Member
888
+ name: "y"
889
+ EOS
890
+ assert_raise(ParseError){C::Dot.parse('} void f() {')}
891
+ assert_raise(ParseError){C::Dot.parse(';')}
892
+ assert_raise(ParseError){C::Dot.parse('int i')}
893
+ assert_raise(ParseError){C::Dot.parse('int')}
894
+ assert_raise(ParseError){C::Dot.parse('if (0)')}
895
+ assert_raise(ParseError){C::Dot.parse('switch (0)')}
896
+ assert_raise(ParseError){C::Dot.parse('for (;;)')}
897
+ assert_raise(ParseError){C::Dot.parse('goto')}
898
+ assert_raise(ParseError){C::Dot.parse('return')}
899
+ end
900
+
901
+ def test_positive
902
+ check C::Positive, <<EOS
903
+ +1
904
+ ----
905
+ Positive
906
+ expr: IntLiteral
907
+ val: 1
908
+ EOS
909
+ assert_raise(ParseError){C::Positive.parse('} void f() {')}
910
+ assert_raise(ParseError){C::Positive.parse(';')}
911
+ assert_raise(ParseError){C::Positive.parse('int i')}
912
+ assert_raise(ParseError){C::Positive.parse('int')}
913
+ assert_raise(ParseError){C::Positive.parse('if (0)')}
914
+ assert_raise(ParseError){C::Positive.parse('switch (0)')}
915
+ assert_raise(ParseError){C::Positive.parse('for (;;)')}
916
+ assert_raise(ParseError){C::Positive.parse('goto')}
917
+ assert_raise(ParseError){C::Positive.parse('return')}
918
+ end
919
+
920
+ def test_negative
921
+ check C::Negative, <<EOS
922
+ -1
923
+ ----
924
+ Negative
925
+ expr: IntLiteral
926
+ val: 1
927
+ EOS
928
+ assert_raise(ParseError){C::Negative.parse('} void f() {')}
929
+ assert_raise(ParseError){C::Negative.parse(';')}
930
+ assert_raise(ParseError){C::Negative.parse('int i')}
931
+ assert_raise(ParseError){C::Negative.parse('int')}
932
+ assert_raise(ParseError){C::Negative.parse('if (0)')}
933
+ assert_raise(ParseError){C::Negative.parse('switch (0)')}
934
+ assert_raise(ParseError){C::Negative.parse('for (;;)')}
935
+ assert_raise(ParseError){C::Negative.parse('goto')}
936
+ assert_raise(ParseError){C::Negative.parse('return')}
937
+ end
938
+
939
+ def test_add
940
+ check C::Add, <<EOS
941
+ 1 + 10
942
+ ----
943
+ Add
944
+ expr1: IntLiteral
945
+ val: 1
946
+ expr2: IntLiteral
947
+ val: 10
948
+ EOS
949
+ assert_raise(ParseError){C::Add.parse('} void f() {')}
950
+ assert_raise(ParseError){C::Add.parse(';')}
951
+ assert_raise(ParseError){C::Add.parse('int i')}
952
+ assert_raise(ParseError){C::Add.parse('int')}
953
+ assert_raise(ParseError){C::Add.parse('if (0)')}
954
+ assert_raise(ParseError){C::Add.parse('switch (0)')}
955
+ assert_raise(ParseError){C::Add.parse('for (;;)')}
956
+ assert_raise(ParseError){C::Add.parse('goto')}
957
+ assert_raise(ParseError){C::Add.parse('return')}
958
+ end
959
+
960
+ def test_subtract
961
+ check C::Subtract, <<EOS
962
+ 1 - 10
963
+ ----
964
+ Subtract
965
+ expr1: IntLiteral
966
+ val: 1
967
+ expr2: IntLiteral
968
+ val: 10
969
+ EOS
970
+ assert_raise(ParseError){C::Subtract.parse('} void f() {')}
971
+ assert_raise(ParseError){C::Subtract.parse(';')}
972
+ assert_raise(ParseError){C::Subtract.parse('int i')}
973
+ assert_raise(ParseError){C::Subtract.parse('int')}
974
+ assert_raise(ParseError){C::Subtract.parse('if (0)')}
975
+ assert_raise(ParseError){C::Subtract.parse('switch (0)')}
976
+ assert_raise(ParseError){C::Subtract.parse('for (;;)')}
977
+ assert_raise(ParseError){C::Subtract.parse('goto')}
978
+ assert_raise(ParseError){C::Subtract.parse('return')}
979
+ end
980
+
981
+ def test_multiply
982
+ check C::Multiply, <<EOS
983
+ 1 * 10
984
+ ----
985
+ Multiply
986
+ expr1: IntLiteral
987
+ val: 1
988
+ expr2: IntLiteral
989
+ val: 10
990
+ EOS
991
+ assert_raise(ParseError){C::Multiply.parse('} void f() {')}
992
+ assert_raise(ParseError){C::Multiply.parse(';')}
993
+ assert_raise(ParseError){C::Multiply.parse('int i')}
994
+ assert_raise(ParseError){C::Multiply.parse('int')}
995
+ assert_raise(ParseError){C::Multiply.parse('if (0)')}
996
+ assert_raise(ParseError){C::Multiply.parse('switch (0)')}
997
+ assert_raise(ParseError){C::Multiply.parse('for (;;)')}
998
+ assert_raise(ParseError){C::Multiply.parse('goto')}
999
+ assert_raise(ParseError){C::Multiply.parse('return')}
1000
+ end
1001
+
1002
+ def test_divide
1003
+ check C::Divide, <<EOS
1004
+ 1 / 10
1005
+ ----
1006
+ Divide
1007
+ expr1: IntLiteral
1008
+ val: 1
1009
+ expr2: IntLiteral
1010
+ val: 10
1011
+ EOS
1012
+ assert_raise(ParseError){C::Divide.parse('} void f() {')}
1013
+ assert_raise(ParseError){C::Divide.parse(';')}
1014
+ assert_raise(ParseError){C::Divide.parse('int i')}
1015
+ assert_raise(ParseError){C::Divide.parse('int')}
1016
+ assert_raise(ParseError){C::Divide.parse('if (0)')}
1017
+ assert_raise(ParseError){C::Divide.parse('switch (0)')}
1018
+ assert_raise(ParseError){C::Divide.parse('for (;;)')}
1019
+ assert_raise(ParseError){C::Divide.parse('goto')}
1020
+ assert_raise(ParseError){C::Divide.parse('return')}
1021
+ end
1022
+
1023
+ def test_mod
1024
+ check C::Mod, <<EOS
1025
+ 1 % 10
1026
+ ----
1027
+ Mod
1028
+ expr1: IntLiteral
1029
+ val: 1
1030
+ expr2: IntLiteral
1031
+ val: 10
1032
+ EOS
1033
+ assert_raise(ParseError){C::Mod.parse('} void f() {')}
1034
+ assert_raise(ParseError){C::Mod.parse(';')}
1035
+ assert_raise(ParseError){C::Mod.parse('int i')}
1036
+ assert_raise(ParseError){C::Mod.parse('int')}
1037
+ assert_raise(ParseError){C::Mod.parse('if (0)')}
1038
+ assert_raise(ParseError){C::Mod.parse('switch (0)')}
1039
+ assert_raise(ParseError){C::Mod.parse('for (;;)')}
1040
+ assert_raise(ParseError){C::Mod.parse('goto')}
1041
+ assert_raise(ParseError){C::Mod.parse('return')}
1042
+ end
1043
+
1044
+ def test_pre_inc
1045
+ check C::PreInc, <<EOS
1046
+ ++i
1047
+ ----
1048
+ PreInc
1049
+ expr: Variable
1050
+ name: "i"
1051
+ EOS
1052
+ assert_raise(ParseError){C::PreInc.parse('} void f() {')}
1053
+ assert_raise(ParseError){C::PreInc.parse(';')}
1054
+ assert_raise(ParseError){C::PreInc.parse('int i')}
1055
+ assert_raise(ParseError){C::PreInc.parse('int')}
1056
+ assert_raise(ParseError){C::PreInc.parse('if (0)')}
1057
+ assert_raise(ParseError){C::PreInc.parse('switch (0)')}
1058
+ assert_raise(ParseError){C::PreInc.parse('for (;;)')}
1059
+ assert_raise(ParseError){C::PreInc.parse('goto')}
1060
+ assert_raise(ParseError){C::PreInc.parse('return')}
1061
+ end
1062
+
1063
+ def test_post_inc
1064
+ check C::PostInc, <<EOS
1065
+ i++
1066
+ ----
1067
+ PostInc
1068
+ expr: Variable
1069
+ name: "i"
1070
+ EOS
1071
+ assert_raise(ParseError){C::PostInc.parse('} void f() {')}
1072
+ assert_raise(ParseError){C::PostInc.parse(';')}
1073
+ assert_raise(ParseError){C::PostInc.parse('int i')}
1074
+ assert_raise(ParseError){C::PostInc.parse('int')}
1075
+ assert_raise(ParseError){C::PostInc.parse('if (0)')}
1076
+ assert_raise(ParseError){C::PostInc.parse('switch (0)')}
1077
+ assert_raise(ParseError){C::PostInc.parse('for (;;)')}
1078
+ assert_raise(ParseError){C::PostInc.parse('goto')}
1079
+ assert_raise(ParseError){C::PostInc.parse('return')}
1080
+ end
1081
+
1082
+ def test_pre_dec
1083
+ check C::PreDec, <<EOS
1084
+ --i
1085
+ ----
1086
+ PreDec
1087
+ expr: Variable
1088
+ name: "i"
1089
+ EOS
1090
+ assert_raise(ParseError){C::PreDec.parse('} void f() {')}
1091
+ assert_raise(ParseError){C::PreDec.parse(';')}
1092
+ assert_raise(ParseError){C::PreDec.parse('int i')}
1093
+ assert_raise(ParseError){C::PreDec.parse('int')}
1094
+ assert_raise(ParseError){C::PreDec.parse('if (0)')}
1095
+ assert_raise(ParseError){C::PreDec.parse('switch (0)')}
1096
+ assert_raise(ParseError){C::PreDec.parse('for (;;)')}
1097
+ assert_raise(ParseError){C::PreDec.parse('goto')}
1098
+ assert_raise(ParseError){C::PreDec.parse('return')}
1099
+ end
1100
+
1101
+ def test_post_dec
1102
+ check C::PostDec, <<EOS
1103
+ i--
1104
+ ----
1105
+ PostDec
1106
+ expr: Variable
1107
+ name: "i"
1108
+ EOS
1109
+ assert_raise(ParseError){C::PostDec.parse('} void f() {')}
1110
+ assert_raise(ParseError){C::PostDec.parse(';')}
1111
+ assert_raise(ParseError){C::PostDec.parse('int i')}
1112
+ assert_raise(ParseError){C::PostDec.parse('int')}
1113
+ assert_raise(ParseError){C::PostDec.parse('if (0)')}
1114
+ assert_raise(ParseError){C::PostDec.parse('switch (0)')}
1115
+ assert_raise(ParseError){C::PostDec.parse('for (;;)')}
1116
+ assert_raise(ParseError){C::PostDec.parse('goto')}
1117
+ assert_raise(ParseError){C::PostDec.parse('return')}
1118
+ end
1119
+
1120
+ def test_equal
1121
+ check C::Equal, <<EOS
1122
+ 1 == 10
1123
+ ----
1124
+ Equal
1125
+ expr1: IntLiteral
1126
+ val: 1
1127
+ expr2: IntLiteral
1128
+ val: 10
1129
+ EOS
1130
+ assert_raise(ParseError){C::Equal.parse('} void f() {')}
1131
+ assert_raise(ParseError){C::Equal.parse(';')}
1132
+ assert_raise(ParseError){C::Equal.parse('int i')}
1133
+ assert_raise(ParseError){C::Equal.parse('int')}
1134
+ assert_raise(ParseError){C::Equal.parse('if (0)')}
1135
+ assert_raise(ParseError){C::Equal.parse('switch (0)')}
1136
+ assert_raise(ParseError){C::Equal.parse('for (;;)')}
1137
+ assert_raise(ParseError){C::Equal.parse('goto')}
1138
+ assert_raise(ParseError){C::Equal.parse('return')}
1139
+ end
1140
+
1141
+ def test_not_equal
1142
+ check C::NotEqual, <<EOS
1143
+ 1 != 10
1144
+ ----
1145
+ NotEqual
1146
+ expr1: IntLiteral
1147
+ val: 1
1148
+ expr2: IntLiteral
1149
+ val: 10
1150
+ EOS
1151
+ assert_raise(ParseError){C::NotEqual.parse('} void f() {')}
1152
+ assert_raise(ParseError){C::NotEqual.parse(';')}
1153
+ assert_raise(ParseError){C::NotEqual.parse('int i')}
1154
+ assert_raise(ParseError){C::NotEqual.parse('int')}
1155
+ assert_raise(ParseError){C::NotEqual.parse('if (0)')}
1156
+ assert_raise(ParseError){C::NotEqual.parse('switch (0)')}
1157
+ assert_raise(ParseError){C::NotEqual.parse('for (;;)')}
1158
+ assert_raise(ParseError){C::NotEqual.parse('goto')}
1159
+ assert_raise(ParseError){C::NotEqual.parse('return')}
1160
+ end
1161
+
1162
+ def test_less
1163
+ check C::Less, <<EOS
1164
+ 1 < 10
1165
+ ----
1166
+ Less
1167
+ expr1: IntLiteral
1168
+ val: 1
1169
+ expr2: IntLiteral
1170
+ val: 10
1171
+ EOS
1172
+ assert_raise(ParseError){C::Less.parse('} void f() {')}
1173
+ assert_raise(ParseError){C::Less.parse(';')}
1174
+ assert_raise(ParseError){C::Less.parse('int i')}
1175
+ assert_raise(ParseError){C::Less.parse('int')}
1176
+ assert_raise(ParseError){C::Less.parse('if (0)')}
1177
+ assert_raise(ParseError){C::Less.parse('switch (0)')}
1178
+ assert_raise(ParseError){C::Less.parse('for (;;)')}
1179
+ assert_raise(ParseError){C::Less.parse('goto')}
1180
+ assert_raise(ParseError){C::Less.parse('return')}
1181
+ end
1182
+
1183
+ def test_more
1184
+ check C::More, <<EOS
1185
+ 1 > 10
1186
+ ----
1187
+ More
1188
+ expr1: IntLiteral
1189
+ val: 1
1190
+ expr2: IntLiteral
1191
+ val: 10
1192
+ EOS
1193
+ assert_raise(ParseError){C::More.parse('} void f() {')}
1194
+ assert_raise(ParseError){C::More.parse(';')}
1195
+ assert_raise(ParseError){C::More.parse('int i')}
1196
+ assert_raise(ParseError){C::More.parse('int')}
1197
+ assert_raise(ParseError){C::More.parse('if (0)')}
1198
+ assert_raise(ParseError){C::More.parse('switch (0)')}
1199
+ assert_raise(ParseError){C::More.parse('for (;;)')}
1200
+ assert_raise(ParseError){C::More.parse('goto')}
1201
+ assert_raise(ParseError){C::More.parse('return')}
1202
+ end
1203
+
1204
+ def test_less_or_equal
1205
+ check C::LessOrEqual, <<EOS
1206
+ 1 <= 10
1207
+ ----
1208
+ LessOrEqual
1209
+ expr1: IntLiteral
1210
+ val: 1
1211
+ expr2: IntLiteral
1212
+ val: 10
1213
+ EOS
1214
+ assert_raise(ParseError){C::LessOrEqual.parse('} void f() {')}
1215
+ assert_raise(ParseError){C::LessOrEqual.parse(';')}
1216
+ assert_raise(ParseError){C::LessOrEqual.parse('int i')}
1217
+ assert_raise(ParseError){C::LessOrEqual.parse('int')}
1218
+ assert_raise(ParseError){C::LessOrEqual.parse('if (0)')}
1219
+ assert_raise(ParseError){C::LessOrEqual.parse('switch (0)')}
1220
+ assert_raise(ParseError){C::LessOrEqual.parse('for (;;)')}
1221
+ assert_raise(ParseError){C::LessOrEqual.parse('goto')}
1222
+ assert_raise(ParseError){C::LessOrEqual.parse('return')}
1223
+ end
1224
+
1225
+ def test_more_or_equal
1226
+ check C::MoreOrEqual, <<EOS
1227
+ 1 >= 10
1228
+ ----
1229
+ MoreOrEqual
1230
+ expr1: IntLiteral
1231
+ val: 1
1232
+ expr2: IntLiteral
1233
+ val: 10
1234
+ EOS
1235
+ assert_raise(ParseError){C::MoreOrEqual.parse('} void f() {')}
1236
+ assert_raise(ParseError){C::MoreOrEqual.parse(';')}
1237
+ assert_raise(ParseError){C::MoreOrEqual.parse('int i')}
1238
+ assert_raise(ParseError){C::MoreOrEqual.parse('int')}
1239
+ assert_raise(ParseError){C::MoreOrEqual.parse('if (0)')}
1240
+ assert_raise(ParseError){C::MoreOrEqual.parse('switch (0)')}
1241
+ assert_raise(ParseError){C::MoreOrEqual.parse('for (;;)')}
1242
+ assert_raise(ParseError){C::MoreOrEqual.parse('goto')}
1243
+ assert_raise(ParseError){C::MoreOrEqual.parse('return')}
1244
+ end
1245
+
1246
+ def test_bit_and
1247
+ check C::BitAnd, <<EOS
1248
+ 1 & 10
1249
+ ----
1250
+ BitAnd
1251
+ expr1: IntLiteral
1252
+ val: 1
1253
+ expr2: IntLiteral
1254
+ val: 10
1255
+ EOS
1256
+ assert_raise(ParseError){C::BitAnd.parse('} void f() {')}
1257
+ assert_raise(ParseError){C::BitAnd.parse(';')}
1258
+ assert_raise(ParseError){C::BitAnd.parse('int i')}
1259
+ assert_raise(ParseError){C::BitAnd.parse('int')}
1260
+ assert_raise(ParseError){C::BitAnd.parse('if (0)')}
1261
+ assert_raise(ParseError){C::BitAnd.parse('switch (0)')}
1262
+ assert_raise(ParseError){C::BitAnd.parse('for (;;)')}
1263
+ assert_raise(ParseError){C::BitAnd.parse('goto')}
1264
+ assert_raise(ParseError){C::BitAnd.parse('return')}
1265
+ end
1266
+
1267
+ def test_bit_or
1268
+ check C::BitOr, <<EOS
1269
+ 1 | 10
1270
+ ----
1271
+ BitOr
1272
+ expr1: IntLiteral
1273
+ val: 1
1274
+ expr2: IntLiteral
1275
+ val: 10
1276
+ EOS
1277
+ assert_raise(ParseError){C::BitOr.parse('} void f() {')}
1278
+ assert_raise(ParseError){C::BitOr.parse(';')}
1279
+ assert_raise(ParseError){C::BitOr.parse('int i')}
1280
+ assert_raise(ParseError){C::BitOr.parse('int')}
1281
+ assert_raise(ParseError){C::BitOr.parse('if (0)')}
1282
+ assert_raise(ParseError){C::BitOr.parse('switch (0)')}
1283
+ assert_raise(ParseError){C::BitOr.parse('for (;;)')}
1284
+ assert_raise(ParseError){C::BitOr.parse('goto')}
1285
+ assert_raise(ParseError){C::BitOr.parse('return')}
1286
+ end
1287
+
1288
+ def test_bit_xor
1289
+ check C::BitXor, <<EOS
1290
+ 1 ^ 10
1291
+ ----
1292
+ BitXor
1293
+ expr1: IntLiteral
1294
+ val: 1
1295
+ expr2: IntLiteral
1296
+ val: 10
1297
+ EOS
1298
+ assert_raise(ParseError){C::BitXor.parse('} void f() {')}
1299
+ assert_raise(ParseError){C::BitXor.parse(';')}
1300
+ assert_raise(ParseError){C::BitXor.parse('int i')}
1301
+ assert_raise(ParseError){C::BitXor.parse('int')}
1302
+ assert_raise(ParseError){C::BitXor.parse('if (0)')}
1303
+ assert_raise(ParseError){C::BitXor.parse('switch (0)')}
1304
+ assert_raise(ParseError){C::BitXor.parse('for (;;)')}
1305
+ assert_raise(ParseError){C::BitXor.parse('goto')}
1306
+ assert_raise(ParseError){C::BitXor.parse('return')}
1307
+ end
1308
+
1309
+ def test_bit_not
1310
+ check C::BitNot, <<EOS
1311
+ ~i
1312
+ ----
1313
+ BitNot
1314
+ expr: Variable
1315
+ name: "i"
1316
+ EOS
1317
+ assert_raise(ParseError){C::BitNot.parse('} void f() {')}
1318
+ assert_raise(ParseError){C::BitNot.parse(';')}
1319
+ assert_raise(ParseError){C::BitNot.parse('int i')}
1320
+ assert_raise(ParseError){C::BitNot.parse('int')}
1321
+ assert_raise(ParseError){C::BitNot.parse('if (0)')}
1322
+ assert_raise(ParseError){C::BitNot.parse('switch (0)')}
1323
+ assert_raise(ParseError){C::BitNot.parse('for (;;)')}
1324
+ assert_raise(ParseError){C::BitNot.parse('goto')}
1325
+ assert_raise(ParseError){C::BitNot.parse('return')}
1326
+ end
1327
+
1328
+ def test_shift_left
1329
+ check C::ShiftLeft, <<EOS
1330
+ 1 << 10
1331
+ ----
1332
+ ShiftLeft
1333
+ expr1: IntLiteral
1334
+ val: 1
1335
+ expr2: IntLiteral
1336
+ val: 10
1337
+ EOS
1338
+ assert_raise(ParseError){C::ShiftLeft.parse('} void f() {')}
1339
+ assert_raise(ParseError){C::ShiftLeft.parse(';')}
1340
+ assert_raise(ParseError){C::ShiftLeft.parse('int i')}
1341
+ assert_raise(ParseError){C::ShiftLeft.parse('int')}
1342
+ assert_raise(ParseError){C::ShiftLeft.parse('if (0)')}
1343
+ assert_raise(ParseError){C::ShiftLeft.parse('switch (0)')}
1344
+ assert_raise(ParseError){C::ShiftLeft.parse('for (;;)')}
1345
+ assert_raise(ParseError){C::ShiftLeft.parse('goto')}
1346
+ assert_raise(ParseError){C::ShiftLeft.parse('return')}
1347
+ end
1348
+
1349
+ def test_shift_right
1350
+ check C::ShiftRight, <<EOS
1351
+ 1 >> 10
1352
+ ----
1353
+ ShiftRight
1354
+ expr1: IntLiteral
1355
+ val: 1
1356
+ expr2: IntLiteral
1357
+ val: 10
1358
+ EOS
1359
+ assert_raise(ParseError){C::ShiftRight.parse('} void f() {')}
1360
+ assert_raise(ParseError){C::ShiftRight.parse(';')}
1361
+ assert_raise(ParseError){C::ShiftRight.parse('int i')}
1362
+ assert_raise(ParseError){C::ShiftRight.parse('int')}
1363
+ assert_raise(ParseError){C::ShiftRight.parse('if (0)')}
1364
+ assert_raise(ParseError){C::ShiftRight.parse('switch (0)')}
1365
+ assert_raise(ParseError){C::ShiftRight.parse('for (;;)')}
1366
+ assert_raise(ParseError){C::ShiftRight.parse('goto')}
1367
+ assert_raise(ParseError){C::ShiftRight.parse('return')}
1368
+ end
1369
+
1370
+ def test_and
1371
+ check C::And, <<EOS
1372
+ 1 && 10
1373
+ ----
1374
+ And
1375
+ expr1: IntLiteral
1376
+ val: 1
1377
+ expr2: IntLiteral
1378
+ val: 10
1379
+ EOS
1380
+ assert_raise(ParseError){C::And.parse('} void f() {')}
1381
+ assert_raise(ParseError){C::And.parse(';')}
1382
+ assert_raise(ParseError){C::And.parse('int i')}
1383
+ assert_raise(ParseError){C::And.parse('int')}
1384
+ assert_raise(ParseError){C::And.parse('if (0)')}
1385
+ assert_raise(ParseError){C::And.parse('switch (0)')}
1386
+ assert_raise(ParseError){C::And.parse('for (;;)')}
1387
+ assert_raise(ParseError){C::And.parse('goto')}
1388
+ assert_raise(ParseError){C::And.parse('return')}
1389
+ end
1390
+
1391
+ def test_or
1392
+ check C::Or, <<EOS
1393
+ 1 || 10
1394
+ ----
1395
+ Or
1396
+ expr1: IntLiteral
1397
+ val: 1
1398
+ expr2: IntLiteral
1399
+ val: 10
1400
+ EOS
1401
+ assert_raise(ParseError){C::Or.parse('} void f() {')}
1402
+ assert_raise(ParseError){C::Or.parse(';')}
1403
+ assert_raise(ParseError){C::Or.parse('int i')}
1404
+ assert_raise(ParseError){C::Or.parse('int')}
1405
+ assert_raise(ParseError){C::Or.parse('if (0)')}
1406
+ assert_raise(ParseError){C::Or.parse('switch (0)')}
1407
+ assert_raise(ParseError){C::Or.parse('for (;;)')}
1408
+ assert_raise(ParseError){C::Or.parse('goto')}
1409
+ assert_raise(ParseError){C::Or.parse('return')}
1410
+ end
1411
+
1412
+ def test_not
1413
+ check C::Not, <<EOS
1414
+ !i
1415
+ ----
1416
+ Not
1417
+ expr: Variable
1418
+ name: "i"
1419
+ EOS
1420
+ assert_raise(ParseError){C::Not.parse('} void f() {')}
1421
+ assert_raise(ParseError){C::Not.parse(';')}
1422
+ assert_raise(ParseError){C::Not.parse('int i')}
1423
+ assert_raise(ParseError){C::Not.parse('int')}
1424
+ assert_raise(ParseError){C::Not.parse('if (0)')}
1425
+ assert_raise(ParseError){C::Not.parse('switch (0)')}
1426
+ assert_raise(ParseError){C::Not.parse('for (;;)')}
1427
+ assert_raise(ParseError){C::Not.parse('goto')}
1428
+ assert_raise(ParseError){C::Not.parse('return')}
1429
+ end
1430
+
1431
+ def test_assign
1432
+ check C::Assign, <<EOS
1433
+ x = 10
1434
+ ----
1435
+ Assign
1436
+ lval: Variable
1437
+ name: "x"
1438
+ rval: IntLiteral
1439
+ val: 10
1440
+ EOS
1441
+ assert_raise(ParseError){C::Assign.parse('} void f() {')}
1442
+ assert_raise(ParseError){C::Assign.parse(';')}
1443
+ assert_raise(ParseError){C::Assign.parse('int i')}
1444
+ assert_raise(ParseError){C::Assign.parse('int')}
1445
+ assert_raise(ParseError){C::Assign.parse('if (0)')}
1446
+ assert_raise(ParseError){C::Assign.parse('switch (0)')}
1447
+ assert_raise(ParseError){C::Assign.parse('for (;;)')}
1448
+ assert_raise(ParseError){C::Assign.parse('goto')}
1449
+ assert_raise(ParseError){C::Assign.parse('return')}
1450
+ end
1451
+
1452
+ def test_multiply_assign
1453
+ check C::MultiplyAssign, <<EOS
1454
+ x *= 10
1455
+ ----
1456
+ MultiplyAssign
1457
+ lval: Variable
1458
+ name: "x"
1459
+ rval: IntLiteral
1460
+ val: 10
1461
+ EOS
1462
+ assert_raise(ParseError){C::MultiplyAssign.parse('} void f() {')}
1463
+ assert_raise(ParseError){C::MultiplyAssign.parse(';')}
1464
+ assert_raise(ParseError){C::MultiplyAssign.parse('int i')}
1465
+ assert_raise(ParseError){C::MultiplyAssign.parse('int')}
1466
+ assert_raise(ParseError){C::MultiplyAssign.parse('if (0)')}
1467
+ assert_raise(ParseError){C::MultiplyAssign.parse('switch (0)')}
1468
+ assert_raise(ParseError){C::MultiplyAssign.parse('for (;;)')}
1469
+ assert_raise(ParseError){C::MultiplyAssign.parse('goto')}
1470
+ assert_raise(ParseError){C::MultiplyAssign.parse('return')}
1471
+ end
1472
+
1473
+ def test_divide_assign
1474
+ check C::DivideAssign, <<EOS
1475
+ x /= 10
1476
+ ----
1477
+ DivideAssign
1478
+ lval: Variable
1479
+ name: "x"
1480
+ rval: IntLiteral
1481
+ val: 10
1482
+ EOS
1483
+ assert_raise(ParseError){C::DivideAssign.parse('} void f() {')}
1484
+ assert_raise(ParseError){C::DivideAssign.parse(';')}
1485
+ assert_raise(ParseError){C::DivideAssign.parse('int i')}
1486
+ assert_raise(ParseError){C::DivideAssign.parse('int')}
1487
+ assert_raise(ParseError){C::DivideAssign.parse('if (0)')}
1488
+ assert_raise(ParseError){C::DivideAssign.parse('switch (0)')}
1489
+ assert_raise(ParseError){C::DivideAssign.parse('for (;;)')}
1490
+ assert_raise(ParseError){C::DivideAssign.parse('goto')}
1491
+ assert_raise(ParseError){C::DivideAssign.parse('return')}
1492
+ end
1493
+
1494
+ def test_mod_assign
1495
+ check C::ModAssign, <<EOS
1496
+ x %= 10
1497
+ ----
1498
+ ModAssign
1499
+ lval: Variable
1500
+ name: "x"
1501
+ rval: IntLiteral
1502
+ val: 10
1503
+ EOS
1504
+ assert_raise(ParseError){C::ModAssign.parse('} void f() {')}
1505
+ assert_raise(ParseError){C::ModAssign.parse(';')}
1506
+ assert_raise(ParseError){C::ModAssign.parse('int i')}
1507
+ assert_raise(ParseError){C::ModAssign.parse('int')}
1508
+ assert_raise(ParseError){C::ModAssign.parse('if (0)')}
1509
+ assert_raise(ParseError){C::ModAssign.parse('switch (0)')}
1510
+ assert_raise(ParseError){C::ModAssign.parse('for (;;)')}
1511
+ assert_raise(ParseError){C::ModAssign.parse('goto')}
1512
+ assert_raise(ParseError){C::ModAssign.parse('return')}
1513
+ end
1514
+
1515
+ def test_add_assign
1516
+ check C::AddAssign, <<EOS
1517
+ x += 10
1518
+ ----
1519
+ AddAssign
1520
+ lval: Variable
1521
+ name: "x"
1522
+ rval: IntLiteral
1523
+ val: 10
1524
+ EOS
1525
+ assert_raise(ParseError){C::AddAssign.parse('} void f() {')}
1526
+ assert_raise(ParseError){C::AddAssign.parse(';')}
1527
+ assert_raise(ParseError){C::AddAssign.parse('int i')}
1528
+ assert_raise(ParseError){C::AddAssign.parse('int')}
1529
+ assert_raise(ParseError){C::AddAssign.parse('if (0)')}
1530
+ assert_raise(ParseError){C::AddAssign.parse('switch (0)')}
1531
+ assert_raise(ParseError){C::AddAssign.parse('for (;;)')}
1532
+ assert_raise(ParseError){C::AddAssign.parse('goto')}
1533
+ assert_raise(ParseError){C::AddAssign.parse('return')}
1534
+ end
1535
+
1536
+ def test_subtract_assign
1537
+ check C::SubtractAssign, <<EOS
1538
+ x -= 10
1539
+ ----
1540
+ SubtractAssign
1541
+ lval: Variable
1542
+ name: "x"
1543
+ rval: IntLiteral
1544
+ val: 10
1545
+ EOS
1546
+ assert_raise(ParseError){C::SubtractAssign.parse('} void f() {')}
1547
+ assert_raise(ParseError){C::SubtractAssign.parse(';')}
1548
+ assert_raise(ParseError){C::SubtractAssign.parse('int i')}
1549
+ assert_raise(ParseError){C::SubtractAssign.parse('int')}
1550
+ assert_raise(ParseError){C::SubtractAssign.parse('if (0)')}
1551
+ assert_raise(ParseError){C::SubtractAssign.parse('switch (0)')}
1552
+ assert_raise(ParseError){C::SubtractAssign.parse('for (;;)')}
1553
+ assert_raise(ParseError){C::SubtractAssign.parse('goto')}
1554
+ assert_raise(ParseError){C::SubtractAssign.parse('return')}
1555
+ end
1556
+
1557
+ def test_shift_left_assign
1558
+ check C::ShiftLeftAssign, <<EOS
1559
+ x <<= 10
1560
+ ----
1561
+ ShiftLeftAssign
1562
+ lval: Variable
1563
+ name: "x"
1564
+ rval: IntLiteral
1565
+ val: 10
1566
+ EOS
1567
+ assert_raise(ParseError){C::ShiftLeftAssign.parse('} void f() {')}
1568
+ assert_raise(ParseError){C::ShiftLeftAssign.parse(';')}
1569
+ assert_raise(ParseError){C::ShiftLeftAssign.parse('int i')}
1570
+ assert_raise(ParseError){C::ShiftLeftAssign.parse('int')}
1571
+ assert_raise(ParseError){C::ShiftLeftAssign.parse('if (0)')}
1572
+ assert_raise(ParseError){C::ShiftLeftAssign.parse('switch (0)')}
1573
+ assert_raise(ParseError){C::ShiftLeftAssign.parse('for (;;)')}
1574
+ assert_raise(ParseError){C::ShiftLeftAssign.parse('goto')}
1575
+ assert_raise(ParseError){C::ShiftLeftAssign.parse('return')}
1576
+ end
1577
+
1578
+ def test_shift_right_assign
1579
+ check C::ShiftRightAssign, <<EOS
1580
+ x >>= 10
1581
+ ----
1582
+ ShiftRightAssign
1583
+ lval: Variable
1584
+ name: "x"
1585
+ rval: IntLiteral
1586
+ val: 10
1587
+ EOS
1588
+ assert_raise(ParseError){C::ShiftRightAssign.parse('} void f() {')}
1589
+ assert_raise(ParseError){C::ShiftRightAssign.parse(';')}
1590
+ assert_raise(ParseError){C::ShiftRightAssign.parse('int i')}
1591
+ assert_raise(ParseError){C::ShiftRightAssign.parse('int')}
1592
+ assert_raise(ParseError){C::ShiftRightAssign.parse('if (0)')}
1593
+ assert_raise(ParseError){C::ShiftRightAssign.parse('switch (0)')}
1594
+ assert_raise(ParseError){C::ShiftRightAssign.parse('for (;;)')}
1595
+ assert_raise(ParseError){C::ShiftRightAssign.parse('goto')}
1596
+ assert_raise(ParseError){C::ShiftRightAssign.parse('return')}
1597
+ end
1598
+
1599
+ def test_bit_and_assign
1600
+ check C::BitAndAssign, <<EOS
1601
+ x &= 10
1602
+ ----
1603
+ BitAndAssign
1604
+ lval: Variable
1605
+ name: "x"
1606
+ rval: IntLiteral
1607
+ val: 10
1608
+ EOS
1609
+ assert_raise(ParseError){C::BitAndAssign.parse('} void f() {')}
1610
+ assert_raise(ParseError){C::BitAndAssign.parse(';')}
1611
+ assert_raise(ParseError){C::BitAndAssign.parse('int i')}
1612
+ assert_raise(ParseError){C::BitAndAssign.parse('int')}
1613
+ assert_raise(ParseError){C::BitAndAssign.parse('if (0)')}
1614
+ assert_raise(ParseError){C::BitAndAssign.parse('switch (0)')}
1615
+ assert_raise(ParseError){C::BitAndAssign.parse('for (;;)')}
1616
+ assert_raise(ParseError){C::BitAndAssign.parse('goto')}
1617
+ assert_raise(ParseError){C::BitAndAssign.parse('return')}
1618
+ end
1619
+
1620
+ def test_bit_xor_assign
1621
+ check C::BitXorAssign, <<EOS
1622
+ x ^= 10
1623
+ ----
1624
+ BitXorAssign
1625
+ lval: Variable
1626
+ name: "x"
1627
+ rval: IntLiteral
1628
+ val: 10
1629
+ EOS
1630
+ assert_raise(ParseError){C::BitXorAssign.parse('} void f() {')}
1631
+ assert_raise(ParseError){C::BitXorAssign.parse(';')}
1632
+ assert_raise(ParseError){C::BitXorAssign.parse('int i')}
1633
+ assert_raise(ParseError){C::BitXorAssign.parse('int')}
1634
+ assert_raise(ParseError){C::BitXorAssign.parse('if (0)')}
1635
+ assert_raise(ParseError){C::BitXorAssign.parse('switch (0)')}
1636
+ assert_raise(ParseError){C::BitXorAssign.parse('for (;;)')}
1637
+ assert_raise(ParseError){C::BitXorAssign.parse('goto')}
1638
+ assert_raise(ParseError){C::BitXorAssign.parse('return')}
1639
+ end
1640
+
1641
+ def test_bit_or_assign
1642
+ check C::BitOrAssign, <<EOS
1643
+ x |= 10
1644
+ ----
1645
+ BitOrAssign
1646
+ lval: Variable
1647
+ name: "x"
1648
+ rval: IntLiteral
1649
+ val: 10
1650
+ EOS
1651
+ assert_raise(ParseError){C::BitOrAssign.parse('} void f() {')}
1652
+ assert_raise(ParseError){C::BitOrAssign.parse(';')}
1653
+ assert_raise(ParseError){C::BitOrAssign.parse('int i')}
1654
+ assert_raise(ParseError){C::BitOrAssign.parse('int')}
1655
+ assert_raise(ParseError){C::BitOrAssign.parse('if (0)')}
1656
+ assert_raise(ParseError){C::BitOrAssign.parse('switch (0)')}
1657
+ assert_raise(ParseError){C::BitOrAssign.parse('for (;;)')}
1658
+ assert_raise(ParseError){C::BitOrAssign.parse('goto')}
1659
+ assert_raise(ParseError){C::BitOrAssign.parse('return')}
1660
+ end
1661
+
1662
+ def test_pointer
1663
+ check C::Pointer, <<EOS
1664
+ int *
1665
+ ----
1666
+ Pointer
1667
+ type: Int
1668
+ EOS
1669
+ check C::Pointer, <<EOS
1670
+ const volatile unsigned int*
1671
+ ----
1672
+ Pointer
1673
+ type: Int (const volatile unsigned)
1674
+ EOS
1675
+ assert_raise(ParseError){C::Pointer.parse('1);} void f() {(int')}
1676
+ assert_raise(ParseError){C::Pointer.parse('1); (int')}
1677
+ assert_raise(ParseError){C::Pointer.parse('void')}
1678
+ end
1679
+
1680
+ def test_array
1681
+ check C::Array, <<EOS
1682
+ int[]
1683
+ ----
1684
+ Array
1685
+ type: Int
1686
+ EOS
1687
+ check C::Array, <<EOS
1688
+ const volatile unsigned int[10]
1689
+ ----
1690
+ Array
1691
+ type: Int (const volatile unsigned)
1692
+ length: IntLiteral
1693
+ val: 10
1694
+ EOS
1695
+ assert_raise(ParseError){C::Array.parse('1);} void f() {(int')}
1696
+ assert_raise(ParseError){C::Array.parse('1); (int')}
1697
+ assert_raise(ParseError){C::Array.parse('void')}
1698
+ end
1699
+
1700
+ def test_function
1701
+ check C::Function, <<EOS
1702
+ void()
1703
+ ----
1704
+ Function
1705
+ type: Void
1706
+ EOS
1707
+ check C::Function, <<EOS
1708
+ const volatile unsigned int(int x, int y)
1709
+ ----
1710
+ Function
1711
+ type: Int (const volatile unsigned)
1712
+ params:
1713
+ - Parameter
1714
+ type: Int
1715
+ name: "x"
1716
+ - Parameter
1717
+ type: Int
1718
+ name: "y"
1719
+ EOS
1720
+ assert_raise(ParseError){C::Function.parse('1);} void f() {(int')}
1721
+ assert_raise(ParseError){C::Function.parse('1); (int')}
1722
+ assert_raise(ParseError){C::Function.parse('void')}
1723
+ end
1724
+
1725
+ def test_struct
1726
+ check C::Struct, <<EOS
1727
+ struct s
1728
+ ----
1729
+ Struct
1730
+ name: "s"
1731
+ EOS
1732
+ check C::Struct, <<EOS
1733
+ const struct {int i, j : 4;}
1734
+ ----
1735
+ Struct (const)
1736
+ members:
1737
+ - Declaration
1738
+ type: Int
1739
+ declarators:
1740
+ - Declarator
1741
+ name: "i"
1742
+ - Declarator
1743
+ name: "j"
1744
+ num_bits: IntLiteral
1745
+ val: 4
1746
+ EOS
1747
+ assert_raise(ParseError){C::Struct.parse('1);} void f() {(int')}
1748
+ assert_raise(ParseError){C::Struct.parse('1); (int')}
1749
+ assert_raise(ParseError){C::Struct.parse('void')}
1750
+ end
1751
+
1752
+ def test_union
1753
+ check C::Union, <<EOS
1754
+ union s
1755
+ ----
1756
+ Union
1757
+ name: "s"
1758
+ EOS
1759
+ check C::Union, <<EOS
1760
+ const union {int i, j : 4;}
1761
+ ----
1762
+ Union (const)
1763
+ members:
1764
+ - Declaration
1765
+ type: Int
1766
+ declarators:
1767
+ - Declarator
1768
+ name: "i"
1769
+ - Declarator
1770
+ name: "j"
1771
+ num_bits: IntLiteral
1772
+ val: 4
1773
+ EOS
1774
+ assert_raise(ParseError){C::Union.parse('1);} void f() {(int')}
1775
+ assert_raise(ParseError){C::Union.parse('1); (int')}
1776
+ assert_raise(ParseError){C::Union.parse('void')}
1777
+ end
1778
+
1779
+ def test_enum
1780
+ check C::Enum, <<EOS
1781
+ enum s
1782
+ ----
1783
+ Enum
1784
+ name: "s"
1785
+ EOS
1786
+ check C::Enum, <<EOS
1787
+ const enum {X = 10, Y, Z}
1788
+ ----
1789
+ Enum (const)
1790
+ members:
1791
+ - Enumerator
1792
+ name: "X"
1793
+ val: IntLiteral
1794
+ val: 10
1795
+ - Enumerator
1796
+ name: "Y"
1797
+ - Enumerator
1798
+ name: "Z"
1799
+ EOS
1800
+ assert_raise(ParseError){C::Enum.parse('1);} void f() {(int')}
1801
+ assert_raise(ParseError){C::Enum.parse('1); (int')}
1802
+ assert_raise(ParseError){C::Enum.parse('void')}
1803
+ end
1804
+
1805
+ def test_custom_type
1806
+ assert_raise(ParseError){C::CustomType.parse('1);} void f() {(int')}
1807
+ assert_raise(ParseError){C::CustomType.parse('1); (int')}
1808
+ assert_raise(ParseError){C::CustomType.parse('void')}
1809
+ end
1810
+
1811
+ def test_void
1812
+ check C::Void, <<EOS
1813
+ const void
1814
+ ----
1815
+ Void (const)
1816
+ EOS
1817
+ assert_raise(ParseError){C::Void.parse('1);} void f() {(int')}
1818
+ assert_raise(ParseError){C::Void.parse('1); (int')}
1819
+ assert_raise(ParseError){C::Void.parse('int')}
1820
+ end
1821
+
1822
+ def test_int
1823
+ check C::Int, <<EOS
1824
+ const int
1825
+ ----
1826
+ Int (const)
1827
+ EOS
1828
+ assert_raise(ParseError){C::Int.parse('1);} void f() {(int')}
1829
+ assert_raise(ParseError){C::Int.parse('1); (int')}
1830
+ assert_raise(ParseError){C::Int.parse('void')}
1831
+ end
1832
+
1833
+ def test_float
1834
+ check C::Float, <<EOS
1835
+ const float
1836
+ ----
1837
+ Float (const)
1838
+ EOS
1839
+ assert_raise(ParseError){C::Float.parse('1);} void f() {(int')}
1840
+ assert_raise(ParseError){C::Float.parse('1); (int')}
1841
+ assert_raise(ParseError){C::Float.parse('void')}
1842
+ end
1843
+
1844
+ def test_char
1845
+ check C::Char, <<EOS
1846
+ const char
1847
+ ----
1848
+ Char (const)
1849
+ EOS
1850
+ assert_raise(ParseError){C::Char.parse('1);} void f() {(int')}
1851
+ assert_raise(ParseError){C::Char.parse('1); (int')}
1852
+ assert_raise(ParseError){C::Char.parse('void')}
1853
+ end
1854
+
1855
+ def test_bool
1856
+ check C::Bool, <<EOS
1857
+ const _Bool
1858
+ ----
1859
+ Bool (const)
1860
+ EOS
1861
+ assert_raise(ParseError){C::Bool.parse('1);} void f() {(int')}
1862
+ assert_raise(ParseError){C::Bool.parse('1); (int')}
1863
+ assert_raise(ParseError){C::Bool.parse('void')}
1864
+ end
1865
+
1866
+ def test_complex
1867
+ check C::Complex, <<EOS
1868
+ const _Complex float
1869
+ ----
1870
+ Complex (const)
1871
+ EOS
1872
+ assert_raise(ParseError){C::Complex.parse('1);} void f() {(int')}
1873
+ assert_raise(ParseError){C::Complex.parse('1); (int')}
1874
+ assert_raise(ParseError){C::Complex.parse('void')}
1875
+ end
1876
+
1877
+ def test_imaginary
1878
+ check C::Imaginary, <<EOS
1879
+ const _Imaginary float
1880
+ ----
1881
+ Imaginary (const)
1882
+ EOS
1883
+ assert_raise(ParseError){C::Imaginary.parse('1);} void f() {(int')}
1884
+ assert_raise(ParseError){C::Imaginary.parse('1); (int')}
1885
+ assert_raise(ParseError){C::Imaginary.parse('void')}
1886
+ end
1887
+
1888
+ def test_string_literal
1889
+ check C::StringLiteral, <<EOS
1890
+ "hello"
1891
+ ----
1892
+ StringLiteral
1893
+ val: "hello"
1894
+ EOS
1895
+ assert_raise(ParseError){C::StringLiteral.parse('} void f() {')}
1896
+ assert_raise(ParseError){C::StringLiteral.parse(';')}
1897
+ assert_raise(ParseError){C::StringLiteral.parse('int i')}
1898
+ assert_raise(ParseError){C::StringLiteral.parse('int')}
1899
+ assert_raise(ParseError){C::StringLiteral.parse('if (0)')}
1900
+ assert_raise(ParseError){C::StringLiteral.parse('switch (0)')}
1901
+ assert_raise(ParseError){C::StringLiteral.parse('for (;;)')}
1902
+ assert_raise(ParseError){C::StringLiteral.parse('goto')}
1903
+ assert_raise(ParseError){C::StringLiteral.parse('return')}
1904
+ end
1905
+
1906
+ def test_char_literal
1907
+ check C::CharLiteral, <<EOS
1908
+ 'x'
1909
+ ----
1910
+ CharLiteral
1911
+ val: "x"
1912
+ EOS
1913
+ assert_raise(ParseError){C::CharLiteral.parse('} void f() {')}
1914
+ assert_raise(ParseError){C::CharLiteral.parse(';')}
1915
+ assert_raise(ParseError){C::CharLiteral.parse('int i')}
1916
+ assert_raise(ParseError){C::CharLiteral.parse('int')}
1917
+ assert_raise(ParseError){C::CharLiteral.parse('if (0)')}
1918
+ assert_raise(ParseError){C::CharLiteral.parse('switch (0)')}
1919
+ assert_raise(ParseError){C::CharLiteral.parse('for (;;)')}
1920
+ assert_raise(ParseError){C::CharLiteral.parse('goto')}
1921
+ assert_raise(ParseError){C::CharLiteral.parse('return')}
1922
+ end
1923
+
1924
+ def test_compound_literal
1925
+ check C::CompoundLiteral, <<EOS
1926
+ (struct s){.x [0] = 10, .y = 20, 30}
1927
+ ----
1928
+ CompoundLiteral
1929
+ type: Struct
1930
+ name: "s"
1931
+ member_inits:
1932
+ - MemberInit
1933
+ member:
1934
+ - Member
1935
+ name: "x"
1936
+ - IntLiteral
1937
+ val: 0
1938
+ init: IntLiteral
1939
+ val: 10
1940
+ - MemberInit
1941
+ member:
1942
+ - Member
1943
+ name: "y"
1944
+ init: IntLiteral
1945
+ val: 20
1946
+ - MemberInit
1947
+ init: IntLiteral
1948
+ val: 30
1949
+ EOS
1950
+ assert_raise(ParseError){C::CompoundLiteral.parse('} void f() {')}
1951
+ assert_raise(ParseError){C::CompoundLiteral.parse(';')}
1952
+ assert_raise(ParseError){C::CompoundLiteral.parse('int i')}
1953
+ assert_raise(ParseError){C::CompoundLiteral.parse('int')}
1954
+ assert_raise(ParseError){C::CompoundLiteral.parse('if (0)')}
1955
+ assert_raise(ParseError){C::CompoundLiteral.parse('switch (0)')}
1956
+ assert_raise(ParseError){C::CompoundLiteral.parse('for (;;)')}
1957
+ assert_raise(ParseError){C::CompoundLiteral.parse('goto')}
1958
+ assert_raise(ParseError){C::CompoundLiteral.parse('return')}
1959
+ end
1960
+
1961
+ def test_int_literal
1962
+ check C::IntLiteral, <<EOS
1963
+ 1
1964
+ ----
1965
+ IntLiteral
1966
+ val: 1
1967
+ EOS
1968
+ assert_raise(ParseError){C::IntLiteral.parse('} void f() {')}
1969
+ assert_raise(ParseError){C::IntLiteral.parse(';')}
1970
+ assert_raise(ParseError){C::IntLiteral.parse('int i')}
1971
+ assert_raise(ParseError){C::IntLiteral.parse('int')}
1972
+ assert_raise(ParseError){C::IntLiteral.parse('if (0)')}
1973
+ assert_raise(ParseError){C::IntLiteral.parse('switch (0)')}
1974
+ assert_raise(ParseError){C::IntLiteral.parse('for (;;)')}
1975
+ assert_raise(ParseError){C::IntLiteral.parse('goto')}
1976
+ assert_raise(ParseError){C::IntLiteral.parse('return')}
1977
+ end
1978
+
1979
+ def test_float_literal
1980
+ check C::FloatLiteral, <<EOS
1981
+ 1.0
1982
+ ----
1983
+ FloatLiteral
1984
+ val: 1.0
1985
+ EOS
1986
+ assert_raise(ParseError){C::FloatLiteral.parse('} void f() {')}
1987
+ assert_raise(ParseError){C::FloatLiteral.parse(';')}
1988
+ assert_raise(ParseError){C::FloatLiteral.parse('int i')}
1989
+ assert_raise(ParseError){C::FloatLiteral.parse('int')}
1990
+ assert_raise(ParseError){C::FloatLiteral.parse('if (0)')}
1991
+ assert_raise(ParseError){C::FloatLiteral.parse('switch (0)')}
1992
+ assert_raise(ParseError){C::FloatLiteral.parse('for (;;)')}
1993
+ assert_raise(ParseError){C::FloatLiteral.parse('goto')}
1994
+ assert_raise(ParseError){C::FloatLiteral.parse('return')}
1995
+ end
1996
+
1997
+ def test_variable
1998
+ check C::Variable, <<EOS
1999
+ x
2000
+ ----
2001
+ Variable
2002
+ name: "x"
2003
+ EOS
2004
+ assert_raise(ParseError){C::Variable.parse('} void f() {')}
2005
+ assert_raise(ParseError){C::Variable.parse(';')}
2006
+ assert_raise(ParseError){C::Variable.parse('int i')}
2007
+ assert_raise(ParseError){C::Variable.parse('int')}
2008
+ assert_raise(ParseError){C::Variable.parse('if (0)')}
2009
+ assert_raise(ParseError){C::Variable.parse('switch (0)')}
2010
+ assert_raise(ParseError){C::Variable.parse('for (;;)')}
2011
+ assert_raise(ParseError){C::Variable.parse('goto')}
2012
+ assert_raise(ParseError){C::Variable.parse('return')}
2013
+ end
2014
+ end