token.rb (2656B)
1 # Copyright, 2014, by Masahiro Sano. 2 # 3 # Permission is hereby granted, free of charge, to any person obtaining a copy 4 # of this software and associated documentation files (the "Software"), to deal 5 # in the Software without restriction, including without limitation the rights 6 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 # copies of the Software, and to permit persons to whom the Software is 8 # furnished to do so, subject to the following conditions: 9 # 10 # The above copyright notice and this permission notice shall be included in 11 # all copies or substantial portions of the Software. 12 # 13 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 # THE SOFTWARE. 20 21 require_relative 'lib/token' 22 require_relative 'lib/cursor' 23 require_relative 'source_location' 24 25 module FFI 26 module Clang 27 class Tokens < AutoPointer 28 include Enumerable 29 30 attr_reader :size 31 attr_reader :tokens 32 33 def initialize(pointer, token_size, translation_unit) 34 ptr = Lib::TokensPointer.new(pointer,token_size, translation_unit) 35 super ptr 36 37 @translation_unit = translation_unit 38 @size = token_size 39 40 @tokens = [] 41 cur_ptr = pointer 42 token_size.times { 43 @tokens << Token.new(cur_ptr, translation_unit) 44 cur_ptr += Lib::CXToken.size 45 } 46 end 47 48 def self.release(pointer) 49 Lib.dispose_tokens(pointer.translation_unit, pointer, pointer.token_size) 50 end 51 52 def each(&block) 53 @tokens.each do |token| 54 block.call(token) 55 end 56 end 57 58 def cursors 59 ptr = MemoryPointer.new(Lib::CXCursor, @size) 60 Lib.annotate_tokens(@translation_unit, self, @size, ptr) 61 62 cur_ptr = ptr 63 arr = [] 64 @size.times { 65 arr << Cursor.new(cur_ptr, @translation_unit) 66 cur_ptr += Lib::CXCursor.size 67 } 68 arr 69 end 70 end 71 72 class Token 73 def initialize(token, translation_unit) 74 @token = token 75 @translation_unit = translation_unit 76 end 77 78 def kind 79 Lib.get_token_kind(@token) 80 end 81 82 def spelling 83 Lib.extract_string Lib.get_token_spelliing(@translation_unit, @token) 84 end 85 86 def location 87 ExpansionLocation.new Lib.get_token_location(@translation_unit, @token) 88 end 89 90 def extent 91 SourceRange.new Lib.get_token_extent(@translation_unit, @token) 92 end 93 end 94 end 95 end