KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fri > patterns > interpreter > parsergenerator > syntax > Rule


1 package fri.patterns.interpreter.parsergenerator.syntax;
2
3 import java.util.*;
4 import java.io.Serializable JavaDoc;
5
6 /**
7     A rule is a list of String symbols on the right side
8     and a nonterminal on the left side: "a ::= b c d;"
9     A nonterminal is represented as a String with no quotes,
10     every terminal must appear quoted by " or ' or ` (backquote).
11     
12     @author (c) 2004 Fritz Ritzberger
13 */

14
15 public class Rule implements Serializable JavaDoc
16 {
17     private List symbols;
18
19     /** Source generator constructor. */
20     public Rule(String JavaDoc nonterminal, int rightSize) {
21         this(new ArrayList(rightSize + 1));
22         symbols.add(nonterminal);
23     }
24
25     /** Constructing a rule from a String array, first element is interpreted as nonterminal. */
26     public Rule(String JavaDoc [] symbols) {
27         this(SyntaxUtil.ruleToList(symbols));
28     }
29
30     /** Constructing a rule from a String List, first element is interpreted as nonterminal. */
31     public Rule(List symbols) {
32         if (symbols == null)
33             throw new IllegalArgumentException JavaDoc("Can not construct rule without nonterminal: "+symbols);
34         this.symbols = symbols;
35     }
36
37     /** Serializable constructor, do not use. */
38     protected Rule() {
39     }
40
41
42     public String JavaDoc getNonterminal() {
43         return (String JavaDoc) symbols.get(0);
44     }
45     
46     public int rightSize() {
47         return symbols.size() - 1;
48     }
49     
50     public String JavaDoc getRightSymbol(int i) {
51         return (String JavaDoc) symbols.get(i + 1);
52     }
53     
54     public void setRightSymbol(String JavaDoc symbol, int i) {
55         symbols.set(i + 1, symbol);
56     }
57     
58     public void addRightSymbol(String JavaDoc symbol) {
59         symbols.add(symbol);
60     }
61     
62     public int indexOnRightSide(String JavaDoc symbol) {
63         for (int i = 0; i < rightSize(); i++)
64             if (getRightSymbol(i).equals(symbol))
65                 return i;
66         return -1;
67     }
68
69
70     /** Returns true if symbol lists are equal. */
71     public boolean equals(Object JavaDoc o) {
72         return ((Rule)o).symbols.equals(symbols);
73     }
74     
75     /** Returns symbol lists hashcode. */
76     public int hashCode() {
77         return symbols.hashCode();
78     }
79     
80     /** Returns the syntax as a multiline string. */
81     public String JavaDoc toString() {
82         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(getNonterminal()+" ::= ");
83         for (int i = 0; i < rightSize(); i++) {
84             sb.append(getRightSymbol(i));
85             sb.append(" ");
86         }
87         sb.append(";");
88         return sb.toString();
89     }
90
91 }
92
Popular Tags