String elementClass = TypeGenerator.className(elementType); String elementClassFromTerm = elementClass + "FromTerm";
println("      return " + makeClassName + "(" + singletonElement + ");"); println("    }"); println(); println("    int length = (list.getLength() / " + headLength + ") + 1;"); println("    java.util.List[] nodes = new java.util.List[length-1];"); println(); println("    for (int i = 0; i < length - 1; i++) {"); println("      java.util.List args = list.match(" + manyPattern + ");"); println("      if (args == null) {"); println("        throw new IllegalArgumentException(\"This is not a " + className + ": \" + trm);"); println("      }"); println("      nodes[i] = args;"); println("      list = (aterm.ATermList) args.get(" + tailIndex + ");"); println("    }"); println(); println("    if (list.getLength() != 1) {"); println("      throw new IllegalArgumentException(\"This is not a " + className + ": \" + trm);"); println("    }"); println(); println("    " + returnTypeName + " result = " + makeClassName + "(" + singletonElement + ");"); println(); println("    for (int i = length - 2; i >= 0; i--) {"); println("      java.util.List children = nodes[i];"); String elementTypeName = TypeGenerator.qualifiedClassName(params, elementType);
    private void genSeparatedListFromTermMethod(SeparatedListType type) {        JavaGenerationParameters params = getJavaGenerationParameters();        String returnTypeName = TypeGenerator.qualifiedClassName(params, type);        String className = TypeGenerator.className(type);        String makeClassName = "make" + className;        String manyPattern = patternListVariable(type);        String elementType = type.getElementType();        // String elementClass = TypeGenerator.className(elementType);        // String elementClassFromTerm = elementClass + "FromTerm";        Separators separators = type.getSeparators();        int headLength = 1 + separators.getLength(); // on the ATerm level        int tailIndex = 1 + type.countSeparatorFields(); // on the abstract        // level        println(            "  public "                + returnTypeName                + " "                + className                + "FromTerm(aterm.ATerm trm) {");        println("    if (!(trm instanceof aterm.ATermList)) {");        println(            "      throw new IllegalArgumentException(\"This is not a "                + className                + ": \" + trm);");        println("    }");        println();        println("    aterm.ATermList list = (aterm.ATermList) trm;");        println("    if (list.isEmpty()) {");        println("      return " + makeClassName + "();");        println("    }");        println();        println("    if (list.getLength() == 1) {");        String singletonElement =            buildFromTermListElement(elementType, "list.getFirst()");        String elementClass = TypeGenerator.className(elementType);        String elementClassFromTerm = elementClass + "FromTerm";        println("      return " + makeClassName + "(" + singletonElement + ");");        println("    }");        println();        println("    int length = (list.getLength() / " + headLength + ") + 1;");        println("    java.util.List[] nodes = new java.util.List[length-1];");        println();        println("    for (int i = 0; i < length - 1; i++) {");        println("      java.util.List args = list.match(" + manyPattern + ");");        println("      if (args == null) {");        println(            "        throw new IllegalArgumentException(\"This is not a "                + className                + ": \" + trm);");        println("      }");        println("      nodes[i] = args;");        println("      list = (aterm.ATermList) args.get(" + tailIndex + ");");        println("    }");        println();        println("    if (list.getLength() != 1) {");        println(            "      throw new IllegalArgumentException(\"This is not a "                + className                + ": \" + trm);");        println("    }");        println();        println(            "    "                + returnTypeName                + " result = "                + makeClassName                + "("                + singletonElement                + ");");        println();        println("    for (int i = length - 2; i >= 0; i--) {");        println("      java.util.List children = nodes[i];");        String elementTypeName = TypeGenerator.qualifiedClassName(params, elementType);        String head =            buildFromTermListElement(elementType, "(aterm.ATerm) children.get(0)");        println("      " + elementTypeName + " head = " + head + ";");        genFromTermSeparatorFieldAssigments(type);        String separatorArgs = buildOptionalSeparatorArguments(type);        println(            "      result = " + makeClassName + "(head, " + separatorArgs + "result);");        println("    }");        println();        println("    return result;");        println("  }");        println();    }