Exemplo n.º 1
0
  public static TextGenerationResult generateText(
      SNode node, boolean withDebugInfo, @Nullable StringBuilder[] buffers) {
    TextGenBuffer buffer = new TextGenBuffer(withDebugInfo, buffers);
    buffer.putUserObject(PACKAGE_NAME, node.getModel().getLongName());
    buffer.putUserObject(ROOT_NODE, node);
    appendNodeText(buffer, node, null);
    String topBufferText = buffer.getTopBufferText();
    int topLength =
        topBufferText.isEmpty() ? 1 : topBufferText.split(buffer.getLineSeparator(), -1).length + 2;

    // position info
    Map<SNode, TraceablePositionInfo> positionInfo = null;
    Map<SNode, ScopePositionInfo> scopeInfo = null;
    Map<SNode, UnitPositionInfo> unitInfo = null;
    if (withDebugInfo) {
      positionInfo =
          TraceInfoGenerationUtil.getUserObjects(buffer, TraceInfoGenerationUtil.POSITION_INFO);
      scopeInfo =
          TraceInfoGenerationUtil.getUserObjects(buffer, TraceInfoGenerationUtil.SCOPE_INFO);
      unitInfo = TraceInfoGenerationUtil.getUserObjects(buffer, TraceInfoGenerationUtil.UNIT_INFO);
      adjustPositions(topLength, positionInfo);
      adjustPositions(topLength, scopeInfo);
      adjustPositions(topLength, unitInfo);
    }

    // dependencies
    List<String> dependencies =
        getUserObjectCollection(
            DEPENDENCY, node, buffer, (Set<String>) buffer.getUserObject(EXTENDS));
    List<String> extend = getUserObjectCollection(EXTENDS, node, buffer, null);

    Map<String, List<String>> deps = new HashMap<String, List<String>>(2);
    deps.put(DEPENDENCY, dependencies);
    deps.put(EXTENDS, extend);

    Object result = buffer.getText();
    String outputEncoding = (String) buffer.getUserObject(OUTPUT_ENCODING);
    if (outputEncoding != null) {
      if (outputEncoding.equals("binary")) {
        result = EncodingUtil.decodeBase64((String) result);
      } else {
        try {
          result = EncodingUtil.encode((String) result, outputEncoding);
        } catch (IOException ex) {
          buffer.foundError("cannot encode the output stream", null, ex);
        }
      }
    }
    return new TextGenerationResult(
        node,
        result,
        buffer.hasErrors(),
        buffer.problems(),
        positionInfo,
        scopeInfo,
        unitInfo,
        deps);
  }
Exemplo n.º 2
0
 // compatibility stuff
 @Deprecated
 public static void appendNodeText(SNodeTextGen textGen, SNode node, TextGenBuffer buffer) {
   textGen.setBuffer(buffer);
   try {
     textGen.setSNode(node);
     textGen.doGenerateText(node);
     textGen.setSNode(null);
   } catch (Exception e) {
     buffer.foundError(
         "failed to generate text for "
             + org.jetbrains.mps.openapi.model.SNodeUtil.getDebugText(node),
         node,
         e);
   }
 }
Exemplo n.º 3
0
  /* package */ static void appendNodeText(
      TextGenBuffer buffer, SNode node, @Nullable SNode contextNode) {
    if (node == null) {
      buffer.append("???");

      if (contextNode != null) {
        buffer.foundError(
            "possible broken reference in "
                + org.jetbrains.mps.openapi.model.SNodeUtil.getDebugText(contextNode),
            contextNode,
            null);
      }

      return;
    }

    getTextGenForNode(node).doGenerateText(node, buffer);
  }