Class: Crokus::ParserOnly

Inherits:
Object
  • Object
show all
Includes:
Indent
Defined in:
lib/crokus/parser_only.rb

Constant Summary collapse

STARTERS_ARRAY_OR_STRUCT_INIT =
[:lbrace]
STARTERS_PRIMARY =
[:ident,:integer_lit,:float_lit,:string_lit,:char_lit,:lparen]+STARTERS_ARRAY_OR_STRUCT_INIT
UNARY_OP =
[:and,:mul,:add,:sub,:tilde,:not]
STARTERS_UNARY =
[:inc_op,:dec_op,:sizeof]+STARTERS_PRIMARY+UNARY_OP
ASSIGN_OP =
[:assign,:add_assign,:mul_assign,:div_assign,:mod_assign,:xor_assign]
STARTERS_TYPE_SPECIFIER =
[:void,:char,:short,:int,:long,:float,:signed,:unsigned,:struct,:union,:enum,:ident]
STARTERS_ABSTRACT_DECLARATOR =

: pointer

| direct_abstract_declarator
| pointer direct_abstract_declarator
;
[:mul,:lparen,:lbrack]
STARTERS_TYPE_QUALIFIER =

pointer : ‘*’ | ‘*’ type_qualifier_list | ‘*’ pointer | ‘*’ type_qualifier_list pointer ;

[:const,:volatile]

Constants included from Indent

Indent::INDENT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Indent

#dedent, #indent, #say

Constructor Details

#initializeParserOnly

Returns a new instance of ParserOnly.



15
16
17
18
19
# File 'lib/crokus/parser_only.rb', line 15

def initialize
  @ppr=PrettyPrinter.new
  @verbose=false
  #@verbose=true
end

Instance Attribute Details

#strObject

Returns the value of attribute str.



12
13
14
# File 'lib/crokus/parser_only.rb', line 12

def str
  @str
end

#tokensObject

Returns the value of attribute tokens.



12
13
14
# File 'lib/crokus/parser_only.rb', line 12

def tokens
  @tokens
end

Instance Method Details

#abstract_declObject



830
831
832
833
834
835
836
837
838
839
840
841
842
843
# File 'lib/crokus/parser_only.rb', line 830

def abstract_decl
  indent "abstract_decl"
  if showNext.is_a? STARTERS_ABSTRACT_DECLARATOR
    case showNext.kind
    when :mul
      pointer
    else
      direct_abstract_declarator
    end
  else
    raise "ERROR : in abstract_declarator. Expecting one of #{STARTERS_ABSTRACT_DECLARATOR}"
  end
  dedent
end

#acceptItObject



21
22
23
24
# File 'lib/crokus/parser_only.rb', line 21

def acceptIt
  say showNext.kind.to_s+" "+showNext.val
  tokens.shift
end

#accuObject



562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
# File 'lib/crokus/parser_only.rb', line 562

def accu
  indent "accu"
  #expect(:ident)
  lhs=expression
  case showNext.kind
  when :addadd,:subsub
    tok=acceptIt
  when :addeq,:subeq
    tok=acceptIt
    e=expression
  else
    show_line(showNext.pos)
    raise "unknown accumulator at #{showNext.pos}"
  end
  dedent
  Accu.new(lhs,tok,e)
end

#additiveObject



726
727
728
729
730
731
732
733
734
# File 'lib/crokus/parser_only.rb', line 726

def additive
  indent "addititve : #{showNext}"
  multitive
  while showNext.is_a? [:add,:sub]
    acceptIt
    multitive
  end
  dedent
end

#andexpObject



686
687
688
689
690
691
692
693
694
# File 'lib/crokus/parser_only.rb', line 686

def andexp
  indent "andexp : #{showNext}"
  eqexp
  while showNext.is_a? :and
    acceptIt
    eqexp
  end
  dedent
end

#argument_expr_listObject



956
957
958
959
960
961
962
# File 'lib/crokus/parser_only.rb', line 956

def argument_expr_list
  expression
  while showNext.is_a? :comma
    acceptIt
    expression
  end
end

#array_or_struct_initObject



964
965
966
967
968
969
970
971
972
973
974
975
976
977
# File 'lib/crokus/parser_only.rb', line 964

def array_or_struct_init
  indent "array_or_struct_init"
  expect :lbrace
  elements=[]
  while !showNext.is_a? :rbrace
    elements << (e=expression)
    if showNext.is_a? :comma
      acceptIt
    end
  end
  expect :rbrace
  dedent
  return ArrayOrStructInit.new(elements)
end

#arrayed?Boolean

Returns:

  • (Boolean)


399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/crokus/parser_only.rb', line 399

def arrayed?
  return if tokens.empty?
  while showNext.is_a? :lbrack
    acceptIt
    if showNext.is_a? :rbrack
      acceptIt
    else
      e=expression
      expect :rbrack
    end
  end
end

#assignObject



622
623
624
625
626
627
628
629
630
631
632
# File 'lib/crokus/parser_only.rb', line 622

def assign
  indent "assign : #{showNext}"
  e1=cond_expr
  while showNext.is_a? ASSIGN_OP
    op=acceptIt
    e2=assign
    e1=Assign.new(e1,op,e2)
  end
  dedent
  return e1
end

#castexpObject



746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
# File 'lib/crokus/parser_only.rb', line 746

def castexp
  indent "castexpr : #{showNext}"
  case showNext.kind
  when :lparen # parenth expr OR casting !
    res=is_casting?
    puts "casting? : #{res}" if @verbose
    if res
      casting
    else
      parenthesized
    end
  else
    unary
  end
  dedent
end

#castingObject



775
776
777
778
779
780
781
782
# File 'lib/crokus/parser_only.rb', line 775

def casting
  indent "casting : #{showNext}"
  expect :lparen
  typename
  expect :rparen
  unary
  dedent
end

#cond_exprObject



634
635
636
637
638
639
640
641
642
643
644
# File 'lib/crokus/parser_only.rb', line 634

def cond_expr
  indent "cond_expr : #{showNext}"
  logor
  while showNext.is_a? :qmark
    acceptIt
    expression
    expect :colon
    cond_expr
  end
  dedent
end

#dbg_print(node) ⇒ Object



61
62
63
64
65
# File 'lib/crokus/parser_only.rb', line 61

def dbg_print node
  puts "debug ast node".center(60,'-')
  puts node.accept(@ppr)
  puts "-"*60
end

#dbg_print_next(n) ⇒ Object




57
58
59
# File 'lib/crokus/parser_only.rb', line 57

def dbg_print_next n
  p tokens[0..n-1].collect{|tok| tok.inspect}
end

#debugObject



600
601
602
# File 'lib/crokus/parser_only.rb', line 600

def debug
  puts " "*@indentation+@tokens[0..4].map{|t| "'#{t.val}'"}.join(" ")
end

#declarationObject

int a int * a int a=1,b=2; int a[] int* f() struct name *ptr; paire_t paire = 1,2; int a,b,*c


TYPE ident ident ident



365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/crokus/parser_only.rb', line 365

def declaration
  ret=[]
  @current_type=type=parse_type()
  declarator()
  arrayed?
  parenthesized?
  initialization?
  while tokens.any? and showNext.is_a?(:comma)
    acceptIt
    pointed?
    declarator
    arrayed?
    initialization?
  end
  if tokens.any?
    maybe :semicolon
  end
  return ret
end

#declaratorObject



385
386
387
388
389
390
# File 'lib/crokus/parser_only.rb', line 385

def declarator
  if showNext.is_a? :ident
    ret=@current_ident=Ident.new(acceptIt)
  end
  return ret
end

#defineObject



150
151
152
153
154
155
156
157
158
# File 'lib/crokus/parser_only.rb', line 150

def define
  indent "define"
  expect :sharp
  expect :ident #define
  name=expect :ident
  e=expression()
  dedent
  return Define.new(name,e)
end

#design_unitObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/crokus/parser_only.rb', line 107

def design_unit
  indent "designUnit"
  du=DesignUnit.new
  while tokens.any?
    case showNext.kind
    when :sharp
      case showNext(2).val
      when "include"
        du << include()
      when "define"
        du << define()
      end
    else
      du << declaration
      maybe :semicolon if tokens.any?
    end
  end
  dedent
  du.list.flatten!
  return du
end

#direct_abstract_declaratorObject



866
867
868
# File 'lib/crokus/parser_only.rb', line 866

def direct_abstract_declarator
  raise
end

#do_whileObject



302
303
304
305
306
307
308
309
310
# File 'lib/crokus/parser_only.rb', line 302

def do_while
  indent "do_while"
  expect :do
  body=statement()
  expect :while
  e=expression
  dedent
  DoWhile.new(e,body)
end

#eqexpObject



696
697
698
699
700
701
702
703
704
# File 'lib/crokus/parser_only.rb', line 696

def eqexp
  indent "eqexp : #{showNext}"
  relexp
  while showNext.is_a? [:eq,:neq]
    acceptIt
    relexp
  end
  dedent
end

#exclorObject



676
677
678
679
680
681
682
683
684
# File 'lib/crokus/parser_only.rb', line 676

def exclor
  indent "exclor : #{showNext}"
  andexp
  while showNext.is_a? :xor
    acceptIt
    andexp
  end
  dedent
end

#expect(kind) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/crokus/parser_only.rb', line 30

def expect kind
  if ((actual=tokens.shift).kind)!=kind
    puts "ERROR :"
    show_line(actual.pos)
    raise "expecting '#{kind}'. Received '#{actual.val}' around #{actual.pos}"
  end
  say actual.kind.to_s+" "+actual.val
  return actual
end

#expressionObject



604
605
606
607
608
609
610
611
612
613
614
# File 'lib/crokus/parser_only.rb', line 604

def expression
  indent "expression : #{showNext}"
  e1=assign()
  while showNext.is_a? :comma
    acceptIt
    e2=assign()
    e1=CommaStmt.new(e1,e2)
  end
  dedent
  return e1
end

#expression_statementObject



590
591
592
593
594
595
596
597
598
# File 'lib/crokus/parser_only.rb', line 590

def expression_statement
  if showNext.is_a? :semicolon
    return SemicolonStmt.new(acceptIt)
  else
    e=expression
    expect :semicolon
    return e
  end
end

#func_call(as_procedure = false) ⇒ Object



427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# File 'lib/crokus/parser_only.rb', line 427

def func_call as_procedure=false
  indent "func_call"
  name=expect(:ident)
  expect :lparen
  args=[]
  while !showNext.is_a? :rparen
    args << expression()
    if showNext.is_a? :comma
      acceptIt
    end
  end
  expect :rparen
  dedent
  FunCall.new(name,args,as_procedure)
end

#func_formal_argObject



219
220
221
222
223
224
225
226
227
# File 'lib/crokus/parser_only.rb', line 219

def func_formal_arg
  indent "function_arg"
  @current_type=parse_type()
  declarator
  arrayed?
  parenthesized?
  dedent
  #return FormalArg.new(name,type)
end

#function_bodyObject



229
230
231
232
233
234
235
236
237
238
239
# File 'lib/crokus/parser_only.rb', line 229

def function_body
  indent "function_body"
  body=Body.new
  expect :lbrace
  while showNext.kind!=:rbrace
    body << statement()
  end
  expect :rbrace
  dedent
  return body
end

#function_decl(name, type_) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/crokus/parser_only.rb', line 189

def function_decl name,type_
  indent "function"
  args=function_formal_args()
  case showNext.kind
  when :semicolon
    acceptIt
    ret =FunctionProto.new(name,type_,args)
  else
    body=function_body()
    ret= Function.new(name,type_,args,body)
  end
  dedent
  return ret
end

#function_formal_argsObject



204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/crokus/parser_only.rb', line 204

def function_formal_args
  indent "function_formal_args"
  args=[]
  expect :lparen
  while !showNext.is_a? :rparen
    args << func_formal_arg()
    if !showNext.is_a? :rparen
      expect :comma
    end
  end
  expect :rparen
  dedent
  return args
end

#inclorObject



666
667
668
669
670
671
672
673
674
# File 'lib/crokus/parser_only.rb', line 666

def inclor
  indent "inclor : #{showNext}"
  exclor
  while showNext.is_a? :or
    acceptIt
    exclor
  end
  dedent
end

#includeObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/crokus/parser_only.rb', line 129

def include
  indent "include"
  expect :sharp
  expect :ident #include
  case showNext.kind
  when :lt
    acceptIt
    id1=expect :ident
    expect :dot
    id2=expect :ident
    expect :gt
    name=Token.new [:ident,id1.val+"."+id2.val,id1.pos]
    env=:env
  when :string_lit
    name=acceptIt
    env=:local
  end
  dedent
  return Include.new(name,env)
end

#initialization?Boolean

Returns:

  • (Boolean)


412
413
414
415
416
417
418
# File 'lib/crokus/parser_only.rb', line 412

def initialization?
  return if tokens.empty?
  if showNext.is_a? :assign
    expect :assign
    expression
  end
end

#is_casting?Boolean

Returns:

  • (Boolean)


763
764
765
766
767
768
769
770
771
772
773
# File 'lib/crokus/parser_only.rb', line 763

def is_casting?
  i=0
  tok=DUMMY
  while tok.kind!=:rparen
    tok=@tokens[i]
    i+=1
  end
  tok=@tokens[i]
  return true if tok.is_a? STARTERS_UNARY-STARTERS_ARRAY_OR_STRUCT_INIT
  return false
end

#logandObject



656
657
658
659
660
661
662
663
664
# File 'lib/crokus/parser_only.rb', line 656

def logand
  indent "logand : #{showNext}"
  inclor
  while showNext.is_a? :andand
    acceptIt
    inclor
  end
  dedent
end

#logorObject



646
647
648
649
650
651
652
653
654
# File 'lib/crokus/parser_only.rb', line 646

def logor
  indent "logor : #{showNext}"
  logand
  while showNext.is_a? :oror
    acceptIt
    logand
  end
  dedent
end

#lookahead(n = 2) ⇒ Object



44
45
46
# File 'lib/crokus/parser_only.rb', line 44

def lookahead(n=2)
  tokens[n] if tokens.any?
end

#maybe(kind) ⇒ Object



26
27
28
# File 'lib/crokus/parser_only.rb', line 26

def maybe kind
  return acceptIt if showNext.is_a? kind
end

#multitiveObject



736
737
738
739
740
741
742
743
744
# File 'lib/crokus/parser_only.rb', line 736

def multitive
  indent "multitive : #{showNext}"
  castexp
  while showNext.is_a? [:mul,:div,:mod]
    acceptIt
    castexp
  end
  dedent
end

#parenthesizedObject



784
785
786
787
788
789
790
# File 'lib/crokus/parser_only.rb', line 784

def parenthesized
  indent "parenthesized : #{showNext}"
  expect :lparen
  expression
  expect :rparen
  dedent
end

#parenthesized?Boolean

Returns:

  • (Boolean)


420
421
422
423
424
425
# File 'lib/crokus/parser_only.rb', line 420

def parenthesized?
  return if tokens.empty?
  if showNext.is_a? :lparen
    function_decl(@current_ident,@current_type)
  end
end

#parse(str) ⇒ Object

.….….… parsing methods .….……



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/crokus/parser_only.rb', line 67

def parse str
  begin
    @str=str
    @tokens=Lexer.new.tokenize(str)
    @tokens=remove_comments()
    warnings=@tokens.select{|tok| tok.is_a? :lexer_warning}
    show_lexer_warnings(warnings)
    @tokens=@tokens.select{|tok| !tok.is_a? [:newline]}
    ast=design_unit()
  rescue Exception => e
    puts "PARSING ERROR : #{e}"
    puts "in C source at line/col #{showNext.pos}"
    puts e.backtrace
    abort
  end
end

#parse_bodyObject



580
581
582
583
584
585
586
587
588
# File 'lib/crokus/parser_only.rb', line 580

def parse_body
  body=Body.new
  expect :lbrace
  while !showNext.is_a? :rbrace
    body << statement()
  end
  expect :rbrace
  return body
end

#parse_elseObject



507
508
509
510
511
512
513
514
# File 'lib/crokus/parser_only.rb', line 507

def parse_else
  indent "parse else"
  expect :else
  ret=Else.new
  ret.body=statement()
  dedent
  return ret
end

#parse_forObject



526
527
528
529
530
531
532
533
534
535
536
537
538
# File 'lib/crokus/parser_only.rb', line 526

def parse_for
  indent "parse_for"
  forloop=For.new
  expect :for
  expect :lparen
  forloop.init=expression_statement()
  forloop.cond=expression_statement()
  forloop.increment=expression()
  expect :rparen
  forloop.body=statement()
  dedent
  forloop
end

#parse_gotoObject



294
295
296
297
298
299
300
# File 'lib/crokus/parser_only.rb', line 294

def parse_goto
  indent "goto"
  expect :goto #label
  id=expect(:ident)
  dedent
  Goto.new(id)
end

#parse_ifObject



490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
# File 'lib/crokus/parser_only.rb', line 490

def parse_if
  indent "parse_if"
  expect :if
  if showNext.is_a? :lparen # helps wrt casting.
    acceptIt
    lparen=true
  end
  cond=expression()
  expect :rparen if lparen
  body=statement()
  if showNext.is_a? :else
    else_=parse_else()
  end
  dedent
  return If.new(cond,body,else_)
end

#parse_labelObject



289
290
291
292
# File 'lib/crokus/parser_only.rb', line 289

def parse_label
  expect :ident
  expect :colon
end

#parse_returnObject



342
343
344
345
346
347
348
349
350
351
# File 'lib/crokus/parser_only.rb', line 342

def parse_return
  indent "parse_return"
  expect :return
  if showNext.is_a? :semicolon
  else
    e=expression
  end
  dedent
  Return.new(e)
end

#parse_structObject



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/crokus/parser_only.rb', line 160

def parse_struct
  indent "struct"
  expect :struct
  name=nil
  if showNext.is_a? :ident
    name=acceptIt
  end
  ret=Struct.new(name)
  if showNext.is_a? :lbrace
    acceptIt
    while !showNext.is_a? :rbrace
      ret.decls << declaration()
    end
    ret.decls.flatten!
    expect :rbrace
  end
  dedent
  return ret
end

#parse_typeObject



443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
# File 'lib/crokus/parser_only.rb', line 443

def parse_type
  indent "parse_type"
  qualifier=type_qualifier?()
  case showNext.kind
  when :signed,:unsigned
    acceptIt
  when :ident,:char,:int,:short,:long,:float,:double,:void
    tok=acceptIt
    ret=Type.new(tok)
    (ret.specifiers << qualifier) if qualifier
  when :struct
    struct=parse_struct()
  when :typedef
    typedef()
  else
    raise "Parsing ERROR in type declaration: '#{showNext}'"
  end
  while showNext.is_a? [:mul,:lparen]
    case showNext.kind
    when :mul
      acceptIt
    when :lparen
      acceptIt
      if showNext.is_a? :rparen
        acceptIt
      else
        expression
        expect :rparen
      end
    end
  end
  dedent
  return ret
end

#parse_whileObject



516
517
518
519
520
521
522
523
524
# File 'lib/crokus/parser_only.rb', line 516

def parse_while
  indent "parse_while"
  expect :while
  cond=expression()
  body=[]
  body << statement()
  dedent
  return While.new(cond,body)
end

#parseLoopCondObject



548
549
550
551
552
553
# File 'lib/crokus/parser_only.rb', line 548

def parseLoopCond
  indent "parseLoopCond"
  e=expression()
  dedent
  return e
end

#parseLoopEndObject



555
556
557
558
559
560
# File 'lib/crokus/parser_only.rb', line 555

def parseLoopEnd
  indent "parseLoopEnd"
  s=statement()
  dedent
  return s
end

#parseLoopInitObject



540
541
542
543
544
545
546
# File 'lib/crokus/parser_only.rb', line 540

def parseLoopInit
  indent "parseLoopInit"
  ret=statement()
  dedent
  return [ret] # because for (int a,b=0;i<10;i++) is also possible.
  # then parser returns an array of Decl
end

#pointed?Boolean

Returns:

  • (Boolean)


392
393
394
395
396
397
# File 'lib/crokus/parser_only.rb', line 392

def pointed?
  return if tokens.empty?
  while showNext.is_a? :mul
    acceptIt
  end
end

#pointerObject



852
853
854
855
856
857
858
859
860
861
862
863
864
# File 'lib/crokus/parser_only.rb', line 852

def pointer
  expect :mul
  while showNext.is_a? STARTERS_TYPE_QUALIFIER+[:mul]
    case showNext.kind
    when :volatile
      acceptIt
    when :const
      acceptIt
    when :mult
      acceptIt
    end
  end
end

#postfixObject



904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
# File 'lib/crokus/parser_only.rb', line 904

def postfix
  indent "postfix : #{showNext}"
  primary
  while showNext.is_a? [:lbrack,:lparen,:dot,:inc_op,:dec_op,:ptr_op]
    case showNext.kind
    when :lbrack
      acceptIt
      expression
      expect :rbrack
    when :lparen
      acceptIt
      if !showNext.is_a? :rparen
        argument_expr_list
      end
      expect :rparen
    when :dot
      acceptIt
      expect :ident
    when :ptr_op
      acceptIt
      expect :ident
    when :inc_op
      acceptIt
    when :dec_op
      acceptIt
    end
  end
  dedent
end

#primaryObject



934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
# File 'lib/crokus/parser_only.rb', line 934

def primary
  case showNext.kind
  when :ident
    return Ident.new(acceptIt)
  when :integer_lit
    return IntLit.new(acceptIt)
  when :float_lit
    return FloatLit.new(acceptIt)
  when :string_lit
    return acceptIt
  when :char_lit
    return acceptIt
  when :lparen
    acceptIt
    e=expression
    expect :rparen
    return Parenth.new(e)
  when :lbrace
    return array_or_struct_init()
  end
end

#relexpObject



706
707
708
709
710
711
712
713
714
# File 'lib/crokus/parser_only.rb', line 706

def relexp
  indent "relexp : #{showNext}"
  shiftexp
  while showNext.is_a? [:lte,:lt,:gte,:gt ]
    acceptIt
    shiftexp
  end
  dedent
end

#remove_commentsObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/crokus/parser_only.rb', line 90

def remove_comments
  ret=[]
  in_comment=false
  tokens.each do |tok|
    case tok.kind
    when :comment
    when :lcomment
      in_comment=true
    when :rcomment
      in_comment=false
    else
      ret << tok unless in_comment
    end
  end
  ret
end

#shiftexpObject



716
717
718
719
720
721
722
723
724
# File 'lib/crokus/parser_only.rb', line 716

def shiftexp
  indent "shiftexp : #{showNext}"
  additive
  while showNext.is_a? [:shift_l,:shift_r]
    acceptIt
    additive
  end
  dedent
end

#show_lexer_warnings(warnings) ⇒ Object



84
85
86
87
88
# File 'lib/crokus/parser_only.rb', line 84

def show_lexer_warnings warnings
  warnings.each do |warn|
    puts "lexer warning : #{warn.val} at #{warn.pos}"
  end
end

#show_line(pos) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/crokus/parser_only.rb', line 48

def show_line pos
  l,c=*pos
  show_lines(str,l-2)
  line=str.split(/\n/)[l-1]
  pointer="-"*(5+c)+ "^"
  puts "#{l.to_s.ljust(5)}|#{line}"
  puts pointer
end

#showNext(n = 1) ⇒ Object



40
41
42
# File 'lib/crokus/parser_only.rb', line 40

def showNext(n=1)
  tokens[n-1] if tokens.any?
end

#sizeofObject



892
893
894
895
896
897
898
899
900
901
902
# File 'lib/crokus/parser_only.rb', line 892

def sizeof
  expect :sizeof
  case showNext.kind
  when :lparen
    acceptIt
    typename
    expect :rparen
  else
    unary
  end
end

#spec_qualifier_listObject



801
802
803
804
805
806
807
808
809
810
811
# File 'lib/crokus/parser_only.rb', line 801

def spec_qualifier_list
  indent "spec_qualifier_list #{showNext.inspect}"
  while showNext.is_a? STARTERS_TYPE_SPECIFIER+STARTERS_TYPE_QUALIFIER
    if showNext.is_a? STARTERS_TYPE_SPECIFIER
      type_specifier
    else
      type_qualifier
    end
  end
  dedent
end

#statement(arg = nil) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/crokus/parser_only.rb', line 241

def statement(arg=nil)
  indent "statement ...#{showNext.kind} #{showNext.pos.first}"
  case showNext.kind
  when :lbrace
    ret=parse_body()
  when :unsigned,:signed,:int,:short,:float,:double,:long,:char,:void
    ret=declaration()
  when :struct
    ret=declaration()
  when :if
    ret=parse_if()
  when :while
    ret=parse_while()
  when :for
    ret=parse_for()
  when :switch
    ret=switch()
  when :return
    ret=parse_return
  when :break
    acceptIt
    ret=Break.new
  when :do
    ret=do_while()
  when :goto
    ret=parse_goto()
  when :ident
    case showNext(2).kind
    when :ident
      declaration
    when :colon
      parse_label
      statement
    else
      expression_statement
    end
  when :const,:volatile
    declaration
  when :semicolon
    expression_statement
  else
    show_line(showNext.pos)
    raise "unknown statement start at #{showNext.pos} .Got #{showNext.kind} #{showNext.val}"
  end
  dedent
  return ret
end

#switchObject



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/crokus/parser_only.rb', line 312

def switch
  indent "switch"
  expect :switch
  expect :lparen
  e=expression
  ret=Switch.new(e,cases=[])
  expect :rparen
  expect :lbrace
  while showNext.is_a? :case
    expect :case
    case_e=expression
    case_body=Body.new
    expect :colon
    while showNext.kind!=:rbrace and showNext.kind!=:case and showNext.kind!=:default
      case_body << statement()
    end
    cases << Case.new(case_e,case_body)
  end
  if showNext.is_a? :default
    acceptIt
    expect :colon
    while showNext.kind!=:rbrace
      statement()
    end
  end
  expect :rbrace
  dedent
  return ret
end

#type_qualifier?Boolean

Returns:

  • (Boolean)


478
479
480
481
482
483
484
485
486
487
488
# File 'lib/crokus/parser_only.rb', line 478

def type_qualifier?
  while showNext.is_a? STARTERS_TYPE_QUALIFIER
    case showNext.kind
    when :volatile
      acceptIt
    when :const
      acceptIt
    end
  end
  nil
end

#type_specifierObject



814
815
816
817
818
819
820
821
822
# File 'lib/crokus/parser_only.rb', line 814

def type_specifier
  indent "type_specifier #{showNext}"
  if showNext.is_a? STARTERS_TYPE_SPECIFIER
    acceptIt
  else
    raise "ERROR : type_specifier. Expecting one of '#{STARTERS_TYPE_SPECIFIER}' at #{showNext.pos}"
  end
  dedent
end

#typedefObject



180
181
182
183
184
185
186
187
# File 'lib/crokus/parser_only.rb', line 180

def typedef
  indent "typedef"
  expect :typedef
  type=parse_type()
  id=expect(:ident)
  dedent
  return Typedef.new(type,id)
end

#typenameObject



792
793
794
795
796
797
798
799
# File 'lib/crokus/parser_only.rb', line 792

def typename
  indent "typename"
  spec_qualifier_list
  while showNext.is_a? STARTERS_ABSTRACT_DECLARATOR
    abstract_decl
  end
  dedent
end

#unaryObject



870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
# File 'lib/crokus/parser_only.rb', line 870

def unary
  if STARTERS_PRIMARY.include? showNext.kind
    postfix
  elsif showNext.is_a? [:and,:mul,:add,:sub,:tilde,:not]
    acceptIt
    castexp
  else
    case showNext.kind
    when :inc_op
      acceptIt
      unary
    when :dec_op
      acceptIt
      unary
    when :sizeof
      sizeof()
    else
      raise "not an unary"
    end
  end
end