Module: Depager::Utils::CodeGeneratorMethods

Included in:
Extension, ExtensionSlaveMethods
Defined in:
lib/depager/utils.rb

Instance Method Summary collapse

Instance Method Details

#expand_inline_code(body, lineno, options = {}) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/depager/utils.rb', line 76

def expand_inline_code(body, lineno, options = {})
  delimiter = expanded_code_delimiter
  delta = options[:delta] || 0

  if (wrap = options[:wrap])
    wrap = [wrap, "end"] if wrap.is_a?(String)
    body = "#{wrap[0]}\n#{body}\n#{wrap[1]}"
    delta += wrap[0].lines.size
  end

  <<~CODE
    module_eval <<-'#{delimiter}', '#{input_path}', #{lineno - delta}
    #{body}
    #{delimiter}
  CODE
end

#generate_action_decorator_code(on_reduce, code) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/depager/utils.rb', line 63

def generate_action_decorator_code(on_reduce, code)
  on_reduce_code = on_reduce.map { |i| "            #{i || 'nil'},\n" }

  generate_decorator_code(decorator_name, "#{d_parser.parsing_method}::Action", <<~CODE)
    ON_REDUCE = [
      #{on_reduce_code.join}
    ]
    def on_reduce; ON_REDUCE; end

    #{code}
  CODE
end

#generate_decorator_code(name, super_class_name, code) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/depager/utils.rb', line 49

def generate_decorator_code(name, super_class_name, code)
  mixin_code = if d_parser.generator.is_a?(Depager::ExtensionGenerator)
                 "include Depager::Utils::ExtensionSlaveDecoratorMethods"
               else
                 ""
               end
  <<~CODE
    class #{target_namespace}::#{name} < #{super_class_name} #:nodoc:all
      #{mixin_code}
    #{code}
    end
  CODE
end

#parse_blockObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/depager/utils.rb', line 93

def parse_block
  result = ""
  start_lineno = file.lineno
  if @line =~ /\A\s*\{(.*)\}\s*(#.*)?\Z/
    result = "    #{$1}\n"
  else
    result.concat @line.sub(/\A\s*\{/, "")
    ind = @original_line.match(/\A([ \t]*)/)[1]
    until file.eof?
      line = file.gets
      break if line =~ /\A#{ind}\}\s*(#.*)?\Z/

      result.concat line
    end
    error_exit "syntax error(parse_block).", start_lineno if file.eof?
  end
  @line = $'
  result
end