Class: IOSParser::IOS

Inherits:
Object
  • Object
show all
Defined in:
lib/ios_parser/ios.rb,
lib/ios_parser/ios/command.rb,
lib/ios_parser/ios/document.rb,
lib/ios_parser/ios/queryable.rb

Defined Under Namespace

Modules: Queryable Classes: Command, Document

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent: nil, lexer: IOSParser::Lexer.new) ⇒ IOS

Returns a new instance of IOS.



10
11
12
13
14
15
# File 'lib/ios_parser/ios.rb', line 10

def initialize(parent: nil, lexer: IOSParser::Lexer.new)
  @document = Document.new(nil)
  @parent = parent
  @lexer  = lexer
  @indent = 0
end

Instance Attribute Details

#documentObject

Returns the value of attribute document.



5
6
7
# File 'lib/ios_parser/ios.rb', line 5

def document
  @document
end

#lexerObject

Returns the value of attribute lexer.



6
7
8
# File 'lib/ios_parser/ios.rb', line 6

def lexer
  @lexer
end

#sourceObject

Returns the value of attribute source.



7
8
9
# File 'lib/ios_parser/ios.rb', line 7

def source
  @source
end

#tokensObject



17
18
19
# File 'lib/ios_parser/ios.rb', line 17

def tokens
  @tokens ||= lexer.call(@source)
end

Instance Method Details

#argument_to_discard?(arg) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/ios_parser/ios.rb', line 64

def argument_to_discard?(arg)
  arguments_to_discard.include?(arg)
end

#arguments_to_discardObject



68
69
70
71
72
# File 'lib/ios_parser/ios.rb', line 68

def arguments_to_discard
  [:INDENT, :DEDENT,
   :CERTIFICATE_BEGIN, :CERTIFICATE_END,
   :BANNER_BEGIN, :BANNER_END]
end

#call(source) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/ios_parser/ios.rb', line 21

def call(source)
  unless source.respond_to? :each_char
    raise ArgumentError, 'Provided configuration source is invalid.'
  end
  @source = source
  @document.source = source
  @document.push(*section) until tokens.empty?
  @document
end

#command(parent = nil, document = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ios_parser/ios.rb', line 41

def command(parent = nil, document = nil)
  opts = {
    tokens: command_tokens,
    parent: parent,
    document: document,
    indent: @indent
  }

  Command.new(**opts).tap do |cmd|
    cmd.commands = subsections(cmd)
  end
end

#command_tokensObject



54
55
56
57
58
59
60
61
62
# File 'lib/ios_parser/ios.rb', line 54

def command_tokens
  toks = []
  until tokens.empty? || tokens.first.value == :EOL
    tok = tokens.shift
    toks << tok unless argument_to_discard?(tok.value)
  end
  tokens.shift # discard :EOL
  toks
end

#section(parent = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/ios_parser/ios.rb', line 31

def section(parent = nil)
  [].tap do |commands|
    until tokens.empty? || tokens.first.value == :DEDENT
      commands.push(command(parent, @document))
    end
    token = tokens.shift # discard :DEDENT
    @indent -= 1 if token && token.value == :DEDENT
  end
end

#subsections(parent = nil) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/ios_parser/ios.rb', line 74

def subsections(parent = nil)
  if !tokens.empty? && tokens.first.value == :INDENT
    @indent += 1
    tokens.shift # discard :INDENT
    section(parent)
  else
    []
  end
end