annotate graal/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/JSONHelper.java @ 16152:da0847068801

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