Module: ZergXcode::Parser

Defined in:
lib/zerg_xcode/file_format/parser.rb

Class Method Summary collapse

Class Method Details

.parse(project_string) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
45
# File 'lib/zerg_xcode/file_format/parser.rb', line 2

def self.parse(project_string)
  tokens = ZergXcode::Lexer.tokenize project_string
  
  context = [[]]
  last_token = nil
  tokens.each do |token|
    case token
    when :begin_array
      context << Array.new
    when :begin_hash
      context << Hash.new
    when :end_array, :end_hash
      last_object = context.pop
      if context.last.kind_of? Array
        context.last << last_object
      elsif context.last.kind_of? String
        hash_key = context.pop
        context.last[hash_key] = last_object
      end
    when :assign, :stop, :comma
      
    when Array
      case token.first
      when :encoding
      when :string, :symbol
        token_string = token.last
        if context.last.kind_of? Hash
          context << token_string
        elsif context.last.kind_of? Array
          context.last << token_string
        elsif context.last.kind_of? String
          key = context.pop
          context.last[key] = token_string
        else
          p context
          raise 'WTFed'
        end
      end        
    else
      raise "Unknown token #{token}"
    end
  end
  return context[0][0]
end