Module: RubyToBlock

Extended by:
ActiveSupport::Concern
Included in:
SourceCode
Defined in:
app/models/concerns/ruby_to_block.rb

Overview

Rubyのソースコードをブロックに変換するモジュール

Defined Under Namespace

Modules: Block Classes: Character

Constant Summary collapse

SUCCESS_DATA_MOCK =
"  require \"smalruby\"\n\n  car1 = Character.new(costume: \"car1.png\", x: 0, y: 0, angle: 0)\n\n  car1.on(:start) do\n    loop do\n      move(10)\n      turn_if_reach_wall\n    end\n  end\n".strip_heredoc
SUCCESS_XML_MOCK =
"  <xml xmlns=\"http://www.w3.org/1999/xhtml\">\n    <character name=\"car1\" x=\"0\" y=\"0\" angle=\"0\" costumes=\"car1.png\" />\n    <block type=\"character_new\" x=\"4\" y=\"4\">\n      <field name=\"NAME\">car1</field>\n      <statement name=\"DO\">\n        <block type=\"events_on_start\">\n          <statement name=\"DO\">\n            <block type=\"control_loop\">\n              <statement name=\"DO\">\n                <block type=\"motion_move\" inline=\"true\">\n                  <value name=\"STEP\">\n                    <block type=\"math_number\">\n                      <field name=\"NUM\">10</field>\n                    </block>\n                  </value>\n                  <next>\n                    <block type=\"motion_turn_if_reach_wall\" />\n                  </next>\n                </block>\n              </statement>\n            </block>\n          </statement>\n        </block>\n      </statement>\n    </block>\n  </xml>\n".strip_heredoc

Instance Method Summary collapse

Instance Method Details

#to_blocksObject

XML形式のブロックに変換する



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'app/models/concerns/ruby_to_block.rb', line 52

def to_blocks
  fail if data == '__FAIL__'
  return SUCCESS_XML_MOCK if data == SUCCESS_DATA_MOCK

  characters = {}
  character_stack = []
  receiver_stack = []
  statement_stack = []
  blocks = []
  current_block = nil
  lines = data.lines
  while (line = lines.shift)
    line.chomp!
    next if line.strip.empty?

    md = STATEMENT_REGEXP.match(line)

    unless md
      block = Block.new('ruby_statement', fields: { STATEMENT: line })
      if current_block # TODO: リファクタリング
        current_block.sibling = block
      else
        blocks.push(block)
      end
      current_block = block

      next
    end

    next if md[:require_smalruby]

    if (s = md[:character])
      md2 = /#{CHARACTER_RE}/.match(s)
      name = md2[1]

      characters[name] = Character.new(name: name, costumes: [md2[2]],
                                       x: md2[3], y: md2[4], angle: md2[5])
      next
    end

    if (s = md[:events_on_start])
      md2 = /#{EVENTS_ON_START_RE}/.match(s)
      name = md2[1] ? md2[1] : receiver_stack.last.name
      c = characters[name]
      character_stack.push(c)

      do_block = Block.new('null')
      events_on_start_block = Block.new('events_on_start', statements: { DO: do_block })
      if current_block && current_block.type == 'character_new' &&
          current_block[:NAME] == name
        current_block.add_statement(:DO, events_on_start_block)
        character_new_block = current_block
      else
        if c == receiver_stack.last
          current_block.sibling = events_on_start_block
          character_new_block = current_block.parent
        else
          character_new_block =
            Block.new('character_new',
                      fields: { NAME: name }, statements: { DO: events_on_start_block })
          if current_block
            if current_block.type == 'character_new'
              blocks.push(character_new_block)
            else
              current_block.sibling = character_new_block
            end
          else
            blocks.push(character_new_block)
          end
        end
      end
      statement_stack.push([:events_on_start, character_new_block])

      receiver_stack.push(c)

      current_block = do_block

      next
    end

    if md[:end]
      ends_num = lines.select { |l|
        md2 = STATEMENT_REGEXP.match(l)
        md2 && md2[:end]
      }.length + 1
      ends_num -= lines.select { |l|
        md2 = STATEMENT_REGEXP.match(l)
        md2 && (md2[:events_on_start])
      }.length
      if (ss = statement_stack.last) &&
          ends_num <= statement_stack.length
        case ss.first
        when :events_on_start
          current_block = ss[1]

          receiver_stack.pop
          character_stack.pop
          statement_stack.pop
        else
          # TODO
        end
      else
        block = Block.new('ruby_statement', fields: { STATEMENT: line })
        if current_block # TODO: リファクタリング
          current_block.sibling = block
        else
          blocks.push(block)
        end
        current_block = block
      end

      next
    end
  end

  return '' if characters.empty? && blocks.empty?

  xml = REXML::Document.new('<xml xmlns="http://www.w3.org/1999/xhtml" />',
                            attribute_quote: :quote,
                            respect_whitespace: :all)
  characters.values.each do |c|
    c.to_xml(xml.root)
  end
  blocks.each do |b|
    b.to_xml(xml.root)
  end

  output = StringIO.new
  formatter = REXML::Formatters::Pretty.new(2, true)
  formatter.compact = true
  # HACK: 行頭、行末などのスペースが1つになってしまうのを修正している
  def formatter.write_text( node, output )
    s = node.to_s()
    s.gsub!(/\s/,' ')
    if !node.is_a?(REXML::Text) ||
        node.is_a?(REXML::Text) && !node.parent.whitespace()
      s.squeeze!(" ")
    end
    s = wrap(s, @width - @level)
    s = indent_text(s, @level, " ", true)
    output << (' '*@level + s)
  end
  formatter.write(xml, output)

  output.string + "\n"
end