KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > lexer > yacc > Keyword


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common Public
5  * License Version 1.0 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.eclipse.org/legal/cpl-v10.html
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  * Copyright (C) 2002-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
15  * Copyright (C) 2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
16  * Copyright (C) 2004 Thomas E Enebo <enebo@acm.org>
17  * Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
18  *
19  * Alternatively, the contents of this file may be used under the terms of
20  * either of the GNU General Public License Version 2 or later (the "GPL"),
21  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
22  * in which case the provisions of the GPL or the LGPL are applicable instead
23  * of those above. If you wish to allow use of your version of this file only
24  * under the terms of either the GPL or the LGPL, and not to allow others to
25  * use your version of this file under the terms of the CPL, indicate your
26  * decision by deleting the provisions above and replace them with the notice
27  * and other provisions required by the GPL or the LGPL. If you do not delete
28  * the provisions above, a recipient may use your version of this file under
29  * the terms of any one of the CPL, the GPL or the LGPL.
30  ***** END LICENSE BLOCK *****/

31 package org.jruby.lexer.yacc;
32
33 import org.jruby.parser.Tokens;
34
35 public class Keyword implements Tokens {
36     public String JavaDoc name;
37     public int id0, id1;
38     public LexState state;
39
40     private Keyword() {
41         this("", 0, 0, LexState.EXPR_BEG);
42     }
43
44     private Keyword(String JavaDoc name, int id0, int id1, LexState state) {
45         this.name = name;
46         this.id0 = id0;
47         this.id1 = id1;
48         this.state = state;
49     }
50
51 // private static final int TOTAL_KEYWORDS = 40;
52
private static final int MIN_WORD_LENGTH = 2;
53     private static final int MAX_WORD_LENGTH = 8;
54     private static final int MIN_HASH_VALUE = 6;
55     private static final int MAX_HASH_VALUE = 55;
56
57     private static final byte[] asso_values = {
58         56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
59         56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
60         56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
61         56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
62         56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
63         56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
64         56, 56, 56, 11, 56, 56, 36, 56, 1, 37,
65         31, 1, 56, 56, 56, 56, 29, 56, 1, 56,
66         56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
67         56, 56, 56, 56, 56, 1, 56, 32, 1, 2,
68         1, 1, 4, 23, 56, 17, 56, 20, 9, 2,
69         9, 26, 14, 56, 5, 1, 1, 16, 56, 21,
70         20, 9, 56, 56, 56, 56, 56, 56, 56, 56,
71         56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
72         56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
73         56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
74         56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
75         56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
76         56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
77         56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
78         56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
79         56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
80         56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
81         56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
82         56, 56, 56, 56, 56, 56, 56, 56, 56, 56,
83         56, 56, 56, 56, 56, 56
84     };
85
86     private static int hash(String JavaDoc str, int len) {
87         int hval = len;
88         switch (hval) {
89         default:
90         case 3:
91             hval += asso_values[str.charAt(2) & 255];
92         case 2:
93         case 1:
94             hval += asso_values[str.charAt(0) & 255];
95             break;
96         }
97         return hval + asso_values[str.charAt(len - 1) & 255];
98     }
99
100     private static final Keyword[] wordlist = {
101         new Keyword(),
102         new Keyword(),
103         new Keyword(),
104         new Keyword(),
105         new Keyword(),
106         new Keyword(),
107         new Keyword("end", kEND, kEND, LexState.EXPR_END),
108         new Keyword("else", kELSE, kELSE, LexState.EXPR_BEG),
109         new Keyword("case", kCASE, kCASE, LexState.EXPR_BEG),
110         new Keyword("ensure", kENSURE, kENSURE, LexState.EXPR_BEG),
111         new Keyword("module", kMODULE, kMODULE, LexState.EXPR_BEG),
112         new Keyword("elsif", kELSIF, kELSIF, LexState.EXPR_BEG),
113         new Keyword("def", kDEF, kDEF, LexState.EXPR_FNAME),
114         new Keyword("rescue", kRESCUE, kRESCUE_MOD, LexState.EXPR_MID),
115         new Keyword("not", kNOT, kNOT, LexState.EXPR_BEG),
116         new Keyword("then", kTHEN, kTHEN, LexState.EXPR_BEG),
117         new Keyword("yield", kYIELD, kYIELD, LexState.EXPR_ARG),
118         new Keyword("for", kFOR, kFOR, LexState.EXPR_BEG),
119         new Keyword("self", kSELF, kSELF, LexState.EXPR_END),
120         new Keyword("false", kFALSE, kFALSE, LexState.EXPR_END),
121         new Keyword("retry", kRETRY, kRETRY, LexState.EXPR_END),
122         new Keyword("return", kRETURN, kRETURN, LexState.EXPR_MID),
123         new Keyword("true", kTRUE, kTRUE, LexState.EXPR_END),
124         new Keyword("if", kIF, kIF_MOD, LexState.EXPR_BEG),
125         new Keyword("defined?", kDEFINED, kDEFINED, LexState.EXPR_ARG),
126         new Keyword("super", kSUPER, kSUPER, LexState.EXPR_ARG),
127         new Keyword("undef", kUNDEF, kUNDEF, LexState.EXPR_FNAME),
128         new Keyword("break", kBREAK, kBREAK, LexState.EXPR_MID),
129         new Keyword("in", kIN, kIN, LexState.EXPR_BEG),
130         new Keyword("do", kDO, kDO, LexState.EXPR_BEG),
131         new Keyword("nil", kNIL, kNIL, LexState.EXPR_END),
132         new Keyword("until", kUNTIL, kUNTIL_MOD, LexState.EXPR_BEG),
133         new Keyword("unless", kUNLESS, kUNLESS_MOD, LexState.EXPR_BEG),
134         new Keyword("or", kOR, kOR, LexState.EXPR_BEG),
135         new Keyword("next", kNEXT, kNEXT, LexState.EXPR_MID),
136         new Keyword("when", kWHEN, kWHEN, LexState.EXPR_BEG),
137         new Keyword("redo", kREDO, kREDO, LexState.EXPR_END),
138         new Keyword("and", kAND, kAND, LexState.EXPR_BEG),
139         new Keyword("begin", kBEGIN, kBEGIN, LexState.EXPR_BEG),
140         new Keyword("__LINE__", k__LINE__, k__LINE__, LexState.EXPR_END),
141         new Keyword("class", kCLASS, kCLASS, LexState.EXPR_CLASS),
142         new Keyword("__FILE__", k__FILE__, k__FILE__, LexState.EXPR_END),
143         new Keyword("END", klEND, klEND, LexState.EXPR_END),
144         new Keyword("BEGIN", klBEGIN, klBEGIN, LexState.EXPR_END),
145         new Keyword("while", kWHILE, kWHILE_MOD, LexState.EXPR_BEG),
146         new Keyword(),
147         new Keyword(),
148         new Keyword(),
149         new Keyword(),
150         new Keyword(),
151         new Keyword(),
152         new Keyword(),
153         new Keyword(),
154         new Keyword(),
155         new Keyword(),
156         new Keyword("alias", kALIAS, kALIAS, LexState.EXPR_FNAME)
157     };
158
159     public static Keyword getKeyword(String JavaDoc str, int len) {
160         if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH) {
161             int key = hash(str, len);
162             if (key <= MAX_HASH_VALUE && key >= MIN_HASH_VALUE) {
163                 if (str.equals(wordlist[key].name)) {
164                     return wordlist[key];
165                 }
166             }
167         }
168         return null;
169     }
170 }
171
Popular Tags