comparison truffle/com.oracle.truffle.tools.debug.shell/src/com/oracle/truffle/tools/debug/shell/client/REPLineLocation.java @ 21951:9c8c0937da41

Moving all sources into truffle subdirectory
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Wed, 17 Jun 2015 10:58:08 +0200
parents graal/com.oracle.truffle.tools.debug.shell/src/com/oracle/truffle/tools/debug/shell/client/REPLineLocation.java@3b8bbf51d320
children dc83cc1f94f2
comparison
equal deleted inserted replaced
21950:2a5011c7e641 21951:9c8c0937da41
1 /*
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package com.oracle.truffle.tools.debug.shell.client;
26
27 import java.io.*;
28
29 import com.oracle.truffle.api.source.*;
30 import com.oracle.truffle.tools.debug.shell.*;
31
32 final class REPLineLocation {
33
34 private final Source source;
35 private final int lineNumber;
36
37 /**
38 * Attempts to extract description of a source line from {@code arg[1]}, either
39 * "<source name>:<n>" or just "<n>".
40 */
41 static REPLineLocation parse(REPLClientContext context, String[] args) throws IllegalArgumentException {
42 if (args.length == 1) {
43 throw new IllegalArgumentException("no location specified");
44 }
45
46 Source source = null;
47 int lineNumber = -1;
48 String lineNumberText = null;
49
50 final String[] split = args[1].split(":");
51 if (split.length == 1) {
52 // Specification only has one part; it should be a line number
53 lineNumberText = split[0];
54 try {
55 lineNumber = Integer.parseInt(lineNumberText);
56 } catch (NumberFormatException e) {
57 throw new IllegalArgumentException("no line number specified");
58 }
59 // If only line number specified, then there must be a selected file
60 final Source selectedSource = context.getSelectedSource();
61 if (selectedSource == null) {
62 throw new IllegalArgumentException("no selected file set");
63 }
64 source = selectedSource;
65
66 } else {
67 final String fileName = split[0];
68 lineNumberText = split[1];
69 try {
70 source = Source.fromFileName(fileName);
71 } catch (IOException e1) {
72 throw new IllegalArgumentException("Can't find file \"" + fileName + "\"");
73 }
74 try {
75 lineNumber = Integer.parseInt(lineNumberText);
76 } catch (NumberFormatException e) {
77 throw new IllegalArgumentException("Invalid line number \"" + lineNumberText + "\"");
78 }
79 if (lineNumber <= 0) {
80 throw new IllegalArgumentException("Invalid line number \"" + lineNumberText + "\"");
81 }
82 }
83
84 return new REPLineLocation(source, lineNumber);
85 }
86
87 REPLineLocation(Source source, int lineNumber) {
88 this.source = source;
89 this.lineNumber = lineNumber;
90 }
91
92 public Source getSource() {
93 return source;
94 }
95
96 public int getLineNumber() {
97 return lineNumber;
98 }
99
100 /**
101 * Creates a message containing an "op" and a line location.
102 *
103 * @param op the operation to be performed on this location
104 */
105 public REPLMessage createMessage(String op) {
106 final REPLMessage msg = new REPLMessage(REPLMessage.OP, op);
107 msg.put(REPLMessage.SOURCE_NAME, source.getShortName());
108 msg.put(REPLMessage.FILE_PATH, source.getPath());
109 msg.put(REPLMessage.LINE_NUMBER, Integer.toString(lineNumber));
110 return msg;
111 }
112
113 }