KickJava   Java API By Example, From Geeks To Geeks.

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


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.model.extended.Element;
12 import net.sourceforge.chaperon.model.extended.Pattern;
13
14 /**
15  * Representation of a kernel state. q1 := q2 q2 q1=previous q2=pattern Possible positions: q1
16  * := .q2 q2 Shift q2 q1 := q2 .q2 Goto q2 / Reduce q2(0) if q2 is final state q1 := q2
17  * q2. Reduce q1(2)
18  *
19  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels</a>
20  * @version CVS $Id: Item.java,v 1.1 2004/01/04 16:49:12 benedikta Exp $
21  */

22 public class Item
23 {
24   public Item next = null;
25
26   // reduce to the symbol, or previous pattern
27
public String JavaDoc symbol;
28   public Pattern previous;
29
30   // definition of the item
31
public Pattern pattern;
32
33   // possible positions: 0,1,2
34
public int position;
35   public static final int SHIFT = 0;
36   public static final int GOTO = 1;
37   public static final int REDUCE = 2;
38
39   // lookahead pattern of the item
40
public Pattern lookahead;
41   public boolean end = false;
42
43   public Item(String JavaDoc symbol, Pattern pattern, int position, Pattern lookahead)
44   {
45     if (symbol==null)
46       throw new IllegalArgumentException JavaDoc("Symbol is null");
47
48     if (pattern==null)
49       throw new IllegalArgumentException JavaDoc("Pattern is null");
50
51     if ((position<0) && (position>2))
52       throw new IllegalArgumentException JavaDoc("Only item positions 0,1 and 2 are allowed");
53
54     if (lookahead instanceof Element)
55       throw new IllegalStateException JavaDoc("bla");
56
57     this.symbol = symbol;
58     this.previous = null;
59     this.pattern = pattern;
60     this.position = position;
61
62     //this.end = end;
63
this.lookahead = lookahead;
64   }
65
66   public Item(Pattern previous, Pattern pattern, int position, Pattern lookahead)
67   {
68     if (previous==null)
69       throw new IllegalArgumentException JavaDoc("Previous pattern is null");
70
71     if (pattern==null)
72       throw new IllegalArgumentException JavaDoc("Pattern is null");
73
74     if ((position<0) && (position>2))
75       throw new IllegalArgumentException JavaDoc("Only item positions 0,1 and 2 are allowed");
76
77     if (lookahead instanceof Element)
78       throw new IllegalStateException JavaDoc("bla");
79
80     this.symbol = null;
81     this.previous = previous;
82     this.pattern = pattern;
83     this.position = position;
84
85     //this.end = end;
86
this.lookahead = lookahead;
87   }
88
89   public Item getFollowItem()
90   {
91     if (position<REDUCE)
92     {
93       if (symbol!=null)
94         return new Item(symbol, pattern, position+1, lookahead);
95
96       return new Item(previous, pattern, position+1, lookahead);
97     }
98     else
99       return null;
100   }
101
102   public boolean equals(Object JavaDoc o)
103   {
104     if (o instanceof Item)
105     {
106       Item item = (Item)o;
107
108       if (symbol!=null)
109         return (item.symbol!=null) && symbol.equals(item.symbol) && (pattern==item.pattern) &&
110                (position==item.position) && (lookahead==item.lookahead);
111
112       return (previous==item.previous) && (pattern==item.pattern) && (position==item.position) &&
113              (lookahead==item.lookahead);
114     }
115
116     return false;
117   }
118
119   public String JavaDoc toString()
120   {
121     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
122
123     if (symbol!=null)
124       buffer.append(symbol);
125     else
126       buffer.append(previous);
127
128     buffer.append(" := ");
129
130     if (position==SHIFT)
131       buffer.append(".");
132
133     buffer.append(pattern);
134     buffer.append(" ");
135
136     if (position==GOTO)
137       buffer.append(".");
138
139     buffer.append(pattern);
140
141     if (position==REDUCE)
142       buffer.append(".");
143
144     buffer.append(",");
145     buffer.append(lookahead);
146
147     return buffer.toString();
148   }
149 }
150
Popular Tags