2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2015-02-05 08:42:45 -05:00
|
|
|
require 'strscan'
|
|
|
|
require 'rubygems/request_set/lockfile/parser'
|
|
|
|
|
|
|
|
class Gem::RequestSet::Lockfile::Tokenizer
|
2015-09-08 18:46:43 -04:00
|
|
|
Token = Struct.new :type, :value, :column, :line
|
|
|
|
EOF = Token.new :EOF
|
|
|
|
|
2015-02-05 08:42:45 -05:00
|
|
|
def self.from_file file
|
|
|
|
new File.read(file), file
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize input, filename = nil, line = 0, pos = 0
|
|
|
|
@line = line
|
|
|
|
@line_pos = pos
|
|
|
|
@tokens = []
|
|
|
|
@filename = filename
|
|
|
|
tokenize input
|
|
|
|
end
|
|
|
|
|
|
|
|
def make_parser set, platforms
|
|
|
|
Gem::RequestSet::Lockfile::Parser.new self, set, platforms, @filename
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_a
|
2015-09-08 18:46:43 -04:00
|
|
|
@tokens.map { |token| [token.type, token.value, token.column, token.line] }
|
2015-02-05 08:42:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def skip type
|
2015-09-08 18:46:43 -04:00
|
|
|
@tokens.shift while not @tokens.empty? and peek.type == type
|
2015-02-05 08:42:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Calculates the column (by byte) and the line of the current token based on
|
|
|
|
# +byte_offset+.
|
|
|
|
|
|
|
|
def token_pos byte_offset # :nodoc:
|
|
|
|
[byte_offset - @line_pos, @line]
|
|
|
|
end
|
|
|
|
|
|
|
|
def empty?
|
|
|
|
@tokens.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
def unshift token
|
|
|
|
@tokens.unshift token
|
|
|
|
end
|
|
|
|
|
|
|
|
def next_token
|
|
|
|
@tokens.shift
|
|
|
|
end
|
|
|
|
alias :shift :next_token
|
|
|
|
|
|
|
|
def peek
|
2015-09-08 18:46:43 -04:00
|
|
|
@tokens.first || EOF
|
2015-02-05 08:42:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def tokenize input
|
|
|
|
s = StringScanner.new input
|
|
|
|
|
|
|
|
until s.eos? do
|
|
|
|
pos = s.pos
|
|
|
|
|
|
|
|
pos = s.pos if leading_whitespace = s.scan(/ +/)
|
|
|
|
|
|
|
|
if s.scan(/[<|=>]{7}/) then
|
|
|
|
message = "your #{@filename} contains merge conflict markers"
|
|
|
|
column, line = token_pos pos
|
|
|
|
|
|
|
|
raise Gem::RequestSet::Lockfile::ParseError.new message, column, line, @filename
|
|
|
|
end
|
|
|
|
|
|
|
|
@tokens <<
|
|
|
|
case
|
|
|
|
when s.scan(/\r?\n/) then
|
2015-09-08 18:46:43 -04:00
|
|
|
token = Token.new(:newline, nil, *token_pos(pos))
|
2015-02-05 08:42:45 -05:00
|
|
|
@line_pos = s.pos
|
|
|
|
@line += 1
|
|
|
|
token
|
|
|
|
when s.scan(/[A-Z]+/) then
|
|
|
|
if leading_whitespace then
|
|
|
|
text = s.matched
|
|
|
|
text += s.scan(/[^\s)]*/).to_s # in case of no match
|
2015-09-08 18:46:43 -04:00
|
|
|
Token.new(:text, text, *token_pos(pos))
|
2015-02-05 08:42:45 -05:00
|
|
|
else
|
2015-09-08 18:46:43 -04:00
|
|
|
Token.new(:section, s.matched, *token_pos(pos))
|
2015-02-05 08:42:45 -05:00
|
|
|
end
|
|
|
|
when s.scan(/([a-z]+):\s/) then
|
|
|
|
s.pos -= 1 # rewind for possible newline
|
2015-09-08 18:46:43 -04:00
|
|
|
Token.new(:entry, s[1], *token_pos(pos))
|
2015-02-05 08:42:45 -05:00
|
|
|
when s.scan(/\(/) then
|
2015-09-08 18:46:43 -04:00
|
|
|
Token.new(:l_paren, nil, *token_pos(pos))
|
2015-02-05 08:42:45 -05:00
|
|
|
when s.scan(/\)/) then
|
2015-09-08 18:46:43 -04:00
|
|
|
Token.new(:r_paren, nil, *token_pos(pos))
|
2015-02-05 08:42:45 -05:00
|
|
|
when s.scan(/<=|>=|=|~>|<|>|!=/) then
|
2015-09-08 18:46:43 -04:00
|
|
|
Token.new(:requirement, s.matched, *token_pos(pos))
|
2015-02-05 08:42:45 -05:00
|
|
|
when s.scan(/,/) then
|
2015-09-08 18:46:43 -04:00
|
|
|
Token.new(:comma, nil, *token_pos(pos))
|
2015-02-05 08:42:45 -05:00
|
|
|
when s.scan(/!/) then
|
2015-09-08 18:46:43 -04:00
|
|
|
Token.new(:bang, nil, *token_pos(pos))
|
2015-02-05 08:42:45 -05:00
|
|
|
when s.scan(/[^\s),!]*/) then
|
2015-09-08 18:46:43 -04:00
|
|
|
Token.new(:text, s.matched, *token_pos(pos))
|
2015-02-05 08:42:45 -05:00
|
|
|
else
|
|
|
|
raise "BUG: can't create token for: #{s.string[s.pos..-1].inspect}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@tokens
|
|
|
|
end
|
|
|
|
end
|