PageRenderTime 22ms CodeModel.GetById 16ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/at2dist110511/src/edu/vub/at/trace/CallSite.java

http://ambienttalk.googlecode.com/
Java | 126 lines | 33 code | 7 blank | 86 comment | 5 complexity | a112abc51e00ef90861b443fe383a5d9 MD5 | raw file
  1/**
  2 * AmbientTalk/2 Project
  3 * (c) Software Languages Lab, 2006 - 2009
  4 * Authors: Ambient Group at SOFT
  5 * 
  6 * The source code in this file is based on source code from Tyler Close's
  7 * Waterken server, Copyright 2008 Waterken Inc. Waterken's code is published
  8 * under the MIT license.
  9 * 
 10 * Permission is hereby granted, free of charge, to any person
 11 * obtaining a copy of this software and associated documentation
 12 * files (the "Software"), to deal in the Software without
 13 * restriction, including without limitation the rights to use,
 14 * copy, modify, merge, publish, distribute, sublicense, and/or
 15 * sell copies of the Software, and to permit persons to whom the
 16 * Software is furnished to do so, subject to the following
 17 * conditions:
 18 *
 19 * The above copyright notice and this permission notice shall be
 20 * included in all copies or substantial portions of the Software.
 21 *
 22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 23 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 24 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 25 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 26 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 27 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 28 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 29 * OTHER DEALINGS IN THE SOFTWARE.
 30 */
 31package edu.vub.at.trace;
 32
 33import java.io.IOException;
 34import java.io.Serializable;
 35
 36/**
 37 * A source code location.
 38 */
 39public class CallSite implements Serializable {
 40    static private final long serialVersionUID = 1L;
 41    
 42    /**
 43     * call site's human meaningful name within the {@linkplain #source}
 44     */
 45    public final String name;
 46
 47    /**
 48     * path to the source code containing the call site
 49     */
 50    public final String source;
 51    
 52    /**
 53     * call site's position within the {@linkplain #source} (optional)
 54     * <p>
 55     * The expected structure of this table defines a span from the start of the
 56     * relevant source code to the end. The first row in the table is the start
 57     * of the span and the second row is the end of the span. Each row lists the
 58     * line number followed by the column number. For example, a span of code
 59     * starting on line 5, column 8 and ending on line 6, column 12 is encoded
 60     * as:
 61     * </p>
 62     * <p>
 63     * <code>[ [ 5, 8 ], [ 6, 12 ] ]</code>
 64     * </p>
 65     * <p>
 66     * The delimited span is inclusive, meaning the character at line 6, column
 67     * 12 is included in the span defined above.
 68     * </p>
 69     * <p>
 70     * If the end of the span is unknown, it may be omitted. If the column
 71     * number is unknown, it may also be omitted. For example, in the case where
 72     * only the starting line number is known:
 73     * </p>
 74     * <p>
 75     * <code>[ [ 5 ] ]</code>
 76     * </p>
 77     * <p>
 78     * If source span information is unknown, this member is <code>null</code>.
 79     * </p>
 80     * <p>
 81     * Both lines and columns are numbered starting from one, so the first
 82     * character in a source file is at <code>[ 1, 1 ]</code>. A column is a
 83     * UTF-16 code unit, the same unit represented by a Java <code>char</code>.
 84     * Lines are separated by any character sequence considered a Unicode <a
 85     * href="http://en.wikipedia.org/wiki/Newline#Unicode">line terminator</a>.
 86     * </p>
 87     */
 88    public final int[][] span;
 89    
 90    /**
 91     * Constructs an instance.
 92     * @param name      {@link #name}
 93     * @param source    {@link #source}
 94     * @param span      {@link #span}
 95     */
 96    public CallSite(final String name,
 97                    final String source,
 98                    final int[][] span) {
 99        this.name = name;
100        this.source = source;
101        this.span = span;
102    }
103    
104    /**
105     * { "name" : "foo()",
106     *   "source" : "foo.at",
107     *   "span" : [ [ lineNo ] ] }
108     */
109    public void toJSON(JSONWriter json) throws IOException {
110    	JSONWriter.ObjectWriter callsite = json.startObject();
111    	callsite.startMember("name").writeString(name);
112    	callsite.startMember("source").writeString(source == null ? "unknown source" : source);
113    	if (span != null) {
114        	JSONWriter.ArrayWriter startAndEnd = callsite.startMember("span").startArray();
115        	for (int i = 0; i < span.length; i++) {
116            	JSONWriter.ArrayWriter lineAndCol = startAndEnd.startElement().startArray();
117    			for (int j = 0; j < span[i].length; j++) {
118    				lineAndCol.startElement().writeInt(span[i][j]);
119    			}
120    			lineAndCol.finish();
121    		}
122        	startAndEnd.finish();	
123    	}
124    	callsite.finish();
125    }
126}