# HG changeset patch # User Thomas Wuerthinger # Date 1366721964 -7200 # Node ID 4497235516df0892e3c34fd9a73bb846af15b144 # Parent ba3dfa9e36d8510c71c8a79f4f81c85aa597793a New API for representing Source objects and SourceSection objects. SourceSection objects can be associated with Truffle interpreter nodes. diff -r ba3dfa9e36d8 -r 4497235516df graal/com.oracle.truffle.api/src/com/oracle/truffle/api/Source.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/Source.java Tue Apr 23 14:59:24 2013 +0200 @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.truffle.api; + +/** + * Represents the source code of a guest language program. + */ +public interface Source { + + /** + * Returns the name of this resource holding a guest language program. An example would be the + * name of a guest language source code file. + * + * @return the name of the guest language program + */ + String getName(); + + /** + * Returns the guest language source code represented by this source object. + * + * @return the source code as a String object + */ + String getCode(); +} diff -r ba3dfa9e36d8 -r 4497235516df graal/com.oracle.truffle.api/src/com/oracle/truffle/api/SourceSection.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/SourceSection.java Tue Apr 23 14:59:24 2013 +0200 @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.truffle.api; + +/** + * Represents a section in the source code of a guest language program. + */ +public class SourceSection { + + private final Source source; + private final String identifier; + private final int startLine; + private final int startColumn; + private final int endLine; + private final int endColumn; + + /** + * Creates a new object representing a section in the source code of a guest language program. + * + * @param source object representing the source program this is should be a section of + * @param identifier an identifier used when printing the section + * @param startLine the index of the start line of the section (inclusive) + * @param startColumn the index of the start column of the section (inclusive) + * @param endLine the index of the end line of the section (inclusive) + * @param endColumn the index of the end column of the section (inclusive) + */ + public SourceSection(Source source, String identifier, int startLine, int startColumn, int endLine, int endColumn) { + this.source = source; + this.identifier = identifier; + this.startLine = startLine; + this.startColumn = startColumn; + this.endLine = endLine; + this.endColumn = endColumn; + } + + /** + * Returns the source object representing the source program this is a section of. + * + * @return the source object + */ + public final Source getSource() { + return source; + } + + /** + * Returns the index of the start line of this source section (inclusive). + * + * @return the start line + */ + public final int getStartLine() { + return startLine; + } + + /** + * Returns the index of the start column of this source section (inclusive). + * + * @return the start column + */ + public final int getStartColumn() { + return startColumn; + } + + /** + * Returns the index of the end line of this source section (inclusive). + * + * @return the end line + */ + public final int getEndLine() { + return endLine; + } + + /** + * Returns the index of the end column of this source section (inclusive). + * + * @return the end column + */ + public final int getEndColumn() { + return endColumn; + } + + /** + * Returns the identifier of this source section that is used for printing the section. + * + * @return the identifier of the section + */ + public final String getIdentifier() { + return identifier; + } +} diff -r ba3dfa9e36d8 -r 4497235516df graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/Node.java --- a/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/Node.java Tue Apr 23 11:29:55 2013 +0200 +++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/Node.java Tue Apr 23 14:59:24 2013 +0200 @@ -25,6 +25,8 @@ import java.lang.annotation.*; import java.util.*; +import com.oracle.truffle.api.*; + /** * Abstract base class for all Truffle nodes. */ @@ -37,6 +39,8 @@ private Node parent; + private SourceSection sourceSection; + /** * Marks array fields that are children of this node. */ @@ -54,6 +58,34 @@ } /** + * Assigns a link to a guest language source section to this node. + * + * @param section the object representing a section in guest language source code + */ + public final void assignSourceSection(SourceSection section) { + if (sourceSection != null) { + throw new IllegalStateException("Source section is already assigned."); + } + this.sourceSection = section; + } + + /** + * Clears any previously assigned guest language source code from this node. + */ + public final void clearSourceSection() { + this.sourceSection = null; + } + + /** + * Retrieves the guest language source code section that is currently assigned to this node. + * + * @return the assigned source code section + */ + public final SourceSection getSourceSection() { + return sourceSection; + } + + /** * Method that updates the link to the parent in the array of specified new child nodes to this * node. * @@ -123,7 +155,7 @@ * @return the new node */ @SuppressWarnings({"unchecked"}) - public T replace(T newNode, String reason) { + public final T replace(T newNode, String reason) { assert this.getParent() != null; return (T) this.getParent().replaceChild(this, newNode); } @@ -134,7 +166,7 @@ * @param newNode the new node that is the replacement * @return the new node */ - public T replace(T newNode) { + public final T replace(T newNode) { return replace(newNode, ""); }