Source code: org/sablecc/sablecc/LR0Item.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 LR0Item implements Cloneable, Comparable
14 {
15 final int production;
16 final int position;
17
18 LR0Item(int production, int position)
19 {
20 this.production = production;
21 this.position = position;
22 }
23
24 public int compareTo(Object object)
25 {
26 LR0Item item = (LR0Item) object;
27
28 int result = production - item.production;
29
30 if(result == 0)
31 {
32 result = position - item.position;
33 }
34
35 return result;
36 }
37
38 public Object clone()
39 {
40 return new LR0Item(production, position);
41 }
42
43 public boolean equals(Object obj)
44 {
45 if((obj == null) ||
46 (obj.getClass() != this.getClass()))
47 {
48 return false;
49 }
50
51 LR0Item item = (LR0Item) obj;
52
53 return (item.production == production) &&
54 (item.position == position);
55 }
56
57 public int hashCode()
58 {
59 return (production * 13) ^ (position * 17);
60 }
61
62 public String toString()
63 {
64 StringBuffer result = new StringBuffer();
65 String prodStr = (Production.production(production)).toString();
66 int pos = 0;
67
68 StringTokenizer list = new StringTokenizer(prodStr, "= ");
69
70 // we know that there is at least one token (lhs)
71 result.append(list.nextToken());
72 result.append(" =");
73
74 while(list.hasMoreElements())
75 {
76 String tmp = list.nextToken();
77
78 if(pos == position)
79 {
80 result.append(" * ");
81 result.append(tmp);
82 }
83 else
84 {
85 result.append(" ");
86 result.append(tmp);
87 }
88
89 pos++;
90 }
91
92 if(pos == position)
93 {
94 result.append(" *");
95 }
96
97 return "[ " + result + " ]";
98 }
99 }
100