Class: IDL::Scanner

Inherits:
Object
  • Object
show all
Defined in:
lib/ridl/scanner.rb

Defined Under Namespace

Classes: CharRegistry, Identifier, In, Position, StrIStream, TokenRegistry

Constant Summary collapse

LFCR =
[ (?\n), (?\r) ]
WHITESPACE =
[ (?\ ), (?\t) ].concat(LFCR)
ANNOTATION =
?@
ANNOTATION_STR =
'@'
BREAKCHARS =
[
?(, ?), ?[, ?], ?{, ?},
?^, ?~,
?*, ?%, ?&, ?|,
?<, ?=, ?>,
?,, ?; ]
SHIFTCHARS =
[ ?<, ?> ]
HEXCHARS =
[(?0..?9).to_a, (?a..?f).to_a, (?A..?F).to_a].flatten
IDCHARS =
[?_ , (?a..?z).to_a, (?A..?Z).to_a].flatten
ESCTBL =
CharRegistry.new({
  :n => ?\n, :t => ?\t, :v => ?\v, :b => ?\b,
  :r => ?\r, :f => ?\f, :a => ?\a
})
KEYWORDS =
%w(
  abstract alias any attribute boolean case char component connector const consumes context custom default double
  exception emits enum eventtype factory FALSE finder fixed float getraises home import in inout interface local
  long manages mirrorport module multiple native Object octet oneway out port porttype primarykey private provides
  public publishes raises readonly setraises sequence short string struct supports switch TRUE truncatable typedef
  typeid typename typeprefix unsigned union uses ValueBase valuetype void wchar wstring
).inject(TokenRegistry.new) { |h,a| h[a.downcase.to_sym] = a; h }
LITERALS =
[
:integer_literal,
:string_literal,
# :wide_string_literal,
:character_literal,
# :wide_character_literal,
:fixed_pt_literal,
:floating_pt_literal,
:boolean_literal ]
BOOL_LITERALS =
{
  'false' => false,
  'true' => true
}

Instance Method Summary collapse

Constructor Details

#initialize(src, directiver, params = {}) ⇒ Scanner

Scanner



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/ridl/scanner.rb', line 223

def initialize(src, directiver, params = {})
  @includepaths = params[:includepaths] || []
  @stack = []
  @expansions = []
  @prefix = nil
  @directiver = directiver
  @defined = TokenRegistry.new
  # initialize with predefined macros
  if params[:macros]
    params[:macros].each do |(name, value)|
      @defined[name] = value
    end
  end
  @ifdef = Array.new
  @ifskip = false
  @ifnest = 0
  i = nil
  nm = ''
  case src
  when String
    i = StrIStream.new(src)
    nm = '<string>'
  when File
    i = src
    nm = src.path
  when IO
    i = src
    nm = '<io>'
  else
    parse_error "illegal type for input source: #{src.class} "
  end
  @in = In.new(i, nm)
  @scan_comment = false # true if parsing commented annotation
  @in_annotation = false # true if parsing annotation
end

Instance Method Details

#do_parse?Boolean

Returns:

  • (Boolean)


312
313
314
# File 'lib/ridl/scanner.rb', line 312

def do_parse?
  @ifdef.empty? || @ifdef.last
end

#enter_expansion(src, define) ⇒ Object



286
287
288
289
290
# File 'lib/ridl/scanner.rb', line 286

def enter_expansion(src, define)
  @stack << [:define, nil, nil, @in, nil]
  @expansions << define
  @in = In.new(StrIStream.new(src), @in.position.name, @in.position.line, @in.position.column)
end

#enter_include(src) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/ridl/scanner.rb', line 270

def enter_include(src)
  if @directiver.is_included?(src)
    @directiver.declare_include(src)
  else
    fpath = find_include(src)
    if fpath.nil?
      parse_error "Cannot open include file '#{src}'"
    end
    @stack << [:include, @prefix, @ifdef, @in, @ifskip]
    @prefix = nil
    @ifdef = Array.new
    @in = In.new(File.open(fpath, 'r'), fpath)
    @directiver.enter_include(src)
    @directiver.pragma_prefix(nil)
  end
end

#eval_directive(s) ⇒ Object



750
751
752
753
754
755
756
757
758
759
760
761
# File 'lib/ridl/scanner.rb', line 750

def eval_directive(s)
  IDL.log(2,"** RIDL - eval_directive(#{s})")
  rc = eval(s)
  case rc
  when FalseClass, TrueClass
    rc
  when Numeric
    rc != 0
  else
    parse_error "invalid preprocessor expression."
  end
end

#extract_annotationObject



405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/ridl/scanner.rb', line 405

def extract_annotation()
  annotation_body = nil
  # next token should be '(' in case of normal/single value annotation
  # or anything else in case of marker annotation
  token = next_token
  if token.first == '('
    begin
      # identifier or value (in case of single value annotation) expected
      token = next_token
      if token.first == ')' # marker annotation; leave body empty
        annotation_body = { }
      else
        parse_error 'annotation member expected!' unless token.first == :identifier || is_literal?(token.first)
        s1 = token.last
        token = next_token # ')'  (in case of single value annotation) or '='
        if token.first == ')'
          parse_error 'invalid annotation member' if annotation_body
          annotation_body = { :value => s1 }
        else
          parse_error 'invalid annotation member' unless token.first == '='
          token, annotation_value = extract_annotation_value()
          parse_error 'invalid annotation body' unless token.first == ',' || token.first == ')'
          (annotation_body ||= {})[s1] = annotation_value
        end
      end
    end until token.first == ')'
    token = next_token # need to get next token
  else
    # marker annotation or symbolic value; leave body nil
  end
  return [token, annotation_body]
end

#extract_annotation_valueObject



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/ridl/scanner.rb', line 375

def extract_annotation_value()
  annotation_value = nil
  token = next_token # needs '{' (array) or literal or identifier (which means nested annotation object or enum value)
  if token.first == '{'
    # extract array of values (literals or identifiers) separated by ','
    annotation_value = []
    begin
      token, ann_value = extract_annotation_value()
      parse_error 'invalid annotation value array' unless token.first == ',' || token.first == '}'
      annotation_value << ann_value
    end until token.first == '}'
    token = next_token
  elsif token.first == :identifier
    member_annotation_id = token.last
    # get nested body
    token, member_annotation_body = extract_annotation()
    # determin vaue type; if it has a body it is an annotation instance
    if member_annotation_body
      annotation_value = { member_annotation_id => member_annotation_body }
    else  # otherwise it is a symbolic value
      annotation_value = member_annotation_id.to_sym
    end
  else
    parse_error 'invalid annotation member' unless is_literal?(token.first)
    annotation_value = token.last
    token = next_token
  end
  return [token, annotation_value]
end

#find_include(fname) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/ridl/scanner.rb', line 258

def find_include(fname)
  path = if File.file?(fname) && File.readable?(fname)
    fname
  else
    fp = @includepaths.find do |p|
      f = p + "/" + fname
      File.file?(f) && File.readable?(f)
    end
    fp += '/' + fname if !fp.nil?
    fp
  end
end

#getlineObject



655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
# File 'lib/ridl/scanner.rb', line 655

def getline
  s = ""
  while TRUE
    ch = @in.lookc
    break if ch.nil?
    case
    when (ch == ?\") #"
      s << @in.getc # opening quote
      while TRUE
        if @in.lookc == ?\\
          # escape sequence
          s << @in.getc
          esctyp, escstr = next_escape_str(true)
          s << escstr
        elsif @in.lookc == ?\" #"
          break
        elsif @in.lookc
          # normal character
          s << @in.getc
        else
          parse_error "unterminated string literal"
        end
      end
      s << @in.getc # closing quote
    when (ch == ?\') #' # quoted character
      s << @in.getc # opening quote
      if @in.lookc == ?\\
        # escape sequence
        s << @in.getc
        esctyp, escstr = next_escape_str(true)
        s << escstr
      elsif @in.lookc && @in.lookc != ?\' #'
        # normal character
        s << @in.getc
      end
      if @in.lookc != ?\' #'
        parse_error "character literal must be single character enclosed in \"'\""
      end
      s << @in.getc # closing quote
    when LFCR.include?(ch)
      @in.skipwhile() { |ch_| LFCR.include? ch_ }
      break
    when ch == ?/
      @in.skipc
      if @in.lookc == ?/
        # //-style comment; skip till eol
        @in.gets
        break
      elsif @in.lookc == ?*
        # /*...*/ style comment; skip comment
        ch1 = nil
        @in.skipuntil { |ch_|
          ch0 = ch1; ch1 = ch_
          ch0 == ?* and ch1 == ?/ #
        }
        if @in.lookc.nil?
          parse_error "cannot find comment closing brace (\'*/\'). "
        end
        @in.skipc
      else
        s << ch
      end
    when ch == ?\\
      @in.skipc
      if LFCR.include?(@in.lookc)
        # line continuation
        @in.skipwhile() { |ch_| LFCR.include? ch_ }
        if @in.lookc.nil?
          parse_error "line continuation character ('\\') not allowed as last character in file."
        end
      else
        s << ch
      end
    else
      @in.skipc
      s << ch
    end
  end
  s
end

#in_expansion?Boolean

Returns:

  • (Boolean)


297
298
299
# File 'lib/ridl/scanner.rb', line 297

def in_expansion?
  more_source? and @stack.last[0] == :define
end

#is_expanded?(define) ⇒ Boolean

Returns:

  • (Boolean)


291
292
293
# File 'lib/ridl/scanner.rb', line 291

def is_expanded?(define)
  @expansions.include?(define)
end

#is_literal?(o) ⇒ Boolean

Returns:

  • (Boolean)


371
372
373
# File 'lib/ridl/scanner.rb', line 371

def is_literal?(o)
  return LITERALS.include?(o)
end

#leave_sourceObject



300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/ridl/scanner.rb', line 300

def leave_source()
  if @stack.size>0
    if @stack.last[0] == :include
      @directiver.leave_include
      type, @prefix, @ifdef, @in, @ifskip = @stack.pop
      @directiver.pragma_prefix(@prefix)
    else
      type, prefix_, ifdef_, @in, elsif_ = @stack.pop
      @expansions.pop
    end
  end
end

#more_source?Boolean

Returns:

  • (Boolean)


294
295
296
# File 'lib/ridl/scanner.rb', line 294

def more_source?
  @stack.size>0
end

#next_escapeObject



522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
# File 'lib/ridl/scanner.rb', line 522

def next_escape
  ret = 0
  case (ch = @in.getc)
  when nil
    parse_error 'illegal escape sequence'
  when ?0..?7
    ret = ""
    ret << ch
    1.upto(2) {
      ch = @in.lookc
      if (?0..?7).include? ch
        ret << ch
      else
        break
      end
      @in.skipc
    }
    ret = ret.oct
  when ?x # i'm not sure '\x' should be 0 or 'x'. currently returns 0.
    ret = ""
    1.upto(2) {
      ch = @in.lookc
      if HEXCHARS.include? ch
        ret << ch
      else
        break
      end
      @in.skipc
    }
    ret = ret.hex
  when ?u
    ret = ""
    1.upto(4) {
      ch = @in.lookc
      if HEXCHARS.include? ch
        ret << ch
      else
        break
      end
      @in.skipc
    }
    ret = ret.hex
  when ?n, ?t, ?v, ?b, ?r, ?f, ?a
    ret = ESCTBL[ch]
  else
    ret = ('' << ch).unpack('C').first
  end
  return ret
end

#next_escape_str(keep_type_ch = false) ⇒ Object



572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
# File 'lib/ridl/scanner.rb', line 572

def next_escape_str(keep_type_ch = false)
  ret = 0
  case (ch = @in.getc)
  when nil
    parse_error 'illegal escape sequence'
  when ?0..?7
    ret = ""
    ret << ch
    1.upto(2) {
      ch = @in.lookc
      if (?0..?7).include? ch
        ret << ch
      else
        break
      end
      @in.skipc
    }
    ret = [ :oct, ret ]
  when ?x # i'm not sure '\x' should be 0 or 'x'. currently returns 0.
    ret = ""
    ret << ch if keep_type_ch
    1.upto(2) {
      ch = @in.lookc
      if HEXCHARS.include? ch
        ret << ch
      else
        break
      end
      @in.skipc
    }
    ret = [ :hex2, ret ]
  when ?u
    ret = ""
    ret << ch if keep_type_ch
    1.upto(4) {
      ch = @in.lookc
      if HEXCHARS.include? ch
        ret << ch
      else
        break
      end
      @in.skipc
    }
    ret = [ :hex4, ret ]
  when ?n, ?t, ?v, ?b, ?r, ?f, ?a
    ret = ''
    ret << ch
    ret = [ :esc, ret ]
  else
    ret = ''
    ret << ch
    ret = [ :esc_ch, ch ]
  end
  return ret
end

#next_identifier(first = nil) ⇒ Object



470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# File 'lib/ridl/scanner.rb', line 470

def next_identifier(first = nil)
  @in.mark(first)
  while TRUE
    case @in.lookc
    when nil
      break
    when ?0..?9, ?a..?z, ?A..?Z, ?_
      @in.skipc
    else
      break
    end
  end
  s0 = @in.getregion
  s1 = s0.downcase

  # simple check
  if (s0.length == 0)
    parse_error "identifier expected!"
  else
    case s0[0]
    when ?a..?z, ?A..?Z
    when ?_   ## if starts with CORBA IDL escape => remove
      s0.slice!(0)
    else
      parse_error "identifier must begin with alphabet character: #{s0}"
    end
  end

  # preprocessor check
  if @defined.has_key?(s0) and !is_expanded?(s0)
    # enter expansion as new source
    enter_expansion(@defined[s0], s0)
    # call next_token to parse expanded source
    next_token
  # keyword check
  elsif @in_annotation
    if BOOL_LITERALS.has_key?(s1)
      [ :boolean_literal, BOOL_LITERALS[s1] ]
    else
      [ :identifier, s0 ]
    end
  elsif (a = KEYWORDS.assoc(s1)).nil?
    # check for language mapping keyword except when
    # - this is an IDL escaped ('_' prefix) identifier
    [ :identifier, Identifier.new(s0, s1[0] == ?_ ? s0 : chk_identifier(s0)) ]
  elsif s0 == a[1]
    [ a[1], nil ]
  else
    parse_error "`#{s0}' collides with a keyword `#{a[1]}'"
  end
end

#next_tokenObject



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
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
# File 'lib/ridl/scanner.rb', line 908

def next_token
  sign = nil
  str = "" #initialize empty string
  while TRUE
    ch = @in.getc
    if ch.nil?
      if @ifdef.size>0 and !in_expansion?
        parse_error "mismatched #if/#endif"
      end
      if more_source?
        leave_source
        next
      else
        return [FALSE, nil]
      end
    end

    if WHITESPACE.include? ch
      @in.skipwhile( WHITESPACE )
      next
    end

    if str.empty? && ch == ?\#
      parse_directive
      next
    end
    unless do_parse?
      skipline
      next
    end

    str << ch
    case
    when BREAKCHARS.include?(ch)
      if SHIFTCHARS.include?(ch) && @in.lookc == ch
        # '<<' or '>>'
        str << @in.getc
      end
      return [str, str]

    when ch == ANNOTATION
      if @in_annotation
        return [str, str]
      else
        return parse_annotation()
      end

    when ch == ?: #
      if @in.lookc == ?: #
        @in.skipc
        return ["::", "::"]
      else
        return [":", ":"]
      end

    when ch == ?L
      _nxtc = @in.lookc
      if _nxtc == ?\'  #' #single quote, for a character literal.
        ret = 0
        @in.skipc # skip 'L'
        _nxtc = @in.lookc
        if _nxtc == ?\\
          @in.skipc
          ret = next_escape_str
        elsif _nxtc == ?\' #'
          ret = [ nil, nil ]
        else
          ret = ''
          ret << @in.getc
          ret = [ :char, ret ]
        end

        if @in.lookc != ?\' #'
          parse_error "wide character literal must be single wide character enclosed in \"'\""
        end

        @in.skipc
        return [ :wide_character_literal, ret ]

      elsif _nxtc == ?\" #" #double quote, for a string literal.
        ret = []
        chs = ''
        @in.skipc # skip 'L'
        while TRUE
          _nxtc = @in.lookc
          if _nxtc == ?\\
            @in.skipc
            ret << [:char, chs] unless chs.empty?
            chs = ''
            ret << next_escape_str
          elsif _nxtc == ?\" #"
            @in.skipc
            ret << [:char, chs] unless chs.empty?
            return [ :wide_string_literal, ret ]
          else
            chs << @in.getc
          end
        end

      else
        return next_identifier(ch)
      end

    when IDCHARS.include?(ch)
      return next_identifier(ch)

    when ch == ?/ #
      _nxtc = @in.lookc
      if _nxtc == ?*
        # skip comment like a `/* ... */'
        @in.skipc # forward stream beyond `/*'
        ch1 = nil
        @in.skipuntil { |ch_|
          ch0 = ch1; ch1 = ch_
          ch0 == ?* and ch1 == ?/ #
        }
        if @in.lookc.nil?
          parse_error "cannot find comment closing brace (\'*/\'). "
        end
        @in.skipc
        str = "" # reset
        next

      elsif _nxtc == ?/
        # skip comment like a `// ...\n'
        @in.skipc
        unless @scan_comment  # scan_comment will be true when parsing commented annotations
          _nxtc = @in.lookc
          if _nxtc == ANNOTATION
            @in.skipc
            return parse_annotation(true)
          else
            @in.skipuntil(?\n, ?\r)
          end
        end
        str = "" # reset
        next

      else
        return [ "/", "/" ]
      end

    when ch == ?+ || ch == ?-
      _nxtc = @in.lookc
      if (?0..?9).include? _nxtc
        sign = ch
        str = "" # reset
        next
      else
        return [str, str]
      end

    when (?1..?9).include?(ch)
      @in.mark(sign, ch)
      sign = nil
      @in.skipwhile(?0..?9)
      num_type = ([?., ?e, ?E, ?d, ?D].include?(@in.lookc)) ? skipfloat_or_fixed : :integer_literal

      r = @in.getregion

      if num_type == :floating_pt_literal
        return [:floating_pt_literal, r.to_f]
      elsif num_type == :fixed_pt_literal
        return [:fixed_pt_literal, r]
      else
        return [:integer_literal, r.to_i]
      end

    when ch == ?. #
      @in.mark(ch)
      @in.skipwhile(?0..?9)
      num_type = (?. != @in.lookc) ? skipfloat_or_fixed : nil
      s = @in.getregion
      if s == "."
        parse_error "token consisting of single dot (.) is invalid."
      end
      if num_type == :floating_pt_literal
        return [:floating_pt_literal, s.to_f]
      elsif num_type == :fixed_pt_literal
        return [:fixed_pt_literal, s]
      else
        parse_error "invalid floating point constant."
      end

    when ch == ?0
      @in.mark(sign, ch)
      sign = nil

      _nxtc = @in.lookc
      if _nxtc == ?x || _nxtc == ?X
        @in.skipc
        @in.skipwhile() { |ch_| HEXCHARS.include? ch_ }
        s = @in.getregion
        return [:integer_literal, s.hex]

      else
        dec = FALSE
        @in.skipwhile(?0..?7)
        if (?8..?9).include? @in.lookc
          dec = TRUE
          @in.skipwhile(?0..?9)
        end

        num_type = ([?., ?e, ?E, ?d, ?D].include?(@in.lookc)) ? skipfloat_or_fixed : :integer_literal

        ret = nil
        s = @in.getregion
        if num_type == :floating_pt_literal
          ret = [:floating_pt_literal, s.to_f]
        elsif num_type == :fixed_pt_literal
          ret = [:fixed_pt_literal, s]
        elsif dec
          parse_error "decimal literal starting with '0' should be octal ('0'..'7' only): #{s}"
        else
          ret = [:integer_literal, s.oct]
        end
        return ret
      end

    when ch == ?\'  #' #single quote, for a character literal.
      ret = 0
      _nxtc = @in.lookc
      if _nxtc == ?\\
        @in.skipc
        ret = next_escape
      elsif _nxtc == ?\' #'
        ret = 0
      elsif _nxtc
        ret = ('' << @in.getc).unpack('C').first
      end

      if @in.lookc != ?\' #'
        parse_error "character literal must be single character enclosed in \"'\""
      end

      @in.skipc
      return [ :character_literal, ret ]

    when ch == ?\" #" #double quote, for a string literal.
      ret = ""
      while TRUE
        _nxtc = @in.lookc
        if _nxtc == ?\\
          @in.skipc
          ret << next_escape
        elsif _nxtc == ?\" #"
          @in.skipc
          return [ :string_literal, ret ]
        elsif _nxtc
          ret << @in.getc
        else
          parse_error "unterminated string literal"
        end
      end

    else
      parse_error 'illegal character [' << ch << ']'

    end #of case

  end #of while
  parse_error "unexcepted error"
end

#parse_annotation(in_comment = false) ⇒ Object



438
439
440
441
442
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
# File 'lib/ridl/scanner.rb', line 438

def parse_annotation(in_comment = false)
  @in_annotation = true
  @scan_comment = in_comment
  begin
    # parse (possibly multiple) annotation(s)
    begin
      # next token should be identifier (must be on same line following '@')
      token = next_token
      parse_error 'annotation identifier expected!' unless token.first == :identifier
      annotation_id = token.last
      token, annotation_body = extract_annotation()
      # pass annotation to directiver for processing
      @directiver.define_annotation(annotation_id, annotation_body || {})
    end until token.first != ANNOTATION_STR
  ensure
    @in_annotation = false
    @scan_comment = false
  end
  # check identifier for keywords
  if token.first == :identifier
    # keyword check
    if (a = KEYWORDS.assoc(token.last)).nil?
      token = [ :identifier, Identifier.new(token.last, chk_identifier(token.last)) ]
    elsif token.last == a[1]
      token = [ a[1], nil ]
    else
      parse_error "`#{token.last}' collides with a keyword `#{a[1]}'"
    end
  end
  return token
end

#parse_directiveObject



763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
# File 'lib/ridl/scanner.rb', line 763

def parse_directive
  @in.skipwhile(?\ , ?\t)
  s = getline
  /^(\w*)\s*/ === s
  s1,s2 = $1, $' #'

  if /(else|endif|elif)/ === s1

    if @ifdef.empty?
      parse_error "#else/#elif/#endif must not appear without preceding #if"
    end
    case s1
    when 'else'
      if @ifnest == 0
        if @ifskip # true branch has already been parsed
          @ifdef[@ifdef.size - 1] = false
        else
          @ifdef[@ifdef.size - 1] ^= true;
          @ifskip = @ifdef.last
        end
      end
    when 'endif'
      if @ifnest == 0
        @ifdef.pop
        @ifskip = @ifdef.last
      else
        @ifnest -= 1
      end
    else
      if @ifnest == 0
        if @ifskip || @ifdef[@ifdef.size - 1]
          # true branch has already been parsed so skip from now on
          @ifdef[@ifdef.size - 1] = false
          @ifskip = true
        else
          while s2 =~ /(^|[\W])defined\s*\(\s*(\w+)\s*\)/
             def_id = $2
             s2.gsub!(/(^|[\W])(defined\s*\(\s*\w+\s*\))/, '\1'+"#{@defined.has_key?(def_id).to_s}")
          end
          s2.gsub!(/(^|[\W])([A-Za-z_][\w]*)/) do |m_| "#{$1}#{resolve_define($2)}" end
          begin
            @ifdef[@ifdef.size - 1] = eval_directive(s2)
            @ifskip = @ifdef[@ifdef.size - 1]
          rescue IDL::ParseError
            raise
          rescue => ex
            p ex
            puts ex.backtrace.join("\n")
            parse_error "error evaluating #elif"
          end
        end
      end
    end

  elsif /(if|ifn?def)/ === s1

    case s1
    when /ifn?def/
      if do_parse?
        if not (/^(\w+)/ === s2)
          parse_error "no #if(n)def target."
        end
        @ifdef.push(@defined[$1].nil? ^ (s1 == "ifdef"))
        @ifskip = @ifdef.last
      else
        @ifnest += 1
      end

    when 'if'
      if do_parse?
        while s2 =~ /(^|[\W])defined\s*\(\s*(\w+)\s*\)/
           def_id = $2
           s2.gsub!(/(^|[\W])(defined\s*\(\s*\w+\s*\))/, '\1'+"#{@defined.has_key?(def_id).to_s}")
        end
        s2.gsub!(/(^|[\W])([A-Za-z_][\w]*)/) do |m_| "#{$1}#{resolve_define($2)}" end
        begin
          @ifdef.push(eval_directive(s2))
          @ifskip = @ifdef.last
        rescue IDL::ParseError
          raise
        rescue => ex
          p ex
          puts ex.backtrace.join("\n")
          parse_error "error evaluating #if"
        end
      else
        @ifnest += 1
      end
    end

  elsif do_parse?

    case s1
    when 'pragma'
      parse_pragma(s2)

    when 'error'
      parse_error(s2)

    when 'define'
      a = s2.split
      a[1] = true if a[1].nil?
      if a[0].nil?
        parse_error "no #define target."
      elsif not @defined[a[0]].nil?
        parse_error "#{a[0]} is already #define-d."
      end
      @defined[a[0]] = a[1]

    when 'undef'
      @defined.delete(s2)

    when 'include'
      if s2[0,1] == '"' || s2[0,1] == '<'
        if s2.size>2
          s2.strip!
          s2 = s2.slice(1..(s2.size-2))
        else
          s2 = ""
        end
      end
      enter_include(s2)

    when /[0-9]+/
      # ignore line directive
    else
      parse_error "unknown directive: #{s}."
    end
  end
end

#parse_error(msg, ex = nil) ⇒ Object



318
319
320
321
322
# File 'lib/ridl/scanner.rb', line 318

def parse_error(msg, ex = nil)
  e = IDL::ParseError.new(msg, positions)
  e.set_backtrace(ex.backtrace) unless ex.nil?
  raise e
end

#parse_pragma(s) ⇒ Object



894
895
896
897
898
899
900
901
902
903
904
905
906
# File 'lib/ridl/scanner.rb', line 894

def parse_pragma(s)
  case s
  when /^ID\s+(.*)\s+"(.*)"\s*$/
    @directiver.pragma_id($1.strip, $2)
  when /^version\s+(.*)\s+([0-9]+)\.([0-9]+)\s*$/
    @directiver.pragma_version($1.strip, $2, $3)
  when /^prefix\s+"(.*)"\s*$/
    @prefix = $1
    @directiver.pragma_prefix(@prefix)
  else
    @directiver.handle_pragma(s)
  end
end

#positionsObject



315
316
317
# File 'lib/ridl/scanner.rb', line 315

def positions
  @stack.reverse.inject(@in.nil? ? [] : [@in.position]) {|pos_arr,(type_,pfx_,ifdef_,in_,elsif_)| pos_arr << in_.position }
end

#resolve_define(id, stack = []) ⇒ Object



736
737
738
739
740
741
742
743
744
745
746
747
748
# File 'lib/ridl/scanner.rb', line 736

def resolve_define(id, stack = [])
  return id if ['true', 'false'].include?(id)
  IDL.log(3,"*** RIDL - resolve_define(#{id})")
  if @defined.has_key?(id)
    define_ = @defined[id]
    stack << id
    parse_error("circular macro reference detected for [#{define_}]") if stack.include?(define_)
    # resolve any nested macro definitions
    define_.gsub(/(^|[\W])([A-Za-z_][\w]*)/) do |m_| "#{$1}#{resolve_define($2, stack)}" end
  else
    '0' # unknown id
  end
end

#skipfloat_or_fixedObject



628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
# File 'lib/ridl/scanner.rb', line 628

def skipfloat_or_fixed
  if (@in.lookc == ?.)
    @in.skipc
    @in.skipwhile(?0..?9)
  end
  if [?e, ?E].include? @in.lookc
    @in.skipc
    @in.skipc if [?+, ?-].include? @in.lookc
    @in.skipwhile(?0..?9)
    return :floating_pt_literal
  elsif [?d, ?D].include? @in.lookc
    @in.skipc
    @in.skipc if [?+, ?-].include? @in.lookc
    @in.skipwhile(?0..?9)
    return :fixed_pt_literal
  end
  return :floating_pt_literal
end

#skiplineObject



647
648
649
650
651
652
653
# File 'lib/ridl/scanner.rb', line 647

def skipline
  while TRUE
    s = @in.gets
    until s.chomp!.nil?; end
    break unless s[s.length - 1] == ?\\
  end
end