Class: IDL::Scanner
- Inherits:
-
Object
- Object
- IDL::Scanner
- Defined in:
- lib/ridl/scanner.rb
Defined Under Namespace
Classes: CharRegistry, Identifier, In, Position, StrIStream, TokenRegistry
Constant Summary collapse
- LFCR =
[ ("\n"), ("\r") ]
- SPACES =
[ ("\ "), ("\t") ]
- WHITESPACE =
SPACES + LFCR
- ANNOTATION =
'@'- ANNOTATION_STR =
'@'- BREAKCHARS =
[ '(', ')', '[', ']', '{', '}', '^', '~', '*', '%', '&', '|', '<', '=', '>', ',', ';' ]
- SHIFTCHARS =
[ '<', '>' ]
- DIGITS =
('0'..'9').to_a
- ALPHA_LC =
('a'..'z').to_a
- ALPHA_UC =
('A'..'Z').to_a
- OCTALS =
('0'..'7').to_a
- HEXCHARS =
DIGITS + ('a'..'f').to_a + ('A'..'F').to_a
- SIGNS =
['-', '+']
- DOT =
'.'- IDCHARS =
['_' ] + ALPHA_LC + ALPHA_UC
- FULL_IDCHARS =
IDCHARS + DIGITS
- 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
- #check_include(path, fname) ⇒ Object
- #do_parse? ⇒ Boolean
- #enter_expansion(src, define) ⇒ Object
- #enter_include(src, all = true) ⇒ Object
- #eval_directive(s) ⇒ Object
- #extract_annotation ⇒ Object
- #extract_annotation_value ⇒ Object
- #find_include(fname, all = true) ⇒ Object
- #getline ⇒ Object
- #in_expansion? ⇒ Boolean
-
#initialize(src, directiver, params = {}) ⇒ Scanner
constructor
Scanner.
- #is_expanded?(define) ⇒ Boolean
- #is_literal?(o) ⇒ Boolean
- #leave_source ⇒ Object
- #more_source? ⇒ Boolean
- #next_escape ⇒ Object
- #next_escape_str(keep_type_ch = false) ⇒ Object
- #next_identifier(first = nil) ⇒ Object
- #next_token ⇒ Object
- #next_token_before_eol ⇒ Object
- #parse_annotation(in_comment = false) ⇒ Object
- #parse_directive ⇒ Object
- #parse_error(msg, ex = nil) ⇒ Object
- #parse_pragma(s) ⇒ Object
- #peek_next ⇒ Object
- #position ⇒ Object
- #positions ⇒ Object
- #resolve_define(id, stack = []) ⇒ Object
- #skip_spaces ⇒ Object
- #skipfloat_or_fixed ⇒ Object
- #skipline ⇒ Object
Constructor Details
#initialize(src, directiver, params = {}) ⇒ Scanner
Scanner
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/ridl/scanner.rb', line 274 def initialize(src, directiver, params = {}) @includepaths = params[:includepaths] || [] @xincludepaths = params[:xincludepaths] || [] @idlversion = params[:idlversion] @stack = [] @expansions = [] @prefix = nil @directiver = directiver @directiver.instance_variable_get('@d').instance_variable_set('@scanner', self) @defined = TokenRegistry.new # initialize with predefined macros if params[:macros] params[:macros].each do |(name, value)| @defined[name] = value end end @ifdef = [] @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, StringIO 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 # Extend the IDL keywords with IDL4 when enabled if @idlversion >= 4 %w(bitfield bitmask bitset map int8 int16 int32 int64 uint8 uint16 uint32 uint64 ).inject(KEYWORDS) { |h, a| h[a.downcase.to_sym] = a h } end end |
Instance Method Details
#check_include(path, fname) ⇒ Object
341 342 343 344 |
# File 'lib/ridl/scanner.rb', line 341 def check_include(path, fname) fp = path + fname File.file?(fp) && File.readable?(fp) end |
#do_parse? ⇒ Boolean
405 406 407 |
# File 'lib/ridl/scanner.rb', line 405 def do_parse? @ifdef.empty? || @ifdef.last end |
#enter_expansion(src, define) ⇒ Object
369 370 371 372 373 374 |
# File 'lib/ridl/scanner.rb', line 369 def enter_expansion(src, define) IDL.log(2, "** RIDL - enter_expansion > #{define} = #{src}") @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, all = true) ⇒ Object
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 |
# File 'lib/ridl/scanner.rb', line 350 def enter_include(src, all = true) if @directiver.is_included?(src) @directiver.declare_include(src) else fpath = find_include(src, all) if fpath.nil? parse_error "Cannot open include file '#{src}'" end @stack << [:include, @prefix, @ifdef, @in, @ifskip] # record file dir as new searchpath @xincludepaths << (File.dirname(fpath) + '/') @prefix = nil @ifdef = [] @in = In.new(File.open(fpath, 'r'), fpath) @directiver.enter_include(src, fpath) @directiver.pragma_prefix(nil) end end |
#eval_directive(s) ⇒ Object
806 807 808 809 810 811 812 813 814 815 816 817 |
# File 'lib/ridl/scanner.rb', line 806 def eval_directive(s) IDL.log(3, "** 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_annotation ⇒ Object
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 |
# File 'lib/ridl/scanner.rb', line 454 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 skip_spaces # skip till next non-space or eol if peek_next == '(' token = next_token # parse '(' 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.to_s] = annotation_value end end end until token.first == ')' token = next_token_before_eol else token = next_token_before_eol # marker annotation or symbolic value; leave body nil end [token, annotation_body] end |
#extract_annotation_value ⇒ Object
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 |
# File 'lib/ridl/scanner.rb', line 423 def extract_annotation_value 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.to_s # get nested body token, member_annotation_body = extract_annotation # determin vaue type; if it has a body it is an annotation instance annotation_value = if member_annotation_body { member_annotation_id => member_annotation_body } else # otherwise it is a symbolic value member_annotation_id.to_sym end # get next token if needed token = next_token unless token else parse_error 'invalid annotation member' unless is_literal?(token.first) annotation_value = token.last token = next_token end [token, annotation_value] end |
#find_include(fname, all = true) ⇒ Object
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
# File 'lib/ridl/scanner.rb', line 320 def find_include(fname, all = true) if File.file?(fname) && File.readable?(fname) File.(fname) else # search transient include paths if allowed (quoted includes) fp = if all then @xincludepaths.find do |p| check_include(p, fname) end else nil end # search system include paths if still needed fp = @includepaths.find do |p| check_include(p, fname) end unless fp fp += fname if fp fp end end |
#getline ⇒ Object
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 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 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 |
# File 'lib/ridl/scanner.rb', line 709 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 _, 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 _, 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
384 385 386 |
# File 'lib/ridl/scanner.rb', line 384 def in_expansion? more_source? and @stack.last[0] == :define end |
#is_expanded?(define) ⇒ Boolean
376 377 378 |
# File 'lib/ridl/scanner.rb', line 376 def (define) @expansions.include?(define) end |
#is_literal?(o) ⇒ Boolean
419 420 421 |
# File 'lib/ridl/scanner.rb', line 419 def is_literal?(o) LITERALS.include?(o) end |
#leave_source ⇒ Object
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
# File 'lib/ridl/scanner.rb', line 388 def leave_source # make sure to close the input source @in.close # check if we have any previous source still stacked up unless @stack.empty? if @stack.last[0] == :include @xincludepaths.pop # remove directory of finished include @directiver.leave_include _, @prefix, @ifdef, @in, @ifskip = @stack.pop @directiver.pragma_prefix(@prefix) else _, _, _, @in, _ = @stack.pop @expansions.pop end end end |
#more_source? ⇒ Boolean
380 381 382 |
# File 'lib/ridl/scanner.rb', line 380 def more_source? !@stack.empty? end |
#next_escape ⇒ Object
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 |
# File 'lib/ridl/scanner.rb', line 576 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 ret end |
#next_escape_str(keep_type_ch = false) ⇒ Object
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 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 |
# File 'lib/ridl/scanner.rb', line 626 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 ret end |
#next_identifier(first = nil) ⇒ Object
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 571 572 573 574 |
# File 'lib/ridl/scanner.rb', line 530 def next_identifier(first = nil) @in.mark(first) while true if FULL_IDCHARS.include?(@in.lookc) @in.skipc else break end end s0 = @in.getregion # raw IDL id s1 = s0.downcase.to_sym # downcased symbolized s2 = s0.dup # (to be) unescaped id # simple check if s2.empty? parse_error 'identifier expected!' else if s2[0] == '_' s2.slice!(0) ## if starts with CORBA IDL escape => remove end parse_error "identifier must begin with alphabet character: #{s2}" unless ALPHA_LC.include?(s2[0]) || ALPHA_UC.include?(s2[0]) end # preprocessor check if @defined.has_key?(s2) and !(s2) # enter expansion as new source enter_expansion(@defined[s2], s2) # 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, Identifier.new(s2, s2, s0)] end elsif (a = KEYWORDS.assoc(s1)).nil? # check for language mapping keyword [:identifier, Identifier.new(s2, chk_identifier(s2), s0)] elsif s0 == a[1] [a[1], nil] else parse_error "'#{s0}' collides with IDL keyword '#{a[1]}'" end end |
#next_token ⇒ Object
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 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 |
# File 'lib/ridl/scanner.rb', line 970 def next_token sign = nil str = '' # initialize empty string while true ch = @in.getc if ch.nil? if !@ifdef.empty? 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 { |c| WHITESPACE.include?(c) } 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 token returned by parse_annotation or parse next token recursively return parse_annotation || next_token end when ch == ':' # if @in.lookc == ':' # @in.skipc return %w(:: ::) else return %w(: :) end when ch == 'L' _nxtc = @in.lookc if _nxtc == "\'" # ' #single quote, for a character literal. @in.skipc # skip 'L' _nxtc = @in.lookc ret = if _nxtc == "\\" @in.skipc next_escape_str elsif _nxtc == "\'" # ' [ nil, nil ] else [:char, '' << @in.getc] 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 token returned by parse_annotation or parse next token recursively return parse_annotation(true) || next_token else @in.skipuntil { |c| LFCR.include?(c) } end end str = '' # reset next else return %w(/ /) end when SIGNS.include?(ch) _nxtc = @in.lookc if DIGITS.include? _nxtc sign = ch str = '' # reset next else return [str, str] end when ('1'..'9').include?(ch) @in.mark(sign, ch) @in.skipwhile { |c| DIGITS.include?(c) } 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 == DOT # @in.mark(ch) @in.skipwhile { |c| DIGITS.include?(c) } num_type = (DOT != @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) _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 { |c| OCTALS.include?(c) } if ('8'..'9').include? @in.lookc dec = TRUE @in.skipwhile { |c| DIGITS.include?(c) } end num_type = (['.', 'e', 'E', 'd', 'D'].include?(@in.lookc)) ? skipfloat_or_fixed : :integer_literal s = @in.getregion ret = if num_type == :floating_pt_literal [:floating_pt_literal, s.to_f] elsif num_type == :fixed_pt_literal [:fixed_pt_literal, s] elsif dec parse_error "decimal literal starting with '0' should be octal ('0'..'7' only): #{s}" else [:integer_literal, s.oct] end return ret end when ch == "\'" # ' #single quote, for a character literal. _nxtc = @in.lookc ret = if _nxtc == "\\" @in.skipc next_escape elsif _nxtc == "\'" # ' 0 elsif _nxtc ('' << @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 |
#next_token_before_eol ⇒ Object
965 966 967 968 |
# File 'lib/ridl/scanner.rb', line 965 def next_token_before_eol @in.skipwhile { |c| SPACES.include?(c) } LFCR.include?(@in.lookc) ? nil : next_token end |
#parse_annotation(in_comment = false) ⇒ Object
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 489 def parse_annotation(in_comment = false) @in_annotation = true @scan_comment = in_comment begin # parse (possibly multiple) annotation(s) begin annotation_position = self.position.dup # 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.to_s token, annotation_body = extract_annotation # pass annotation to directiver for processing @directiver.define_annotation(annotation_id, annotation_position, in_comment, annotation_body || {}) end until token.nil? || 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.to_s)).nil? token = [:identifier, Identifier.new(token.last.to_s, chk_identifier(token.last.to_s), token.last.unescaped_name)] elsif token.last == a[1] token = [a[1], nil] else parse_error "'#{token.last}' collides with a keyword '#{a[1]}'" end end token end |
#parse_directive ⇒ Object
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 893 894 895 896 897 898 899 900 901 902 903 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 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 |
# File 'lib/ridl/scanner.rb', line 819 def parse_directive @in.skipwhile { |c| SPACES.include?(c) } s = getline /^(\w*)\s*/ === s s1 = $1 s2 = $' # ' 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.zero? 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.zero? @ifdef.pop @ifskip = @ifdef.last else @ifnest -= 1 end else if @ifnest.zero? 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 |_| "#{$1}#{resolve_define($2)}" end begin @ifdef[@ifdef.size - 1] = eval_directive(s2) @ifskip = @ifdef[@ifdef.size - 1] rescue IDL::ParseError raise rescue => e p e puts e.backtrace.join("\n") parse_error 'error evaluating #elif' end end end end elsif /(if|ifn?def)/ === s1 if /ifn?def/ === s1 if do_parse? parse_error 'no #if(n)def target.' unless /^(\w+)/ === s2 @ifdef.push(@defined[$1].nil? ^ (s1 == 'ifdef')) @ifskip = @ifdef.last else @ifnest += 1 end else # when 'if' if do_parse? # match 'defined(Foo)' or 'defined Foo' while s2 =~ /(^|[\W])defined(\s*\(\s*(\w+)\s*\)|\s+(\w+))/ IDL.log(3, "** RIDL - parse_directive : resolving 'defined(#{$3 || $4})'") def_id = $3 || $4 # resolve 'defined' expression to 'true' or 'false' according to actual macro definition s2.gsub!(/(^|[\W])(defined\s*[\s\(]\s*#{def_id}(\s*\))?)/, '\1' + "#{@defined.has_key?(def_id).to_s}") end # match and resolve any macro variables listed in conditional expression s2.gsub!(/(^|[\W])([A-Za-z_][\w]*)/) do |_| "#{$1}#{resolve_define($2)}" end begin @ifdef.push(eval_directive(s2)) @ifskip = @ifdef.last rescue IDL::ParseError raise rescue => e p e puts e.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' defid = s2.split.first parse_error 'no #define target.' unless defid parse_error "#{defid} is already #define-d." if @defined[defid] defval = s2.sub(/^\s*#{defid}/, '').strip defval = true if defval.empty? @defined[defid] = defval when 'undef' @defined.delete(s2) when 'include' if s2[0, 1] == '"' || s2[0, 1] == '<' quoted_inc = (s2[0, 1] == '"') if s2.size > 2 s2.strip! s2 = s2.slice(1..(s2.size - 2)) else s2 = '' end end enter_include(s2, quoted_inc) when /[0-9]+/ # ignore line directive else parse_error "unknown directive: #{s}." end end end |
#parse_error(msg, ex = nil) ⇒ Object
413 414 415 416 417 |
# File 'lib/ridl/scanner.rb', line 413 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
951 952 953 954 955 956 957 958 959 960 961 962 963 |
# File 'lib/ridl/scanner.rb', line 951 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 |
#peek_next ⇒ Object
522 523 524 |
# File 'lib/ridl/scanner.rb', line 522 def peek_next @in.lookc end |
#position ⇒ Object
346 347 348 |
# File 'lib/ridl/scanner.rb', line 346 def position @in.position end |
#positions ⇒ Object
409 410 411 |
# File 'lib/ridl/scanner.rb', line 409 def positions @stack.reverse.inject(@in.nil? ? [] : [@in.position]) { |pos_arr, (_, _, _, in_, _)| pos_arr << in_.position } end |
#resolve_define(id, stack = []) ⇒ Object
791 792 793 794 795 796 797 798 799 800 801 802 803 804 |
# File 'lib/ridl/scanner.rb', line 791 def resolve_define(id, stack = []) return id if %w(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 |_| "#{$1}#{resolve_define($2, stack)}" end else '0' # unknown id end end |
#skip_spaces ⇒ Object
526 527 528 |
# File 'lib/ridl/scanner.rb', line 526 def skip_spaces @in.skipwhile { |c| SPACES.include?(c) } end |
#skipfloat_or_fixed ⇒ Object
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 |
# File 'lib/ridl/scanner.rb', line 682 def skipfloat_or_fixed if @in.lookc == DOT @in.skipc @in.skipwhile { |c| DIGITS.include?(c) } end if ['e', 'E'].include? @in.lookc @in.skipc @in.skipc if SIGNS.include? @in.lookc @in.skipwhile { |c| DIGITS.include?(c) } return :floating_pt_literal elsif ['d', 'D'].include? @in.lookc @in.skipc @in.skipc if SIGNS.include? @in.lookc @in.skipwhile { |c| DIGITS.include?(c) } return :fixed_pt_literal end :floating_pt_literal end |
#skipline ⇒ Object
701 702 703 704 705 706 707 |
# File 'lib/ridl/scanner.rb', line 701 def skipline while true s = @in.gets until s.chomp!.nil?; end break unless s[s.length - 1] == "\\" end end |