Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ public static Op fromStringOfJavaCodeModel(String in) {
}

static List<Op> parse(OpFactory opFactory, TypeElementFactory typeFactory, String in) {
in = in.replaceAll("\\033\\[\\d+m", ""); // remove ANSI coloring
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cute hack, but it makes me uncomfortable.

Lexer lexer = Scanner.factory().newScanner(in);
lexer.nextToken();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;

Expand All @@ -50,6 +51,11 @@ public final class OpWriter {
*/
static final String ATTRIBUTE_LOCATION = "loc";

/**
* Boolean property to provide ANSI-colored output.
*/
static final boolean COLOR = Boolean.getBoolean("jdk.incubator.code.extern.OpWriter.COLOR");

static final class GlobalValueBlockNaming implements Function<CodeItem, String> {
final Map<CodeItem, String> gn;
int valueOrdinal = 0;
Expand Down Expand Up @@ -337,11 +343,23 @@ public static VoidOpResultOption defaultValue() {
}
}

static BiFunction<Class<? extends CodeItem>, String, String> getDyer() {
return COLOR
? (itemType, text) ->
(itemType == Op.class ? "\033[34m" : // blue
itemType == Block.class ? "\033[35m": // purple
itemType == TypeElement.class ? "\033[32m": "\033[31m") // green : red
+ text
+ "\033[0m" // reset
: (_, text) -> text;
}

final Function<CodeItem, String> namer;
final IndentWriter w;
final boolean dropLocation;
final boolean dropOpDescendants;
final boolean writeVoidOpResult;
final BiFunction<Class<? extends CodeItem>, String, String> dyer;

/**
* Creates a writer of code models (operations) to their textual form.
Expand All @@ -354,6 +372,7 @@ public OpWriter(Writer w) {
this.dropLocation = false;
this.dropOpDescendants = false;
this.writeVoidOpResult = false;
this.dyer = getDyer();
}

/**
Expand Down Expand Up @@ -391,6 +410,7 @@ public OpWriter(Writer w, Option... options) {
this.dropLocation = dropLocation;
this.dropOpDescendants = dropOpDescendants;
this.writeVoidOpResult = writeVoidOpResult;
this.dyer = getDyer();
}

/**
Expand All @@ -413,7 +433,7 @@ public void writeOp(Op op) {
write(" = ");
}
}
write(op.opName());
write(Op.class, op.opName());

if (!op.operands().isEmpty()) {
write(" ");
Expand Down Expand Up @@ -519,18 +539,15 @@ void writeBlock(Block block) {
}

void writeBlockName(Block b) {
write("^");
write(namer.apply(b));
write(Block.class, "^" + namer.apply(b));
}

void writeValueUse(Value v) {
write("%");
write(namer.apply(v));
write(Value.class, "%" + namer.apply(v));
}

void writeValueDeclaration(Value v) {
write("%");
write(namer.apply(v));
write(Value.class, "%" + namer.apply(v));
write(" : ");
writeType(v.type());
}
Expand All @@ -555,7 +572,11 @@ <T> void writeSeparatedList(String separator, Iterable<T> l, Consumer<T> c) {
}

void writeType(TypeElement te) {
write(JavaTypeUtils.flatten(te.externalize()).toString());
write(TypeElement.class, JavaTypeUtils.flatten(te.externalize()).toString());
}

void write(Class<? extends CodeItem> itemType, String s) {
write(dyer.apply(itemType, s));
}

void write(String s) {
Expand Down