001: /*
002: * Copyright (C) Chaperon. All rights reserved.
003: * -------------------------------------------------------------------------
004: * This software is published under the terms of the Apache Software License
005: * version 1.1, a copy of which has been included with this distribution in
006: * the LICENSE file.
007: */
008:
009: package net.sourceforge.chaperon.process.extended;
010:
011: import net.sourceforge.chaperon.common.Decoder;
012:
013: import org.xml.sax.ContentHandler;
014: import org.xml.sax.SAXException;
015: import org.xml.sax.helpers.AttributesImpl;
016:
017: public class DefinitionStackNode extends StackNode {
018: public DefinitionStackNode(ReduceAction action, int index,
019: StackNode first, StackNode second, State state,
020: StackNode ancestor) {
021: this .action = action;
022: this .index = index;
023: this .first = first;
024: this .second = second;
025: this .state = state;
026: this .ancestor = ancestor;
027: this .ancestors = new StackNode[] { ancestor };
028:
029: if (first != null) {
030: this .columnNumber = first.columnNumber;
031: this .lineNumber = first.lineNumber;
032: this .length = first.length + second.length;
033: } else {
034: this .columnNumber = ancestor.columnNumber + ancestor.length;
035: this .lineNumber = ancestor.lineNumber;
036: this .length = 0;
037: }
038: }
039:
040: public ReduceAction action = null;
041: public int index = 0; // index of reduce action
042: public StackNode first = null;
043: public StackNode second = null;
044:
045: public boolean compare(StackNode node) {
046: if (node instanceof DefinitionStackNode) {
047: DefinitionStackNode definitionStackNode = (DefinitionStackNode) node;
048: if (action.symbol != null)
049: return super .compare(node)
050: && (action.symbol
051: .equals(definitionStackNode.action.symbol));
052:
053: return super .compare(node)
054: && (action.pattern == definitionStackNode.action.pattern);
055: } else
056: return false;
057: }
058:
059: public String toString() {
060: StringBuffer buffer = new StringBuffer();
061:
062: if (first != null)
063: buffer.append(first.toString());
064:
065: if (second != null)
066: buffer.append(second.toString());
067:
068: return buffer.toString();
069: }
070:
071: public String toCanonicalString(ExtendedParserAutomaton automaton) {
072: String text = toString();
073:
074: if (text.length() <= 10)
075: text = Decoder.toString(text);
076: else
077: text = "";
078:
079: if (ancestors.length > 1) {
080: StringBuffer buffer = new StringBuffer();
081:
082: buffer.append("(");
083: for (int i = 0; i < ancestors.length; i++) {
084: if (i > 0)
085: buffer.append("|");
086:
087: buffer.append(ancestor.toCanonicalString(automaton));
088: }
089:
090: buffer.append(")");
091:
092: buffer.append("<-"
093: + automaton.indexOf(state)
094: + ":"
095: + ((action.symbol != null) ? action.symbol
096: : action.pattern.toString()) + ":" + text);
097:
098: return buffer.toString();
099: }
100:
101: return ((ancestor != null) ? (ancestor
102: .toCanonicalString(automaton) + "<-") : "")
103: + automaton.indexOf(state)
104: + ":"
105: + ((action.symbol != null) ? action.symbol
106: : action.pattern.toString()) + ":" + text;
107: }
108:
109: public void toXML(ContentHandler contentHandler)
110: throws SAXException {
111: if (action.symbol != null)
112: contentHandler.startElement(
113: ExtendedBacktrackingParserProcessor.NS_OUTPUT,
114: action.symbol, action.symbol, new AttributesImpl());
115:
116: if (first != null)
117: first.toXML(contentHandler);
118:
119: if (second != null)
120: second.toXML(contentHandler);
121:
122: if (action.symbol != null)
123: contentHandler.endElement(
124: ExtendedBacktrackingParserProcessor.NS_OUTPUT,
125: action.symbol, action.symbol);
126: }
127: }
|