KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > chaperon > process > extended > DefinitionStackNode


1 /*
2  * Copyright (C) Chaperon. All rights reserved.
3  * -------------------------------------------------------------------------
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  */

8
9 package net.sourceforge.chaperon.process.extended;
10
11 import net.sourceforge.chaperon.common.Decoder;
12
13 import org.xml.sax.ContentHandler JavaDoc;
14 import org.xml.sax.SAXException JavaDoc;
15 import org.xml.sax.helpers.AttributesImpl JavaDoc;
16
17 public class DefinitionStackNode extends StackNode
18 {
19   public DefinitionStackNode(ReduceAction action, int index, StackNode first, StackNode second,
20                              State state, StackNode ancestor)
21   {
22     this.action = action;
23     this.index = index;
24     this.first = first;
25     this.second = second;
26     this.state = state;
27     this.ancestor = ancestor;
28     this.ancestors = new StackNode[]{ancestor};
29
30     if (first!=null)
31     {
32       this.columnNumber = first.columnNumber;
33       this.lineNumber = first.lineNumber;
34       this.length = first.length+second.length;
35     }
36     else
37     {
38       this.columnNumber = ancestor.columnNumber+ancestor.length;
39       this.lineNumber = ancestor.lineNumber;
40       this.length = 0;
41     }
42   }
43
44   public ReduceAction action = null;
45   public int index = 0; // index of reduce action
46
public StackNode first = null;
47   public StackNode second = null;
48
49   public boolean compare(StackNode node)
50   {
51     if (node instanceof DefinitionStackNode)
52     {
53       DefinitionStackNode definitionStackNode = (DefinitionStackNode)node;
54       if (action.symbol!=null)
55         return super.compare(node) && (action.symbol.equals(definitionStackNode.action.symbol));
56
57       return super.compare(node) && (action.pattern==definitionStackNode.action.pattern);
58     }
59     else
60       return false;
61   }
62
63   public String JavaDoc toString()
64   {
65     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
66
67     if (first!=null)
68       buffer.append(first.toString());
69
70     if (second!=null)
71       buffer.append(second.toString());
72
73     return buffer.toString();
74   }
75
76   public String JavaDoc toCanonicalString(ExtendedParserAutomaton automaton)
77   {
78     String JavaDoc text = toString();
79
80     if (text.length()<=10)
81       text = Decoder.toString(text);
82     else
83       text = "";
84
85     if (ancestors.length>1)
86     {
87       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
88
89       buffer.append("(");
90       for (int i = 0; i<ancestors.length; i++)
91       {
92         if (i>0)
93           buffer.append("|");
94
95         buffer.append(ancestor.toCanonicalString(automaton));
96       }
97
98       buffer.append(")");
99
100       buffer.append("<-"+automaton.indexOf(state)+":"+
101                     ((action.symbol!=null) ? action.symbol : action.pattern.toString())+":"+text);
102
103       return buffer.toString();
104     }
105
106     return ((ancestor!=null) ? (ancestor.toCanonicalString(automaton)+"<-") : "")+
107            automaton.indexOf(state)+":"+
108            ((action.symbol!=null) ? action.symbol : action.pattern.toString())+":"+text;
109   }
110
111   public void toXML(ContentHandler JavaDoc contentHandler) throws SAXException JavaDoc
112   {
113     if (action.symbol!=null)
114       contentHandler.startElement(ExtendedBacktrackingParserProcessor.NS_OUTPUT, action.symbol,
115                                   action.symbol, new AttributesImpl JavaDoc());
116
117     if (first!=null)
118       first.toXML(contentHandler);
119
120     if (second!=null)
121       second.toXML(contentHandler);
122
123     if (action.symbol!=null)
124       contentHandler.endElement(ExtendedBacktrackingParserProcessor.NS_OUTPUT, action.symbol,
125                                 action.symbol);
126   }
127 }
128
Popular Tags