diff graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/SLStatementNode.java @ 16913:b879421229db

SL: better toString for SL nodes.
author Christian Humer <christian.humer@gmail.com>
date Sat, 23 Aug 2014 19:31:33 +0200
parents 7661cc464239
children 826c172a048f
line wrap: on
line diff
--- a/graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/SLStatementNode.java	Sat Aug 23 19:31:29 2014 +0200
+++ b/graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/SLStatementNode.java	Sat Aug 23 19:31:33 2014 +0200
@@ -52,6 +52,39 @@
     }
 
     @Override
+    public String toString() {
+        return formatSourceSection(this);
+    }
+
+    /**
+     * Formats a source section of a node in human readable form. If no source section could be
+     * found it looks up the parent hierarchy until it finds a source section. Nodes where this was
+     * required append a <code>'~'</code> at the end.
+     *
+     * @param node the node to format.
+     * @return a formatted source section string
+     */
+    private static String formatSourceSection(Node node) {
+        if (node == null) {
+            return "<unknown>";
+        }
+        SourceSection section = node.getSourceSection();
+        boolean estimated = false;
+        if (section == null) {
+            section = node.getEncapsulatingSourceSection();
+            estimated = true;
+        }
+
+        if (section == null || section.getSource() == null) {
+            return "<unknown source>";
+        } else {
+            String sourceName = new File(section.getSource().getName()).getName();
+            int startLine = section.getStartLine();
+            return String.format("%s:%d%s", sourceName, startLine, estimated ? "~" : "");
+        }
+    }
+
+    @Override
     public Probe probe(ExecutionContext context) {
         Node parent = getParent();