]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/parser/generator/table/Item.java
Moved SCL parser generator to platform repository.
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / parser / generator / table / Item.java
1 package org.simantics.scl.compiler.parser.generator.table;
2
3 import org.simantics.scl.compiler.parser.generator.grammar.AnaGrammar;
4 import org.simantics.scl.compiler.parser.generator.grammar.Prod;
5
6
7 public class Item implements Comparable<Item> {
8     public final int production;
9     public final int position;
10     public int stackPos;
11     
12     public Item(int production, int position, int stackPos) {
13         this.production = production;
14         this.position = position;
15         this.stackPos = stackPos;
16     }
17
18     public int[] nextSymbols(AnaGrammar grammar) {
19         Prod prod = grammar.prods.get(production);
20         return prod.rhs.nextStates(position);
21     }
22
23     public int nextPosition(AnaGrammar grammar, int symbol) {
24         Prod prod = grammar.prods.get(production);
25         return prod.rhs.getTransition(position, symbol);
26     }
27     
28     @Override
29     public int hashCode() {
30         return (production * 31 + position) * 31 + stackPos;
31     }
32
33     @Override
34     public boolean equals(Object obj) {
35         if (this == obj)
36             return true;
37         if (obj == null || getClass() != obj.getClass())
38             return false;
39         Item other = (Item) obj;
40         return production == other.production
41                 && position == other.position
42                 && stackPos == other.stackPos;
43     }
44
45     @Override
46     public int compareTo(Item o) {
47         if(production < o.production)
48             return -1;
49         if(production > o.production)
50             return 1;
51         if(position < o.position)
52             return -1;
53         if(position > o.position)
54             return 1;
55         return 0;        
56     }
57     
58     public String toString(AnaGrammar grammar) {
59         Prod prod = grammar.prods.get(production);
60         StringBuilder b = new StringBuilder();
61         b.append(grammar.nonterminalNames[prod.lhs]);
62         b.append(" ::= ");
63         /*prod.rhs.toRegexpTo(position).toString(b, grammar, 0);
64         b.append(" . ");
65         prod.rhs.toRegexpFrom(position).toString(b, grammar, 0);*/
66         prod.rhs.toPositionalRegexp(position).toString(b, grammar, 0);
67         if(stackPos >= 0)
68             b.append(" (stack ").append(stackPos).append(')');
69         /*for(int i=0;i<position;++i)
70             b.append(' ').append(grammar.getName(prod.rhs[i]));
71         b.append(" *");
72         for(int i=position;i<prod.rhs.length;++i)
73             b.append(' ').append(grammar.getName(prod.rhs[i]));*/
74         return b.toString();
75     }
76
77     public boolean isReducible(AnaGrammar grammar) {
78         return grammar.prods.get(production).rhs.getAccepts(position);
79     }
80
81 }