1 package jdk.incubator.code;
2
3 /**
4 * Source location information.
5 *
6 * @param sourceRef the reference to the source
7 * @param line the line in the source
8 * @param column the column in the source
9 */
10 public record Location(String sourceRef, int line, int column) {
11
12 /**
13 * The location value, {@code null}, indicating no location information.
14 */
15 public static final Location NO_LOCATION = null;
16
17 public Location(int line, int column) {
18 this(null, line, column);
19 }
20
21 @Override
22 public String toString() {
23 StringBuilder s = new StringBuilder();
24 s.append(line).append(":").append(column);
25 if (sourceRef != null) {
26 s.append(":").append(sourceRef);
27 }
28 return s.toString();
29 }
30
31 public static Location fromString(String s) {
32 String[] split = s.split(":", 3);
33 if (split.length < 2) {
34 throw new IllegalArgumentException();
35 }
36
37 int line = Integer.parseInt(split[0]);
38 int column = Integer.parseInt(split[1]);
39 String sourceRef;
40 if (split.length == 3) {
41 sourceRef = split[2];
42 } else {
43 sourceRef = null;
44 }
45 return new Location(sourceRef, line, column);
46 }
47 }