annotate graal/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/JSONHelper.java @ 16151:76895499bc88

Add facility to create JSON dump of AST creation/rewriting
author Christian Wirth <christian.wirth@oracle.com>
date Fri, 20 Jun 2014 15:13:16 +0200
parents
children da0847068801
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16151
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
1 /*
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
4 *
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
7 * published by the Free Software Foundation. Oracle designates this
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
8 * particular file as subject to the "Classpath" exception as provided
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
9 * by Oracle in the LICENSE file that accompanied this code.
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
10 *
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
11 * This code is distributed in the hope that it will be useful, but WITHOUT
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
14 * version 2 for more details (a copy is included in the LICENSE file that
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
15 * accompanied this code).
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
16 *
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
17 * You should have received a copy of the GNU General Public License version
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
18 * 2 along with this work; if not, write to the Free Software Foundation,
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
20 *
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
22 * or visit www.oracle.com if you need additional information or have any
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
23 * questions.
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
24 */
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
25 package com.oracle.truffle.api.utilities;
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
26
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
27 import com.oracle.truffle.api.nodes.*;
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
28 import com.oracle.truffle.api.source.*;
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
29
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
30 /**
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
31 * Helper function that allows to dump the AST during creation to a JSON format.
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
32 */
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
33 public class JSONHelper {
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
34
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
35 private static StringBuilder AstJsonDumpBuilder = new StringBuilder();
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
36
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
37 public static void dumpNewChild(Node parentNode, Node childNode) {
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
38 if (AstJsonDumpBuilder != null) {
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
39 AstJsonDumpBuilder.append("{ \"action\": \"insertNode\", \"parentId\": \"" + getID(parentNode) + "\", \"newId\": \"" + getID(childNode) + "\" },\n");
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
40 }
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
41 }
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
42
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
43 public static void dumpReplaceChild(Node oldNode, Node newNode, CharSequence reason) {
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
44 if (AstJsonDumpBuilder != null) {
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
45 AstJsonDumpBuilder.append("{ \"action\": \"replaceNode\", \"oldId\": \"" + getID(oldNode) + "\", \"newId\": \"" + getID(newNode) + "\", \"reason\": " + quote(reason) + " },\n");
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
46 }
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
47 }
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
48
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
49 public static void dumpNewNode(Node newNode) {
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
50 if (AstJsonDumpBuilder != null) {
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
51 AstJsonDumpBuilder.append("{ \"action\": \"createNode\", \"newId\": \"" + getID(newNode) + "\", \"newType\": \"" + getType(newNode) + "\"" + getSourceSectionInfo(newNode) + " },\n");
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
52 }
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
53 }
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
54
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
55 private static String getSourceSectionInfo(Node newNode) {
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
56 SourceSection sourceSection = newNode.getSourceSection();
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
57 if (sourceSection != null) {
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
58 return ", \"identifier\": \"" + sourceSection.getIdentifier() + "\" ";
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
59 } else {
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
60 return "";
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
61 }
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
62 }
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
63
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
64 public static String getResult() {
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
65 return AstJsonDumpBuilder.toString();
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
66 }
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
67
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
68 private static String getID(Node newChild) {
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
69 return String.valueOf(newChild.hashCode());
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
70 }
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
71
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
72 private static String getType(Node node) {
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
73 return node.getClass().getSimpleName();
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
74 }
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
75
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
76 private static String quote(CharSequence value) {
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
77 StringBuilder builder = new StringBuilder(value.length() + 2);
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
78 builder.append('"');
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
79 for (int i = 0; i < value.length(); i++) {
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
80 char c = value.charAt(i);
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
81 switch (c) {
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
82 case '"':
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
83 builder.append("\\\"");
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
84 break;
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
85 case '\\':
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
86 builder.append("\\\\");
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
87 break;
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
88 case '\b':
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
89 builder.append("\\b");
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
90 break;
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
91 case '\f':
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
92 builder.append("\\f");
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
93 break;
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
94 case '\n':
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
95 builder.append("\\n");
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
96 break;
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
97 case '\r':
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
98 builder.append("\\r");
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
99 break;
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
100 case '\t':
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
101 builder.append("\\t");
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
102 break;
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
103 default: {
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
104 if (c < ' ') {
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
105 builder.append("\\u00");
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
106 builder.append(Character.forDigit((c >> 4) & 0xF, 16));
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
107 builder.append(Character.forDigit(c & 0xF, 16));
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
108 } else {
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
109 builder.append(c);
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
110 }
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
111 }
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
112 }
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
113 }
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
114 builder.append('"');
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
115 return builder.toString();
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
116 }
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
117
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
118 public static void restart() {
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
119 AstJsonDumpBuilder = new StringBuilder();
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
120 }
76895499bc88 Add facility to create JSON dump of AST creation/rewriting
Christian Wirth <christian.wirth@oracle.com>
parents:
diff changeset
121 }