@Override
  public StringBuilder visitDefinition_function(
      @NotNull SiddhiQLParser.Definition_functionContext ctx) {
    StringBuilder functionText = new StringBuilder(" {  \"Function\":  ");

    functionText
        .append("  [{ \"functionName\":\"")
        .append(ctx.function_name().getText())
        .append("\",");
    functionText.append("\"languageName\":\"").append(ctx.language_name().getText()).append("\",");

    functionText
        .append("\"attributeType\":\"")
        .append(ctx.attribute_type().getText())
        .append("\",");
    String functionBody =
        excapeCode(
            tokenStreamRewriter
                .getTokenStream()
                .getText(ctx.function_body().getStart(), ctx.function_body().getStop()));
    functionText.append("\"functionBody\":\"").append(functionBody).append("\",");
    ;
    String function_Text =
        excapeCode(tokenStreamRewriter.getTokenStream().getText(ctx.getStart(), ctx.getStop()));
    functionText.append("\"functionText\":\"").append(function_Text).append("\",");

    functionText = new StringBuilder(functionText.substring(0, functionText.length() - 1));
    functionText.append(" }]}");

    return functionText;
  }
  @Override
  public StringBuilder visitQuery(@NotNull SiddhiQLParser.QueryContext ctx) {
    StringBuilder queryText = new StringBuilder(" {  \"Query\": [{ ");

    if (ctx.annotation().isEmpty()) {
      queryText.append(" \"annotationElement\":\"").append("query").append("\",");
    } else {
      queryText
          .append("\"annotationElement\":\"")
          .append(ctx.annotation().get(0).annotation_element().get(0).property_value().getText())
          .append("\",");
    }

    queryText.append("\"inputStream\":").append(visit(ctx.query_input())).append(",");

    if (ctx.query_section() != null) {
      queryText.append("\"selection\":\"").append(ctx.query_section().getText()).append("\",");
    }

    queryText.append("\"outputStream\":").append(visit(ctx.query_output())).append(",");

    String q_Text =
        excapeCode(tokenStreamRewriter.getTokenStream().getText(ctx.getStart(), ctx.getStop()));
    queryText.append("\"query_Text\":\"").append(q_Text).append("\",");

    queryText = new StringBuilder(queryText.substring(0, queryText.length() - 1));
    queryText.append(" }]}");

    return queryText;
  }
  @Override
  public StringBuilder visitPartition(@NotNull SiddhiQLParser.PartitionContext ctx) {
    StringBuilder partitionText = new StringBuilder(" { \"Partition\":  ");

    for (SiddhiQLParser.Partition_with_streamContext with_streamContext :
        ctx.partition_with_stream()) {
      String pw_Text =
          excapeCode(
              tokenStreamRewriter
                  .getTokenStream()
                  .getText(with_streamContext.getStart(), with_streamContext.getStop()));
      partitionText.append("  [{  \"Partition_with_Text\":\"").append(pw_Text).append("\", ");
    }

    for (SiddhiQLParser.Partition_with_streamContext with_streamContext :
        ctx.partition_with_stream()) {
      partitionText.append(visitPartition_with_stream(with_streamContext));
    }

    partitionText.append(" \"Query_size\":").append(ctx.query().size()).append(",");

    for (int i = 0; i < ctx.query().size(); i++) {
      SiddhiQLParser.QueryContext queryContext = ctx.query().get(i);
      partitionText
          .append("\"Query_" + i + "\" : [")
          .append(visitQuery(queryContext))
          .append("], ");
    }
    String p_Text =
        excapeCode(tokenStreamRewriter.getTokenStream().getText(ctx.getStart(), ctx.getStop()));
    partitionText.append(" \"Partition_Text\":\"").append(p_Text).append("\",");

    partitionText = new StringBuilder(partitionText.substring(0, partitionText.length() - 1));
    partitionText.append(" }]}");

    return partitionText;
  }
  @Override
  public StringBuilder visitDefinition_table(@NotNull SiddhiQLParser.Definition_tableContext ctx) {
    StringBuilder tableText = new StringBuilder(" { \"Table\":  ");

    tableText
        .append("  [{ \"tableId\":\"")
        .append(ctx.source().stream_id().getText())
        .append("\",");

    String table_Text =
        excapeCode(tokenStreamRewriter.getTokenStream().getText(ctx.getStart(), ctx.getStop()));
    tableText.append(" \"table_Text\":\"").append(table_Text).append("\",");
    tableText = new StringBuilder(tableText.substring(0, tableText.length() - 1));
    tableText.append(" }]}");

    return tableText;
  }
  @Override
  public StringBuilder visitDefinition_stream(
      @NotNull SiddhiQLParser.Definition_streamContext ctx) {

    StringBuilder streamText = new StringBuilder("  {\"Stream\":  ");

    streamText
        .append("  [{ \"streamId\":\"")
        .append(ctx.source().stream_id().getText())
        .append("\" ,");

    String stream_Text =
        excapeCode(tokenStreamRewriter.getTokenStream().getText(ctx.getStart(), ctx.getStop()));
    streamText.append(" \"stream_Text\":\"").append(stream_Text).append("\" ,");

    if (ctx.annotation().isEmpty()) {
      streamText.append(" \"annoName\": ").append("null").append(" ,");
      streamText.append(" \"annoElement\": ").append("null").append(" ,");
    } else {
      for (SiddhiQLParser.AnnotationContext annotationContext : ctx.annotation()) {
        streamText
            .append(" \"annoName\":\"")
            .append(annotationContext.name().getText())
            .append("\" ,");
        if (annotationContext.annotation_element().isEmpty()) {
          streamText.append(" \"annoElement\": ").append("null").append(" ,");
        } else {
          for (SiddhiQLParser.Annotation_elementContext elementContext :
              annotationContext.annotation_element()) {
            streamText
                .append(" \"annoElement\":\"")
                .append(elementContext.property_value().getText())
                .append("\" ,");
          }
        }
      }
    }
    streamText = new StringBuilder(streamText.substring(0, streamText.length() - 1));
    streamText.append(" }]}");

    return streamText;
  }
  @Override
  public StringBuilder visitPartition_with_stream(
      @NotNull SiddhiQLParser.Partition_with_streamContext ctx) {
    StringBuilder partitionWith = new StringBuilder(" \"PartitionWith\": [{  ");

    partitionWith
        .append(" \"Partition_Stream\":\"")
        .append(ctx.stream_id().getText())
        .append("\", ");

    List<String> condition = new ArrayList<String>();
    List<String> attribute = new ArrayList<String>();

    if (ctx.condition_ranges() != null) {
      for (SiddhiQLParser.Condition_rangeContext context :
          ctx.condition_ranges().condition_range()) {
        condition.add(
            "\""
                + excapeCode(
                    tokenStreamRewriter
                        .getTokenStream()
                        .getText(context.expression().getStart(), context.expression().getStop()))
                + "\"");
        attribute.add("\"" + context.string_value().getText() + "\"");
      }
      partitionWith.append(" \"condition\":").append(condition).append(", ");
      partitionWith.append(" \"attribute\":").append(attribute).append(" ");

    } else if (ctx.attribute() != null) {
      condition.add("null");
      attribute.add("\"" + ctx.attribute().getText() + "\"");
      partitionWith.append(" \"condition\":").append(condition).append(", ");
      partitionWith.append(" \"attribute\":").append(attribute).append("  ");
    }
    partitionWith = new StringBuilder(partitionWith.substring(0, partitionWith.length() - 1));
    partitionWith.append(" }],");

    return partitionWith;
  }
  @Override
  public StringBuilder visitDefinition_trigger(
      @NotNull SiddhiQLParser.Definition_triggerContext ctx) {
    StringBuilder triggerText = new StringBuilder(" {  \"Trigger\":  ");

    triggerText
        .append("  [{ \"triggerName\":\"")
        .append(ctx.trigger_name().getText())
        .append("\",");
    if (ctx.time_value() != null) {
      triggerText.append(" \"triggerValue\":\"").append(ctx.time_value().getText()).append("\",");
    } else {
      triggerText.append(" \"triggerValue\":\"").append(ctx.string_value().getText()).append("\",");
    }
    String trigger_text =
        excapeCode(tokenStreamRewriter.getTokenStream().getText(ctx.getStart(), ctx.getStop()));
    triggerText.append(" \"triggerText\":\"").append(trigger_text).append("\",");

    triggerText = new StringBuilder(triggerText.substring(0, triggerText.length() - 1));
    triggerText.append(" }]}");

    return triggerText;
  }