/*
* Copyright (C) Chaperon. All rights reserved.
* -------------------------------------------------------------------------
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package net.sourceforge.chaperon.process.extended;
import net.sourceforge.chaperon.common.Decoder;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
public class DefinitionStackNode extends StackNode
{
public DefinitionStackNode(ReduceAction action, int index, StackNode first, StackNode second,
State state, StackNode ancestor)
{
this.action = action;
this.index = index;
this.first = first;
this.second = second;
this.state = state;
this.ancestor = ancestor;
this.ancestors = new StackNode[]{ancestor};
if (first!=null)
{
this.columnNumber = first.columnNumber;
this.lineNumber = first.lineNumber;
this.length = first.length+second.length;
}
else
{
this.columnNumber = ancestor.columnNumber+ancestor.length;
this.lineNumber = ancestor.lineNumber;
this.length = 0;
}
}
public ReduceAction action = null;
public int index = 0; // index of reduce action
public StackNode first = null;
public StackNode second = null;
public boolean compare(StackNode node)
{
if (node instanceof DefinitionStackNode)
{
DefinitionStackNode definitionStackNode = (DefinitionStackNode)node;
if (action.symbol!=null)
return super.compare(node) && (action.symbol.equals(definitionStackNode.action.symbol));
return super.compare(node) && (action.pattern==definitionStackNode.action.pattern);
}
else
return false;
}
public String toString()
{
StringBuffer buffer = new StringBuffer();
if (first!=null)
buffer.append(first.toString());
if (second!=null)
buffer.append(second.toString());
return buffer.toString();
}
public String toCanonicalString(ExtendedParserAutomaton automaton)
{
String text = toString();
if (text.length()<=10)
text = Decoder.toString(text);
else
text = "";
if (ancestors.length>1)
{
StringBuffer buffer = new StringBuffer();
buffer.append("(");
for (int i = 0; i<ancestors.length; i++)
{
if (i>0)
buffer.append("|");
buffer.append(ancestor.toCanonicalString(automaton));
}
buffer.append(")");
buffer.append("<-"+automaton.indexOf(state)+":"+
((action.symbol!=null) ? action.symbol : action.pattern.toString())+":"+text);
return buffer.toString();
}
return ((ancestor!=null) ? (ancestor.toCanonicalString(automaton)+"<-") : "")+
automaton.indexOf(state)+":"+
((action.symbol!=null) ? action.symbol : action.pattern.toString())+":"+text;
}
public void toXML(ContentHandler contentHandler) throws SAXException
{
if (action.symbol!=null)
contentHandler.startElement(ExtendedBacktrackingParserProcessor.NS_OUTPUT, action.symbol,
action.symbol, new AttributesImpl());
if (first!=null)
first.toXML(contentHandler);
if (second!=null)
second.toXML(contentHandler);
if (action.symbol!=null)
contentHandler.endElement(ExtendedBacktrackingParserProcessor.NS_OUTPUT, action.symbol,
action.symbol);
}
}