/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. */
  31. package edu.vub.at.trace;
  32. import java.io.IOException;
  33. import java.io.Serializable;
  34. /**
  35. * A source code location.
  36. */
  37. public class CallSite implements Serializable {
  38. static private final long serialVersionUID = 1L;
  39. /**
  40. * call site's human meaningful name within the {@linkplain #source}
  41. */
  42. public final String name;
  43. /**
  44. * path to the source code containing the call site
  45. */
  46. public final String source;
  47. /**
  48. * call site's position within the {@linkplain #source} (optional)
  49. * <p>
  50. * The expected structure of this table defines a span from the start of the
  51. * relevant source code to the end. The first row in the table is the start
  52. * of the span and the second row is the end of the span. Each row lists the
  53. * line number followed by the column number. For example, a span of code
  54. * starting on line 5, column 8 and ending on line 6, column 12 is encoded
  55. * as:
  56. * </p>
  57. * <p>
  58. * <code>[ [ 5, 8 ], [ 6, 12 ] ]</code>
  59. * </p>
  60. * <p>
  61. * The delimited span is inclusive, meaning the character at line 6, column
  62. * 12 is included in the span defined above.
  63. * </p>
  64. * <p>
  65. * If the end of the span is unknown, it may be omitted. If the column
  66. * number is unknown, it may also be omitted. For example, in the case where
  67. * only the starting line number is known:
  68. * </p>
  69. * <p>
  70. * <code>[ [ 5 ] ]</code>
  71. * </p>
  72. * <p>
  73. * If source span information is unknown, this member is <code>null</code>.
  74. * </p>
  75. * <p>
  76. * Both lines and columns are numbered starting from one, so the first
  77. * character in a source file is at <code>[ 1, 1 ]</code>. A column is a
  78. * UTF-16 code unit, the same unit represented by a Java <code>char</code>.
  79. * Lines are separated by any character sequence considered a Unicode <a
  80. * href="http://en.wikipedia.org/wiki/Newline#Unicode">line terminator</a>.
  81. * </p>
  82. */
  83. public final int[][] span;
  84. /**
  85. * Constructs an instance.
  86. * @param name {@link #name}
  87. * @param source {@link #source}
  88. * @param span {@link #span}
  89. */
  90. public CallSite(final String name,
  91. final String source,
  92. final int[][] span) {
  93. this.name = name;
  94. this.source = source;
  95. this.span = span;
  96. }
  97. /**
  98. * { "name" : "foo()",
  99. * "source" : "foo.at",
  100. * "span" : [ [ lineNo ] ] }
  101. */
  102. public void toJSON(JSONWriter json) throws IOException {
  103. JSONWriter.ObjectWriter callsite = json.startObject();
  104. callsite.startMember("name").writeString(name);
  105. callsite.startMember("source").writeString(source == null ? "unknown source" : source);
  106. if (span != null) {
  107. JSONWriter.ArrayWriter startAndEnd = callsite.startMember("span").startArray();
  108. for (int i = 0; i < span.length; i++) {
  109. JSONWriter.ArrayWriter lineAndCol = startAndEnd.startElement().startArray();
  110. for (int j = 0; j < span[i].length; j++) {
  111. lineAndCol.startElement().writeInt(span[i][j]);
  112. }
  113. lineAndCol.finish();
  114. }
  115. startAndEnd.finish();
  116. }
  117. callsite.finish();
  118. }
  119. }