Module: XcodeResultBundleProcessor::SLF0::Tokenizer

Defined in:
lib/xcoderesultbundleprocessor/slf0/tokenizer.rb

Defined Under Namespace

Classes: ClassName, ClassNameRef, ObjectList, Token

Constant Summary collapse

TOKEN_INT =
'#'
TOKEN_CLASS_NAME =
'%'
TOKEN_CLASS_NAME_REF =
'@'
TOKEN_STRING =
'"'
TOKEN_DOUBLE =
'^'
OBJECT_LIST_NIL =
'-'
OBJECT_LIST =
'('

Class Method Summary collapse

Class Method Details

.read_length_and_token_type(io) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/xcoderesultbundleprocessor/slf0/tokenizer.rb', line 51

def self.read_length_and_token_type(io)
  # The length appears as the start of the token as 0 or more ASCII decimal or hex digits until the token type marker
  length_string = ''
  until [TOKEN_INT, TOKEN_CLASS_NAME, TOKEN_CLASS_NAME_REF, TOKEN_STRING, TOKEN_DOUBLE, OBJECT_LIST_NIL, OBJECT_LIST].include?((c = io.readchar)) do
    length_string << c
  end

  token = c

  # TODO: Doubles are stored as hex strings. If it turns out the doubles contain useful information, implement
  # logic to convert the hex string to doubles
  if token == TOKEN_DOUBLE
    length_string = ''
  end

  return Token.new(length_string.to_i, c)
end

.read_token_stream(io) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/xcoderesultbundleprocessor/slf0/tokenizer.rb', line 16

def self.read_token_stream(io)
  raise 'Not an SLF0 file' unless self.valid_slf0_io?(io)

  Enumerator.new do |enumerator|
    until io.eof?
      token = self.read_length_and_token_type(io)

      case token.identifier
        when TOKEN_INT
          enumerator.yield(token.int)
        when TOKEN_DOUBLE
          enumerator.yield(token.int)
        when TOKEN_STRING
          enumerator.yield(io.read(token.int).gsub("\r", "\n"))
        when TOKEN_CLASS_NAME
          enumerator.yield(ClassName.new(io.read(token.int)))
        when TOKEN_CLASS_NAME_REF
          enumerator.yield(ClassNameRef.new(token.int))
        when OBJECT_LIST
          enumerator.yield(ObjectList.new(token.int))
        when OBJECT_LIST_NIL
          enumerator.yield(nil)
        else
          enumerator.yield(token)
      end

    end
  end
end

.valid_slf0_io?(io) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
# File 'lib/xcoderesultbundleprocessor/slf0/tokenizer.rb', line 46

def self.valid_slf0_io?(io)
  return false if io.nil?
  io.read(4) == 'SLF0'
end