ridl 2.5.6 → 2.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,7 +8,6 @@
8
8
  # included with this program.
9
9
  #
10
10
  # Copyright (c) Remedy IT Expertise BV
11
- # Chamber of commerce Rotterdam nr.276339, The Netherlands
12
11
  #--------------------------------------------------------------------
13
12
  require 'racc/parser'
14
13
  require 'delegate'
@@ -44,7 +43,9 @@ module IDL
44
43
 
45
44
  class In
46
45
  def initialize(src, name = '', line = 0, column = 1)
47
- @src, @fwd, @bwd = src, src.getc, nil
46
+ @src = src
47
+ @fwd = src.getc # look ahead character
48
+ @bwd = nil # look back character
48
49
  @pos = Position.new(name, line, column)
49
50
  @mark = nil
50
51
  end
@@ -97,46 +98,24 @@ module IDL
97
98
  end
98
99
  alias skipc getc
99
100
 
100
- def _include?(ch, *chars)
101
- chars.each { |v|
102
- return true if case v
103
- when Array
104
- _include?(ch, *v)
105
- when Enumerable
106
- v.include? ch
107
- when Fixnum
108
- v == ch
109
- when String
110
- v == ch
111
- else
112
- false
113
- end
114
- }
115
- false
116
- end
117
-
118
101
  def skipwhile(*chars, &block)
119
- if block.nil?
120
- block = Proc.new { |ch| _include?(ch, *chars) }
121
- end
122
-
123
- until (ch = lookc).nil?
124
- break unless block.call(ch)
125
- skipc
102
+ if block
103
+ until (ch = lookc).nil?
104
+ return ch unless block.call(ch)
105
+ skipc
106
+ end
126
107
  end
127
- ch
108
+ nil
128
109
  end
129
110
 
130
111
  def skipuntil(*chars, &block)
131
- if block.nil?
132
- block = Proc.new { |ch| _include?(ch, *chars) }
133
- end
134
-
135
- until (ch = lookc).nil?
136
- break if block.call(ch)
137
- skipc
112
+ if block
113
+ until (ch = lookc).nil?
114
+ return ch if block.call(ch)
115
+ skipc
116
+ end
138
117
  end
139
- ch
118
+ nil
140
119
  end
141
120
 
142
121
  def mark(*ini)
@@ -159,6 +138,10 @@ module IDL
159
138
  @mark = nil
160
139
  ret
161
140
  end
141
+
142
+ def close
143
+ @src.close # close input source
144
+ end
162
145
  end ## of class In
163
146
 
164
147
  class StrIStream
@@ -249,7 +232,7 @@ module IDL
249
232
  when File
250
233
  i = src
251
234
  nm = src.path
252
- when IO
235
+ when IO, StringIO
253
236
  i = src
254
237
  nm = '<io>'
255
238
  else
@@ -261,7 +244,7 @@ module IDL
261
244
  end
262
245
  def find_include(fname, all = true)
263
246
  if File.file?(fname) && File.readable?(fname)
264
- fname
247
+ File.expand_path(fname)
265
248
  else
266
249
  # search transient include paths if allowed (quoted includes)
267
250
  fp = if all then
@@ -322,6 +305,9 @@ module IDL
322
305
  more_source? and @stack.last[0] == :define
323
306
  end
324
307
  def leave_source
308
+ # make sure to close the input source
309
+ @in.close
310
+ # check if we have any previous source still stacked up
325
311
  if @stack.size>0
326
312
  if @stack.last[0] == :include
327
313
  @xincludepaths.pop # remove directory of finished include
@@ -347,7 +333,8 @@ module IDL
347
333
  end
348
334
 
349
335
  LFCR = [ (?\n), (?\r) ]
350
- WHITESPACE = [ (?\ ), (?\t) ].concat(LFCR)
336
+ SPACES = [ (?\ ), (?\t) ]
337
+ WHITESPACE = SPACES+LFCR
351
338
 
352
339
  ANNOTATION = ?@
353
340
  ANNOTATION_STR = '@'
@@ -361,9 +348,16 @@ module IDL
361
348
 
362
349
  SHIFTCHARS = [ ?<, ?> ]
363
350
 
364
- HEXCHARS = [(?0..?9).to_a, (?a..?f).to_a, (?A..?F).to_a].flatten
351
+ DIGITS = (?0..?9).to_a
352
+ ALPHA_LC = (?a..?z).to_a
353
+ ALPHA_UC = (?A..?Z).to_a
354
+ OCTALS = (?0..?7).to_a
355
+ HEXCHARS = DIGITS + (?a..?f).to_a + (?A..?F).to_a
356
+ SIGNS = [?-, ?+]
357
+ DOT = ?.
365
358
 
366
- IDCHARS = [?_ , (?a..?z).to_a, (?A..?Z).to_a].flatten
359
+ IDCHARS = [?_ ] + ALPHA_LC + ALPHA_UC
360
+ FULL_IDCHARS = IDCHARS + DIGITS
367
361
 
368
362
  ESCTBL = CharRegistry.new({
369
363
  :n => ?\n, :t => ?\t, :v => ?\v, :b => ?\b,
@@ -432,8 +426,9 @@ module IDL
432
426
  annotation_body = nil
433
427
  # next token should be '(' in case of normal/single value annotation
434
428
  # or anything else in case of marker annotation
435
- token = next_token
436
- if token.first == '('
429
+ skip_spaces # skip till next non-space or eol
430
+ if peek_next == '('
431
+ token = next_token # parse '('
437
432
  begin
438
433
  # identifier or value (in case of single value annotation) expected
439
434
  token = next_token
@@ -454,9 +449,9 @@ module IDL
454
449
  end
455
450
  end
456
451
  end until token.first == ')'
457
- # token = next_token # need to get next token
458
- token = nil
452
+ token = next_token_before_eol
459
453
  else
454
+ token = next_token_before_eol
460
455
  # marker annotation or symbolic value; leave body nil
461
456
  end
462
457
  [token, annotation_body]
@@ -495,13 +490,18 @@ module IDL
495
490
  token
496
491
  end
497
492
 
493
+ def peek_next
494
+ @in.lookc
495
+ end
496
+
497
+ def skip_spaces
498
+ @in.skipwhile {|c| SPACES.include?(c) }
499
+ end
500
+
498
501
  def next_identifier(first = nil)
499
502
  @in.mark(first)
500
- while TRUE
501
- case @in.lookc
502
- when nil
503
- break
504
- when ?0..?9, ?a..?z, ?A..?Z, ?_
503
+ while true
504
+ if FULL_IDCHARS.include?(@in.lookc)
505
505
  @in.skipc
506
506
  else
507
507
  break
@@ -512,17 +512,13 @@ module IDL
512
512
  s2 = s0.dup # (to be) unescaped id
513
513
 
514
514
  # simple check
515
- escape = false
516
515
  if s2.length == 0
517
516
  parse_error 'identifier expected!'
518
517
  else
519
- case s2[0]
520
- when ?a..?z, ?A..?Z
521
- when ?_ ## if starts with CORBA IDL escape => remove
522
- escape = true
523
- s2.slice!(0)
518
+ if s2[0] == ?_
519
+ s2.slice!(0) ## if starts with CORBA IDL escape => remove
524
520
  else
525
- parse_error "identifier must begin with alphabet character: #{s2}"
521
+ parse_error "identifier must begin with alphabet character: #{s2}" unless ALPHA_LC.include?(s2[0]) || ALPHA_UC.include?(s2[0])
526
522
  end
527
523
  end
528
524
 
@@ -540,9 +536,8 @@ module IDL
540
536
  [ :identifier, Identifier.new(s2, s2, s0) ]
541
537
  end
542
538
  elsif (a = KEYWORDS.assoc(s1)).nil?
543
- # check for language mapping keyword except when
544
- # - this is an IDL escaped ('_' prefix) identifier
545
- [ :identifier, Identifier.new(s2, escape ? s2 : chk_identifier(s2), s0) ]
539
+ # check for language mapping keyword
540
+ [ :identifier, Identifier.new(s2, chk_identifier(s2), s0) ]
546
541
  elsif s0 == a[1]
547
542
  [ a[1], nil ]
548
543
  else
@@ -657,26 +652,26 @@ module IDL
657
652
  end
658
653
 
659
654
  def skipfloat_or_fixed
660
- if @in.lookc == ?.
655
+ if @in.lookc == DOT
661
656
  @in.skipc
662
- @in.skipwhile(?0..?9)
657
+ @in.skipwhile {|c| DIGITS.include?(c) }
663
658
  end
664
659
  if [?e, ?E].include? @in.lookc
665
660
  @in.skipc
666
- @in.skipc if [?+, ?-].include? @in.lookc
667
- @in.skipwhile(?0..?9)
661
+ @in.skipc if SIGNS.include? @in.lookc
662
+ @in.skipwhile {|c| DIGITS.include?(c) }
668
663
  return :floating_pt_literal
669
664
  elsif [?d, ?D].include? @in.lookc
670
665
  @in.skipc
671
- @in.skipc if [?+, ?-].include? @in.lookc
672
- @in.skipwhile(?0..?9)
666
+ @in.skipc if SIGNS.include? @in.lookc
667
+ @in.skipwhile {|c| DIGITS.include?(c) }
673
668
  return :fixed_pt_literal
674
669
  end
675
670
  :floating_pt_literal
676
671
  end
677
672
 
678
673
  def skipline
679
- while TRUE
674
+ while true
680
675
  s = @in.gets
681
676
  until s.chomp!.nil?; end
682
677
  break unless s[s.length - 1] == ?\\
@@ -685,13 +680,13 @@ module IDL
685
680
 
686
681
  def getline
687
682
  s = ''
688
- while TRUE
683
+ while true
689
684
  ch = @in.lookc
690
685
  break if ch.nil?
691
686
  case
692
687
  when (ch == ?\") #"
693
688
  s << @in.getc # opening quote
694
- while TRUE
689
+ while true
695
690
  if @in.lookc == ?\\
696
691
  # escape sequence
697
692
  s << @in.getc
@@ -779,7 +774,7 @@ module IDL
779
774
  end
780
775
 
781
776
  def eval_directive(s)
782
- IDL.log(2,"** RIDL - eval_directive(#{s})")
777
+ IDL.log(3,"** RIDL - eval_directive(#{s})")
783
778
  rc = eval(s)
784
779
  case rc
785
780
  when FalseClass, TrueClass
@@ -792,7 +787,7 @@ module IDL
792
787
  end
793
788
 
794
789
  def parse_directive
795
- @in.skipwhile(?\ , ?\t)
790
+ @in.skipwhile {|c| SPACES.include?(c) }
796
791
  s = getline
797
792
  /^(\w*)\s*/ === s
798
793
  s1,s2 = $1, $' #'
@@ -860,7 +855,7 @@ module IDL
860
855
  if do_parse?
861
856
  # match 'defined(Foo)' or 'defined Foo'
862
857
  while s2 =~ /(^|[\W])defined(\s*\(\s*(\w+)\s*\)|\s+(\w+))/
863
- IDL.log(2,"** RIDL - parse_directive : resolving 'defined(#{$3 || $4})'")
858
+ IDL.log(3,"** RIDL - parse_directive : resolving 'defined(#{$3 || $4})'")
864
859
  def_id = $3 || $4
865
860
  # resolve 'defined' expression to 'true' or 'false' according to actual macro definition
866
861
  s2.gsub!(/(^|[\W])(defined\s*[\s\(]\s*#{def_id}(\s*\))?)/, '\1'+"#{@defined.has_key?(def_id).to_s}")
@@ -936,10 +931,15 @@ module IDL
936
931
  end
937
932
  end
938
933
 
934
+ def next_token_before_eol
935
+ @in.skipwhile {|c| SPACES.include?(c)}
936
+ LFCR.include?(@in.lookc) ? nil : next_token
937
+ end
938
+
939
939
  def next_token
940
940
  sign = nil
941
941
  str = '' #initialize empty string
942
- while TRUE
942
+ while true
943
943
  ch = @in.getc
944
944
  if ch.nil?
945
945
  if @ifdef.size>0 and !in_expansion?
@@ -949,12 +949,12 @@ module IDL
949
949
  leave_source
950
950
  next
951
951
  else
952
- return [FALSE, nil]
952
+ return [false, nil]
953
953
  end
954
954
  end
955
955
 
956
956
  if WHITESPACE.include? ch
957
- @in.skipwhile( WHITESPACE )
957
+ @in.skipwhile {|c| WHITESPACE.include?(c) }
958
958
  next
959
959
  end
960
960
 
@@ -1017,7 +1017,7 @@ module IDL
1017
1017
  ret = []
1018
1018
  chs = ''
1019
1019
  @in.skipc # skip 'L'
1020
- while TRUE
1020
+ while true
1021
1021
  _nxtc = @in.lookc
1022
1022
  if _nxtc == ?\\
1023
1023
  @in.skipc
@@ -1067,7 +1067,7 @@ module IDL
1067
1067
  # return token returned by parse_annotation or parse next token recursively
1068
1068
  return parse_annotation(true) || next_token
1069
1069
  else
1070
- @in.skipuntil(?\n, ?\r)
1070
+ @in.skipuntil {|c| LFCR.include?(c) }
1071
1071
  end
1072
1072
  end
1073
1073
  str = '' # reset
@@ -1077,9 +1077,9 @@ module IDL
1077
1077
  return %w(/ /)
1078
1078
  end
1079
1079
 
1080
- when ch == ?+ || ch == ?-
1080
+ when SIGNS.include?(ch)
1081
1081
  _nxtc = @in.lookc
1082
- if (?0..?9).include? _nxtc
1082
+ if DIGITS.include? _nxtc
1083
1083
  sign = ch
1084
1084
  str = '' # reset
1085
1085
  next
@@ -1089,7 +1089,7 @@ module IDL
1089
1089
 
1090
1090
  when (?1..?9).include?(ch)
1091
1091
  @in.mark(sign, ch)
1092
- @in.skipwhile(?0..?9)
1092
+ @in.skipwhile {|c| DIGITS.include?(c) }
1093
1093
  num_type = ([?., ?e, ?E, ?d, ?D].include?(@in.lookc)) ? skipfloat_or_fixed : :integer_literal
1094
1094
 
1095
1095
  r = @in.getregion
@@ -1102,10 +1102,10 @@ module IDL
1102
1102
  return [:integer_literal, r.to_i]
1103
1103
  end
1104
1104
 
1105
- when ch == ?. #
1105
+ when ch == DOT #
1106
1106
  @in.mark(ch)
1107
- @in.skipwhile(?0..?9)
1108
- num_type = (?. != @in.lookc) ? skipfloat_or_fixed : nil
1107
+ @in.skipwhile {|c| DIGITS.include?(c) }
1108
+ num_type = (DOT != @in.lookc) ? skipfloat_or_fixed : nil
1109
1109
  s = @in.getregion
1110
1110
  if s == '.'
1111
1111
  parse_error 'token consisting of single dot (.) is invalid.'
@@ -1128,11 +1128,11 @@ module IDL
1128
1128
  s = @in.getregion
1129
1129
  return [:integer_literal, s.hex]
1130
1130
  else
1131
- dec = FALSE
1132
- @in.skipwhile(?0..?7)
1131
+ dec = false
1132
+ @in.skipwhile {|c| OCTALS.include?(c) }
1133
1133
  if (?8..?9).include? @in.lookc
1134
1134
  dec = TRUE
1135
- @in.skipwhile(?0..?9)
1135
+ @in.skipwhile {|c| DIGITS.include?(c) }
1136
1136
  end
1137
1137
 
1138
1138
  num_type = ([?., ?e, ?E, ?d, ?D].include?(@in.lookc)) ? skipfloat_or_fixed : :integer_literal
@@ -1170,7 +1170,7 @@ module IDL
1170
1170
 
1171
1171
  when ch == ?\" #" #double quote, for a string literal.
1172
1172
  ret = ''
1173
- while TRUE
1173
+ while true
1174
1174
  _nxtc = @in.lookc
1175
1175
  if _nxtc == ?\\
1176
1176
  @in.skipc
@@ -8,7 +8,6 @@
8
8
  # included with this program.
9
9
  #
10
10
  # Copyright (c) Remedy IT Expertise BV
11
- # Chamber of commerce Rotterdam nr.276339, The Netherlands
12
11
  #--------------------------------------------------------------------
13
12
  module IDL
14
13
  class Type
@@ -42,6 +41,9 @@ module IDL
42
41
  def is_template?
43
42
  false
44
43
  end
44
+ def matches?(idltype)
45
+ self.class == idltype.class
46
+ end
45
47
 
46
48
  def instantiate(_context)
47
49
  self
@@ -74,6 +76,9 @@ module IDL
74
76
  def resolved_node
75
77
  @node
76
78
  end
79
+ def matches?(idltype)
80
+ super && self.resolved_node == idltype.resolved_node
81
+ end
77
82
  end
78
83
 
79
84
  class ScopedName < NodeType
@@ -259,6 +264,9 @@ module IDL
259
264
  def is_template?
260
265
  (@size && @size.is_a?(IDL::Expression) && @size.is_template?)
261
266
  end
267
+ def matches?(idltype)
268
+ super && self.size == idltype.size
269
+ end
262
270
  def instantiate(_context)
263
271
  self.is_template? ? (Type::String.new(@size.instantiate(_context).value)) : self
264
272
  end
@@ -301,8 +309,11 @@ module IDL
301
309
  def is_template?
302
310
  (@size && @size.is_a?(IDL::Expression::ScopedName) && @size.node.is_a?(IDL::AST::TemplateParam)) || @basetype.is_template?
303
311
  end
312
+ def matches?(idltype)
313
+ super && self.size == idltype.size && self.basetype.resolved_type.matches?(idltype.basetype.resolved_type)
314
+ end
304
315
  def instantiate(_context)
305
- self.is_template? ? Type::Sequence.new(@basetype.instantiate(_context), @size.instantiate(_context).value) : self
316
+ self.is_template? ? Type::Sequence.new(@basetype.instantiate(_context), @size ? @size.instantiate(_context).value : nil) : self
306
317
  end
307
318
  end
308
319
 
@@ -338,6 +349,9 @@ module IDL
338
349
  def is_template?
339
350
  @sizes.any? { |sz| (sz.is_a?(IDL::Expression::ScopedName) && sz.node.is_a?(IDL::AST::TemplateParam)) } || @basetype.is_template?
340
351
  end
352
+ def matches?(idltype)
353
+ super && self.sizes == idltype.sizes && self.basetype.resolved_type.matches?(idltype.basetype.resolved_type)
354
+ end
341
355
  def instantiate(_context)
342
356
  self.is_template? ? Type::Array.new(@basetype.instantiate(_context), @sizes.collect { |sz| sz.instantiate(_context).value }) : self
343
357
  end
@@ -366,6 +380,9 @@ module IDL
366
380
  def is_template?
367
381
  (@size && @size.is_a?(IDL::Expression::ScopedName) && @size.node.is_a?(IDL::AST::TemplateParam))
368
382
  end
383
+ def matches?(idltype)
384
+ super && self.size == idltype.size
385
+ end
369
386
  def instantiate(_context)
370
387
  self.is_template? ? Type::WString.new(@size.instantiate(_context).value) : self
371
388
  end
@@ -501,6 +518,9 @@ module IDL
501
518
  def resolved_node
502
519
  @type.resolved_node
503
520
  end
521
+ def matches?(idltype)
522
+ super && self.type.resolved_type.matches?(idltype.type.resolved_type)
523
+ end
504
524
  end
505
525
 
506
526
  end