KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > beaver > Symbol


1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * This file is part of Beaver Parser Generator. *
3  * Copyright (C) 2003,2004 Alexander Demenchuk <alder@softanvil.com>. *
4  * All rights reserved. *
5  * See the file "LICENSE" for the terms and conditions for copying, *
6  * distribution and modification of Beaver. *
7  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

8
9 package beaver;
10
11 /**
12  * Represents a symbol of a grammar.
13  */

14 public class Symbol
15 {
16     static private final int COLUMN_FIELD_BITS = 12;
17     static private final int COLUMN_FIELD_MASK = (1 << COLUMN_FIELD_BITS) - 1;
18     
19     /**
20      * Packes symbol "coordinates" into a single number.
21      */

22     static public int makePosition(int line, int column)
23     {
24         return line << COLUMN_FIELD_BITS | column;
25     }
26     
27     /**
28      * Extracts line number from a packed position.
29      */

30     static public int getLine(int position)
31     {
32         return position >>> COLUMN_FIELD_BITS;
33     }
34     
35     /**
36      * Extracts column number from a packed position.
37      */

38     static public int getColumn(int position)
39     {
40         return position & COLUMN_FIELD_MASK;
41     }
42     
43     /**
44      * Value assigned to this symbol.
45      */

46     public final Object JavaDoc value;
47
48     /**
49      * Numeric symbol ID.
50      */

51     protected short id;
52     
53     /**
54      * Line and column where this symbol begins.
55      */

56     protected int start;
57     
58     /**
59      * Line and column where this symbol ends.
60      */

61     protected int end;
62     
63     public Symbol(short id)
64     {
65         this.id = id;
66         this.value = null;
67     }
68
69     public Symbol(short id, Object JavaDoc value)
70     {
71         this.id = id;
72         this.value = value;
73     }
74
75     public Symbol(short id, int start, int end)
76     {
77         this.id = id;
78         this.value = null;
79         this.start = start;
80         this.end = end;
81     }
82
83     public Symbol(short id, int left, int right, Object JavaDoc value)
84     {
85         this.id = id;
86         this.value = value;
87         this.start = left;
88         this.end = right;
89     }
90
91     public Symbol(short id, int start_line, int start_column, int length)
92     {
93         this.id = id;
94         this.value = null;
95         this.start = makePosition(start_line, start_column);
96         this.end = makePosition(start_line, start_column + length - 1);
97     }
98
99     public Symbol(short id, int start_line, int start_column, int length, Object JavaDoc value)
100     {
101         this.id = id;
102         this.value = value;
103         this.start = makePosition(start_line, start_column);
104         this.end = makePosition(start_line, start_column + length - 1);
105     }
106     
107     /**
108      * Creates Symbol for non-symbolic results of action routines
109      *
110      * @param value attached Symbol's value
111      */

112     public Symbol(Object JavaDoc value)
113     {
114         this.value = value;
115     }
116
117     /**
118      * Special case constructor that allows creation of explicitly Symbol-ized nonterminals.
119      * <p/>
120      * Used by classes descending from Symbol and which instances are returned by reduce actions.
121      * In this case ID and symbol position will be assigned by the parser when reduce action
122      * code returns this symbol.
123      */

124     protected Symbol()
125     {
126         this.value = this;
127     }
128     
129     /**
130      * Returns an ID of this symbol.
131      * <p/>
132      * This ID typically is, depending on a symbol type, either a terminal ID if a Symbol is a token
133      * created and returned by a Scanner, or a nonterminal ID if a symbol was created by parser based
134      * on that nonterminal definition. In the former case the ID is one of IDs generated by Beaver
135      * for terminal symbols. The latter keeps IDs of nonterminal symbols.
136      *
137      * @return symbol's ID
138      */

139     public short getId()
140     {
141         return id;
142     }
143
144     /**
145      * Returns a position in a source where this symbol starts.
146      *
147      * @return packed line and column numbers
148      */

149     public int getStart()
150     {
151         return start;
152     }
153
154     /**
155      * Returns a position in a source where this symbol ends.
156      *
157      * @return packed line and column numbers
158      */

159     public int getEnd()
160     {
161         return end;
162     }
163 }
164
Popular Tags