private void buildBlocks(Method method) throws InvalidSpecCompileException {
    List<Statement> stats = AstUtil.getStatements(method.getAst());
    Block currBlock = method.addBlock(new AnonymousBlock(method));

    String statementLabelToTransplant = null;
    for (Statement stat : stats) {
      if (stat.getStatementLabel() == null) {
        if (statementLabelToTransplant != null) {
          stat.setStatementLabel(statementLabelToTransplant);
        }
        currBlock.getAst().add(stat);
      } else {
        currBlock = addBlock(method, stat);
      }
      // Usually, you have a label on a statement like
      //
      // combined:
      //   x << [1]
      //
      // and the label stays on that statement and could be used in later stages of the AST processing.
      // Especially for "combined" this is essential for proper operation.
      //
      // But if the label has a description like
      //
      // combined: 'combined with x'
      //   x << [1]
      //
      // then the description is added as "text" to the current block and the whole statement is swallowed.
      // If the label is needed for further processing like for "combined", this is then missing,
      // so transplant the label to the following statement which it actually affects.
      statementLabelToTransplant = (getDescription(stat) == null) ? null : stat.getStatementLabel();
    }

    checkIsValidSuccessor(method, BlockParseInfo.METHOD_END,
        method.getAst().getLastLineNumber(), method.getAst().getLastColumnNumber());

    // set the block metadata index for each block this must be equal to the index of the block in the @BlockMetadata annotation
    int i = -1;
    for (Block block : method.getBlocks()) {
      if(!block.hasBlockMetadata()) continue;
      block.setBlockMetaDataIndex(++i);
    }
    // now that statements have been copied to blocks, the original statement
    // list is cleared; statements will be copied back after rewriting is done
    stats.clear();
  }
Given the abstract syntax tree of a Groovy class representing a Spock
specification, builds an object model of the specification.

@author Peter Niederwieser
spock-core/src/main/java/org/spockframework/compiler/SpecParser.java