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.model.extended.Element;
012: import net.sourceforge.chaperon.model.extended.Pattern;
013:
014: /**
015: * Representation of a kernel state. q1 := q2 q2 q1=previous q2=pattern Possible positions: q1
016: * := .q2 q2 Shift q2 q1 := q2 .q2 Goto q2 / Reduce q2(0) if q2 is final state q1 := q2
017: * q2. Reduce q1(2)
018: *
019: * @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
020: * @version CVS $Id: Item.java,v 1.1 2004/01/04 16:49:12 benedikta Exp $
021: */
022: public class Item {
023: public Item next = null;
024:
025: // reduce to the symbol, or previous pattern
026: public String symbol;
027: public Pattern previous;
028:
029: // definition of the item
030: public Pattern pattern;
031:
032: // possible positions: 0,1,2
033: public int position;
034: public static final int SHIFT = 0;
035: public static final int GOTO = 1;
036: public static final int REDUCE = 2;
037:
038: // lookahead pattern of the item
039: public Pattern lookahead;
040: public boolean end = false;
041:
042: public Item(String symbol, Pattern pattern, int position,
043: Pattern lookahead) {
044: if (symbol == null)
045: throw new IllegalArgumentException("Symbol is null");
046:
047: if (pattern == null)
048: throw new IllegalArgumentException("Pattern is null");
049:
050: if ((position < 0) && (position > 2))
051: throw new IllegalArgumentException(
052: "Only item positions 0,1 and 2 are allowed");
053:
054: if (lookahead instanceof Element)
055: throw new IllegalStateException("bla");
056:
057: this .symbol = symbol;
058: this .previous = null;
059: this .pattern = pattern;
060: this .position = position;
061:
062: //this.end = end;
063: this .lookahead = lookahead;
064: }
065:
066: public Item(Pattern previous, Pattern pattern, int position,
067: Pattern lookahead) {
068: if (previous == null)
069: throw new IllegalArgumentException(
070: "Previous pattern is null");
071:
072: if (pattern == null)
073: throw new IllegalArgumentException("Pattern is null");
074:
075: if ((position < 0) && (position > 2))
076: throw new IllegalArgumentException(
077: "Only item positions 0,1 and 2 are allowed");
078:
079: if (lookahead instanceof Element)
080: throw new IllegalStateException("bla");
081:
082: this .symbol = null;
083: this .previous = previous;
084: this .pattern = pattern;
085: this .position = position;
086:
087: //this.end = end;
088: this .lookahead = lookahead;
089: }
090:
091: public Item getFollowItem() {
092: if (position < REDUCE) {
093: if (symbol != null)
094: return new Item(symbol, pattern, position + 1,
095: lookahead);
096:
097: return new Item(previous, pattern, position + 1, lookahead);
098: } else
099: return null;
100: }
101:
102: public boolean equals(Object o) {
103: if (o instanceof Item) {
104: Item item = (Item) o;
105:
106: if (symbol != null)
107: return (item.symbol != null)
108: && symbol.equals(item.symbol)
109: && (pattern == item.pattern)
110: && (position == item.position)
111: && (lookahead == item.lookahead);
112:
113: return (previous == item.previous)
114: && (pattern == item.pattern)
115: && (position == item.position)
116: && (lookahead == item.lookahead);
117: }
118:
119: return false;
120: }
121:
122: public String toString() {
123: StringBuffer buffer = new StringBuffer();
124:
125: if (symbol != null)
126: buffer.append(symbol);
127: else
128: buffer.append(previous);
129:
130: buffer.append(" := ");
131:
132: if (position == SHIFT)
133: buffer.append(".");
134:
135: buffer.append(pattern);
136: buffer.append(" ");
137:
138: if (position == GOTO)
139: buffer.append(".");
140:
141: buffer.append(pattern);
142:
143: if (position == REDUCE)
144: buffer.append(".");
145:
146: buffer.append(",");
147: buffer.append(lookahead);
148:
149: return buffer.toString();
150: }
151: }
|