Class: Ascode::Parser::ConditionBlock

Inherits:
Object
  • Object
show all
Defined in:
lib/ascode/parser/condition_block.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, block_begin) ⇒ ConditionBlock

Returns a new instance of ConditionBlock.



8
9
10
11
12
13
14
# File 'lib/ascode/parser/condition_block.rb', line 8

def initialize(code, block_begin)
  @code = code
  @begin = block_begin + 1

  @true = ""
  @false = ""
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



6
7
8
# File 'lib/ascode/parser/condition_block.rb', line 6

def block
  @block
end

Instance Method Details

#ast_actionObject



69
70
71
72
73
74
75
# File 'lib/ascode/parser/condition_block.rb', line 69

def ast_action
  @ast = {
    action: "condition",
    true_block: @true,
    false_block: @false
  }
end

#find(what) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ascode/parser/condition_block.rb', line 30

def find(what)
  level = 0
  skip = 0
  @block.split("").to_enum.each_with_index do |char, index|
    if skip > 0
      skip -= 1
      next
    elsif char == "\""
      literal = Literal.new @block, index
      literal.parse
      skip = literal.length
    else
      return index if char == what && level.zero?
      level += 1 if char == "["
      level -= 1 if char == "]"
    end
  end

  -1
end

#parseObject



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ascode/parser/condition_block.rb', line 16

def parse
  @block = @code[@begin..-1]

  @end = find "]"
  raise "Unmatched '[' detected." if @end == -1

  @block = @block[0..(@end - 1)]

  @split_pos = find ":"
  split

  ast_action
end

#parse_part(part) ⇒ Object



65
66
67
# File 'lib/ascode/parser/condition_block.rb', line 65

def parse_part(part)
  (Main.new part, false).parse
end

#splitObject



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ascode/parser/condition_block.rb', line 51

def split
  if @split_pos == -1
    @true = @block
  elsif @split_pos.zero?
    @false = @block
  else
    @true = @block[0..(@split_pos - 1)]
    @false = @block[(@split_pos + 1)..-1]
  end

  @true = parse_part(@true)
  @false = parse_part(@false)
end