Class: Gem::RequestSet::Lockfile::Tokenizer

Inherits:
Object
  • Object
show all
Defined in:
lib/rubygems/request_set/lockfile/tokenizer.rb

Defined Under Namespace

Classes: Token

Constant Summary collapse

EOF =
Token.new :EOF

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, filename = nil, line = 0, pos = 0) ⇒ Tokenizer

Returns a new instance of Tokenizer.


13
14
15
16
17
18
19
# File 'lib/rubygems/request_set/lockfile/tokenizer.rb', line 13

def initialize(input, filename = nil, line = 0, pos = 0)
  @line     = line
  @line_pos = pos
  @tokens   = []
  @filename = filename
  tokenize input
end

Class Method Details

.from_file(file) ⇒ Object


9
10
11
# File 'lib/rubygems/request_set/lockfile/tokenizer.rb', line 9

def self.from_file(file)
  new File.read(file), file
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)

41
42
43
# File 'lib/rubygems/request_set/lockfile/tokenizer.rb', line 41

def empty?
  @tokens.empty?
end

#make_parser(set, platforms) ⇒ Object


21
22
23
# File 'lib/rubygems/request_set/lockfile/tokenizer.rb', line 21

def make_parser(set, platforms)
  Gem::RequestSet::Lockfile::Parser.new self, set, platforms, @filename
end

#next_tokenObject Also known as: shift


49
50
51
# File 'lib/rubygems/request_set/lockfile/tokenizer.rb', line 49

def next_token
  @tokens.shift
end

#peekObject


54
55
56
# File 'lib/rubygems/request_set/lockfile/tokenizer.rb', line 54

def peek
  @tokens.first || EOF
end

#skip(type) ⇒ Object


29
30
31
# File 'lib/rubygems/request_set/lockfile/tokenizer.rb', line 29

def skip(type)
  @tokens.shift while not @tokens.empty? and peek.type == type
end

#to_aObject


25
26
27
# File 'lib/rubygems/request_set/lockfile/tokenizer.rb', line 25

def to_a
  @tokens.map { |token| [token.type, token.value, token.column, token.line] }
end

#token_pos(byte_offset) ⇒ Object

Calculates the column (by byte) and the line of the current token based on byte_offset.


37
38
39
# File 'lib/rubygems/request_set/lockfile/tokenizer.rb', line 37

def token_pos(byte_offset) # :nodoc:
  [byte_offset - @line_pos, @line]
end

#unshift(token) ⇒ Object


45
46
47
# File 'lib/rubygems/request_set/lockfile/tokenizer.rb', line 45

def unshift(token)
  @tokens.unshift token
end