public static void emitShader(TranslationUnit shader, Optional<String> license, PrintStream stream, int indentationWidth, Supplier<String> newlineSupplier, boolean emitGraphicsFuzzDefines) { new PrettyPrinterVisitor(stream, indentationWidth, newlineSupplier, emitGraphicsFuzzDefines, license).visit(shader); }
@Override public void visitBlockStmt(BlockStmt stmt) { out.append(indent() + "{" + newLine()); increaseIndent(); for (Stmt s : stmt.getStmts()) { visit(s); } decreaseIndent(); out.append(indent() + "}" + newLine()); }
@Override public void visitFunctionPrototype(FunctionPrototype functionPrototype) { visit(functionPrototype.getReturnType()); out.append(" " + functionPrototype.getName() + "("); boolean first = true; for (ParameterDecl p : functionPrototype.getParameters()) { if (!first) { out.append(", "); } first = false; visit(p); } out.append(")"); if (!inFunctionDefinition) { out.append(";"); } out.append(newLine()); }
@Override public void visitPrecisionDeclaration(PrecisionDeclaration precisionDeclaration) { out.append(indent() + precisionDeclaration.getText() + "\n\n"); }
private static void writeShader( TranslationUnit tu, Optional<String> license, File outputFile, boolean emitGraphicsFuzzDefines ) throws FileNotFoundException { try (PrintStream stream = ps(outputFile)) { PrettyPrinterVisitor.emitShader( tu, license, stream, PrettyPrinterVisitor.DEFAULT_INDENTATION_WIDTH, PrettyPrinterVisitor.DEFAULT_NEWLINE_SUPPLIER, emitGraphicsFuzzDefines ); } }
@Override public void visitTranslationUnit(TranslationUnit translationUnit) { if (translationUnit.hasShadingLanguageVersion()) { out.append("#version " + translationUnit.getShadingLanguageVersion().getVersionString() + "\n"); if (translationUnit.getShadingLanguageVersion().isWebGl()) { out.append("//WebGL\n"); } } if (license.isPresent()) { out.append(license.get() + "\n"); } if (emitGraphicsFuzzDefines) { emitGraphicsFuzzDefines(out); } super.visitTranslationUnit(translationUnit); }
@Override public void visitParenExpr(ParenExpr parenExpr) { out.append("("); visit(parenExpr.getExpr()); out.append(")"); }
@Override public void visitDoStmt(DoStmt doStmt) { out.append(indent() + "do" + newLine()); increaseIndent(); visit(doStmt.getBody()); decreaseIndent(); out.append(indent() + "while("); visit(doStmt.getCondition()); out.append(");" + newLine()); }
/** * Uses the pretty printer to turn a node into a text representation. * @return Text representation of a node */ default String getText() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (PrintStream stream = new PrintStream(baos)) { new PrettyPrinterVisitor(stream).visit(this); return baos.toString("UTF8"); } catch (UnsupportedEncodingException exception) { return "<unknown>"; } }
@Override public void visitWhileStmt(WhileStmt whileStmt) { out.append(indent() + "while("); visit(whileStmt.getCondition()); out.append(")" + newLine()); increaseIndent(); visit(whileStmt.getBody()); decreaseIndent(); }
/** * Returns, via pretty printing, a string representation of the given node. * * @param node Node for which string representation is required * @return String representation of the node */ public static String prettyPrintAsString(IAstNode node) { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); new PrettyPrinterVisitor(new PrintStream(bytes)).visit(node); return new String(bytes.toByteArray(), StandardCharsets.UTF_8); }
@Override public void visitArrayType(ArrayType arrayType) { // Do not generate array info, as this has to come after the associated variable name visit(arrayType.getBaseType()); }