001: /*
002: * xtc - The eXTensible Compiler
003: * Copyright (C) 2006-2007 Robert Grimm
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * version 2 as published by the Free Software Foundation.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
017: * USA.
018: */
019: package xtc.parser;
020:
021: import java.io.IOException;
022:
023: import java.util.List;
024:
025: /**
026: * Element to create a parse tree node capturing formatting.
027: *
028: * @see xtc.tree.Formatting
029: *
030: * @author Robert Grimm
031: * @version $Revision: 1.4 $
032: */
033: public class ParseTreeNode extends Element implements InternalElement {
034:
035: /**
036: * The bindings capturing the values preceding the annotated node.
037: */
038: public List<Binding> predecessors;
039:
040: /**
041: * The binding capturing the annotated node, which may be
042: * <code>null</code>.
043: */
044: public Binding node;
045:
046: /**
047: * The bindings capturing the values succeeding the annotated node.
048: */
049: public List<Binding> successors;
050:
051: /**
052: * Create a new parse tree node.
053: *
054: * @param predecessors The predecessors.
055: * @param node The node.
056: * @param successors The successors.
057: */
058: public ParseTreeNode(List<Binding> predecessors, Binding node,
059: List<Binding> successors) {
060: this .predecessors = predecessors;
061: this .node = node;
062: this .successors = successors;
063: }
064:
065: public Tag tag() {
066: return Tag.PARSE_TREE_NODE;
067: }
068:
069: public int hashCode() {
070: return 49 * predecessors.hashCode() + 7 * successors.hashCode()
071: + (null == node ? 0 : node.hashCode());
072: }
073:
074: public boolean equals(Object o) {
075: if (this == o)
076: return true;
077: if (!(o instanceof ParseTreeNode))
078: return false;
079: ParseTreeNode other = (ParseTreeNode) o;
080: return (predecessors.equals(other.predecessors)
081: && (null == node ? null == other.node : node
082: .equals(other.node)) && successors
083: .equals(other.successors));
084: }
085:
086: public void write(Appendable out) throws IOException {
087: out.append("ParseTreeNode([");
088: boolean first = true;
089: for (Binding b : predecessors) {
090: if (first) {
091: first = false;
092: } else {
093: out.append(", ");
094: }
095: out.append(b.name);
096: }
097: out.append("], ");
098: if (null == node) {
099: out.append("null");
100: } else {
101: out.append(node.name);
102: }
103: out.append(", [");
104: first = true;
105: for (Binding b : successors) {
106: if (first) {
107: first = false;
108: } else {
109: out.append(", ");
110: }
111: out.append(b.name);
112: }
113: out.append("])");
114: }
115:
116: }
|