casty 0.3.1

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