Source code: org/sablecc/sablecc/LR1Item.java
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * This file is part of SableCC. *
3 * See the file "LICENSE" for copyright information and the *
4 * terms and conditions for copying, distribution and *
5 * modification of SableCC. *
6 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
7
8 package org.sablecc.sablecc;
9
10 import java.util.*;
11 import com.sun.java.util.collections.*;
12
13 final class LR1Item implements Cloneable, Comparable
14 {
15 final LR0Item lr0Item;
16 final int terminal;
17
18 LR1Item(LR0Item lr0Item, int terminal)
19 {
20 this.lr0Item = lr0Item;
21 this.terminal = terminal;
22 }
23
24 public Object clone()
25 {
26 return new LR1Item(lr0Item, terminal);
27 }
28
29 public boolean equals(Object obj)
30 {
31 if((obj == null) ||
32 (obj.getClass() != this.getClass()))
33 {
34 return false;
35 }
36
37 LR1Item item = (LR1Item) obj;
38
39 return (item.lr0Item.equals(lr0Item)) &&
40 (item.terminal == terminal);
41 }
42
43 public int hashCode()
44 {
45 return lr0Item.hashCode() * (terminal + 1) * 37;
46 }
47
48 public String toString()
49 {
50 return lr0Item + ":" + Symbol.symbol(terminal, true);
51 }
52
53 public String toString(Symbol lookahead)
54 {
55 // two cases:
56 // (1) we are facing a reduction, and the lookahed
57 // should match
58 // (2) we are in the middle of a production. The
59 // next element should match.
60
61 Symbol[] rightside = Production.production(lr0Item.production).rightside();
62
63 if(lr0Item.position == rightside.length)
64 {
65 Symbol term = Symbol.symbol(terminal, true);
66
67 if(term == lookahead)
68 {
69 return lr0Item + " followed by " + term + " (reduce)";
70 }
71 else
72 {
73 return null;
74 }
75 }
76
77 if(rightside[lr0Item.position] == lookahead)
78 {
79 return lr0Item + " (shift)";
80 }
81 else
82 {
83 return null;
84 }
85 }
86
87 public int compareTo(Object object)
88 {
89 LR1Item item = (LR1Item) object;
90
91 int result = lr0Item.compareTo(item.lr0Item);
92
93 if(result == 0)
94 {
95 result = terminal - item.terminal;
96 }
97
98 return result;
99 }
100 }
101
102